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

custom_email_credential_spec.rb « service_desk « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dbf47a8f6a73f6b352503c0732c7e344f57fdddc (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ServiceDesk::CustomEmailCredential, feature_category: :service_desk do
  let(:project) { build_stubbed(:project) }
  let(:credential) { build_stubbed(:service_desk_custom_email_credential, project: project) }
  let(:smtp_username) { "user@example.com" }
  let(:smtp_password) { "supersecret" }

  describe 'validations' do
    it { is_expected.to validate_presence_of(:project) }

    it { is_expected.to validate_presence_of(:smtp_address) }
    it { is_expected.to validate_length_of(:smtp_address).is_at_most(255) }
    it { is_expected.to allow_value('smtp.gmail.com').for(:smtp_address) }
    it { is_expected.to allow_value('1.1.1.1').for(:smtp_address) }
    it { is_expected.to allow_value('199.1.1.1').for(:smtp_address) }
    it { is_expected.not_to allow_value('https://example.com').for(:smtp_address) }
    it { is_expected.not_to allow_value('file://example').for(:smtp_address) }
    it { is_expected.not_to allow_value('/example').for(:smtp_address) }
    it { is_expected.not_to allow_value('localhost').for(:smtp_address) }
    it { is_expected.not_to allow_value('127.0.0.1').for(:smtp_address) }
    it { is_expected.not_to allow_value('192.168.12.12').for(:smtp_address) } # disallow local network

    it { is_expected.to validate_presence_of(:smtp_port) }
    it { is_expected.to validate_numericality_of(:smtp_port).only_integer.is_greater_than(0) }

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

    it { is_expected.to validate_presence_of(:smtp_password) }
    it { is_expected.to validate_length_of(:smtp_password).is_at_least(8).is_at_most(128) }
  end

  describe 'encrypted #smtp_username' do
    subject { build_stubbed(:service_desk_custom_email_credential, smtp_username: smtp_username) }

    it 'saves and retrieves the encrypted smtp username and iv correctly' do
      expect(subject.encrypted_smtp_username).not_to be_nil
      expect(subject.encrypted_smtp_username_iv).not_to be_nil

      expect(subject.smtp_username).to eq(smtp_username)
    end
  end

  describe 'encrypted #smtp_password' do
    subject { build_stubbed(:service_desk_custom_email_credential, smtp_password: smtp_password) }

    it 'saves and retrieves the encrypted smtp password and iv correctly' do
      expect(subject.encrypted_smtp_password).not_to be_nil
      expect(subject.encrypted_smtp_password_iv).not_to be_nil

      expect(subject.smtp_password).to eq(smtp_password)
    end
  end

  describe '#delivery_options' do
    let(:expected_attributes) do
      {
        address: 'smtp.example.com',
        domain: 'example.com',
        user_name: 'user@example.com',
        port: 587,
        password: 'supersecret',
        authentication: nil
      }
    end

    let(:setting) { build_stubbed(:service_desk_setting, project: project, custom_email: 'user@example.com') }

    subject { credential.delivery_options }

    before do
      # credential.service_desk_setting is delegated to project and we only use build_stubbed
      project.service_desk_setting = setting
    end

    it { is_expected.to include(expected_attributes) }

    context 'when authentication is set' do
      before do
        credential.smtp_authentication = 'login'
        expected_attributes[:authentication] = 'login'
      end

      it { is_expected.to include(expected_attributes) }
    end
  end

  describe 'associations' do
    it { is_expected.to belong_to(:project) }

    it 'can access service desk setting from project' do
      setting = build_stubbed(:service_desk_setting, project: project)

      expect(credential.service_desk_setting).to eq(setting)
    end
  end
end