Commit d2b8e54c authored by Jan Provaznik's avatar Jan Provaznik

Use let_it_be in requests/api/projects_spec.rb

* uses let_it_be for resources
parent 8672da3c
...@@ -49,15 +49,15 @@ end ...@@ -49,15 +49,15 @@ end
RSpec.describe API::Projects do RSpec.describe API::Projects do
include ProjectForksHelper include ProjectForksHelper
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:user2) { create(:user) } let_it_be(:user2) { create(:user) }
let(:user3) { create(:user) } let_it_be(:user3) { create(:user) }
let(:admin) { create(:admin) } let_it_be(:admin) { create(:admin) }
let(:project) { create(:project, :repository, namespace: user.namespace) } let_it_be(:project, reload: true) { create(:project, :repository, namespace: user.namespace) }
let(:project2) { create(:project, namespace: user.namespace) } let_it_be(:project2, reload: true) { create(:project, namespace: user.namespace) }
let(:project_member) { create(:project_member, :developer, user: user3, project: project) } let_it_be(:project_member) { create(:project_member, :developer, user: user3, project: project) }
let(:user4) { create(:user, username: 'user.with.dot') } let_it_be(:user4) { create(:user, username: 'user.with.dot') }
let(:project3) do let_it_be(:project3, reload: true) do
create(:project, create(:project,
:private, :private,
:repository, :repository,
...@@ -71,14 +71,14 @@ RSpec.describe API::Projects do ...@@ -71,14 +71,14 @@ RSpec.describe API::Projects do
snippets_enabled: false) snippets_enabled: false)
end end
let(:project_member2) do let_it_be(:project_member2) do
create(:project_member, create(:project_member,
user: user4, user: user4,
project: project3, project: project3,
access_level: ProjectMember::MAINTAINER) access_level: ProjectMember::MAINTAINER)
end end
let(:project4) do let_it_be(:project4, reload: true) do
create(:project, create(:project,
name: 'third_project', name: 'third_project',
path: 'third_project', path: 'third_project',
...@@ -86,6 +86,8 @@ RSpec.describe API::Projects do ...@@ -86,6 +86,8 @@ RSpec.describe API::Projects do
namespace: user4.namespace) namespace: user4.namespace)
end end
let(:user_projects) { [public_project, project, project2, project3] }
shared_context 'with language detection' do shared_context 'with language detection' do
let(:ruby) { create(:programming_language, name: 'Ruby') } let(:ruby) { create(:programming_language, name: 'Ruby') }
let(:javascript) { create(:programming_language, name: 'JavaScript') } let(:javascript) { create(:programming_language, name: 'JavaScript') }
...@@ -146,14 +148,7 @@ RSpec.describe API::Projects do ...@@ -146,14 +148,7 @@ RSpec.describe API::Projects do
end end
end end
let!(:public_project) { create(:project, :public, name: 'public_project') } let_it_be(:public_project) { create(:project, :public, name: 'public_project') }
before do
project
project2
project3
project4
end
context 'when unauthenticated' do context 'when unauthenticated' do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
...@@ -171,7 +166,7 @@ RSpec.describe API::Projects do ...@@ -171,7 +166,7 @@ RSpec.describe API::Projects do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { {} } let(:filter) { {} }
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3] } let(:projects) { user_projects }
end end
it_behaves_like 'projects response without N + 1 queries' do it_behaves_like 'projects response without N + 1 queries' do
...@@ -257,7 +252,7 @@ RSpec.describe API::Projects do ...@@ -257,7 +252,7 @@ RSpec.describe API::Projects do
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).to be_an Array expect(json_response).to be_an Array
statistics = json_response.first['statistics'] statistics = json_response.find { |p| p['id'] == project.id }['statistics']
expect(statistics).to be_present expect(statistics).to be_present
expect(statistics).to include('commit_count', 'storage_size', 'repository_size', 'wiki_size', 'lfs_objects_size', 'job_artifacts_size', 'snippets_size') expect(statistics).to include('commit_count', 'storage_size', 'repository_size', 'wiki_size', 'lfs_objects_size', 'job_artifacts_size', 'snippets_size')
end end
...@@ -386,14 +381,14 @@ RSpec.describe API::Projects do ...@@ -386,14 +381,14 @@ RSpec.describe API::Projects do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { { id_after: project2.id } } let(:filter) { { id_after: project2.id } }
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3].select { |p| p.id > project2.id } } let(:projects) { user_projects.select { |p| p.id > project2.id } }
end end
context 'regression: empty string is ignored' do context 'regression: empty string is ignored' do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { { id_after: '' } } let(:filter) { { id_after: '' } }
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3] } let(:projects) { user_projects }
end end
end end
end end
...@@ -402,14 +397,14 @@ RSpec.describe API::Projects do ...@@ -402,14 +397,14 @@ RSpec.describe API::Projects do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { { id_before: project2.id } } let(:filter) { { id_before: project2.id } }
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3].select { |p| p.id < project2.id } } let(:projects) { user_projects.select { |p| p.id < project2.id } }
end end
context 'regression: empty string is ignored' do context 'regression: empty string is ignored' do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { { id_before: '' } } let(:filter) { { id_before: '' } }
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3] } let(:projects) { user_projects }
end end
end end
end end
...@@ -418,7 +413,7 @@ RSpec.describe API::Projects do ...@@ -418,7 +413,7 @@ RSpec.describe API::Projects do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { { id_before: project2.id, id_after: public_project.id } } let(:filter) { { id_before: project2.id, id_after: public_project.id } }
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3].select { |p| p.id < project2.id && p.id > public_project.id } } let(:projects) { user_projects.select { |p| p.id < project2.id && p.id > public_project.id } }
end end
end end
...@@ -481,7 +476,7 @@ RSpec.describe API::Projects do ...@@ -481,7 +476,7 @@ RSpec.describe API::Projects do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.first['id']).to eq(project3.id) expect(json_response.map { |p| p['id'] }).to eq(user_projects.map(&:id).sort.reverse)
end end
end end
...@@ -501,7 +496,6 @@ RSpec.describe API::Projects do ...@@ -501,7 +496,6 @@ RSpec.describe API::Projects do
let(:public_project) { create(:project, :public) } let(:public_project) { create(:project, :public) }
before do before do
project_member
user3.update!(starred_projects: [project, project2, project3, public_project]) user3.update!(starred_projects: [project, project2, project3, public_project])
end end
...@@ -642,7 +636,6 @@ RSpec.describe API::Projects do ...@@ -642,7 +636,6 @@ RSpec.describe API::Projects do
context 'non-admin user' do context 'non-admin user' do
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3] }
it 'returns projects ordered normally' do it 'returns projects ordered normally' do
get api('/projects', current_user), params: { order_by: order_by } get api('/projects', current_user), params: { order_by: order_by }
...@@ -650,7 +643,7 @@ RSpec.describe API::Projects do ...@@ -650,7 +643,7 @@ RSpec.describe API::Projects do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.map { |project| project['id'] }).to eq(projects.map(&:id).reverse) expect(json_response.map { |project| project['id'] }).to eq(user_projects.map(&:id).sort.reverse)
end end
end end
end end
...@@ -686,7 +679,8 @@ RSpec.describe API::Projects do ...@@ -686,7 +679,8 @@ RSpec.describe API::Projects do
context 'with keyset pagination' do context 'with keyset pagination' do
let(:current_user) { user } let(:current_user) { user }
let(:projects) { [public_project, project, project2, project3] } let(:first_project_id) { user_projects.map(&:id).min }
let(:last_project_id) { user_projects.map(&:id).max }
context 'headers and records' do context 'headers and records' do
let(:params) { { pagination: 'keyset', order_by: :id, sort: :asc, per_page: 1 } } let(:params) { { pagination: 'keyset', order_by: :id, sort: :asc, per_page: 1 } }
...@@ -696,11 +690,11 @@ RSpec.describe API::Projects do ...@@ -696,11 +690,11 @@ RSpec.describe API::Projects do
expect(response.header).to include('Links') expect(response.header).to include('Links')
expect(response.header['Links']).to include('pagination=keyset') expect(response.header['Links']).to include('pagination=keyset')
expect(response.header['Links']).to include("id_after=#{public_project.id}") expect(response.header['Links']).to include("id_after=#{first_project_id}")
expect(response.header).to include('Link') expect(response.header).to include('Link')
expect(response.header['Link']).to include('pagination=keyset') expect(response.header['Link']).to include('pagination=keyset')
expect(response.header['Link']).to include("id_after=#{public_project.id}") expect(response.header['Link']).to include("id_after=#{first_project_id}")
end end
it 'contains only the first project with per_page = 1' do it 'contains only the first project with per_page = 1' do
...@@ -708,7 +702,7 @@ RSpec.describe API::Projects do ...@@ -708,7 +702,7 @@ RSpec.describe API::Projects do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.map { |p| p['id'] }).to contain_exactly(public_project.id) expect(json_response.map { |p| p['id'] }).to contain_exactly(first_project_id)
end end
it 'still includes a link if the end has reached and there is no more data after this page' do it 'still includes a link if the end has reached and there is no more data after this page' do
...@@ -752,11 +746,11 @@ RSpec.describe API::Projects do ...@@ -752,11 +746,11 @@ RSpec.describe API::Projects do
expect(response.header).to include('Links') expect(response.header).to include('Links')
expect(response.header['Links']).to include('pagination=keyset') expect(response.header['Links']).to include('pagination=keyset')
expect(response.header['Links']).to include("id_before=#{project3.id}") expect(response.header['Links']).to include("id_before=#{last_project_id}")
expect(response.header).to include('Link') expect(response.header).to include('Link')
expect(response.header['Link']).to include('pagination=keyset') expect(response.header['Link']).to include('pagination=keyset')
expect(response.header['Link']).to include("id_before=#{project3.id}") expect(response.header['Link']).to include("id_before=#{last_project_id}")
end end
it 'contains only the last project with per_page = 1' do it 'contains only the last project with per_page = 1' do
...@@ -764,7 +758,7 @@ RSpec.describe API::Projects do ...@@ -764,7 +758,7 @@ RSpec.describe API::Projects do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.map { |p| p['id'] }).to contain_exactly(project3.id) expect(json_response.map { |p| p['id'] }).to contain_exactly(last_project_id)
end end
end end
...@@ -793,7 +787,7 @@ RSpec.describe API::Projects do ...@@ -793,7 +787,7 @@ RSpec.describe API::Projects do
ids += Gitlab::Json.parse(response.body).map { |p| p['id'] } ids += Gitlab::Json.parse(response.body).map { |p| p['id'] }
end end
expect(ids).to contain_exactly(*projects.map(&:id)) expect(ids).to contain_exactly(*user_projects.map(&:id))
end end
end end
end end
...@@ -814,7 +808,7 @@ RSpec.describe API::Projects do ...@@ -814,7 +808,7 @@ RSpec.describe API::Projects do
.to change { Project.count }.by(1) .to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:created) expect(response).to have_gitlab_http_status(:created)
project = Project.first project = Project.last
expect(project.name).to eq('Foo Project') expect(project.name).to eq('Foo Project')
expect(project.path).to eq('foo-project') expect(project.path).to eq('foo-project')
...@@ -825,7 +819,7 @@ RSpec.describe API::Projects do ...@@ -825,7 +819,7 @@ RSpec.describe API::Projects do
.to change { Project.count }.by(1) .to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:created) expect(response).to have_gitlab_http_status(:created)
project = Project.first project = Project.last
expect(project.name).to eq('foo_project') expect(project.name).to eq('foo_project')
expect(project.path).to eq('foo_project') expect(project.path).to eq('foo_project')
...@@ -836,7 +830,7 @@ RSpec.describe API::Projects do ...@@ -836,7 +830,7 @@ RSpec.describe API::Projects do
.to change { Project.count }.by(1) .to change { Project.count }.by(1)
expect(response).to have_gitlab_http_status(:created) expect(response).to have_gitlab_http_status(:created)
project = Project.first project = Project.last
expect(project.name).to eq('Foo Project') expect(project.name).to eq('Foo Project')
expect(project.path).to eq('path-project-Foo') expect(project.path).to eq('path-project-Foo')
...@@ -1985,7 +1979,8 @@ RSpec.describe API::Projects do ...@@ -1985,7 +1979,8 @@ RSpec.describe API::Projects do
context 'when authenticated' do context 'when authenticated' do
context 'valid request' do context 'valid request' do
it_behaves_like 'project users response' do it_behaves_like 'project users response' do
let(:current_user) { user } let(:project) { project4 }
let(:current_user) { user4 }
end end
end end
...@@ -2011,8 +2006,8 @@ RSpec.describe API::Projects do ...@@ -2011,8 +2006,8 @@ RSpec.describe API::Projects do
get api("/projects/#{project.id}/users?skip_users=#{user.id}", user) get api("/projects/#{project.id}/users?skip_users=#{user.id}", user)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to eq(1) expect(json_response.size).to eq(2)
expect(json_response[0]['id']).to eq(other_user.id) expect(json_response.map { |m| m['id'] }).not_to include(user.id)
end end
end end
end end
......
...@@ -14,8 +14,7 @@ RSpec.shared_examples 'custom attributes endpoints' do |attributable_name| ...@@ -14,8 +14,7 @@ RSpec.shared_examples 'custom attributes endpoints' do |attributable_name|
get api("/#{attributable_name}", user), params: { custom_attributes: { foo: 'foo', bar: 'bar' } } get api("/#{attributable_name}", user), params: { custom_attributes: { foo: 'foo', bar: 'bar' } }
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to be 2 expect(json_response.map { |r| r['id'] }).to include(attributable.id, other_attributable.id)
expect(json_response.map { |r| r['id'] }).to contain_exactly attributable.id, other_attributable.id
end end
end end
...@@ -40,7 +39,7 @@ RSpec.shared_examples 'custom attributes endpoints' do |attributable_name| ...@@ -40,7 +39,7 @@ RSpec.shared_examples 'custom attributes endpoints' do |attributable_name|
get api("/#{attributable_name}", user), params: { with_custom_attributes: true } get api("/#{attributable_name}", user), params: { with_custom_attributes: true }
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to be 2 expect(json_response).not_to be_empty
expect(json_response.first).not_to include 'custom_attributes' expect(json_response.first).not_to include 'custom_attributes'
end end
end end
...@@ -50,16 +49,15 @@ RSpec.shared_examples 'custom attributes endpoints' do |attributable_name| ...@@ -50,16 +49,15 @@ RSpec.shared_examples 'custom attributes endpoints' do |attributable_name|
get api("/#{attributable_name}", admin) get api("/#{attributable_name}", admin)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to be 2 expect(json_response).not_to be_empty
expect(json_response.first).not_to include 'custom_attributes' expect(json_response.first).not_to include 'custom_attributes'
expect(json_response.second).not_to include 'custom_attributes'
end end
it 'includes custom attributes if requested' do it 'includes custom attributes if requested' do
get api("/#{attributable_name}", admin), params: { with_custom_attributes: true } get api("/#{attributable_name}", admin), params: { with_custom_attributes: true }
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response.size).to be 2 expect(json_response).not_to be_empty
attributable_response = json_response.find { |r| r['id'] == attributable.id } attributable_response = json_response.find { |r| r['id'] == attributable.id }
other_attributable_response = json_response.find { |r| r['id'] == other_attributable.id } other_attributable_response = json_response.find { |r| r['id'] == other_attributable.id }
......
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