diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-11 15:07:38 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-12-11 15:07:38 +0000 |
commit | 4eea104c69e59f6fa53c7bc15b986c69f29b60c8 (patch) | |
tree | 2eff1ce7ac4a58de15b1f5980acfdb22c7b92ac0 /spec | |
parent | b86f474bf51e20d2db4cf0895d0a8e0894e31c08 (diff) | |
download | gitlab-ce-4eea104c69e59f6fa53c7bc15b986c69f29b60c8.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/merge_requests/diffs_controller_spec.rb | 139 | ||||
-rw-r--r-- | spec/features/snippets/public_snippets_spec.rb | 1 | ||||
-rw-r--r-- | spec/fixtures/api/schemas/environment.json | 1 | ||||
-rw-r--r-- | spec/frontend/error_tracking/store/list/mutation_spec.js | 2 | ||||
-rw-r--r-- | spec/frontend/fixtures/snippet.rb | 2 | ||||
-rw-r--r-- | spec/frontend/ide/components/panes/right_spec.js | 2 | ||||
-rw-r--r-- | spec/frontend/snippets_spec.js | 70 | ||||
-rw-r--r-- | spec/frontend/test_setup.js | 3 | ||||
-rw-r--r-- | spec/helpers/snippets_helper_spec.rb | 24 | ||||
-rw-r--r-- | spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb | 41 | ||||
-rw-r--r-- | spec/lib/gitlab/diff/highlight_cache_spec.rb | 9 | ||||
-rw-r--r-- | spec/lib/gitlab/diff/line_spec.rb | 44 | ||||
-rw-r--r-- | spec/lib/gitlab/discussions_diff/highlight_cache_spec.rb | 9 | ||||
-rw-r--r-- | spec/models/merge_request_diff_spec.rb | 59 | ||||
-rw-r--r-- | spec/requests/api/releases_spec.rb | 37 |
15 files changed, 389 insertions, 54 deletions
diff --git a/spec/controllers/projects/merge_requests/diffs_controller_spec.rb b/spec/controllers/projects/merge_requests/diffs_controller_spec.rb index 8f4f85b55bb..d7e790360e3 100644 --- a/spec/controllers/projects/merge_requests/diffs_controller_spec.rb +++ b/spec/controllers/projects/merge_requests/diffs_controller_spec.rb @@ -5,6 +5,19 @@ require 'spec_helper' describe Projects::MergeRequests::DiffsController do include ProjectForksHelper + shared_examples '404 for unexistent diffable' do + context 'when diffable does not exists' do + it 'returns 404' do + unexistent_diff_id = 9999 + + go(diff_id: unexistent_diff_id) + + expect(MergeRequestDiff.find_by(id: unexistent_diff_id)).to be_nil + expect(response).to have_gitlab_http_status(404) + end + end + end + shared_examples 'forked project with submodules' do render_views @@ -137,6 +150,8 @@ describe Projects::MergeRequests::DiffsController do get :diffs_metadata, params: params.merge(extra_params) end + it_behaves_like '404 for unexistent diffable' + context 'when not authorized' do let(:another_user) { create(:user) } @@ -159,14 +174,6 @@ describe Projects::MergeRequests::DiffsController do end end - context 'when diffable does not exists' do - it 'returns 404' do - go(diff_id: 9999) - - expect(response).to have_gitlab_http_status(404) - end - end - context 'with valid diff_id' do it 'returns success' do go(diff_id: merge_request.merge_request_diff.id) @@ -328,17 +335,53 @@ describe Projects::MergeRequests::DiffsController do end describe 'GET diffs_batch' do + shared_examples_for 'serializes diffs with expected arguments' do + it 'serializes paginated merge request diff collection' do + expect_next_instance_of(PaginatedDiffSerializer) do |instance| + expect(instance).to receive(:represent) + .with(an_instance_of(collection), expected_options) + .and_call_original + end + + subject + end + end + + shared_examples_for 'successful request' do + it 'returns success' do + subject + + expect(response).to have_gitlab_http_status(200) + end + end + + def collection_arguments(pagination_data = {}) + { + merge_request: merge_request, + diff_view: :inline, + pagination_data: { + current_page: nil, + next_page: nil, + total_pages: nil + }.merge(pagination_data) + } + end + def go(extra_params = {}) params = { namespace_id: project.namespace.to_param, project_id: project, id: merge_request.iid, + page: 1, + per_page: 20, format: 'json' } get :diffs_batch, params: params.merge(extra_params) end + it_behaves_like '404 for unexistent diffable' + context 'when feature is disabled' do before do stub_feature_flags(diffs_batch_load: false) @@ -365,52 +408,60 @@ describe Projects::MergeRequests::DiffsController do end end - context 'with default params' do - let(:expected_options) do - { - merge_request: merge_request, - diff_view: :inline, - pagination_data: { - current_page: 1, - next_page: nil, - total_pages: 1 - } - } + context 'with valid diff_id' do + subject { go(diff_id: merge_request.merge_request_diff.id) } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch } + let(:expected_options) { collection_arguments(current_page: 1, total_pages: 1) } end - it 'serializes paginated merge request diff collection' do - expect_next_instance_of(PaginatedDiffSerializer) do |instance| - expect(instance).to receive(:represent) - .with(an_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiffBatch), expected_options) - .and_call_original - end + it_behaves_like 'successful request' + end - go + context 'with commit_id param' do + subject { go(commit_id: merge_request.diff_head_sha) } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::Commit } + let(:expected_options) { collection_arguments } end end - context 'with smaller diff batch params' do - let(:expected_options) do - { - merge_request: merge_request, - diff_view: :inline, - pagination_data: { - current_page: 2, - next_page: 3, - total_pages: 4 - } - } + context 'with diff_id and start_sha params' do + subject do + go(diff_id: merge_request.merge_request_diff.id, + start_sha: merge_request.merge_request_diff.start_commit_sha) end - it 'serializes paginated merge request diff collection' do - expect_next_instance_of(PaginatedDiffSerializer) do |instance| - expect(instance).to receive(:represent) - .with(an_instance_of(Gitlab::Diff::FileCollection::MergeRequestDiffBatch), expected_options) - .and_call_original - end + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::Compare } + let(:expected_options) { collection_arguments } + end + + it_behaves_like 'successful request' + end - go(page: 2, per_page: 5) + context 'with default params' do + subject { go } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch } + let(:expected_options) { collection_arguments(current_page: 1, total_pages: 1) } + end + + it_behaves_like 'successful request' + end + + context 'with smaller diff batch params' do + subject { go(page: 2, per_page: 5) } + + it_behaves_like 'serializes diffs with expected arguments' do + let(:collection) { Gitlab::Diff::FileCollection::MergeRequestDiffBatch } + let(:expected_options) { collection_arguments(current_page: 2, next_page: 3, total_pages: 4) } end + + it_behaves_like 'successful request' end it_behaves_like 'forked project with submodules' diff --git a/spec/features/snippets/public_snippets_spec.rb b/spec/features/snippets/public_snippets_spec.rb index 045afcf1c12..295e61ffb56 100644 --- a/spec/features/snippets/public_snippets_spec.rb +++ b/spec/features/snippets/public_snippets_spec.rb @@ -16,6 +16,7 @@ describe 'Public Snippets', :js do expect(page).to have_content(public_snippet.content) expect(page).to have_css('.js-embed-btn', visible: false) expect(page).to have_css('.js-share-btn', visible: false) + expect(page.find('.js-snippet-url-area')).to be_readonly end it 'Unauthenticated user should see raw public snippets' do diff --git a/spec/fixtures/api/schemas/environment.json b/spec/fixtures/api/schemas/environment.json index 7e7e5ce37e3..321c495a575 100644 --- a/spec/fixtures/api/schemas/environment.json +++ b/spec/fixtures/api/schemas/environment.json @@ -26,6 +26,7 @@ "stop_path": { "type": "string" }, "cancel_auto_stop_path": { "type": "string" }, "folder_path": { "type": "string" }, + "project_path": { "type": "string" }, "created_at": { "type": "string", "format": "date-time" }, "updated_at": { "type": "string", "format": "date-time" }, "auto_stop_at": { "type": "string", "format": "date-time" }, diff --git a/spec/frontend/error_tracking/store/list/mutation_spec.js b/spec/frontend/error_tracking/store/list/mutation_spec.js index 5e6505e13cd..44a75b6aa1f 100644 --- a/spec/frontend/error_tracking/store/list/mutation_spec.js +++ b/spec/frontend/error_tracking/store/list/mutation_spec.js @@ -1,6 +1,6 @@ +import { useLocalStorageSpy } from 'helpers/local_storage_helper'; import mutations from '~/error_tracking/store/list/mutations'; import * as types from '~/error_tracking/store/list/mutation_types'; -import { useLocalStorageSpy } from 'helpers/local_storage_helper'; const ADD_RECENT_SEARCH = mutations[types.ADD_RECENT_SEARCH]; const CLEAR_RECENT_SEARCHES = mutations[types.CLEAR_RECENT_SEARCHES]; diff --git a/spec/frontend/fixtures/snippet.rb b/spec/frontend/fixtures/snippet.rb index 33dfebac3e7..e91050cd2c5 100644 --- a/spec/frontend/fixtures/snippet.rb +++ b/spec/frontend/fixtures/snippet.rb @@ -8,7 +8,7 @@ describe SnippetsController, '(JavaScript fixtures)', type: :controller do let(:admin) { create(:admin) } let(:namespace) { create(:namespace, name: 'frontend-fixtures' )} let(:project) { create(:project, :repository, namespace: namespace, path: 'branches-project') } - let(:snippet) { create(:personal_snippet, title: 'snippet.md', content: '# snippet', file_name: 'snippet.md', author: admin) } + let(:snippet) { create(:personal_snippet, :public, title: 'snippet.md', content: '# snippet', file_name: 'snippet.md', author: admin) } render_views diff --git a/spec/frontend/ide/components/panes/right_spec.js b/spec/frontend/ide/components/panes/right_spec.js index 6f7228add4e..6908790aaa8 100644 --- a/spec/frontend/ide/components/panes/right_spec.js +++ b/spec/frontend/ide/components/panes/right_spec.js @@ -1,9 +1,9 @@ import Vue from 'vue'; import '~/behaviors/markdown/render_gfm'; +import { createComponentWithStore } from 'helpers/vue_mount_component_helper'; import { createStore } from '~/ide/stores'; import RightPane from '~/ide/components/panes/right.vue'; import { rightSidebarViews } from '~/ide/constants'; -import { createComponentWithStore } from 'helpers/vue_mount_component_helper'; describe('IDE right pane', () => { let Component; diff --git a/spec/frontend/snippets_spec.js b/spec/frontend/snippets_spec.js new file mode 100644 index 00000000000..5b391606371 --- /dev/null +++ b/spec/frontend/snippets_spec.js @@ -0,0 +1,70 @@ +import snippetEmbed from '~/snippet/snippet_embed'; +import { loadHTMLFixture } from './helpers/fixtures'; + +describe('Snippets', () => { + let embedBtn; + let snippetUrlArea; + let shareBtn; + let scriptTag; + + const snippetUrl = 'http://test.host/snippets/1'; + + beforeEach(() => { + loadHTMLFixture('snippets/show.html'); + + embedBtn = document.querySelector('.js-embed-btn'); + snippetUrlArea = document.querySelector('.js-snippet-url-area'); + shareBtn = document.querySelector('.js-share-btn'); + }); + + it('selects the fields content when it is clicked', () => { + jest.spyOn(snippetUrlArea, 'select'); + snippetEmbed(); + + expect(snippetUrlArea.select).not.toHaveBeenCalled(); + snippetUrlArea.dispatchEvent(new Event('click')); + expect(snippetUrlArea.select).toHaveBeenCalled(); + }); + + describe('when the snippet url does not include params', () => { + beforeEach(() => { + snippetEmbed(); + + scriptTag = `<script src="${snippetUrl}.js"></script>`; + }); + + it('shows the script tag as default', () => { + expect(snippetUrlArea.value).toEqual(scriptTag); + }); + + it('sets the proper url depending on the button clicked', () => { + shareBtn.dispatchEvent(new Event('click')); + expect(snippetUrlArea.value).toEqual(snippetUrl); + + embedBtn.dispatchEvent(new Event('click')); + expect(snippetUrlArea.value).toEqual(scriptTag); + }); + }); + + describe('when the snippet url includes params', () => { + beforeEach(() => { + scriptTag = `<script src="${snippetUrl}.js?foo=bar"></script>`; + snippetUrlArea.value = scriptTag; + snippetUrlArea.dataset.url = `${snippetUrl}?foo=bar`; + + snippetEmbed(); + }); + + it('shows the script tag as default', () => { + expect(snippetUrlArea.value).toEqual(scriptTag); + }); + + it('sets the proper url depending on the button clicked', () => { + shareBtn.dispatchEvent(new Event('click')); + expect(snippetUrlArea.value).toEqual(`${snippetUrl}?foo=bar`); + + embedBtn.dispatchEvent(new Event('click')); + expect(snippetUrlArea.value).toEqual(scriptTag); + }); + }); +}); diff --git a/spec/frontend/test_setup.js b/spec/frontend/test_setup.js index b39b7375d80..4636de6b8b6 100644 --- a/spec/frontend/test_setup.js +++ b/spec/frontend/test_setup.js @@ -40,6 +40,9 @@ Object.defineProperty(global.Element.prototype, 'innerText', { get() { return this.textContent; }, + set(value) { + this.textContext = value; + }, configurable: true, // make it so that it doesn't blow chunks on re-running tests with things like --watch }); diff --git a/spec/helpers/snippets_helper_spec.rb b/spec/helpers/snippets_helper_spec.rb index 428fe97f172..6fdf4f5cfb4 100644 --- a/spec/helpers/snippets_helper_spec.rb +++ b/spec/helpers/snippets_helper_spec.rb @@ -127,4 +127,28 @@ describe SnippetsHelper do end end end + + describe '#snippet_embed_input' do + subject { snippet_embed_input(snippet) } + + context 'with PersonalSnippet' do + let(:snippet) { public_personal_snippet } + + it 'returns the input component' do + expect(subject).to eq embed_input(snippet_url(snippet)) + end + end + + context 'with ProjectSnippet' do + let(:snippet) { public_project_snippet } + + it 'returns the input component' do + expect(subject).to eq embed_input(project_snippet_url(snippet.project, snippet)) + end + end + + def embed_input(url) + "<input type=\"text\" readonly=\"readonly\" class=\"js-snippet-url-area snippet-embed-input form-control\" data-url=\"#{url}\" value=\"<script src="#{url}.js"></script>\" autocomplete=\"off\"></input>" + end + end end diff --git a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb index 74fa2be5930..2493855f851 100644 --- a/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb +++ b/spec/lib/gitlab/ci/build/prerequisite/kubernetes_namespace_spec.rb @@ -128,6 +128,47 @@ describe Gitlab::Ci::Build::Prerequisite::KubernetesNamespace do subject end + + context 'the build has a namespace configured via CI template' do + let(:kubernetes_namespace) { double(namespace: existing_namespace) } + + before do + allow(build).to receive(:expanded_kubernetes_namespace) + .and_return(requested_namespace) + end + + context 'the requested namespace matches the default' do + let(:requested_namespace) { 'production' } + let(:existing_namespace) { requested_namespace } + + it 'creates a namespace' do + expect(Clusters::BuildKubernetesNamespaceService) + .to receive(:new) + .with(cluster, environment: deployment.environment) + .and_return(namespace_builder) + + expect(Clusters::Kubernetes::CreateOrUpdateNamespaceService) + .to receive(:new) + .with(cluster: cluster, kubernetes_namespace: kubernetes_namespace) + .and_return(service) + + expect(service).to receive(:execute).once + + subject + end + end + + context 'the requested namespace differs from the default' do + let(:requested_namespace) { 'production' } + let(:existing_namespace) { 'other-namespace' } + + it 'does not create a namespace' do + expect(Clusters::Kubernetes::CreateOrUpdateNamespaceService).not_to receive(:new) + + subject + end + end + end end context 'kubernetes namespace exists (but has no service_account_token)' do diff --git a/spec/lib/gitlab/diff/highlight_cache_spec.rb b/spec/lib/gitlab/diff/highlight_cache_spec.rb index 6dabfd0ef2c..7daf086843b 100644 --- a/spec/lib/gitlab/diff/highlight_cache_spec.rb +++ b/spec/lib/gitlab/diff/highlight_cache_spec.rb @@ -68,6 +68,15 @@ describe Gitlab::Diff::HighlightCache, :clean_gitlab_redis_cache do expect(diff_file.highlighted_diff_lines.size).to be > 5 end + + it 'assigns highlighted diff lines which rich_text are HTML-safe' do + cache.write_if_empty + cache.decorate(diff_file) + + rich_texts = diff_file.highlighted_diff_lines.map(&:rich_text) + + expect(rich_texts).to all(be_html_safe) + end end describe '#write_if_empty' do diff --git a/spec/lib/gitlab/diff/line_spec.rb b/spec/lib/gitlab/diff/line_spec.rb index 29b9951ba4c..7961bec9d57 100644 --- a/spec/lib/gitlab/diff/line_spec.rb +++ b/spec/lib/gitlab/diff/line_spec.rb @@ -1,18 +1,48 @@ # frozen_string_literal: true +require 'spec_helper' + describe Gitlab::Diff::Line do - describe '.init_from_hash' do + shared_examples 'line object initialized by hash' do it 'round-trips correctly with to_hash' do - line = described_class.new('<input>', 'match', 0, 0, 1, - parent_file: double(:file), - line_code: double(:line_code), - rich_text: '<input>') - - expect(described_class.init_from_hash(line.to_hash).to_hash) + expect(described_class.safe_init_from_hash(line.to_hash).to_hash) .to eq(line.to_hash) end end + let(:line) do + described_class.new('<input>', 'match', 0, 0, 1, + parent_file: double(:file), + line_code: double(:line_code), + rich_text: rich_text) + end + + describe '.init_from_hash' do + let(:rich_text) { '<input>' } + + it_behaves_like 'line object initialized by hash' + end + + describe '.safe_init_from_hash' do + let(:rich_text) { '<input>' } + + it_behaves_like 'line object initialized by hash' + + it 'ensures rich_text is HTML-safe' do + expect(line.rich_text).not_to be_html_safe + + new_line = described_class.safe_init_from_hash(line.to_hash) + + expect(new_line.rich_text).to be_html_safe + end + + context 'when given hash has no rich_text' do + it_behaves_like 'line object initialized by hash' do + let(:rich_text) { nil } + end + end + end + context "when setting rich text" do it 'escapes any HTML special characters in the diff chunk header' do subject = described_class.new("<input>", "", 0, 0, 0) diff --git a/spec/lib/gitlab/discussions_diff/highlight_cache_spec.rb b/spec/lib/gitlab/discussions_diff/highlight_cache_spec.rb index 15ee8c40b55..97d3a49ea90 100644 --- a/spec/lib/gitlab/discussions_diff/highlight_cache_spec.rb +++ b/spec/lib/gitlab/discussions_diff/highlight_cache_spec.rb @@ -62,6 +62,15 @@ describe Gitlab::DiscussionsDiff::HighlightCache, :clean_gitlab_redis_cache do expect(found.second.size).to eq(2) expect(found.second).to all(be_a(Gitlab::Diff::Line)) end + + it 'returns lines which rich_text are HTML-safe' do + described_class.write_multiple(mapping) + + found = described_class.read_multiple(mapping.keys) + rich_texts = found.flatten.map(&:rich_text) + + expect(rich_texts).to all(be_html_safe) + end end describe '#clear_multiple' do diff --git a/spec/models/merge_request_diff_spec.rb b/spec/models/merge_request_diff_spec.rb index 0fbe4903b6d..78b9e8bc217 100644 --- a/spec/models/merge_request_diff_spec.rb +++ b/spec/models/merge_request_diff_spec.rb @@ -211,6 +211,65 @@ describe MergeRequestDiff do end end + describe '#diffs_in_batch' do + let(:diff_options) { {} } + + shared_examples_for 'fetching full diffs' do + it 'returns diffs from repository comparison' do + expect_next_instance_of(Compare) do |comparison| + expect(comparison).to receive(:diffs_in_batch) + .with(1, 10, diff_options: diff_options) + .and_call_original + end + + diff_with_commits.diffs_in_batch(1, 10, diff_options: diff_options) + end + + it 'returns a Gitlab::Diff::FileCollection::Compare with full diffs' do + diffs = diff_with_commits.diffs_in_batch(1, 10, diff_options: diff_options) + + expect(diffs).to be_a(Gitlab::Diff::FileCollection::Compare) + expect(diffs.diff_files.size).to be > 10 + end + + it 'returns empty pagination data' do + diffs = diff_with_commits.diffs_in_batch(1, 10, diff_options: diff_options) + + expect(diffs.pagination_data).to eq(current_page: nil, + next_page: nil, + total_pages: nil) + end + end + + context 'when no persisted files available' do + before do + diff_with_commits.clean! + end + + it_behaves_like 'fetching full diffs' + end + + context 'when diff_options include ignore_whitespace_change' do + it_behaves_like 'fetching full diffs' do + let(:diff_options) do + { ignore_whitespace_change: true } + end + end + end + + context 'when persisted files available' do + it 'returns paginated diffs' do + diffs = diff_with_commits.diffs_in_batch(1, 10, diff_options: {}) + + expect(diffs).to be_a(Gitlab::Diff::FileCollection::MergeRequestDiffBatch) + expect(diffs.diff_files.size).to eq(10) + expect(diffs.pagination_data).to eq(current_page: 1, + next_page: 2, + total_pages: 2) + end + end + end + describe '#raw_diffs' do context 'when the :ignore_whitespace_change option is set' do it 'creates a new compare object instead of using preprocessed data' do diff --git a/spec/requests/api/releases_spec.rb b/spec/requests/api/releases_spec.rb index bf05587fe03..da04e852795 100644 --- a/spec/requests/api/releases_spec.rb +++ b/spec/requests/api/releases_spec.rb @@ -558,6 +558,43 @@ describe API::Releases do end end + context 'when using JOB-TOKEN auth' do + let(:job) { create(:ci_build, user: maintainer) } + let(:params) do + { + name: 'Another release', + tag_name: 'v0.2', + description: 'Another nice release', + released_at: '2019-04-25T10:00:00+09:00' + } + end + + context 'when no token is provided' do + it 'returns a :not_found error' do + post api("/projects/#{project.id}/releases"), params: params + + expect(response).to have_gitlab_http_status(:not_found) + end + end + + context 'when an invalid token is provided' do + it 'returns an :unauthorized error' do + post api("/projects/#{project.id}/releases"), params: params.merge(job_token: 'yadayadayada') + + expect(response).to have_gitlab_http_status(:unauthorized) + end + end + + context 'when a valid token is provided' do + it 'creates the release' do + post api("/projects/#{project.id}/releases"), params: params.merge(job_token: job.token) + + expect(response).to have_gitlab_http_status(:created) + expect(project.releases.last.description).to eq('Another nice release') + end + end + end + context 'when tag does not exist in git repository' do let(:params) do { |