diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 18:42:06 +0000 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/support/shared_examples/services | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) | |
download | gitlab-ce-6e4e1050d9dba2b7b2523fdd1768823ab85feef4.tar.gz |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/support/shared_examples/services')
8 files changed, 164 insertions, 28 deletions
diff --git a/spec/support/shared_examples/services/boards/issues_list_service_shared_examples.rb b/spec/support/shared_examples/services/boards/issues_list_service_shared_examples.rb index 756c4136059..06e2b715e6d 100644 --- a/spec/support/shared_examples/services/boards/issues_list_service_shared_examples.rb +++ b/spec/support/shared_examples/services/boards/issues_list_service_shared_examples.rb @@ -19,6 +19,15 @@ RSpec.shared_examples 'issues list service' do end end + it 'avoids N+1' do + params = { board_id: board.id } + control = ActiveRecord::QueryRecorder.new { described_class.new(parent, user, params).execute } + + create(:list, board: board) + + expect { described_class.new(parent, user, params).execute }.not_to exceed_query_limit(control) + end + context 'issues are ordered by priority' do it 'returns opened issues when list_id is missing' do params = { board_id: board.id } @@ -71,4 +80,17 @@ RSpec.shared_examples 'issues list service' do expect { service.execute }.to raise_error(ActiveRecord::RecordNotFound) end end + + context 'when :all_lists is used' do + it 'returns issues from all lists' do + params = { board_id: board.id, all_lists: true } + + issues = described_class.new(parent, user, params).execute + + expected = [opened_issue2, reopened_issue1, opened_issue1, list1_issue1, + list1_issue2, list1_issue3, list2_issue1, closed_issue1, + closed_issue2, closed_issue3, closed_issue4, closed_issue5] + expect(issues).to match_array(expected) + end + end end diff --git a/spec/support/shared_examples/services/boards/lists_list_service_shared_examples.rb b/spec/support/shared_examples/services/boards/lists_list_service_shared_examples.rb index 07a6353296d..41fd286682e 100644 --- a/spec/support/shared_examples/services/boards/lists_list_service_shared_examples.rb +++ b/spec/support/shared_examples/services/boards/lists_list_service_shared_examples.rb @@ -26,4 +26,22 @@ RSpec.shared_examples 'lists list service' do expect(service.execute(board)).to eq [board.backlog_list, list, board.closed_list] end end + + context 'when wanting a specific list' do + let!(:list1) { create(:list, board: board) } + + it 'returns list specified by id' do + service = described_class.new(parent, user, list_id: list1.id) + + expect(service.execute(board, create_default_lists: false)).to eq [list1] + end + + it 'returns empty result when list is not found' do + external_board = create(:board, resource_parent: create(:project)) + external_list = create(:list, board: external_board) + service = described_class.new(parent, user, list_id: external_list.id) + + expect(service.execute(board, create_default_lists: false)).to eq(List.none) + end + end end diff --git a/spec/support/shared_examples/services/jira_import/user_mapper_services_shared_examples.rb b/spec/support/shared_examples/services/jira_import/user_mapper_services_shared_examples.rb new file mode 100644 index 00000000000..7fc7ff8a8de --- /dev/null +++ b/spec/support/shared_examples/services/jira_import/user_mapper_services_shared_examples.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'mapping jira users' do + let(:client) { double } + + let_it_be(:project) { create(:project) } + let_it_be(:jira_service) { create(:jira_service, project: project, active: true) } + + before do + allow(subject).to receive(:client).and_return(client) + allow(client).to receive(:get).with(url).and_return(jira_users) + end + + subject { described_class.new(jira_service, start_at) } + + context 'jira_users is nil' do + let(:jira_users) { nil } + + it 'returns an empty array' do + expect(subject.execute).to be_empty + end + end + + context 'when jira_users is present' do + # TODO: now we only create an array in a proper format + # mapping is tracked in https://gitlab.com/gitlab-org/gitlab/-/issues/219023 + let(:mapped_users) do + [ + { jira_account_id: 'abcd', jira_display_name: 'user1', jira_email: nil, gitlab_id: nil, gitlab_username: nil, gitlab_name: nil }, + { jira_account_id: 'efg', jira_display_name: nil, jira_email: nil, gitlab_id: nil, gitlab_username: nil, gitlab_name: nil }, + { jira_account_id: 'hij', jira_display_name: 'user3', jira_email: 'user3@example.com', gitlab_id: nil, gitlab_username: nil, gitlab_name: nil } + ] + end + + it 'returns users mapped to Gitlab' do + expect(subject.execute).to eq(mapped_users) + end + end +end diff --git a/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb b/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb index c8fabfe30b9..1501a2a0f52 100644 --- a/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb +++ b/spec/support/shared_examples/services/metrics/dashboard_shared_examples.rb @@ -62,7 +62,7 @@ end RSpec.shared_examples 'dashboard_version contains SHA256 hash of dashboard file content' do specify do dashboard = File.read(Rails.root.join(dashboard_path)) - expect(Digest::SHA256.hexdigest(dashboard)).to eq(dashboard_version) + expect(dashboard_version).to eq(Digest::SHA256.hexdigest(dashboard)) end end @@ -78,6 +78,12 @@ RSpec.shared_examples 'raises error for users with insufficient permissions' do it_behaves_like 'misconfigured dashboard service response', :unauthorized end + + context 'when the user is anonymous' do + let(:user) { nil } + + it_behaves_like 'misconfigured dashboard service response', :unauthorized + end end RSpec.shared_examples 'valid dashboard cloning process' do |dashboard_template, sequence| diff --git a/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb b/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb index 2ddbdebdb97..f201c7b1780 100644 --- a/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb +++ b/spec/support/shared_examples/services/projects/update_repository_storage_service_shared_examples.rb @@ -2,9 +2,11 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| let(:project_repository_double) { double(:repository) } + let(:original_project_repository_double) { double(:repository) } let!(:project_repository_checksum) { project.repository.checksum } let(:repository_double) { double(:repository) } + let(:original_repository_double) { double(:repository) } let(:repository_checksum) { repository.checksum } before do @@ -14,10 +16,16 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| allow(Gitlab::Git::Repository).to receive(:new) .with('test_second_storage', project.repository.raw.relative_path, project.repository.gl_repository, project.repository.full_path) .and_return(project_repository_double) + allow(Gitlab::Git::Repository).to receive(:new) + .with('default', project.repository.raw.relative_path, nil, nil) + .and_return(original_project_repository_double) allow(Gitlab::Git::Repository).to receive(:new) .with('test_second_storage', repository.raw.relative_path, repository.gl_repository, repository.full_path) .and_return(repository_double) + allow(Gitlab::Git::Repository).to receive(:new) + .with('default', repository.raw.relative_path, nil, nil) + .and_return(original_repository_double) end context 'when the move succeeds', :clean_gitlab_redis_shared_state do @@ -35,8 +43,8 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| allow(repository_double).to receive(:checksum) .and_return(repository_checksum) - expect(GitlabShellWorker).to receive(:perform_async).with(:mv_repository, 'default', anything, anything) - .twice.and_call_original + expect(original_project_repository_double).to receive(:remove) + expect(original_repository_double).to receive(:remove) end it "moves the project and its #{repository_type} repository to the new storage and unmarks the repository as read only" do @@ -110,13 +118,36 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| .with(repository.raw) .and_raise(Gitlab::Git::CommandError) - expect(GitlabShellWorker).not_to receive(:perform_async) - result = subject.execute expect(result).to be_error expect(project).not_to be_repository_read_only expect(project.repository_storage).to eq('default') + expect(repository_storage_move).to be_failed + end + end + + context "when the cleanup of the #{repository_type} repository fails" do + it 'sets the correct state' do + allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('default').and_call_original + allow(Gitlab::GitalyClient).to receive(:filesystem_id).with('test_second_storage').and_return(SecureRandom.uuid) + allow(project_repository_double).to receive(:replicate) + .with(project.repository.raw) + allow(project_repository_double).to receive(:checksum) + .and_return(project_repository_checksum) + allow(original_project_repository_double).to receive(:remove) + allow(repository_double).to receive(:replicate) + .with(repository.raw) + allow(repository_double).to receive(:checksum) + .and_return(repository_checksum) + + expect(original_repository_double).to receive(:remove) + .and_raise(Gitlab::Git::CommandError) + + result = subject.execute + + expect(result).to be_error + expect(repository_storage_move).to be_cleanup_failed end end @@ -134,8 +165,6 @@ RSpec.shared_examples 'moves repository to another storage' do |repository_type| allow(repository_double).to receive(:checksum) .and_return('not matching checksum') - expect(GitlabShellWorker).not_to receive(:perform_async) - result = subject.execute expect(result).to be_error diff --git a/spec/support/shared_examples/services/resource_events/change_milestone_service_shared_examples.rb b/spec/support/shared_examples/services/resource_events/change_milestone_service_shared_examples.rb index ef41c2fcc13..d70ed707822 100644 --- a/spec/support/shared_examples/services/resource_events/change_milestone_service_shared_examples.rb +++ b/spec/support/shared_examples/services/resource_events/change_milestone_service_shared_examples.rb @@ -1,49 +1,63 @@ # frozen_string_literal: true -RSpec.shared_examples 'a milestone events creator' do +RSpec.shared_examples 'timebox(milestone or iteration) resource events creator' do |timebox_event_class| let_it_be(:user) { create(:user) } - let(:created_at_time) { Time.utc(2019, 12, 30) } - let(:service) { described_class.new(resource, user, created_at: created_at_time, old_milestone: nil) } - - context 'when milestone is present' do - let_it_be(:milestone) { create(:milestone) } + context 'when milestone/iteration is added' do + let(:service) { described_class.new(resource, user, add_timebox_args) } before do - resource.milestone = milestone + set_timebox(timebox_event_class, timebox) end it 'creates the expected event record' do - expect { service.execute }.to change { ResourceMilestoneEvent.count }.by(1) + expect { service.execute }.to change { timebox_event_class.count }.by(1) - expect_event_record(ResourceMilestoneEvent.last, action: 'add', milestone: milestone, state: 'opened') + expect_event_record(timebox_event_class, timebox_event_class.last, action: 'add', state: 'opened', timebox: timebox) end end - context 'when milestones is not present' do + context 'when milestone/iteration is removed' do + let(:service) { described_class.new(resource, user, remove_timebox_args) } + before do - resource.milestone = nil + set_timebox(timebox_event_class, nil) end - let(:old_milestone) { create(:milestone, project: resource.project) } - let(:service) { described_class.new(resource, user, created_at: created_at_time, old_milestone: old_milestone) } - it 'creates the expected event records' do - expect { service.execute }.to change { ResourceMilestoneEvent.count }.by(1) + expect { service.execute }.to change { timebox_event_class.count }.by(1) - expect_event_record(ResourceMilestoneEvent.last, action: 'remove', milestone: old_milestone, state: 'opened') + expect_event_record(timebox_event_class, timebox_event_class.last, action: 'remove', timebox: timebox, state: 'opened') end end - def expect_event_record(event, expected_attrs) + def expect_event_record(timebox_event_class, event, expected_attrs) expect(event.action).to eq(expected_attrs[:action]) - expect(event.state).to eq(expected_attrs[:state]) expect(event.user).to eq(user) expect(event.issue).to eq(resource) if resource.is_a?(Issue) expect(event.issue).to be_nil unless resource.is_a?(Issue) expect(event.merge_request).to eq(resource) if resource.is_a?(MergeRequest) expect(event.merge_request).to be_nil unless resource.is_a?(MergeRequest) - expect(event.milestone).to eq(expected_attrs[:milestone]) expect(event.created_at).to eq(created_at_time) + expect_timebox(timebox_event_class, event, expected_attrs) + end + + def set_timebox(timebox_event_class, timebox) + case timebox_event_class.name + when 'ResourceMilestoneEvent' + resource.milestone = timebox + when 'ResourceIterationEvent' + resource.iteration = timebox + end + end + + def expect_timebox(timebox_event_class, event, expected_attrs) + case timebox_event_class.name + when 'ResourceMilestoneEvent' + expect(event.state).to eq(expected_attrs[:state]) + expect(event.milestone).to eq(expected_attrs[:timebox]) + when 'ResourceIterationEvent' + expect(event.iteration).to eq(expected_attrs[:timebox]) + end end end diff --git a/spec/support/shared_examples/services/wiki_pages/create_service_shared_examples.rb b/spec/support/shared_examples/services/wiki_pages/create_service_shared_examples.rb index ebe78c299a5..980a752cf86 100644 --- a/spec/support/shared_examples/services/wiki_pages/create_service_shared_examples.rb +++ b/spec/support/shared_examples/services/wiki_pages/create_service_shared_examples.rb @@ -16,8 +16,10 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type| subject(:service) { described_class.new(container: container, current_user: user, params: opts) } it 'creates wiki page with valid attributes' do - page = service.execute + response = service.execute + page = response.payload[:page] + expect(response).to be_success expect(page).to be_valid expect(page).to be_persisted expect(page.title).to eq(opts[:title]) @@ -77,7 +79,12 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type| end it 'reports the error' do - expect(service.execute).to be_invalid + response = service.execute + page = response.payload[:page] + + expect(response).to be_error + + expect(page).to be_invalid .and have_attributes(errors: be_present) end end diff --git a/spec/support/shared_examples/services/wikis/create_attachment_service_shared_examples.rb b/spec/support/shared_examples/services/wikis/create_attachment_service_shared_examples.rb index 541e332e3a1..555a6d5eed0 100644 --- a/spec/support/shared_examples/services/wikis/create_attachment_service_shared_examples.rb +++ b/spec/support/shared_examples/services/wikis/create_attachment_service_shared_examples.rb @@ -14,6 +14,7 @@ RSpec.shared_examples 'Wikis::CreateAttachmentService#execute' do |container_typ file_content: 'Content of attachment' } end + let(:opts) { file_opts } let(:service) { Wikis::CreateAttachmentService.new(container: container, current_user: user, params: opts) } |