-
Notifications
You must be signed in to change notification settings - Fork 0
/
quebec_validator_spec.rb
171 lines (134 loc) · 4.73 KB
/
quebec_validator_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
RSpec.describe HealthCard::Validators::Canada::QuebecValidator do
let(:validator) { described_class.new }
describe '#card_valid?' do
context 'when no options are specified' do
context 'and the card value is correct' do
it 'verifies the card value' do
%w(
AAAA01010112
BAAA01010113
ZAAA01010112
ABAA01010115
AZAA01010112
AABA01010119
AAZA01010112
AAAB01010111
AAAZ01010112
AAAA02010115
AAAA99010115
AAAA01020119
AAAA01120114
AAAA01010211
AAAA01013110
AAAA01010123
AAAA01010190
).each do |card_value|
expect(validator.card_valid?(card_value)).to eq(true)
end
end
it 'tries birth dates in 19XX and 20XX' do
%w(
AAAA01010112
AAAA01010110
).each do |card_value|
expect(validator.card_valid?(card_value)).to eq(true)
end
end
it 'understands the `female` flag' do
%w(
AAAA01010112
AAAA01510117
).each do |card_value|
expect(validator.card_valid?(card_value)).to eq(true)
end
end
end
context 'but the card value is not correct' do
subject { validator.card_valid?(card_value) }
context 'because of the length' do
before(:example) { allow(validator).to receive(:validate_checksum).and_return(true) }
let(:card_value) { 'AAAA010101100' }
it { is_expected.to eq(false) }
end
context 'because of the letters' do
before(:example) { allow(validator).to receive(:validate_checksum).and_return(true) }
let(:card_value) { 'AA3A01010110' }
it { is_expected.to eq(false) }
end
context 'because of the date' do
before(:example) { allow(validator).to receive(:validate_checksum).and_return(true) }
let(:card_value) { 'AAAA01014110' }
it { is_expected.to eq(false) }
end
context 'because of the count index' do
before(:example) { allow(validator).to receive(:validate_checksum).and_return(true) }
let(:card_value) { 'AAAA01010100' }
it { is_expected.to eq(false) }
end
context 'because of the checksum' do
let(:card_value) { 'AAAA01010119' }
it { is_expected.to eq(false) }
end
end
end
context 'when options is specified' do
let(:options) do
{
last_name: 'Last',
first_name: 'First',
birth_date: Date.new(1980, 2, 14),
gender: :male
}
end
# These tests are to validate discrepancies between the card value and
# the provided informations. Without these info, they must pass all
# validations.
before(:example) { expect(validator.card_valid?(card_value)).to eq(true) }
subject { validator.card_valid?(card_value, options) }
context 'and the card value is correct' do
let(:card_value) { 'LASF80021411' }
it { is_expected.to eq(true) }
end
context 'and the card value is not correct' do
context 'because of the last name' do
let(:card_value) { 'LAAF80021410' }
it { is_expected.to eq(false) }
end
context 'because of the first name' do
let(:card_value) { 'LASA80021416' }
it { is_expected.to eq(false) }
end
context 'because of the birth date' do
let(:card_value) { 'LASF81021414' }
it { is_expected.to eq(false) }
end
context 'because of the gender' do
let(:card_value) { 'LASF80521416' }
it { is_expected.to eq(false) }
end
end
end
context 'when the checksum should be skipped' do
subject { validator.card_valid?(card_value, options) }
let(:card_value) { 'AAAA01010119' }
let(:options) { { skip_checksum: true } }
it { is_expected.to eq(true) }
end
end
describe '#card_valid!' do
let(:card_value) { double(:card_value) }
subject { validator.card_valid!(card_value) }
context 'when the card value is correct' do
before(:example) { allow(validator).to receive(:card_valid?).with(card_value, {}).and_return(true) }
it 'returns true' do
expect(subject).to eq(true)
end
end
context 'when the card value is incorrect' do
before(:example) { allow(validator).to receive(:card_valid?).with(card_value, {}).and_return(false) }
it 'raises an exception' do
expect { subject }.to raise_error(HealthCard::Errors::InvalidCardValueError)
end
end
end
end