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: 2f0fd1d3ac9c5762cafcdcb059ce6707288ff8c4 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::PhoneNumberValidation do
  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' }

    let_it_be(:user) { create(:user) }
    let_it_be(:banned_user) { create(:user, :banned) }

    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
end