Commit ea346826 authored by Vijay Hawoldar's avatar Vijay Hawoldar

Improve spec performance for snippets API specs

Total DB events 3864 to 3219
Total Factories 224 to 162
Total time 50.11s to 46.16 (local)
parent c5fa7c4e
......@@ -2,19 +2,28 @@
require 'spec_helper'
RSpec.describe API::Snippets do
RSpec.describe API::Snippets, factory_default: :keep do
include SnippetHelpers
let_it_be(:admin) { create(:user, :admin) }
let_it_be(:user) { create(:user) }
let_it_be(:other_user) { create(:user) }
let_it_be(:admin) { create(:user, :admin) }
let_it_be(:user) { create(:user) }
let_it_be(:other_user) { create(:user) }
let_it_be(:public_snippet) { create(:personal_snippet, :repository, :public, author: user) }
let_it_be(:private_snippet) { create(:personal_snippet, :repository, :private, author: user) }
let_it_be(:internal_snippet) { create(:personal_snippet, :repository, :internal, author: user) }
let_it_be(:user_token) { create(:personal_access_token, user: user) }
let_it_be(:other_user_token) { create(:personal_access_token, user: other_user) }
let_it_be(:project) do
create_default(:project, :public).tap do |p|
p.add_maintainer(user)
end
end
describe 'GET /snippets/' do
it 'returns snippets available for user' do
get api("/snippets/", user)
get api("/snippets/", personal_access_token: user_token)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
......@@ -30,7 +39,7 @@ RSpec.describe API::Snippets do
end
it 'hides private snippets from regular user' do
get api("/snippets/", other_user)
get api("/snippets/", personal_access_token: other_user_token)
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
......@@ -45,12 +54,10 @@ RSpec.describe API::Snippets do
end
it 'does not return snippets related to a project with disable feature visibility' do
project = create(:project)
create(:project_member, project: project, user: user)
public_snippet = create(:personal_snippet, :public, author: user, project: project)
public_snippet = create(:project_snippet, :public, author: user, project: project)
project.project_feature.update_attribute(:snippets_access_level, 0)
get api("/snippets/", user)
get api("/snippets/", personal_access_token: user_token)
json_response.each do |snippet|
expect(snippet["id"]).not_to eq(public_snippet.id)
......@@ -69,7 +76,7 @@ RSpec.describe API::Snippets do
let(:path) { "/snippets/public" }
it 'returns only public snippets from all users when authenticated' do
get api(path, user)
get api(path, personal_access_token: user_token)
aggregate_failures do
expect(response).to have_gitlab_http_status(:ok)
......@@ -101,7 +108,7 @@ RSpec.describe API::Snippets do
end
it 'returns raw text' do
get api("/snippets/#{snippet.id}/raw", user)
get api("/snippets/#{snippet.id}/raw", personal_access_token: user_token)
expect(response).to have_gitlab_http_status(:ok)
expect(response.media_type).to eq 'text/plain'
......@@ -111,7 +118,7 @@ RSpec.describe API::Snippets do
it 'returns 404 for invalid snippet id' do
snippet.destroy!
get api("/snippets/#{snippet.id}/raw", user)
get api("/snippets/#{snippet.id}/raw", personal_access_token: user_token)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Snippet Not Found')
......@@ -120,12 +127,12 @@ RSpec.describe API::Snippets do
it_behaves_like 'snippet blob content' do
let_it_be(:snippet_with_empty_repo) { create(:personal_snippet, :empty_repo, :private, author: user) }
subject { get api("/snippets/#{snippet.id}/raw", snippet.author) }
subject { get api("/snippets/#{snippet.id}/raw", snippet.author, personal_access_token: user_token) }
end
end
describe 'GET /snippets/:id/files/:ref/:file_path/raw' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :private) }
let(:snippet) { private_snippet }
it_behaves_like 'raw snippet files' do
let(:api_path) { "/snippets/#{snippet_id}/files/#{ref}/#{file_path}/raw" }
......@@ -139,7 +146,7 @@ RSpec.describe API::Snippets do
describe 'GET /snippets/:id' do
let(:snippet_id) { private_snippet.id }
subject { get api("/snippets/#{snippet_id}", user) }
subject { get api("/snippets/#{snippet_id}", personal_access_token: user_token) }
context 'with the author' do
it 'returns snippet json' do
......@@ -189,7 +196,7 @@ RSpec.describe API::Snippets do
let(:file_params) { { files: [{ file_path: file_path, content: file_content }] } }
let(:extra_params) { {} }
subject { post api("/snippets/", user), params: params }
subject { post api("/snippets/", personal_access_token: user_token), params: params }
shared_examples 'snippet creation' do
let(:snippet) { Snippet.find(json_response["id"]) }
......@@ -255,6 +262,7 @@ RSpec.describe API::Snippets do
context 'with an external user' do
let(:user) { create(:user, :external) }
let(:user_token) { create(:personal_access_token, user: user) }
it 'does not create a new snippet' do
subject
......@@ -328,8 +336,6 @@ RSpec.describe API::Snippets do
end
describe 'PUT /snippets/:id' do
let_it_be(:other_user) { create(:user) }
let(:visibility_level) { Snippet::PUBLIC }
let(:snippet) do
create(:personal_snippet, :repository, author: user, visibility_level: visibility_level)
......@@ -409,11 +415,10 @@ RSpec.describe API::Snippets do
end
context "when admin" do
let(:admin) { create(:admin) }
let(:token) { create(:personal_access_token, user: admin, scopes: [:sudo]) }
let_it_be(:token) { create(:personal_access_token, user: admin, scopes: [:sudo]) }
subject do
put api("/snippets/#{snippet.id}", admin, personal_access_token: token), params: { visibility: 'private', sudo: user.id }
put api("/snippets/#{snippet.id}", personal_access_token: token), params: { visibility: 'private', sudo: user.id }
end
context 'when sudo is defined' do
......@@ -440,34 +445,32 @@ RSpec.describe API::Snippets do
end
describe 'DELETE /snippets/:id' do
let!(:public_snippet) { create(:personal_snippet, :public, author: user) }
it 'deletes snippet' do
expect do
delete api("/snippets/#{public_snippet.id}", user)
delete api("/snippets/#{public_snippet.id}", personal_access_token: user_token)
expect(response).to have_gitlab_http_status(:no_content)
end.to change { PersonalSnippet.count }.by(-1)
end
it 'returns 404 for invalid snippet id' do
delete api("/snippets/#{non_existing_record_id}", user)
delete api("/snippets/#{non_existing_record_id}", personal_access_token: user_token)
expect(response).to have_gitlab_http_status(:not_found)
expect(json_response['message']).to eq('404 Snippet Not Found')
end
it_behaves_like '412 response' do
let(:request) { api("/snippets/#{public_snippet.id}", user) }
let(:request) { api("/snippets/#{public_snippet.id}", personal_access_token: user_token) }
end
end
describe "GET /snippets/:id/user_agent_detail" do
let(:admin) { create(:admin) }
let(:snippet) { create(:personal_snippet, :public, author: user) }
let!(:user_agent_detail) { create(:user_agent_detail, subject: snippet) }
let(:snippet) { public_snippet }
it 'exposes known attributes' do
user_agent_detail = create(:user_agent_detail, subject: snippet)
get api("/snippets/#{snippet.id}/user_agent_detail", admin)
expect(response).to have_gitlab_http_status(:ok)
......
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