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

require 'spec_helper'

RSpec.describe X509Issuer do
  describe 'validation' do
    it { is_expected.to validate_presence_of(:subject_key_identifier) }
    it { is_expected.to validate_presence_of(:subject) }
    it { is_expected.to validate_presence_of(:crl_url) }
  end

  describe '.safe_create!' do
    let(:issuer_subject_key_identifier) { 'AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB' }
    let(:issuer_subject) { 'CN=PKI,OU=Example,O=World' }
    let(:issuer_crl_url) { 'http://example.com/pki.crl' }

    let(:attributes) do
      {
        subject_key_identifier: issuer_subject_key_identifier,
        subject: issuer_subject,
        crl_url: issuer_crl_url
      }
    end

    it 'creates a new issuer if it was not found' do
      expect { described_class.safe_create!(attributes) }.to change { described_class.count }.by(1)
    end

    it 'assigns the correct attributes when creating' do
      issuer = described_class.safe_create!(attributes)

      expect(issuer.subject_key_identifier).to eq(issuer_subject_key_identifier)
      expect(issuer.subject).to eq(issuer_subject)
      expect(issuer.crl_url).to eq(issuer_crl_url)
    end
  end

  describe 'validators' do
    it 'accepts correct subject_key_identifier' do
      subject_key_identifiers = [
        'AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB',
        'CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD'
      ]

      subject_key_identifiers.each do |identifier|
        expect(build(:x509_issuer, subject_key_identifier: identifier)).to be_valid
      end
    end

    it 'rejects invalid subject_key_identifier' do
      subject_key_identifiers = [
        'AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB:AB',
        'CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:CD:GG',
        'random string',
        '12321342545356434523412341245452345623453542345234523453245'
      ]

      subject_key_identifiers.each do |identifier|
        expect(build(:x509_issuer, subject_key_identifier: identifier)).to be_invalid
      end
    end

    it 'accepts valid crl_url' do
      expect(build(:x509_issuer, crl_url: "https://pki.example.org")).to be_valid
    end

    it 'rejects invalid crl_url' do
      expect(build(:x509_issuer, crl_url: "ht://pki.example.org")).to be_invalid
    end
  end
end