Skip to content

Commit

Permalink
Merge pull request #8 from sealink/pattern_matching
Browse files Browse the repository at this point in the history
Pattern matching
  • Loading branch information
blaknite committed Dec 19, 2016
2 parents 8d64a4b + 4efe3a4 commit 71f4661
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).

## [Unreleased]
### Fixed
- Implements stricter credit cards pattern matching

## [0.2.1] - 2016-12-19
### Changed
- Updates README for usage with Rails
Expand Down
8 changes: 4 additions & 4 deletions lib/sensitive_data_filter/types/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module Types
module CreditCard
SEPARATORS = /[ -]/
SEPRS = SEPARATORS.source + '*'
CARD_16_DIGITS = /\d{4} #{SEPRS} \d{4} #{SEPRS} \d{4} #{SEPRS} \d{4}/
CARD_13_DIGITS = /\d{3} #{SEPRS} \d{3} #{SEPRS} \d{3} #{SEPRS} \d #{SEPRS} \d{3}/
CARD_14_DIGITS = /\d{4} #{SEPRS} \d{6} #{SEPRS} \d{4}/
CARD_15_DIGITS = /\d{4} #{SEPRS} \d{6} #{SEPRS} \d{5}/
CARD_16_DIGITS = /\d{4}#{SEPRS}\d{4}#{SEPRS}\d{4}#{SEPRS}\d{4}/
CARD_13_DIGITS = /\d{3}#{SEPRS}\d{3}#{SEPRS}\d{3}#{SEPRS}\d#{SEPRS}\d{3}/
CARD_14_DIGITS = /\d{4}#{SEPRS}\d{6}#{SEPRS}\d{4}/
CARD_15_DIGITS = /\d{4}#{SEPRS}\d{6}#{SEPRS}\d{5}/
CARD = /
(?<!\d)(?:
#{CARD_16_DIGITS.source}
Expand Down
44 changes: 44 additions & 0 deletions spec/sensitive_data/types/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,48 @@ def validations(cards)
specify { expect(mask).to eq value }
end
end

describe 'pattern matching' do
shared_examples_for 'a pattern matcher' do
context 'valid pattern' do
it 'should match' do
expect(subject.match(valid_match)[0]).to eq valid_match
end
end

context 'invalid pattern' do
it 'should not match' do
expect(subject.match(invalid_match)).to be_nil
end
end
end

context '13 digit card pattern' do
subject { SensitiveDataFilter::Types::CreditCard::CARD_13_DIGITS }
let(:valid_match) { '123-123-123-1-123' }
let(:invalid_match) { '1234-123-123-1-35' }
it_behaves_like 'a pattern matcher'
end

context '14 digit card pattern' do
subject { SensitiveDataFilter::Types::CreditCard::CARD_14_DIGITS }
let(:valid_match) { '1234-123456-1234' }
let(:invalid_match) { '1234-12345-123' }
it_behaves_like 'a pattern matcher'
end

context '15 digit card' do
subject { SensitiveDataFilter::Types::CreditCard::CARD_15_DIGITS }
let(:valid_match) { '1234-123456-12345' }
let(:invalid_match) { '123-1234567-12345' }
it_behaves_like 'a pattern matcher'
end

context '16 digit card pattern' do
subject(:card_16_digits) { SensitiveDataFilter::Types::CreditCard::CARD_16_DIGITS }
let(:valid_match) { '1234-5678-9012-3528' }
let(:invalid_match) { '1234-15678-012-3528' }
it_behaves_like 'a pattern matcher'
end
end
end

0 comments on commit 71f4661

Please sign in to comment.