summaryrefslogtreecommitdiff
path: root/spec/models/concerns/x509_serial_number_attribute_spec.rb
blob: 88550823748daa793a5bd2121acfdefc1cdcf9a7 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe X509SerialNumberAttribute do
  let(:model) { Class.new { include X509SerialNumberAttribute } }

  before do
    columns = [
      double(:column, name: 'name', type: :text),
      double(:column, name: 'serial_number', type: :binary)
    ]

    allow(model).to receive(:columns).and_return(columns)
  end

  describe '#x509_serial_number_attribute' do
    context 'when in non-production' do
      before do
        stub_rails_env('development')
      end

      context 'when the table exists' do
        before do
          allow(model).to receive(:table_exists?).and_return(true)
        end

        it 'defines a x509 serial number attribute for a binary column' do
          expect(model).to receive(:attribute)
            .with(:serial_number, an_instance_of(Gitlab::Database::X509SerialNumberAttribute))

          model.x509_serial_number_attribute(:serial_number)
        end

        it 'raises ArgumentError when the column type is not :binary' do
          expect { model.x509_serial_number_attribute(:name) }.to raise_error(ArgumentError)
        end
      end

      context 'when the table does not exist' do
        it 'allows the attribute to be added and issues a warning' do
          allow(model).to receive(:table_exists?).and_return(false)

          expect(model).not_to receive(:columns)
          expect(model).to receive(:attribute)
          expect(model).to receive(:warn)

          model.x509_serial_number_attribute(:name)
        end
      end

      context 'when the column does not exist' do
        it 'allows the attribute to be added and issues a warning' do
          allow(model).to receive(:table_exists?).and_return(true)

          expect(model).to receive(:columns)
          expect(model).to receive(:attribute)
          expect(model).to receive(:warn)

          model.x509_serial_number_attribute(:no_name)
        end
      end

      context 'when other execeptions are raised' do
        it 'logs and re-rasises the error' do
          allow(model).to receive(:table_exists?).and_raise(ActiveRecord::NoDatabaseError.new('does not exist'))

          expect(model).not_to receive(:columns)
          expect(model).not_to receive(:attribute)
          expect(Gitlab::AppLogger).to receive(:error)

          expect { model.x509_serial_number_attribute(:name) }.to raise_error(ActiveRecord::NoDatabaseError)
        end
      end
    end

    context 'when in production' do
      before do
        stub_rails_env('production')
      end

      it 'defines a x509 serial number attribute' do
        expect(model).not_to receive(:table_exists?)
        expect(model).not_to receive(:columns)
        expect(model).to receive(:attribute).with(:serial_number, an_instance_of(Gitlab::Database::X509SerialNumberAttribute))

        model.x509_serial_number_attribute(:serial_number)
      end
    end
  end
end