summaryrefslogtreecommitdiff
path: root/spec/presenters/projects
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2019-12-12 00:07:43 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2019-12-12 00:07:43 +0000
commit2e3cbf7d89815e2915f77677388c49b48f8d20c3 (patch)
tree03bdbc99e829295e8077b2ec4032300c15b48e37 /spec/presenters/projects
parente44bb86539a8fb4cfb06dfe281632b6f206bd0a7 (diff)
downloadgitlab-ce-2e3cbf7d89815e2915f77677388c49b48f8d20c3.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/presenters/projects')
-rw-r--r--spec/presenters/projects/settings/deploy_keys_presenter_spec.rb61
1 files changed, 56 insertions, 5 deletions
diff --git a/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb b/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb
index de58733c8ea..b9cb60e414f 100644
--- a/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb
+++ b/spec/presenters/projects/settings/deploy_keys_presenter_spec.rb
@@ -5,11 +5,6 @@ require 'spec_helper'
describe Projects::Settings::DeployKeysPresenter do
let(:project) { create(:project) }
let(:user) { create(:user) }
- let(:deploy_key) { create(:deploy_key, public: true) }
-
- let!(:deploy_keys_project) do
- create(:deploy_keys_project, project: project, deploy_key: deploy_key)
- end
subject(:presenter) do
described_class.new(project, current_user: user)
@@ -20,6 +15,12 @@ describe Projects::Settings::DeployKeysPresenter do
end
describe '#enabled_keys' do
+ let!(:deploy_key) { create(:deploy_key, public: true) }
+
+ let!(:deploy_keys_project) do
+ create(:deploy_keys_project, project: project, deploy_key: deploy_key)
+ end
+
it 'returns currently enabled keys' do
expect(presenter.enabled_keys).to eq [deploy_keys_project.deploy_key]
end
@@ -53,4 +54,54 @@ describe Projects::Settings::DeployKeysPresenter do
expect(presenter.available_project_keys_size).to eq(1)
end
end
+
+ 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
end