diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-23 15:09:54 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-23 15:09:54 +0000 |
commit | 9dbca64417abbec779a219b9e0df9d289d945032 (patch) | |
tree | 3721592153aa2c991a4a5fe686eb4471dabd98b4 /spec | |
parent | 5c9f6c66fabf22927e862b2b60362e4ea25b250b (diff) | |
download | gitlab-ce-9dbca64417abbec779a219b9e0df9d289d945032.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
13 files changed, 431 insertions, 320 deletions
diff --git a/spec/fixtures/api/schemas/public_api/v4/snippet_repository_storage_move.json b/spec/fixtures/api/schemas/public_api/v4/snippet_repository_storage_move.json new file mode 100644 index 00000000000..f51e7e8edc5 --- /dev/null +++ b/spec/fixtures/api/schemas/public_api/v4/snippet_repository_storage_move.json @@ -0,0 +1,20 @@ +{ + "type": "object", + "required": [ + "id", + "created_at", + "state", + "source_storage_name", + "destination_storage_name", + "snippet" + ], + "properties" : { + "id": { "type": "integer" }, + "created_at": { "type": "date" }, + "state": { "type": "string" }, + "source_storage_name": { "type": "string" }, + "destination_storage_name": { "type": "string" }, + "snippet": { "type": "object" } + }, + "additionalProperties": false +} diff --git a/spec/fixtures/api/schemas/public_api/v4/snippet_repository_storage_moves.json b/spec/fixtures/api/schemas/public_api/v4/snippet_repository_storage_moves.json new file mode 100644 index 00000000000..292bb335539 --- /dev/null +++ b/spec/fixtures/api/schemas/public_api/v4/snippet_repository_storage_moves.json @@ -0,0 +1,6 @@ +{ + "type": "array", + "items": { + "$ref": "./snippet_repository_storage_move.json" + } +} diff --git a/spec/frontend/boards/components/board_configuration_options_spec.js b/spec/frontend/boards/components/board_configuration_options_spec.js index e9a1cb6a4e8..d9614c254e2 100644 --- a/spec/frontend/boards/components/board_configuration_options_spec.js +++ b/spec/frontend/boards/components/board_configuration_options_spec.js @@ -3,38 +3,30 @@ import BoardConfigurationOptions from '~/boards/components/board_configuration_o describe('BoardConfigurationOptions', () => { let wrapper; - const board = { hide_backlog_list: false, hide_closed_list: false }; const defaultProps = { - currentBoard: board, - board, - isNewForm: false, + hideBacklogList: false, + hideClosedList: false, }; - const createComponent = () => { + const createComponent = (props = {}) => { wrapper = shallowMount(BoardConfigurationOptions, { - propsData: { ...defaultProps }, + propsData: { ...defaultProps, ...props }, }); }; - beforeEach(() => { - createComponent(); - }); - afterEach(() => { wrapper.destroy(); }); - const backlogListCheckbox = el => el.find('[data-testid="backlog-list-checkbox"]'); - const closedListCheckbox = el => el.find('[data-testid="closed-list-checkbox"]'); + const backlogListCheckbox = () => wrapper.find('[data-testid="backlog-list-checkbox"]'); + const closedListCheckbox = () => wrapper.find('[data-testid="closed-list-checkbox"]'); const checkboxAssert = (backlogCheckbox, closedCheckbox) => { - expect(backlogListCheckbox(wrapper).attributes('checked')).toEqual( + expect(backlogListCheckbox().attributes('checked')).toEqual( backlogCheckbox ? undefined : 'true', ); - expect(closedListCheckbox(wrapper).attributes('checked')).toEqual( - closedCheckbox ? undefined : 'true', - ); + expect(closedListCheckbox().attributes('checked')).toEqual(closedCheckbox ? undefined : 'true'); }; it.each` @@ -45,15 +37,28 @@ describe('BoardConfigurationOptions', () => { ${false} | ${false} `( 'renders two checkbox when one is $backlogCheckboxValue and other is $closedCheckboxValue', - async ({ backlogCheckboxValue, closedCheckboxValue }) => { - await wrapper.setData({ + ({ backlogCheckboxValue, closedCheckboxValue }) => { + createComponent({ hideBacklogList: backlogCheckboxValue, hideClosedList: closedCheckboxValue, }); - - return wrapper.vm.$nextTick().then(() => { - checkboxAssert(backlogCheckboxValue, closedCheckboxValue); - }); + checkboxAssert(backlogCheckboxValue, closedCheckboxValue); }, ); + + it('emits a correct value on backlog checkbox change', () => { + createComponent(); + + backlogListCheckbox().vm.$emit('change'); + + expect(wrapper.emitted('update:hideBacklogList')).toEqual([[true]]); + }); + + it('emits a correct value on closed checkbox change', () => { + createComponent(); + + closedListCheckbox().vm.$emit('change'); + + expect(wrapper.emitted('update:hideClosedList')).toEqual([[true]]); + }); }); diff --git a/spec/frontend/boards/components/board_form_spec.js b/spec/frontend/boards/components/board_form_spec.js index 3b15cbb6b7e..7b143f64e93 100644 --- a/spec/frontend/boards/components/board_form_spec.js +++ b/spec/frontend/boards/components/board_form_spec.js @@ -1,19 +1,18 @@ import { shallowMount } from '@vue/test-utils'; -import AxiosMockAdapter from 'axios-mock-adapter'; import { TEST_HOST } from 'jest/helpers/test_constants'; import { GlModal } from '@gitlab/ui'; import waitForPromises from 'helpers/wait_for_promises'; -import axios from '~/lib/utils/axios_utils'; import { visitUrl } from '~/lib/utils/url_utility'; import boardsStore from '~/boards/stores/boards_store'; import BoardForm from '~/boards/components/board_form.vue'; -import BoardConfigurationOptions from '~/boards/components/board_configuration_options.vue'; -import createBoardMutation from '~/boards/graphql/board.mutation.graphql'; +import updateBoardMutation from '~/boards/graphql/board_update.mutation.graphql'; +import createBoardMutation from '~/boards/graphql/board_create.mutation.graphql'; jest.mock('~/lib/utils/url_utility', () => ({ visitUrl: jest.fn().mockName('visitUrlMock'), + stripFinalUrlSegment: jest.requireActual('~/lib/utils/url_utility').stripFinalUrlSegment, })); const currentBoard = { @@ -28,18 +27,6 @@ const currentBoard = { hide_closed_list: false, }; -const boardDefaults = { - id: false, - name: '', - labels: [], - milestone_id: undefined, - assignee: {}, - assignee_id: undefined, - weight: null, - hide_backlog_list: false, - hide_closed_list: false, -}; - const defaultProps = { canAdminBoard: false, labelsPath: `${TEST_HOST}/labels/path`, @@ -51,18 +38,21 @@ const endpoints = { boardsEndpoint: 'test-endpoint', }; -const mutate = jest.fn().mockResolvedValue({}); +const mutate = jest.fn().mockResolvedValue({ + data: { + createBoard: { board: { id: 'gid://gitlab/Board/123' } }, + updateBoard: { board: { id: 'gid://gitlab/Board/321' } }, + }, +}); describe('BoardForm', () => { let wrapper; - let axiosMock; const findModal = () => wrapper.find(GlModal); const findModalActionPrimary = () => findModal().props('actionPrimary'); const findForm = () => wrapper.find('[data-testid="board-form"]'); const findFormWrapper = () => wrapper.find('[data-testid="board-form-wrapper"]'); const findDeleteConfirmation = () => wrapper.find('[data-testid="delete-confirmation-message"]'); - const findConfigurationOptions = () => wrapper.find(BoardConfigurationOptions); const findInput = () => wrapper.find('#board-new-name'); const createComponent = (props, data) => { @@ -86,13 +76,12 @@ describe('BoardForm', () => { }; beforeEach(() => { - axiosMock = new AxiosMockAdapter(axios); + delete window.location; }); afterEach(() => { wrapper.destroy(); wrapper = null; - axiosMock.restore(); boardsStore.state.currentPage = null; }); @@ -145,7 +134,7 @@ describe('BoardForm', () => { }); it('clears the form', () => { - expect(findConfigurationOptions().props('board')).toEqual(boardDefaults); + expect(findInput().element.value).toBe(''); }); it('shows a correct title about creating a board', () => { @@ -164,18 +153,9 @@ describe('BoardForm', () => { it('renders form wrapper', () => { expect(findFormWrapper().exists()).toBe(true); }); - - it('passes a true isNewForm prop to BoardConfigurationOptions component', () => { - expect(findConfigurationOptions().props('isNewForm')).toBe(true); - }); }); describe('when submitting a create event', () => { - beforeEach(() => { - const url = `${endpoints.boardsEndpoint}.json`; - axiosMock.onPost(url).reply(200, { id: '2', board_path: 'new path' }); - }); - it('does not call API if board name is empty', async () => { createComponent({ canAdminBoard: true }); findInput().trigger('keyup.enter', { metaKey: true }); @@ -185,7 +165,8 @@ describe('BoardForm', () => { expect(mutate).not.toHaveBeenCalled(); }); - it('calls REST and GraphQL API and redirects to correct page', async () => { + it('calls a correct GraphQL mutation and redirects to correct page from existing board', async () => { + window.location = new URL('https://test/boards/1'); createComponent({ canAdminBoard: true }); findInput().value = 'Test name'; @@ -194,19 +175,40 @@ describe('BoardForm', () => { await waitForPromises(); - expect(axiosMock.history.post[0].data).toBe( - JSON.stringify({ board: { ...boardDefaults, name: 'test', label_ids: [''] } }), - ); + expect(mutate).toHaveBeenCalledWith({ + mutation: createBoardMutation, + variables: { + input: expect.objectContaining({ + name: 'test', + }), + }, + }); + + await waitForPromises(); + expect(visitUrl).toHaveBeenCalledWith('123'); + }); + + it('calls a correct GraphQL mutation and redirects to correct page from boards list', async () => { + window.location = new URL('https://test/boards'); + createComponent({ canAdminBoard: true }); + + findInput().value = 'Test name'; + findInput().trigger('input'); + findInput().trigger('keyup.enter', { metaKey: true }); + + await waitForPromises(); expect(mutate).toHaveBeenCalledWith({ mutation: createBoardMutation, variables: { - id: 'gid://gitlab/Board/2', + input: expect.objectContaining({ + name: 'test', + }), }, }); await waitForPromises(); - expect(visitUrl).toHaveBeenCalledWith('new path'); + expect(visitUrl).toHaveBeenCalledWith('boards/123'); }); }); }); @@ -222,7 +224,7 @@ describe('BoardForm', () => { }); it('clears the form', () => { - expect(findConfigurationOptions().props('board')).toEqual(currentBoard); + expect(findInput().element.value).toEqual(currentBoard.name); }); it('shows a correct title about creating a board', () => { @@ -241,35 +243,28 @@ describe('BoardForm', () => { it('renders form wrapper', () => { expect(findFormWrapper().exists()).toBe(true); }); - - it('passes a false isNewForm prop to BoardConfigurationOptions component', () => { - expect(findConfigurationOptions().props('isNewForm')).toBe(false); - }); }); describe('when submitting an update event', () => { - beforeEach(() => { - const url = endpoints.boardsEndpoint; - axiosMock.onPut(url).reply(200, { board_path: 'new path' }); - }); - it('calls REST and GraphQL API with correct parameters', async () => { + window.location = new URL('https://test/boards/1'); createComponent({ canAdminBoard: true }); findInput().trigger('keyup.enter', { metaKey: true }); await waitForPromises(); - expect(axiosMock.history.put[0].data).toBe( - JSON.stringify({ board: { ...currentBoard, label_ids: [''] } }), - ); - expect(mutate).toHaveBeenCalledWith({ - mutation: createBoardMutation, + mutation: updateBoardMutation, variables: { - id: `gid://gitlab/Board/${currentBoard.id}`, + input: expect.objectContaining({ + id: `gid://gitlab/Board/${currentBoard.id}`, + }), }, }); + + await waitForPromises(); + expect(visitUrl).toHaveBeenCalledWith('321'); }); }); }); diff --git a/spec/frontend/ide/components/pipelines/list_spec.js b/spec/frontend/ide/components/pipelines/list_spec.js index 0238fff6aa9..a1fbfd96c31 100644 --- a/spec/frontend/ide/components/pipelines/list_spec.js +++ b/spec/frontend/ide/components/pipelines/list_spec.js @@ -1,4 +1,5 @@ -import { shallowMount, createLocalVue } from '@vue/test-utils'; +import { shallowMount } from '@vue/test-utils'; +import Vue from 'vue'; import Vuex from 'vuex'; import { GlLoadingIcon, GlTab } from '@gitlab/ui'; import { TEST_HOST } from 'helpers/test_constants'; @@ -8,8 +9,7 @@ import JobsList from '~/ide/components/jobs/list.vue'; import CiIcon from '~/vue_shared/components/ci_icon.vue'; import IDEServices from '~/ide/services'; -const localVue = createLocalVue(); -localVue.use(Vuex); +Vue.use(Vuex); jest.mock('~/ide/services', () => ({ pingUsage: jest.fn(), @@ -59,9 +59,6 @@ describe('IDE pipelines list', () => { failedStages: failedStagesGetterMock, pipelineFailed: () => false, }, - methods: { - fetchLatestPipeline: jest.fn(), - }, }, }, }); @@ -69,7 +66,6 @@ describe('IDE pipelines list', () => { const createComponent = (state = {}, pipelinesState = {}) => { wrapper = shallowMount(List, { - localVue, store: createStore(state, pipelinesState), }); }; diff --git a/spec/frontend/tooltips/components/tooltips_spec.js b/spec/frontend/tooltips/components/tooltips_spec.js index 50848ca2978..0b8c76db11d 100644 --- a/spec/frontend/tooltips/components/tooltips_spec.js +++ b/spec/frontend/tooltips/components/tooltips_spec.js @@ -51,6 +51,16 @@ describe('tooltips/components/tooltips.vue', () => { expect(wrapper.find(GlTooltip).props('target')).toBe(target); }); + it('does not attach a tooltip to a target with empty title', async () => { + target.setAttribute('title', ''); + + wrapper.vm.addTooltips([target]); + + await wrapper.vm.$nextTick(); + + expect(wrapper.find(GlTooltip).exists()).toBe(false); + }); + it('does not attach a tooltip twice to the same element', async () => { wrapper.vm.addTooltips([target]); wrapper.vm.addTooltips([target]); diff --git a/spec/frontend/tooltips/index_spec.js b/spec/frontend/tooltips/index_spec.js index 511003fdb8f..86da1caab3e 100644 --- a/spec/frontend/tooltips/index_spec.js +++ b/spec/frontend/tooltips/index_spec.js @@ -42,7 +42,7 @@ describe('tooltips/index.js', () => { }; beforeEach(() => { - window.gon.glTooltipsEnabled = true; + window.gon.features = { glTooltips: true }; }); afterEach(() => { @@ -149,7 +149,7 @@ describe('tooltips/index.js', () => { describe('when glTooltipsEnabled feature flag is disabled', () => { beforeEach(() => { - window.gon.glTooltipsEnabled = false; + window.gon.features.glTooltips = false; }); it.each` diff --git a/spec/requests/api/graphql/user/starred_projects_query_spec.rb b/spec/requests/api/graphql/user/starred_projects_query_spec.rb index b098058a735..6cb02068f2a 100644 --- a/spec/requests/api/graphql/user/starred_projects_query_spec.rb +++ b/spec/requests/api/graphql/user/starred_projects_query_spec.rb @@ -17,7 +17,13 @@ RSpec.describe 'Getting starredProjects of the user' do let_it_be(:user, reload: true) { create(:user) } let(:user_fields) { 'starredProjects { nodes { id } }' } - let(:starred_projects) { graphql_data_at(:user, :starred_projects, :nodes) } + let(:current_user) { nil } + + let(:starred_projects) do + post_graphql(query, current_user: current_user) + + graphql_data_at(:user, :starred_projects, :nodes) + end before do project_b.add_reporter(user) @@ -26,11 +32,13 @@ RSpec.describe 'Getting starredProjects of the user' do user.toggle_star(project_a) user.toggle_star(project_b) user.toggle_star(project_c) - - post_graphql(query) end - it_behaves_like 'a working graphql query' + it_behaves_like 'a working graphql query' do + before do + post_graphql(query) + end + end it 'found only public project' do expect(starred_projects).to contain_exactly( @@ -41,10 +49,6 @@ RSpec.describe 'Getting starredProjects of the user' do context 'the current user is the user' do let(:current_user) { user } - before do - post_graphql(query, current_user: current_user) - end - it 'found all projects' do expect(starred_projects).to contain_exactly( a_hash_including('id' => global_id_of(project_a)), @@ -56,11 +60,10 @@ RSpec.describe 'Getting starredProjects of the user' do context 'the current user is a member of a private project the user starred' do let_it_be(:other_user) { create(:user) } + let(:current_user) { other_user } before do project_b.add_reporter(other_user) - - post_graphql(query, current_user: other_user) end it 'finds public and member projects' do @@ -74,7 +77,6 @@ RSpec.describe 'Getting starredProjects of the user' do context 'the user has a private profile' do before do user.update!(private_profile: true) - post_graphql(query, current_user: current_user) end context 'the current user does not have access to view the private profile of the user' do diff --git a/spec/requests/api/project_repository_storage_moves_spec.rb b/spec/requests/api/project_repository_storage_moves_spec.rb index 15e69c2aa16..5e200312d1f 100644 --- a/spec/requests/api/project_repository_storage_moves_spec.rb +++ b/spec/requests/api/project_repository_storage_moves_spec.rb @@ -3,220 +3,10 @@ require 'spec_helper' RSpec.describe API::ProjectRepositoryStorageMoves do - include AccessMatchersForRequest - - let_it_be(:user) { create(:admin) } - let_it_be(:project) { create(:project, :repository).tap { |project| project.track_project_repository } } - let_it_be(:storage_move) { create(:project_repository_storage_move, :scheduled, container: project) } - - shared_examples 'get single project repository storage move' do - let(:project_repository_storage_move_id) { storage_move.id } - - def get_project_repository_storage_move - get api(url, user) - end - - it 'returns a project repository storage move' do - get_project_repository_storage_move - - expect(response).to have_gitlab_http_status(:ok) - expect(response).to match_response_schema('public_api/v4/project_repository_storage_move') - expect(json_response['id']).to eq(storage_move.id) - expect(json_response['state']).to eq(storage_move.human_state_name) - end - - context 'non-existent project repository storage move' do - let(:project_repository_storage_move_id) { non_existing_record_id } - - it 'returns not found' do - get_project_repository_storage_move - - expect(response).to have_gitlab_http_status(:not_found) - end - end - - describe 'permissions' do - it { expect { get_project_repository_storage_move }.to be_allowed_for(:admin) } - it { expect { get_project_repository_storage_move }.to be_denied_for(:user) } - end - end - - shared_examples 'get project repository storage move list' do - def get_project_repository_storage_moves - get api(url, user) - end - - it 'returns project repository storage moves' do - get_project_repository_storage_moves - - expect(response).to have_gitlab_http_status(:ok) - expect(response).to include_pagination_headers - expect(response).to match_response_schema('public_api/v4/project_repository_storage_moves') - expect(json_response.size).to eq(1) - expect(json_response.first['id']).to eq(storage_move.id) - expect(json_response.first['state']).to eq(storage_move.human_state_name) - end - - it 'avoids N+1 queries', :request_store do - # prevent `let` from polluting the control - get_project_repository_storage_moves - - control = ActiveRecord::QueryRecorder.new { get_project_repository_storage_moves } - - create(:project_repository_storage_move, :scheduled, container: project) - - expect { get_project_repository_storage_moves }.not_to exceed_query_limit(control) - end - - it 'returns the most recently created first' do - storage_move_oldest = create(:project_repository_storage_move, :scheduled, container: project, created_at: 2.days.ago) - storage_move_middle = create(:project_repository_storage_move, :scheduled, container: project, created_at: 1.day.ago) - - get_project_repository_storage_moves - - json_ids = json_response.map {|storage_move| storage_move['id'] } - expect(json_ids).to eq([ - storage_move.id, - storage_move_middle.id, - storage_move_oldest.id - ]) - end - - describe 'permissions' do - it { expect { get_project_repository_storage_moves }.to be_allowed_for(:admin) } - it { expect { get_project_repository_storage_moves }.to be_denied_for(:user) } - end - end - - describe 'GET /project_repository_storage_moves' do - it_behaves_like 'get project repository storage move list' do - let(:url) { '/project_repository_storage_moves' } - end - end - - describe 'GET /project_repository_storage_moves/:repository_storage_move_id' do - it_behaves_like 'get single project repository storage move' do - let(:url) { "/project_repository_storage_moves/#{project_repository_storage_move_id}" } - end - end - - describe 'GET /projects/:id/repository_storage_moves' do - it_behaves_like 'get project repository storage move list' do - let(:url) { "/projects/#{project.id}/repository_storage_moves" } - end - end - - describe 'GET /projects/:id/repository_storage_moves/:repository_storage_move_id' do - it_behaves_like 'get single project repository storage move' do - let(:url) { "/projects/#{project.id}/repository_storage_moves/#{project_repository_storage_move_id}" } - end - end - - describe 'POST /projects/:id/repository_storage_moves' do - let(:url) { "/projects/#{project.id}/repository_storage_moves" } - let(:destination_storage_name) { 'test_second_storage' } - - def create_project_repository_storage_move - post api(url, user), params: { destination_storage_name: destination_storage_name } - end - - before do - stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' }) - end - - it 'schedules a project repository storage move' do - create_project_repository_storage_move - - storage_move = project.repository_storage_moves.last - - expect(response).to have_gitlab_http_status(:created) - expect(response).to match_response_schema('public_api/v4/project_repository_storage_move') - expect(json_response['id']).to eq(storage_move.id) - expect(json_response['state']).to eq('scheduled') - expect(json_response['source_storage_name']).to eq('default') - expect(json_response['destination_storage_name']).to eq(destination_storage_name) - end - - describe 'permissions' do - it { expect { create_project_repository_storage_move }.to be_allowed_for(:admin) } - it { expect { create_project_repository_storage_move }.to be_denied_for(:user) } - end - - context 'destination_storage_name is missing' do - let(:destination_storage_name) { nil } - - it 'schedules a project repository storage move' do - create_project_repository_storage_move - - storage_move = project.repository_storage_moves.last - - expect(response).to have_gitlab_http_status(:created) - expect(response).to match_response_schema('public_api/v4/project_repository_storage_move') - expect(json_response['id']).to eq(storage_move.id) - expect(json_response['state']).to eq('scheduled') - expect(json_response['source_storage_name']).to eq('default') - expect(json_response['destination_storage_name']).to be_present - end - end - end - - describe 'POST /project_repository_storage_moves' do - let(:source_storage_name) { 'default' } - let(:destination_storage_name) { 'test_second_storage' } - - def create_project_repository_storage_moves - post api('/project_repository_storage_moves', user), params: { - source_storage_name: source_storage_name, - destination_storage_name: destination_storage_name - } - end - - before do - stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' }) - end - - it 'schedules the worker' do - expect(ProjectScheduleBulkRepositoryShardMovesWorker).to receive(:perform_async).with(source_storage_name, destination_storage_name) - - create_project_repository_storage_moves - - expect(response).to have_gitlab_http_status(:accepted) - end - - context 'source_storage_name is invalid' do - let(:destination_storage_name) { 'not-a-real-storage' } - - it 'gives an error' do - create_project_repository_storage_moves - - expect(response).to have_gitlab_http_status(:bad_request) - end - end - - context 'destination_storage_name is missing' do - let(:destination_storage_name) { nil } - - it 'schedules the worker' do - expect(ProjectScheduleBulkRepositoryShardMovesWorker).to receive(:perform_async).with(source_storage_name, destination_storage_name) - - create_project_repository_storage_moves - - expect(response).to have_gitlab_http_status(:accepted) - end - end - - context 'destination_storage_name is invalid' do - let(:destination_storage_name) { 'not-a-real-storage' } - - it 'gives an error' do - create_project_repository_storage_moves - - expect(response).to have_gitlab_http_status(:bad_request) - end - end - - describe 'normal user' do - it { expect { create_project_repository_storage_moves }.to be_denied_for(:user) } - end + it_behaves_like 'repository_storage_moves API', 'projects' do + let_it_be(:container) { create(:project, :repository).tap { |project| project.track_project_repository } } + let_it_be(:storage_move) { create(:project_repository_storage_move, :scheduled, container: container) } + let(:repository_storage_move_factory) { :project_repository_storage_move } + let(:bulk_worker_klass) { ProjectScheduleBulkRepositoryShardMovesWorker } end end diff --git a/spec/requests/api/snippet_repository_storage_moves_spec.rb b/spec/requests/api/snippet_repository_storage_moves_spec.rb new file mode 100644 index 00000000000..edb92569823 --- /dev/null +++ b/spec/requests/api/snippet_repository_storage_moves_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::SnippetRepositoryStorageMoves do + it_behaves_like 'repository_storage_moves API', 'snippets' do + let_it_be(:container) { create(:snippet, :repository).tap { |snippet| snippet.create_repository } } + let_it_be(:storage_move) { create(:snippet_repository_storage_move, :scheduled, container: container) } + let(:repository_storage_move_factory) { :snippet_repository_storage_move } + let(:bulk_worker_klass) { SnippetScheduleBulkRepositoryShardMovesWorker } + end +end diff --git a/spec/support/shared_examples/features/wiki/user_views_wiki_sidebar_shared_examples.rb b/spec/support/shared_examples/features/wiki/user_views_wiki_sidebar_shared_examples.rb index a7ba7a8ad07..9be7d739065 100644 --- a/spec/support/shared_examples/features/wiki/user_views_wiki_sidebar_shared_examples.rb +++ b/spec/support/shared_examples/features/wiki/user_views_wiki_sidebar_shared_examples.rb @@ -17,23 +17,55 @@ RSpec.shared_examples 'User views wiki sidebar' do create(:wiki_page, wiki: wiki, title: 'another', content: 'another') end - it 'renders a default sidebar when there is no customized sidebar' do - visit wiki_path(wiki) + context 'when there is no custom sidebar' do + before do + visit wiki_path(wiki) + end - expect(page).to have_content('another') - expect(page).not_to have_link('View All Pages') + it 'renders a default sidebar' do + within('.right-sidebar') do + expect(page).to have_content('another') + expect(page).not_to have_link('View All Pages') + end + end + + it 'can create a custom sidebar' do + click_on 'Edit sidebar' + fill_in :wiki_content, with: 'My custom sidebar' + click_on 'Create page' + + within('.right-sidebar') do + expect(page).to have_content('My custom sidebar') + expect(page).not_to have_content('another') + end + end end - context 'when there is a customized sidebar' do + context 'when there is a custom sidebar' do before do - create(:wiki_page, wiki: wiki, title: '_sidebar', content: 'My customized sidebar') - end + create(:wiki_page, wiki: wiki, title: '_sidebar', content: 'My custom sidebar') - it 'renders my customized sidebar instead of the default one' do visit wiki_path(wiki) + end + + it 'renders the custom sidebar instead of the default one' do + within('.right-sidebar') do + expect(page).to have_content('My custom sidebar') + expect(page).not_to have_content('another') + end + end + + it 'can edit the custom sidebar' do + click_on 'Edit sidebar' + + expect(page).to have_field(:wiki_content, with: 'My custom sidebar') + + fill_in :wiki_content, with: 'My other custom sidebar' + click_on 'Save changes' - expect(page).to have_content('My customized sidebar') - expect(page).not_to have_content('Another') + within('.right-sidebar') do + expect(page).to have_content('My other custom sidebar') + end end end end diff --git a/spec/support/shared_examples/requests/api/repository_storage_moves_shared_examples.rb b/spec/support/shared_examples/requests/api/repository_storage_moves_shared_examples.rb new file mode 100644 index 00000000000..b2970fd265d --- /dev/null +++ b/spec/support/shared_examples/requests/api/repository_storage_moves_shared_examples.rb @@ -0,0 +1,219 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'repository_storage_moves API' do |container_type| + include AccessMatchersForRequest + + let_it_be(:user) { create(:admin) } + + shared_examples 'get single container repository storage move' do + let(:repository_storage_move_id) { storage_move.id } + + def get_container_repository_storage_move + get api(url, user) + end + + it 'returns a container repository storage move', :aggregate_failures do + get_container_repository_storage_move + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_move") + expect(json_response['id']).to eq(storage_move.id) + expect(json_response['state']).to eq(storage_move.human_state_name) + end + + context 'non-existent container repository storage move' do + let(:repository_storage_move_id) { non_existing_record_id } + + it 'returns not found' do + get_container_repository_storage_move + + expect(response).to have_gitlab_http_status(:not_found) + end + end + + describe 'permissions' do + it { expect { get_container_repository_storage_move }.to be_allowed_for(:admin) } + it { expect { get_container_repository_storage_move }.to be_denied_for(:user) } + end + end + + shared_examples 'get container repository storage move list' do + def get_container_repository_storage_moves + get api(url, user) + end + + it 'returns container repository storage moves', :aggregate_failures do + get_container_repository_storage_moves + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to include_pagination_headers + expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_moves") + expect(json_response.size).to eq(1) + expect(json_response.first['id']).to eq(storage_move.id) + expect(json_response.first['state']).to eq(storage_move.human_state_name) + end + + it 'avoids N+1 queries', :request_store do + # prevent `let` from polluting the control + get_container_repository_storage_moves + + control = ActiveRecord::QueryRecorder.new { get_container_repository_storage_moves } + + create(repository_storage_move_factory, :scheduled, container: container) + + expect { get_container_repository_storage_moves }.not_to exceed_query_limit(control) + end + + it 'returns the most recently created first' do + storage_move_oldest = create(repository_storage_move_factory, :scheduled, container: container, created_at: 2.days.ago) + storage_move_middle = create(repository_storage_move_factory, :scheduled, container: container, created_at: 1.day.ago) + + get_container_repository_storage_moves + + json_ids = json_response.map {|storage_move| storage_move['id'] } + expect(json_ids).to eq([ + storage_move.id, + storage_move_middle.id, + storage_move_oldest.id + ]) + end + + describe 'permissions' do + it { expect { get_container_repository_storage_moves }.to be_allowed_for(:admin) } + it { expect { get_container_repository_storage_moves }.to be_denied_for(:user) } + end + end + + describe "GET /#{container_type}/:id/repository_storage_moves" do + it_behaves_like 'get container repository storage move list' do + let(:url) { "/#{container_type}/#{container.id}/repository_storage_moves" } + end + end + + describe "GET /#{container_type}/:id/repository_storage_moves/:repository_storage_move_id" do + it_behaves_like 'get single container repository storage move' do + let(:url) { "/#{container_type}/#{container.id}/repository_storage_moves/#{repository_storage_move_id}" } + end + end + + describe "GET /#{container_type.singularize}_repository_storage_moves" do + it_behaves_like 'get container repository storage move list' do + let(:url) { "/#{container_type.singularize}_repository_storage_moves" } + end + end + + describe "GET /#{container_type.singularize}_repository_storage_moves/:repository_storage_move_id" do + it_behaves_like 'get single container repository storage move' do + let(:url) { "/#{container_type.singularize}_repository_storage_moves/#{repository_storage_move_id}" } + end + end + + describe "POST /#{container_type}/:id/repository_storage_moves" do + let(:url) { "/#{container_type}/#{container.id}/repository_storage_moves" } + let(:destination_storage_name) { 'test_second_storage' } + + def create_container_repository_storage_move + post api(url, user), params: { destination_storage_name: destination_storage_name } + end + + before do + stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' }) + end + + it 'schedules a container repository storage move', :aggregate_failures do + create_container_repository_storage_move + + storage_move = container.repository_storage_moves.last + + expect(response).to have_gitlab_http_status(:created) + expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_move") + expect(json_response['id']).to eq(storage_move.id) + expect(json_response['state']).to eq('scheduled') + expect(json_response['source_storage_name']).to eq('default') + expect(json_response['destination_storage_name']).to eq(destination_storage_name) + end + + describe 'permissions' do + it { expect { create_container_repository_storage_move }.to be_allowed_for(:admin) } + it { expect { create_container_repository_storage_move }.to be_denied_for(:user) } + end + + context 'destination_storage_name is missing', :aggregate_failures do + let(:destination_storage_name) { nil } + + it 'schedules a container repository storage move' do + create_container_repository_storage_move + + storage_move = container.repository_storage_moves.last + + expect(response).to have_gitlab_http_status(:created) + expect(response).to match_response_schema("public_api/v4/#{container_type.singularize}_repository_storage_move") + expect(json_response['id']).to eq(storage_move.id) + expect(json_response['state']).to eq('scheduled') + expect(json_response['source_storage_name']).to eq('default') + expect(json_response['destination_storage_name']).to be_present + end + end + end + + describe "POST /#{container_type.singularize}_repository_storage_moves" do + let(:url) { "/#{container_type.singularize}_repository_storage_moves" } + let(:source_storage_name) { 'default' } + let(:destination_storage_name) { 'test_second_storage' } + + def create_container_repository_storage_moves + post api(url, user), params: { + source_storage_name: source_storage_name, + destination_storage_name: destination_storage_name + } + end + + before do + stub_storage_settings('test_second_storage' => { 'path' => 'tmp/tests/extra_storage' }) + end + + it 'schedules the worker' do + expect(bulk_worker_klass).to receive(:perform_async).with(source_storage_name, destination_storage_name) + + create_container_repository_storage_moves + + expect(response).to have_gitlab_http_status(:accepted) + end + + context 'source_storage_name is invalid' do + let(:destination_storage_name) { 'not-a-real-storage' } + + it 'gives an error' do + create_container_repository_storage_moves + + expect(response).to have_gitlab_http_status(:bad_request) + end + end + + context 'destination_storage_name is missing' do + let(:destination_storage_name) { nil } + + it 'schedules the worker' do + expect(bulk_worker_klass).to receive(:perform_async).with(source_storage_name, destination_storage_name) + + create_container_repository_storage_moves + + expect(response).to have_gitlab_http_status(:accepted) + end + end + + context 'destination_storage_name is invalid' do + let(:destination_storage_name) { 'not-a-real-storage' } + + it 'gives an error' do + create_container_repository_storage_moves + + expect(response).to have_gitlab_http_status(:bad_request) + end + end + + describe 'normal user' do + it { expect { create_container_repository_storage_moves }.to be_denied_for(:user) } + end + end +end diff --git a/spec/views/shared/wikis/_sidebar.html.haml_spec.rb b/spec/views/shared/wikis/_sidebar.html.haml_spec.rb index 3e691862937..70991369506 100644 --- a/spec/views/shared/wikis/_sidebar.html.haml_spec.rb +++ b/spec/views/shared/wikis/_sidebar.html.haml_spec.rb @@ -80,4 +80,28 @@ RSpec.describe 'shared/wikis/_sidebar.html.haml' do end end end + + describe 'link to edit the sidebar' do + before do + allow(view).to receive(:can?).with(anything, :create_wiki, anything).and_return(can_edit) + + render + end + + context 'when the user has edit permission' do + let(:can_edit) { true } + + it 'renders the link' do + expect(rendered).to have_link('Edit sidebar', href: wiki_page_path(wiki, Wiki::SIDEBAR, action: :edit)) + end + end + + context 'when the user does not have edit permission' do + let(:can_edit) { false } + + it 'does not render the link' do + expect(rendered).not_to have_link('Edit sidebar') + end + end + end end |