summaryrefslogtreecommitdiff
path: root/spec/features/admin/admin_deploy_keys_spec.rb
blob: 9b74aa2ac5a7c9f84f04c2f0278bcdbd1f057ff3 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'admin deploy keys' do
  include Spec::Support::Helpers::ModalHelpers

  let_it_be(:admin) { create(:admin) }

  let!(:deploy_key) { create(:deploy_key, public: true) }
  let!(:another_deploy_key) { create(:another_deploy_key, public: true) }

  before do
    sign_in(admin)
    gitlab_enable_admin_mode_sign_in(admin)
  end

  shared_examples 'renders deploy keys correctly' do
    it 'show all public deploy keys' do
      visit admin_deploy_keys_path

      page.within(find('[data-testid="deploy-keys-list"]', match: :first)) do
        expect(page).to have_content(deploy_key.title)
        expect(page).to have_content(another_deploy_key.title)
      end
    end

    it 'shows all the projects the deploy key has write access' do
      write_key = create(:deploy_keys_project, :write_access, deploy_key: deploy_key)

      visit admin_deploy_keys_path

      page.within(find('[data-testid="deploy-keys-list"]', match: :first)) do
        expect(page).to have_content(write_key.project.full_name)
      end
    end

    describe 'create a new deploy key' do
      let(:new_ssh_key) { attributes_for(:key)[:key] }

      before do
        visit admin_deploy_keys_path
        click_link 'New deploy key'
      end

      it 'creates a new deploy key' do
        fill_in 'deploy_key_title', with: 'laptop'
        fill_in 'deploy_key_key', with: new_ssh_key
        click_button 'Create'

        expect(current_path).to eq admin_deploy_keys_path

        page.within(find('[data-testid="deploy-keys-list"]', match: :first)) do
          expect(page).to have_content('laptop')
        end
      end
    end

    describe 'update an existing deploy key' do
      before do
        visit admin_deploy_keys_path
        page.within('tr', text: deploy_key.title) do
          click_link(_('Edit deploy key'))
        end
      end

      it 'updates an existing deploy key' do
        fill_in 'deploy_key_title', with: 'new-title'
        click_button 'Save changes'

        expect(current_path).to eq admin_deploy_keys_path

        page.within(find('[data-testid="deploy-keys-list"]', match: :first)) do
          expect(page).to have_content('new-title')
        end
      end
    end
  end

  context 'when `admin_deploy_keys_vue` feature flag is enabled', :js do
    it_behaves_like 'renders deploy keys correctly'

    describe 'remove an existing deploy key' do
      before do
        visit admin_deploy_keys_path
      end

      it 'removes an existing deploy key' do
        accept_gl_confirm('Are you sure you want to delete this deploy key?', button_text: 'Delete') do
          page.within('tr', text: deploy_key.title) do
            click_button _('Delete deploy key')
          end
        end

        expect(current_path).to eq admin_deploy_keys_path
        page.within(find('[data-testid="deploy-keys-list"]', match: :first)) do
          expect(page).not_to have_content(deploy_key.title)
        end
      end
    end
  end

  context 'when `admin_deploy_keys_vue` feature flag is disabled' do
    before do
      stub_feature_flags(admin_deploy_keys_vue: false)
    end

    it_behaves_like 'renders deploy keys correctly'

    describe 'remove an existing deploy key' do
      before do
        visit admin_deploy_keys_path
      end

      it 'removes an existing deploy key' do
        page.within('tr', text: deploy_key.title) do
          click_link _('Remove deploy key')
        end

        expect(current_path).to eq admin_deploy_keys_path
        page.within(find('[data-testid="deploy-keys-list"]', match: :first)) do
          expect(page).not_to have_content(deploy_key.title)
        end
      end
    end
  end
end