Commit a45ad58e authored by fjsanpedro's avatar fjsanpedro

Improve performance of snippets_controller spec

parent 934bbceb
...@@ -4,6 +4,8 @@ require 'spec_helper' ...@@ -4,6 +4,8 @@ require 'spec_helper'
RSpec.describe SnippetsController do RSpec.describe SnippetsController do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:other_user) { create(:user) }
let_it_be(:public_snippet) { create(:personal_snippet, :public, :repository, author: user) }
describe 'GET #index' do describe 'GET #index' do
let(:base_params) { { username: user.username } } let(:base_params) { { username: user.username } }
...@@ -12,10 +14,6 @@ RSpec.describe SnippetsController do ...@@ -12,10 +14,6 @@ RSpec.describe SnippetsController do
it_behaves_like 'paginated collection' do it_behaves_like 'paginated collection' do
let(:collection) { Snippet.all } let(:collection) { Snippet.all }
let(:params) { { username: user.username } } let(:params) { { username: user.username } }
before do
create(:personal_snippet, :public, author: user)
end
end end
it 'renders snippets of a user when username is present' do it 'renders snippets of a user when username is present' do
...@@ -97,8 +95,7 @@ RSpec.describe SnippetsController do ...@@ -97,8 +95,7 @@ RSpec.describe SnippetsController do
end end
context 'when signed in user is not the author' do context 'when signed in user is not the author' do
let(:other_author) { create(:author) } let(:other_personal_snippet) { create(:personal_snippet, :private, author: other_user) }
let(:other_personal_snippet) { create(:personal_snippet, :private, author: other_author) }
it 'responds with status 404' do it 'responds with status 404' do
get :show, params: { id: other_personal_snippet.to_param } get :show, params: { id: other_personal_snippet.to_param }
...@@ -158,7 +155,7 @@ RSpec.describe SnippetsController do ...@@ -158,7 +155,7 @@ RSpec.describe SnippetsController do
end end
context 'when the personal snippet is public' do context 'when the personal snippet is public' do
let_it_be(:personal_snippet) { create(:personal_snippet, :public, :repository, author: user) } let(:personal_snippet) { public_snippet }
context 'when signed in' do context 'when signed in' do
before do before do
...@@ -166,22 +163,22 @@ RSpec.describe SnippetsController do ...@@ -166,22 +163,22 @@ RSpec.describe SnippetsController do
end end
it_behaves_like 'successful response' do it_behaves_like 'successful response' do
subject { get :show, params: { id: personal_snippet.to_param } } subject { get :show, params: { id: public_snippet.to_param } }
end end
it 'responds with status 200 when embeddable content is requested' do it 'responds with status 200 when embeddable content is requested' do
get :show, params: { id: personal_snippet.to_param }, format: :js get :show, params: { id: public_snippet.to_param }, format: :js
expect(assigns(:snippet)).to eq(personal_snippet) expect(assigns(:snippet)).to eq(public_snippet)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
end end
context 'when not signed in' do context 'when not signed in' do
it 'renders the snippet' do it 'renders the snippet' do
get :show, params: { id: personal_snippet.to_param } get :show, params: { id: public_snippet.to_param }
expect(assigns(:snippet)).to eq(personal_snippet) expect(assigns(:snippet)).to eq(public_snippet)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
end end
...@@ -211,37 +208,34 @@ RSpec.describe SnippetsController do ...@@ -211,37 +208,34 @@ RSpec.describe SnippetsController do
context 'when requesting JSON' do context 'when requesting JSON' do
it 'renders the blob from the repository' do it 'renders the blob from the repository' do
personal_snippet = create(:personal_snippet, :public, :repository, author: user) get :show, params: { id: public_snippet.to_param }, format: :json
get :show, params: { id: personal_snippet.to_param }, format: :json expect(assigns(:blob)).to eq(public_snippet.blobs.first)
expect(assigns(:blob)).to eq(personal_snippet.blobs.first)
end end
end end
end end
describe 'POST #mark_as_spam' do describe 'POST #mark_as_spam' do
let(:snippet) { create(:personal_snippet, :public, author: user) }
before do before do
allow_next_instance_of(Spam::AkismetService) do |instance| allow_next_instance_of(Spam::AkismetService) do |instance|
allow(instance).to receive_messages(submit_spam: true) allow(instance).to receive_messages(submit_spam: true)
end end
stub_application_setting(akismet_enabled: true) stub_application_setting(akismet_enabled: true)
end end
def mark_as_spam def mark_as_spam
admin = create(:admin) admin = create(:admin)
create(:user_agent_detail, subject: snippet) create(:user_agent_detail, subject: public_snippet)
sign_in(admin) sign_in(admin)
post :mark_as_spam, params: { id: snippet.id } post :mark_as_spam, params: { id: public_snippet.id }
end end
it 'updates the snippet' do it 'updates the snippet' do
mark_as_spam mark_as_spam
expect(snippet.reload).not_to be_submittable_as_spam expect(public_snippet.reload).not_to be_submittable_as_spam
end end
end end
...@@ -269,9 +263,7 @@ RSpec.describe SnippetsController do ...@@ -269,9 +263,7 @@ RSpec.describe SnippetsController do
shared_examples 'CRLF line ending' do shared_examples 'CRLF line ending' do
let(:content) { "first line\r\nsecond line\r\nthird line" } let(:content) { "first line\r\nsecond line\r\nthird line" }
let(:formatted_content) { content.gsub(/\r\n/, "\n") } let(:formatted_content) { content.gsub(/\r\n/, "\n") }
let(:snippet) do let(:snippet) { public_snippet }
create(:personal_snippet, :public, :repository, author: user, content: content)
end
before do before do
allow_next_instance_of(Blob) do |instance| allow_next_instance_of(Blob) do |instance|
...@@ -340,8 +332,7 @@ RSpec.describe SnippetsController do ...@@ -340,8 +332,7 @@ RSpec.describe SnippetsController do
end end
context 'when signed in user is not the author' do context 'when signed in user is not the author' do
let(:other_author) { create(:author) } let(:other_personal_snippet) { create(:personal_snippet, :private, author: other_user) }
let(:other_personal_snippet) { create(:personal_snippet, :private, author: other_author) }
it 'responds with status 404' do it 'responds with status 404' do
get :raw, params: { id: other_personal_snippet.to_param } get :raw, params: { id: other_personal_snippet.to_param }
...@@ -385,7 +376,7 @@ RSpec.describe SnippetsController do ...@@ -385,7 +376,7 @@ RSpec.describe SnippetsController do
end end
context 'when the personal snippet is public' do context 'when the personal snippet is public' do
let_it_be(:snippet) { create(:personal_snippet, :public, :repository, author: user) } let(:snippet) { public_snippet }
context 'when signed in' do context 'when signed in' do
before do before do
...@@ -429,11 +420,10 @@ RSpec.describe SnippetsController do ...@@ -429,11 +420,10 @@ RSpec.describe SnippetsController do
end end
context 'award emoji on snippets' do context 'award emoji on snippets' do
let(:personal_snippet) { create(:personal_snippet, :public, author: user) } let(:personal_snippet) { public_snippet }
let(:another_user) { create(:user) }
before do before do
sign_in(another_user) sign_in(other_user)
end end
describe 'POST #toggle_award_emoji' do describe 'POST #toggle_award_emoji' do
...@@ -458,12 +448,10 @@ RSpec.describe SnippetsController do ...@@ -458,12 +448,10 @@ RSpec.describe SnippetsController do
end end
describe 'POST #preview_markdown' do describe 'POST #preview_markdown' do
let(:snippet) { create(:personal_snippet, :public) }
it 'renders json in a correct format' do it 'renders json in a correct format' do
sign_in(user) sign_in(user)
post :preview_markdown, params: { id: snippet, text: '*Markdown* text' } post :preview_markdown, params: { id: public_snippet, text: '*Markdown* text' }
expect(json_response.keys).to match_array(%w(body references)) expect(json_response.keys).to match_array(%w(body references))
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