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

require 'spec_helper'

RSpec.describe DeployKeysProject do
  describe "Associations" do
    it { is_expected.to belong_to(:deploy_key) }
    it { is_expected.to belong_to(:project) }
  end

  describe "Validation" do
    it { is_expected.to validate_presence_of(:project_id) }
    it { is_expected.to validate_presence_of(:deploy_key) }
  end

  describe '.with_deploy_keys' do
    subject(:scoped_query) { described_class.with_deploy_keys.last }

    it 'includes deploy_keys in query' do
      project = create(:project)
      create(:deploy_keys_project, project: project, deploy_key: create(:deploy_key))

      includes_query_count = ActiveRecord::QueryRecorder.new { scoped_query }.count
      deploy_key_query_count = ActiveRecord::QueryRecorder.new { scoped_query.deploy_key }.count

      expect(includes_query_count).to eq(2)
      expect(deploy_key_query_count).to eq(0)
    end
  end

  describe "Destroying" do
    let(:project)     { create(:project) }
    subject           { create(:deploy_keys_project, project: project) }

    let(:deploy_key)  { subject.deploy_key }

    context "when the deploy key is only used by this project" do
      context "when the deploy key is public" do
        before do
          deploy_key.update_attribute(:public, true)
        end

        it "doesn't destroy the deploy key" do
          subject.destroy

          expect { deploy_key.reload }.not_to raise_error
        end
      end

      context "when the deploy key is private" do
        it "destroys the deploy key" do
          subject.destroy

          expect { deploy_key.reload }.to raise_error(ActiveRecord::RecordNotFound)
        end
      end
    end

    context "when the deploy key is used by more than one project" do
      let!(:other_project) { create(:project) }

      before do
        other_project.deploy_keys << deploy_key
      end

      it "doesn't destroy the deploy key" do
        subject.destroy

        expect { deploy_key.reload }.not_to raise_error
      end
    end
  end
end