2019-10-25 05:06:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-31 09:12:02 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe Projects::Settings::DeployKeysPresenter do
|
2021-03-23 08:09:33 -04:00
|
|
|
let_it_be(:project, refind: true) { create(:project) }
|
|
|
|
let_it_be(:other_project) { create(:project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
2017-01-31 09:12:02 -05:00
|
|
|
|
|
|
|
subject(:presenter) do
|
|
|
|
described_class.new(project, current_user: user)
|
|
|
|
end
|
|
|
|
|
2021-03-23 08:09:33 -04:00
|
|
|
before_all do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
other_project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2017-01-31 09:12:02 -05:00
|
|
|
it 'inherits from Gitlab::View::Presenter::Simple' do
|
|
|
|
expect(described_class.superclass).to eq(Gitlab::View::Presenter::Simple)
|
|
|
|
end
|
|
|
|
|
2021-03-23 08:09:33 -04:00
|
|
|
describe 'deploy key groups' do
|
|
|
|
let_it_be(:deploy_key) { create(:deploy_key, projects: [project]) }
|
|
|
|
let_it_be(:other_deploy_key) { create(:deploy_key, projects: [other_project]) }
|
|
|
|
let_it_be(:public_deploy_key) { create(:deploy_key, public: true) }
|
|
|
|
let_it_be(:unrelated_project) { create(:project, :private) }
|
|
|
|
let_it_be(:unrelated_deploy_key) { create(:deploy_key, projects: [unrelated_project]) }
|
|
|
|
|
2021-03-29 20:09:26 -04:00
|
|
|
context 'with enabled keys' do
|
|
|
|
it 'returns correct deploy keys' do
|
|
|
|
expect(presenter.enabled_keys).to eq([deploy_key])
|
|
|
|
expect(presenter.enabled_keys_size).to eq(1)
|
2021-03-23 08:09:33 -04:00
|
|
|
end
|
2021-03-29 20:09:26 -04:00
|
|
|
end
|
2021-03-23 08:09:33 -04:00
|
|
|
|
2021-03-29 20:09:26 -04:00
|
|
|
context 'with available keys' do
|
|
|
|
it 'returns correct deploy keys' do
|
|
|
|
expect(presenter.available_keys).to eq([other_deploy_key, public_deploy_key])
|
2021-03-23 08:09:33 -04:00
|
|
|
end
|
2021-03-29 20:09:26 -04:00
|
|
|
end
|
2021-03-23 08:09:33 -04:00
|
|
|
|
2021-03-29 20:09:26 -04:00
|
|
|
context 'with available project keys' do
|
|
|
|
it 'returns correct deploy keys' do
|
|
|
|
expect(presenter.available_project_keys).to eq([other_deploy_key])
|
|
|
|
expect(presenter.available_project_keys_size).to eq(1)
|
2021-03-23 08:09:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-29 20:09:26 -04:00
|
|
|
context 'with available public keys' do
|
|
|
|
it 'returns correct deploy keys' do
|
|
|
|
expect(presenter.available_public_keys).to eq([public_deploy_key])
|
|
|
|
expect(presenter.available_public_keys_size).to eq(1)
|
2021-03-23 08:09:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-31 09:12:02 -05:00
|
|
|
describe '#enabled_keys' do
|
2019-12-11 19:07:43 -05:00
|
|
|
let!(:deploy_key) { create(:deploy_key, public: true) }
|
|
|
|
|
|
|
|
let!(:deploy_keys_project) do
|
|
|
|
create(:deploy_keys_project, project: project, deploy_key: deploy_key)
|
|
|
|
end
|
|
|
|
|
2017-02-21 16:37:00 -05:00
|
|
|
it 'returns currently enabled keys' do
|
|
|
|
expect(presenter.enabled_keys).to eq [deploy_keys_project.deploy_key]
|
2017-01-31 09:12:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not contain enabled_keys inside available_keys' do
|
|
|
|
expect(presenter.available_keys).not_to include deploy_key
|
|
|
|
end
|
2017-02-21 16:37:00 -05:00
|
|
|
|
|
|
|
it 'returns the enabled_keys size' do
|
|
|
|
expect(presenter.enabled_keys_size).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#available_keys/#available_project_keys' do
|
2017-03-06 15:42:04 -05:00
|
|
|
let(:other_deploy_key) { create(:another_deploy_key) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project_key = create(:deploy_keys_project, deploy_key: other_deploy_key)
|
|
|
|
project_key.project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2017-02-21 16:37:00 -05:00
|
|
|
it 'returns the current available_keys' do
|
2017-03-06 15:42:04 -05:00
|
|
|
expect(presenter.available_keys).not_to be_empty
|
2017-02-21 16:37:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the current available_project_keys' do
|
2017-03-06 15:42:04 -05:00
|
|
|
expect(presenter.available_project_keys).not_to be_empty
|
2017-02-21 16:37:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the available_project_keys size' do
|
2017-03-06 15:42:04 -05:00
|
|
|
expect(presenter.available_project_keys_size).to eq(1)
|
2017-02-21 16:37:00 -05:00
|
|
|
end
|
2017-01-31 09:12:02 -05:00
|
|
|
end
|
2019-12-11 19:07:43 -05:00
|
|
|
|
|
|
|
context 'prevent N + 1 queries' do
|
|
|
|
before do
|
|
|
|
create_records
|
|
|
|
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_records
|
|
|
|
other_project = create(:project)
|
|
|
|
other_project.add_maintainer(user)
|
|
|
|
|
|
|
|
create(:deploy_keys_project, project: project, deploy_key: create(:deploy_key))
|
|
|
|
create(:deploy_keys_project, project: other_project, deploy_key: create(:deploy_key))
|
|
|
|
create(:deploy_key, public: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_with_query_count
|
|
|
|
ActiveRecord::QueryRecorder.new { execute_presenter }.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_presenter
|
|
|
|
described_class.new(project, current_user: user).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct counts' do
|
|
|
|
result = execute_presenter
|
|
|
|
|
|
|
|
expect(result[:enabled_keys].size).to eq(1)
|
|
|
|
expect(result[:available_project_keys].size).to eq(1)
|
|
|
|
expect(result[:public_keys].size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not increase the query count' do
|
|
|
|
execute_presenter # make sure everything is cached
|
|
|
|
|
|
|
|
count_before = execute_with_query_count
|
|
|
|
|
|
|
|
3.times { create_records }
|
|
|
|
|
|
|
|
count_after = execute_with_query_count
|
|
|
|
|
|
|
|
expect(count_after).to eq(count_before)
|
|
|
|
|
|
|
|
result = execute_presenter
|
|
|
|
expect(result[:enabled_keys].size).to eq(4)
|
|
|
|
expect(result[:available_project_keys].size).to eq(4)
|
|
|
|
expect(result[:public_keys].size).to eq(4)
|
|
|
|
end
|
|
|
|
end
|
2017-01-31 09:12:02 -05:00
|
|
|
end
|