summaryrefslogtreecommitdiff
path: root/spec/models/aws/role_spec.rb
blob: f23f18ab44fa2e6faaeea391a439eb2ef1b0deb2 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Aws::Role do
  it { is_expected.to belong_to(:user) }
  it { is_expected.to validate_length_of(:role_external_id).is_at_least(1).is_at_most(64) }

  describe 'custom validations' do
    subject { role.valid? }

    context ':role_arn' do
      let(:role) { build(:aws_role, role_arn: role_arn) }

      context 'length is zero' do
        let(:role_arn) { '' }

        it { is_expected.to be_falsey }
      end

      context 'length is longer than 2048' do
        let(:role_arn) { '1' * 2049 }

        it { is_expected.to be_falsey }
      end

      context 'ARN is valid' do
        let(:role_arn) { 'arn:aws:iam::123456789012:role/test-role' }

        it { is_expected.to be_truthy }
      end

      context 'ARN is nil' do
        let(:role_arn) {}

        it { is_expected.to be_truthy }
      end
    end
  end

  describe 'callbacks' do
    describe '#ensure_role_external_id!' do
      subject { role.validate }

      context 'for a new record' do
        let(:role) { build(:aws_role, role_external_id: nil) }

        it 'calls #ensure_role_external_id!' do
          expect(role).to receive(:ensure_role_external_id!)

          subject
        end
      end

      context 'for an existing record' do
        let(:role) { create(:aws_role) }

        it 'does not call #ensure_role_external_id!' do
          expect(role).not_to receive(:ensure_role_external_id!)

          subject
        end
      end
    end
  end

  describe '#ensure_role_external_id!' do
    let(:role) { build(:aws_role, role_external_id: external_id) }

    subject { role.ensure_role_external_id! }

    context 'role_external_id is blank' do
      let(:external_id) { nil }

      it 'generates an external ID and assigns it to the record' do
        subject

        expect(role.role_external_id).to be_present
      end
    end

    context 'role_external_id is already set' do
      let(:external_id) { 'external-id' }

      it 'does not change the existing external id' do
        subject

        expect(role.role_external_id).to eq external_id
      end
    end
  end
end