Commit 4c8520fd authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '228693-fj-improve-project-snippet-controller-spec' into 'master'

Improve performance of project snippet controller spec

Closes #228693

See merge request gitlab-org/gitlab!42069
parents 814f4691 7f6362a2
...@@ -6,12 +6,12 @@ RSpec.describe Projects::SnippetsController do ...@@ -6,12 +6,12 @@ RSpec.describe Projects::SnippetsController do
include Gitlab::Routing include Gitlab::Routing
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:user2) { create(:user) } let_it_be(:other_user) { create(:user) }
let(:project) { create(:project_empty_repo, :public) } let_it_be(:project) { create(:project_empty_repo, :public) }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
project.add_maintainer(user2) project.add_developer(other_user)
end end
describe 'GET #index' do describe 'GET #index' do
...@@ -47,7 +47,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -47,7 +47,7 @@ RSpec.describe Projects::SnippetsController do
end end
context 'when the project snippet is private' do context 'when the project snippet is private' do
let!(:project_snippet) { create(:project_snippet, :private, project: project, author: user) } let_it_be(:project_snippet) { create(:project_snippet, :private, project: project, author: user) }
context 'when anonymous' do context 'when anonymous' do
it 'does not include the private snippet' do it 'does not include the private snippet' do
...@@ -59,11 +59,9 @@ RSpec.describe Projects::SnippetsController do ...@@ -59,11 +59,9 @@ RSpec.describe Projects::SnippetsController do
end end
context 'when signed in as the author' do context 'when signed in as the author' do
before do it 'renders the snippet' do
sign_in(user) sign_in(user)
end
it 'renders the snippet' do
subject subject
expect(assigns(:snippets)).to include(project_snippet) expect(assigns(:snippets)).to include(project_snippet)
...@@ -72,11 +70,9 @@ RSpec.describe Projects::SnippetsController do ...@@ -72,11 +70,9 @@ RSpec.describe Projects::SnippetsController do
end end
context 'when signed in as a project member' do context 'when signed in as a project member' do
before do
sign_in(user2)
end
it 'renders the snippet' do it 'renders the snippet' do
sign_in(other_user)
subject subject
expect(assigns(:snippets)).to include(project_snippet) expect(assigns(:snippets)).to include(project_snippet)
...@@ -171,7 +167,6 @@ RSpec.describe Projects::SnippetsController do ...@@ -171,7 +167,6 @@ RSpec.describe Projects::SnippetsController do
end end
describe 'PUT #update' do describe 'PUT #update' do
let(:project) { create :project, :public }
let(:visibility_level) { Snippet::PUBLIC } let(:visibility_level) { Snippet::PUBLIC }
let(:snippet) { create :project_snippet, author: user, project: project, visibility_level: visibility_level } let(:snippet) { create :project_snippet, author: user, project: project, visibility_level: visibility_level }
...@@ -297,7 +292,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -297,7 +292,7 @@ RSpec.describe Projects::SnippetsController do
end end
describe 'POST #mark_as_spam' do describe 'POST #mark_as_spam' do
let(:snippet) { create(:project_snippet, :private, project: project, author: user) } let_it_be(:snippet) { create(:project_snippet, :private, project: project, author: user) }
before do before do
allow_next_instance_of(Spam::AkismetService) do |instance| allow_next_instance_of(Spam::AkismetService) do |instance|
...@@ -345,7 +340,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -345,7 +340,7 @@ RSpec.describe Projects::SnippetsController do
%w[show raw].each do |action| %w[show raw].each do |action|
describe "GET ##{action}" do describe "GET ##{action}" do
context 'when the project snippet is private' do context 'when the project snippet is private' do
let(:project_snippet) { create(:project_snippet, :private, :repository, project: project, author: user) } let_it_be(: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 } } subject { get action, params: { namespace_id: project.namespace, project_id: project, id: project_snippet.to_param } }
...@@ -367,7 +362,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -367,7 +362,7 @@ RSpec.describe Projects::SnippetsController do
context 'when signed in as a project member' do context 'when signed in as a project member' do
before do before do
sign_in(user2) sign_in(other_user)
end end
it_behaves_like 'successful response' it_behaves_like 'successful response'
...@@ -480,9 +475,8 @@ RSpec.describe Projects::SnippetsController do ...@@ -480,9 +475,8 @@ RSpec.describe Projects::SnippetsController do
subject { get :raw, params: params } subject { get :raw, params: params }
context 'when repository is empty' do context 'when repository is empty' do
let(:content) { "first line\r\nsecond line\r\nthird line" } let_it_be(:content) { "first line\r\nsecond line\r\nthird line" }
let(:formatted_content) { content.gsub(/\r\n/, "\n") } let_it_be(:project_snippet) do
let(:project_snippet) do
create( create(
:project_snippet, :public, :empty_repo, :project_snippet, :public, :empty_repo,
project: project, project: project,
...@@ -491,6 +485,8 @@ RSpec.describe Projects::SnippetsController do ...@@ -491,6 +485,8 @@ RSpec.describe Projects::SnippetsController do
) )
end end
let(:formatted_content) { content.gsub(/\r\n/, "\n") }
context 'CRLF line ending' do context 'CRLF line ending' do
before do before do
allow_next_instance_of(Blob) do |instance| allow_next_instance_of(Blob) do |instance|
...@@ -517,7 +513,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -517,7 +513,7 @@ RSpec.describe Projects::SnippetsController do
end end
context 'when repository is not empty' do context 'when repository is not empty' do
let(:project_snippet) do let_it_be(:project_snippet) do
create( create(
:project_snippet, :public, :repository, :project_snippet, :public, :repository,
project: project, project: project,
...@@ -539,7 +535,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -539,7 +535,7 @@ RSpec.describe Projects::SnippetsController do
end end
describe 'DELETE #destroy' do describe 'DELETE #destroy' do
let!(:snippet) { create(:project_snippet, :private, project: project, author: user) } let_it_be(:snippet) { create(:project_snippet, :private, project: project, author: user) }
let(:params) do let(:params) do
{ {
...@@ -549,20 +545,22 @@ RSpec.describe Projects::SnippetsController do ...@@ -549,20 +545,22 @@ RSpec.describe Projects::SnippetsController do
} }
end end
subject { delete :destroy, params: params }
context 'when current user has ability to destroy the snippet' do context 'when current user has ability to destroy the snippet' do
before do before do
sign_in(user) sign_in(user)
end end
it 'removes the snippet' do it 'removes the snippet' do
delete :destroy, params: params subject
expect { snippet.reload }.to raise_error(ActiveRecord::RecordNotFound) expect { snippet.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
context 'when snippet is succesfuly destroyed' do context 'when snippet is succesfuly destroyed' do
it 'redirects to the project snippets page' do it 'redirects to the project snippets page' do
delete :destroy, params: params subject
expect(response).to redirect_to(project_snippets_path(project)) expect(response).to redirect_to(project_snippets_path(project))
end end
...@@ -575,7 +573,7 @@ RSpec.describe Projects::SnippetsController do ...@@ -575,7 +573,7 @@ RSpec.describe Projects::SnippetsController do
end end
it 'renders the snippet page with errors' do it 'renders the snippet page with errors' do
delete :destroy, params: params subject
expect(flash[:alert]).to eq('Failed to remove snippet.') expect(flash[:alert]).to eq('Failed to remove snippet.')
expect(response).to redirect_to(project_snippet_path(project, snippet)) expect(response).to redirect_to(project_snippet_path(project, snippet))
...@@ -584,14 +582,10 @@ RSpec.describe Projects::SnippetsController do ...@@ -584,14 +582,10 @@ RSpec.describe Projects::SnippetsController do
end end
context 'when current_user does not have ability to destroy the snippet' do context 'when current_user does not have ability to destroy the snippet' do
let(:another_user) { create(:user) }
before do
sign_in(another_user)
end
it 'responds with status 404' do it 'responds with status 404' do
delete :destroy, params: params sign_in(other_user)
subject
expect(response).to have_gitlab_http_status(:not_found) expect(response).to have_gitlab_http_status(:not_found)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment