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

phone_number_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: e41719d8ca3b84bd14e28d6b46e55728e80ed3d6 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::PhoneNumberValidation, feature_category: :instance_resiliency do
  let_it_be(:user) { create(:user) }
  let_it_be(:banned_user) { create(:user, :banned) }

  it { is_expected.to belong_to(:user) }
  it { is_expected.to belong_to(:banned_user) }

  it { is_expected.to validate_presence_of(:country) }
  it { is_expected.to validate_length_of(:country).is_at_most(3) }

  it { is_expected.to validate_presence_of(:international_dial_code) }

  it {
    is_expected.to validate_numericality_of(:international_dial_code)
      .only_integer
      .is_greater_than_or_equal_to(1)
      .is_less_than_or_equal_to(999)
  }

  it { is_expected.to validate_presence_of(:phone_number) }
  it { is_expected.to validate_length_of(:phone_number).is_at_most(12) }
  it { is_expected.to allow_value('555555').for(:phone_number) }
  it { is_expected.not_to allow_value('555-555').for(:phone_number) }
  it { is_expected.not_to allow_value('+555555').for(:phone_number) }
  it { is_expected.not_to allow_value('555 555').for(:phone_number) }

  it { is_expected.to validate_length_of(:telesign_reference_xid).is_at_most(255) }

  describe '.related_to_banned_user?' do
    let_it_be(:international_dial_code) { 1 }
    let_it_be(:phone_number) { '555' }

    subject(:related_to_banned_user?) do
      described_class.related_to_banned_user?(international_dial_code, phone_number)
    end

    context 'when banned user has the same international dial code and phone number' do
      before do
        create(:phone_number_validation, user: banned_user)
      end

      it { is_expected.to eq(true) }
    end

    context 'when banned user has the same international dial code and phone number, but different country code' do
      before do
        create(:phone_number_validation, user: banned_user, country: 'CA')
      end

      it { is_expected.to eq(true) }
    end

    context 'when banned user does not have the same international dial code' do
      before do
        create(:phone_number_validation, user: banned_user, international_dial_code: 61)
      end

      it { is_expected.to eq(false) }
    end

    context 'when banned user does not have the same phone number' do
      before do
        create(:phone_number_validation, user: banned_user, phone_number: '666')
      end

      it { is_expected.to eq(false) }
    end

    context 'when not-banned user has the same international dial code and phone number' do
      before do
        create(:phone_number_validation, user: user)
      end

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

  describe 'scopes' do
    let_it_be(:another_user) { create(:user) }

    let_it_be(:phone_number_record_1) { create(:phone_number_validation, user: user, telesign_reference_xid: 'target') }
    let_it_be(:phone_number_record_2) { create(:phone_number_validation, user: another_user) }

    describe '#for_user' do
      context 'when multiple records exist for multiple users' do
        it 'returns the correct phone number record for user' do
          records = described_class.for_user(user.id)

          expect(records.count).to be(1)
          expect(records.first).to eq(phone_number_record_1)
        end
      end
    end
  end

  describe '#validated?' do
    let_it_be(:phone_number_record) { create(:phone_number_validation, user: user) }

    context 'when phone number record is not validated' do
      it 'returns false' do
        expect(phone_number_record.validated?).to be(false)
      end
    end

    context 'when phone number record is validated' do
      before do
        phone_number_record.update!(validated_at: Time.now.utc)
      end

      it 'returns true' do
        expect(phone_number_record.validated?).to be(true)
      end
    end
  end

  describe '.by_reference_id' do
    let_it_be(:phone_number_record) { create(:phone_number_validation) }

    let(:ref_id) { phone_number_record.telesign_reference_xid }

    subject { described_class.by_reference_id(ref_id) }

    it { is_expected.to eq phone_number_record }

    context 'when there is no matching record' do
      let(:ref_id) { 'does-not-exist' }

      it { is_expected.to be_nil }
    end
  end
end