diff options
Diffstat (limited to 'spec/models/users')
-rw-r--r-- | spec/models/users/credit_card_validation_spec.rb | 143 | ||||
-rw-r--r-- | spec/models/users/group_visit_spec.rb | 25 | ||||
-rw-r--r-- | spec/models/users/project_visit_spec.rb | 25 |
3 files changed, 191 insertions, 2 deletions
diff --git a/spec/models/users/credit_card_validation_spec.rb b/spec/models/users/credit_card_validation_spec.rb index 4db3683c057..486d1c6d3ea 100644 --- a/spec/models/users/credit_card_validation_spec.rb +++ b/spec/models/users/credit_card_validation_spec.rb @@ -2,14 +2,19 @@ require 'spec_helper' -RSpec.describe Users::CreditCardValidation do +RSpec.describe Users::CreditCardValidation, feature_category: :user_profile do 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) } - describe '.similar_records' do + 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(:card_details) do subject.attributes.with_indifferent_access.slice(:expiration_date, :last_digits, :network, :holder_name) end @@ -53,6 +58,22 @@ RSpec.describe Users::CreditCardValidation do 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 let(:banned_user) { create(:banned_user) } let!(:credit_card) { create(:credit_card_validation) } @@ -154,4 +175,122 @@ RSpec.describe Users::CreditCardValidation do it { is_expected.not_to be_used_by_banned_user } 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 { save_credit_card_validation }.not_to change { credit_card_validation.last_digits_hash } } + end + + context 'when last_digits has a value' do + let(:last_digits) { 1111 } + let(:expected_last_digits_hash) { Gitlab::CryptoHelper.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) { Gitlab::CryptoHelper.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) { Gitlab::CryptoHelper.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) { Gitlab::CryptoHelper.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 diff --git a/spec/models/users/group_visit_spec.rb b/spec/models/users/group_visit_spec.rb new file mode 100644 index 00000000000..63c4631ad7d --- /dev/null +++ b/spec/models/users/group_visit_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::GroupVisit, feature_category: :navigation do + let_it_be(:entity) { create(:group) } + let_it_be(:user) { create(:user) } + let_it_be(:base_time) { DateTime.now } + + before do + described_class.create!(entity_id: entity.id, user_id: user.id, visited_at: base_time) + end + + it_behaves_like 'namespace visits model' + + it_behaves_like 'cleanup by a loose foreign key' do + let!(:model) { create(:group_visit, entity_id: entity.id, user_id: user.id, visited_at: base_time) } + let!(:parent) { entity } + end + + it_behaves_like 'cleanup by a loose foreign key' do + let!(:model) { create(:group_visit, entity_id: entity.id, user_id: user.id, visited_at: base_time) } + let!(:parent) { user } + end +end diff --git a/spec/models/users/project_visit_spec.rb b/spec/models/users/project_visit_spec.rb new file mode 100644 index 00000000000..38747bd6462 --- /dev/null +++ b/spec/models/users/project_visit_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Users::ProjectVisit, feature_category: :navigation do + let_it_be(:entity) { create(:project) } + let_it_be(:user) { create(:user) } + let_it_be(:base_time) { DateTime.now } + + before do + described_class.create!(entity_id: entity.id, user_id: user.id, visited_at: base_time) + end + + it_behaves_like 'namespace visits model' + + it_behaves_like 'cleanup by a loose foreign key' do + let!(:model) { create(:project_visit, entity_id: entity.id, user_id: user.id, visited_at: base_time) } + let!(:parent) { entity } + end + + it_behaves_like 'cleanup by a loose foreign key' do + let!(:model) { create(:project_visit, entity_id: entity.id, user_id: user.id, visited_at: base_time) } + let!(:parent) { user } + end +end |