diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-25 12:09:10 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-25 12:09:10 +0300 |
commit | b98fa9ef3d5bead417ae2f325cb64637883264e9 (patch) | |
tree | 409f2002dd056f12d82d3959b3e6f012c4087123 /spec/controllers | |
parent | 7e3005967df23a957fe1998c8de4f50b412e69e7 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/controllers')
-rw-r--r-- | spec/controllers/projects/snippets_controller_spec.rb | 93 | ||||
-rw-r--r-- | spec/controllers/snippets_controller_spec.rb | 195 |
2 files changed, 192 insertions, 96 deletions
diff --git a/spec/controllers/projects/snippets_controller_spec.rb b/spec/controllers/projects/snippets_controller_spec.rb index 4d1537ae787..900569af6c6 100644 --- a/spec/controllers/projects/snippets_controller_spec.rb +++ b/spec/controllers/projects/snippets_controller_spec.rb @@ -3,9 +3,11 @@ require 'spec_helper' describe Projects::SnippetsController do + include Gitlab::Routing + + let_it_be(:user) { create(:user) } + let_it_be(:user2) { create(:user) } let(:project) { create(:project_empty_repo, :public) } - let(:user) { create(:user) } - let(:user2) { create(:user) } before do project.add_maintainer(user) @@ -318,14 +320,45 @@ describe Projects::SnippetsController do end end + shared_examples 'successful response' do + it 'renders the snippet' do + subject + + expect(assigns(:snippet)).to eq(project_snippet) + expect(response).to have_gitlab_http_status(:ok) + end + + it 'renders the blob from the repository' do + subject + + expect(assigns(:blob)).to eq(project_snippet.blobs.first) + end + + context 'when feature flag version_snippets is disabled' do + before do + stub_feature_flags(version_snippets: false) + end + + it 'returns the snippet database content' do + subject + + blob = assigns(:blob) + + expect(blob.data).to eq(project_snippet.content) + end + end + end + %w[show raw].each do |action| describe "GET ##{action}" do context 'when the project snippet is private' do - let(:project_snippet) { create(:project_snippet, :private, project: project, author: user) } + let(:project_snippet) { create(:project_snippet, :private, :repository, project: project, author: user) } + + subject { get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } } context 'when anonymous' do it 'responds with status 404' do - get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } + subject expect(response).to have_gitlab_http_status(:not_found) end @@ -336,12 +369,7 @@ describe Projects::SnippetsController do sign_in(user) end - it 'renders the snippet' do - get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } - - expect(assigns(:snippet)).to eq(project_snippet) - expect(response).to have_gitlab_http_status(:ok) - end + it_behaves_like 'successful response' end context 'when signed in as a project member' do @@ -349,19 +377,16 @@ describe Projects::SnippetsController do sign_in(user2) end - it 'renders the snippet' do - get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } - - expect(assigns(:snippet)).to eq(project_snippet) - expect(response).to have_gitlab_http_status(:ok) - end + it_behaves_like 'successful response' end end context 'when the project snippet does not exist' do + subject { get action, params: { namespace_id: project.namespace, project_id: project, id: 42 } } + context 'when anonymous' do it 'responds with status 404' do - get action, params: { namespace_id: project.namespace, project_id: project, id: 42 } + subject expect(response).to have_gitlab_http_status(:not_found) end @@ -373,7 +398,7 @@ describe Projects::SnippetsController do end it 'responds with status 404' do - get action, params: { namespace_id: project.namespace, project_id: project, id: 42 } + subject expect(response).to have_gitlab_http_status(:not_found) end @@ -383,18 +408,20 @@ describe Projects::SnippetsController do end describe "GET #show for embeddable content" do - let(:project_snippet) { create(:project_snippet, snippet_permission, project: project, author: user) } + let(:project_snippet) { create(:project_snippet, :repository, snippet_permission, project: project, author: user) } before do sign_in(user) - - get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param }, format: :js end + subject { get :show, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param }, format: :js } + context 'when snippet is private' do let(:snippet_permission) { :private } it 'responds with status 404' do + subject + expect(response).to have_gitlab_http_status(:not_found) end end @@ -402,10 +429,7 @@ describe Projects::SnippetsController do context 'when snippet is public' do let(:snippet_permission) { :public } - it 'responds with status 200' do - expect(assigns(:snippet)).to eq(project_snippet) - expect(response).to have_gitlab_http_status(:ok) - end + it_behaves_like 'successful response' end context 'when the project is private' do @@ -415,6 +439,8 @@ describe Projects::SnippetsController do let(:project_snippet) { create(:project_snippet, :public, project: project, author: user) } it 'responds with status 404' do + subject + expect(assigns(:snippet)).to eq(project_snippet) expect(response).to have_gitlab_http_status(:not_found) end @@ -423,14 +449,17 @@ describe Projects::SnippetsController do end describe 'GET #raw' do + let(:content) { "first line\r\nsecond line\r\nthird line" } + let(:formatted_content) { content.gsub(/\r\n/, "\n") } let(:project_snippet) do create( - :project_snippet, :public, + :project_snippet, :public, :repository, project: project, author: user, - content: "first line\r\nsecond line\r\nthird line" + content: content ) end + let(:blob) { project_snippet.blobs.first } context 'CRLF line ending' do let(:params) do @@ -441,16 +470,22 @@ describe Projects::SnippetsController do } end + before do + allow_next_instance_of(Blob) do |instance| + allow(instance).to receive(:data).and_return(content) + end + end + it 'returns LF line endings by default' do get :raw, params: params - expect(response.body).to eq("first line\nsecond line\nthird line") + expect(response.body).to eq(formatted_content) end it 'does not convert line endings when parameter present' do get :raw, params: params.merge(line_ending: :raw) - expect(response.body).to eq("first line\r\nsecond line\r\nthird line") + expect(response.body).to eq(content) end end end diff --git a/spec/controllers/snippets_controller_spec.rb b/spec/controllers/snippets_controller_spec.rb index daa560649f0..16947f4d3be 100644 --- a/spec/controllers/snippets_controller_spec.rb +++ b/spec/controllers/snippets_controller_spec.rb @@ -3,11 +3,9 @@ require 'spec_helper' describe SnippetsController do - let(:user) { create(:user) } + let_it_be(:user) { create(:user) } describe 'GET #index' do - let(:user) { create(:user) } - context 'when username parameter is present' do it_behaves_like 'paginated collection' do let(:collection) { Snippet.all } @@ -75,8 +73,37 @@ describe SnippetsController do end describe 'GET #show' do + shared_examples 'successful response' do + it 'renders the snippet' do + subject + + expect(assigns(:snippet)).to eq(personal_snippet) + expect(response).to have_gitlab_http_status(:ok) + end + + it 'renders the blob from the repository' do + subject + + expect(assigns(:blob)).to eq(personal_snippet.blobs.first) + end + + context 'when feature flag version_snippets is disabled' do + before do + stub_feature_flags(version_snippets: false) + end + + it 'returns the snippet database content' do + subject + + blob = assigns(:blob) + + expect(blob.data).to eq(personal_snippet.content) + end + end + end + context 'when the personal snippet is private' do - let(:personal_snippet) { create(:personal_snippet, :private, author: user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :private, :repository, author: user) } context 'when signed in' do before do @@ -95,11 +122,8 @@ describe SnippetsController do end context 'when signed in user is the author' do - it 'renders the snippet' do - get :show, params: { id: personal_snippet.to_param } - - expect(assigns(:snippet)).to eq(personal_snippet) - expect(response).to have_gitlab_http_status(:ok) + it_behaves_like 'successful response' do + subject { get :show, params: { id: personal_snippet.to_param } } end it 'responds with status 404 when embeddable content is requested' do @@ -120,18 +144,15 @@ describe SnippetsController do end context 'when the personal snippet is internal' do - let(:personal_snippet) { create(:personal_snippet, :internal, author: user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :internal, :repository, author: user) } context 'when signed in' do before do sign_in(user) end - it 'renders the snippet' do - get :show, params: { id: personal_snippet.to_param } - - expect(assigns(:snippet)).to eq(personal_snippet) - expect(response).to have_gitlab_http_status(:ok) + it_behaves_like 'successful response' do + subject { get :show, params: { id: personal_snippet.to_param } } end it 'responds with status 404 when embeddable content is requested' do @@ -151,18 +172,15 @@ describe SnippetsController do end context 'when the personal snippet is public' do - let(:personal_snippet) { create(:personal_snippet, :public, author: user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :public, :repository, author: user) } context 'when signed in' do before do sign_in(user) end - it 'renders the snippet' do - get :show, params: { id: personal_snippet.to_param } - - expect(assigns(:snippet)).to eq(personal_snippet) - expect(response).to have_gitlab_http_status(:ok) + it_behaves_like 'successful response' do + subject { get :show, params: { id: personal_snippet.to_param } } end it 'responds with status 200 when embeddable content is requested' do @@ -481,8 +499,82 @@ describe SnippetsController do end describe "GET #raw" do + shared_examples '200 status' do + before do + subject + end + + it 'responds with status 200' do + expect(assigns(:snippet)).to eq(snippet) + expect(response).to have_gitlab_http_status(:ok) + end + + it 'has expected headers' do + expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8') + expect(response.header['Content-Disposition']).to match(/inline/) + end + + it "sets #{Gitlab::Workhorse::DETECT_HEADER} header" do + expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true' + end + end + + shared_examples 'CRLF line ending' do + let(:content) { "first line\r\nsecond line\r\nthird line" } + let(:formatted_content) { content.gsub(/\r\n/, "\n") } + let(:snippet) do + create(:personal_snippet, :public, :repository, author: user, content: content) + end + + before do + allow_next_instance_of(Blob) do |instance| + allow(instance).to receive(:data).and_return(content) + end + + subject + end + + it 'returns LF line endings by default' do + expect(response.body).to eq(formatted_content) + end + + context 'when parameter present' do + let(:params) { { id: snippet.to_param, line_ending: :raw } } + + it 'does not convert line endings when parameter present' do + expect(response.body).to eq(content) + end + end + end + + shared_examples 'successful response' do + it_behaves_like '200 status' + it_behaves_like 'CRLF line ending' + + it 'returns snippet first blob data' do + subject + + expect(response.body).to eq snippet.blobs.first.data + end + + context 'when feature flag version_snippets is disabled' do + before do + stub_feature_flags(version_snippets: false) + end + + it_behaves_like '200 status' + it_behaves_like 'CRLF line ending' + + it 'returns snippet database content' do + subject + + expect(response.body).to eq snippet.content + end + end + end + context 'when the personal snippet is private' do - let(:personal_snippet) { create(:personal_snippet, :private, author: user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :private, :repository, author: user) } context 'when signed in' do before do @@ -501,24 +593,11 @@ describe SnippetsController do end context 'when signed in user is the author' do - before do - get :raw, params: { id: personal_snippet.to_param } - end + it_behaves_like 'successful response' do + let(:snippet) { personal_snippet } + let(:params) { { id: snippet.to_param } } - it 'responds with status 200' do - expect(assigns(:snippet)).to eq(personal_snippet) - expect(response).to have_gitlab_http_status(:ok) - end - - it 'has expected headers' do - expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8') - - expect(response.header['Content-Disposition']).to match(/inline/) - end - - it "sets #{Gitlab::Workhorse::DETECT_HEADER} header" do - expect(response).to have_gitlab_http_status(:ok) - expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq "true" + subject { get :raw, params: params } end end end @@ -533,18 +612,18 @@ describe SnippetsController do end context 'when the personal snippet is internal' do - let(:personal_snippet) { create(:personal_snippet, :internal, author: user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :internal, :repository, author: user) } context 'when signed in' do before do sign_in(user) end - it 'responds with status 200' do - get :raw, params: { id: personal_snippet.to_param } + it_behaves_like 'successful response' do + let(:snippet) { personal_snippet } + let(:params) { { id: snippet.to_param } } - expect(assigns(:snippet)).to eq(personal_snippet) - expect(response).to have_gitlab_http_status(:ok) + subject { get :raw, params: params } end end @@ -558,36 +637,18 @@ describe SnippetsController do end context 'when the personal snippet is public' do - let(:personal_snippet) { create(:personal_snippet, :public, author: user) } + let_it_be(:personal_snippet) { create(:personal_snippet, :public, :repository, author: user) } context 'when signed in' do before do sign_in(user) end - it 'responds with status 200' do - get :raw, params: { id: personal_snippet.to_param } - - expect(assigns(:snippet)).to eq(personal_snippet) - expect(response).to have_gitlab_http_status(:ok) - end - - context 'CRLF line ending' do - let(:personal_snippet) do - create(:personal_snippet, :public, author: user, content: "first line\r\nsecond line\r\nthird line") - end + it_behaves_like 'successful response' do + let(:snippet) { personal_snippet } + let(:params) { { id: snippet.to_param } } - it 'returns LF line endings by default' do - get :raw, params: { id: personal_snippet.to_param } - - expect(response.body).to eq("first line\nsecond line\nthird line") - end - - it 'does not convert line endings when parameter present' do - get :raw, params: { id: personal_snippet.to_param, line_ending: :raw } - - expect(response.body).to eq("first line\r\nsecond line\r\nthird line") - end + subject { get :raw, params: params } end end |