Welcome to mirror list, hosted at ThFree Co, Russian Federation.

credit_card_validation_spec.rb « users « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae75020c76857b5281da28891ae5c92e9c9893b1 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::CreditCardValidation, feature_category: :user_profile do
  include CryptoHelpers

  it { is_expected.to belong_to(:user) }

  it { is_expected.to validate_length_of(:holder_name).is_at_most(50) }
  it { is_expected.to validate_length_of(:network).is_at_most(32) }
  it { is_expected.to validate_numericality_of(:last_digits).is_less_than_or_equal_to(9999) }

  it { is_expected.to validate_length_of(:last_digits_hash).is_at_most(44) }
  it { is_expected.to validate_length_of(:holder_name_hash).is_at_most(44) }
  it { is_expected.to validate_length_of(:expiration_date_hash).is_at_most(44) }
  it { is_expected.to validate_length_of(:network_hash).is_at_most(44) }

  describe '#similar_records' do
    let_it_be(:credit_card_validation) { create(:credit_card_validation) }

    let_it_be(:card_details) do
      credit_card_validation.attributes.with_indifferent_access.slice(
        :expiration_date, :last_digits, :network, :holder_name
      )
    end

    let_it_be(:match_1) { create(:credit_card_validation, card_details) }
    let_it_be(:match_2) { create(:credit_card_validation, card_details.merge(holder_name: 'Bob')) }

    let_it_be(:non_match_1) { create(:credit_card_validation, card_details.merge(last_digits: 9999)) }
    let_it_be(:non_match_2) { create(:credit_card_validation, card_details.merge(network: 'Mastercard')) }
    let_it_be(:non_match_3) do
      create(:credit_card_validation, card_details.merge(expiration_date: 2.years.from_now.to_date))
    end

    it 'returns matches with the same last_digits, expiration and network, ordered by credit_card_validated_at' do
      # eq is used instead of match_array because rows are sorted by credit_card_validated_at in desc order
      expect(credit_card_validation.similar_records).to eq([match_2, match_1, credit_card_validation])
    end
  end

  describe '#similar_holder_names_count' do
    context 'when holder_name is present' do
      let_it_be(:credit_card_validation) { create(:credit_card_validation, holder_name: 'ALICE M SMITH') }

      let_it_be(:match) { create(:credit_card_validation, holder_name: 'Alice M Smith') }
      let_it_be(:non_match) { create(:credit_card_validation, holder_name: 'Bob B Brown') }

      it 'returns the count of cards with similar case insensitive holder names' do
        expect(credit_card_validation.similar_holder_names_count).to eq(2)
      end
    end

    context 'when holder_name is nil' do
      let_it_be(:credit_card_validation) { create(:credit_card_validation, holder_name: nil) }

      it 'returns 0' do
        expect(subject.similar_holder_names_count).to eq(0)
      end
    end
  end

  describe 'scopes' do
    describe '.find_or_initialize_by_user' do
      subject(:find_or_initialize_by_user) { described_class.find_or_initialize_by_user(user.id) }

      let_it_be(:user) { create(:user) }

      context 'with no existing credit card record' do
        it { is_expected.to be_a_new_record }
      end

      context 'with existing credit card record' do
        let_it_be(:credit_card_validation) { create(:credit_card_validation, user: user) }

        it { is_expected.to eq(credit_card_validation) }
      end
    end

    describe '.by_banned_user' do
      subject(:by_banned_user) { described_class.by_banned_user }

      let_it_be(:banned_user) { create(:banned_user) }
      let_it_be(:credit_card) { create(:credit_card_validation) }
      let_it_be(:banned_user_credit_card) { create(:credit_card_validation, user: banned_user.user) }

      it 'returns only records associated to banned users' do
        expect(by_banned_user).to match_array([banned_user_credit_card])
      end
    end

    describe '.similar_by_holder_name' do
      subject(:similar_by_holder_name) { described_class.similar_by_holder_name(holder_name_hash) }

      let_it_be(:credit_card_validation) { create(:credit_card_validation, holder_name: 'Alice M Smith') }
      let_it_be(:match) { create(:credit_card_validation, holder_name: 'ALICE M SMITH') }

      context 'when holder_name_hash is present' do
        let_it_be(:holder_name_hash) { credit_card_validation.holder_name_hash }

        it 'returns records with similar holder names case-insensitively' do
          expect(similar_by_holder_name).to match_array([credit_card_validation, match])
        end
      end

      context 'when holder_name_hash is nil' do
        let_it_be(:holder_name_hash) { nil }

        it 'returns an empty array' do
          expect(similar_by_holder_name).to match_array([])
        end
      end
    end

    describe '.similar_to' do
      subject(:similar_to) { described_class.similar_to(credit_card_validation) }

      let_it_be(:credit_card_validation) { create(:credit_card_validation) }

      let_it_be(:match) do
        create(:credit_card_validation,
          expiration_date: credit_card_validation.expiration_date,
          last_digits: credit_card_validation.last_digits,
          network: credit_card_validation.network
        )
      end

      let_it_be(:non_match) do
        create(:credit_card_validation,
          expiration_date: credit_card_validation.expiration_date,
          last_digits: credit_card_validation.last_digits,
          network: 'Mastercard'
        )
      end

      it 'returns only records with similar expiration_date, last_digits, and network attribute values' do
        expect(similar_to).to match_array([credit_card_validation, match])
      end
    end
  end

  describe '#used_by_banned_user?' do
    subject(:used_by_banned_user) { credit_card_validation.used_by_banned_user? }

    let_it_be(:credit_card_validation) { create(:credit_card_validation) }

    let_it_be(:card_details) do
      credit_card_validation.attributes.with_indifferent_access.slice(
        :expiration_date, :last_digits, :network, :holder_name
      )
    end

    let_it_be(:banned_user) { create(:banned_user) }

    context 'when there is a similar credit card associated to a banned user' do
      context 'when holder names match exactly' do
        before do
          create(:credit_card_validation, card_details.merge(user: banned_user.user))
        end

        it { is_expected.to be(true) }
      end

      context 'when holder names do not match exactly' do
        before do
          create(:credit_card_validation, card_details.merge(user: banned_user.user, holder_name: 'John M Smith'))
        end

        it { is_expected.to be(false) }
      end
    end

    context 'when there are no similar credit cards associated to a banned user' do
      before do
        create(:credit_card_validation,
          user: banned_user.user,
          network: 'Mastercard',
          last_digits: 1111,
          holder_name: 'Jane Smith'
        )
      end

      it { is_expected.to be(false) }
    end

    context 'when there is a similar credit card but it is not associated to a banned user' do
      before do
        create(:credit_card_validation, card_details)
      end

      it { is_expected.to be(false) }
    end
  end

  describe 'before_save' do
    describe '#set_last_digits_hash' do
      let(:credit_card_validation) { build(:credit_card_validation, last_digits: last_digits) }

      subject(:save_credit_card_validation) { credit_card_validation.save! }

      context 'when last_digits are nil' do
        let(:last_digits) { nil }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.last_digits_hash } }
      end

      context 'when last_digits has a blank value' do
        let(:last_digits) { ' ' }

        it { expect(credit_card_validation).to be_invalid }
      end

      context 'when last_digits has a value' do
        let(:last_digits) { 1111 }
        let(:expected_last_digits_hash) { sha256(last_digits) }

        it 'assigns correct last_digits_hash value' do
          expect { save_credit_card_validation }.to change {
                                                      credit_card_validation.last_digits_hash
                                                    }.from(nil).to(expected_last_digits_hash)
        end
      end
    end

    describe '#set_holder_name_hash' do
      let(:credit_card_validation) { build(:credit_card_validation, holder_name: holder_name) }

      subject(:save_credit_card_validation) { credit_card_validation.save! }

      context 'when holder_name is nil' do
        let(:holder_name) { nil }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.holder_name_hash } }
      end

      context 'when holder_name has a blank value' do
        let(:holder_name) { ' ' }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.holder_name_hash } }
      end

      context 'when holder_name has a value' do
        let(:holder_name) { 'John Smith' }
        let(:expected_holder_name_hash) { sha256(holder_name.downcase) }

        it 'lowercases holder_name and assigns correct holder_name_hash value' do
          expect { save_credit_card_validation }.to change {
                                                      credit_card_validation.holder_name_hash
                                                    }.from(nil).to(expected_holder_name_hash)
        end
      end
    end

    describe '#set_network_hash' do
      let(:credit_card_validation) { build(:credit_card_validation, network: network) }

      subject(:save_credit_card_validation) { credit_card_validation.save! }

      context 'when network is nil' do
        let(:network) { nil }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.network_hash } }
      end

      context 'when network has a blank value' do
        let(:network) { ' ' }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.network_hash } }
      end

      context 'when network has a value' do
        let(:network) { 'Visa' }
        let(:expected_network_hash) { sha256(network.downcase) }

        it 'lowercases network and assigns correct network_hash value' do
          expect { save_credit_card_validation }.to change {
                                                      credit_card_validation.network_hash
                                                    }.from(nil).to(expected_network_hash)
        end
      end
    end

    describe '#set_expiration_date_hash' do
      let(:credit_card_validation) { build(:credit_card_validation, expiration_date: expiration_date) }

      subject(:save_credit_card_validation) { credit_card_validation.save! }

      context 'when expiration_date is nil' do
        let(:expiration_date) { nil }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.expiration_date_hash } }
      end

      context 'when expiration_date has a blank value' do
        let(:expiration_date) { ' ' }

        it { expect { save_credit_card_validation }.not_to change { credit_card_validation.expiration_date_hash } }
      end

      context 'when expiration_date has a value' do
        let(:expiration_date) { 1.year.from_now.to_date }
        let(:expected_expiration_date_hash) { sha256(expiration_date.to_s) }

        it 'assigns correct expiration_date_hash value' do
          expect { save_credit_card_validation }.to change {
                                                      credit_card_validation.expiration_date_hash
                                                    }.from(nil).to(expected_expiration_date_hash)
        end
      end
    end
  end
end