Commit 30aaf004 authored by Patrick Bajao's avatar Patrick Bajao

Refactor specs to be a bit DRYer

parent 91f92fa7
...@@ -27,22 +27,25 @@ describe API::Internal do ...@@ -27,22 +27,25 @@ describe API::Internal do
context "project alias" do context "project alias" do
let(:project) { create(:project, :public, :repository) } let(:project) { create(:project, :public, :repository) }
let(:project_alias) { create(:project_alias, project: project) }
def check_access_by_alias(alias_name)
post(
api("/internal/allowed"),
params: {
action: "git-upload-pack",
key_id: key.id,
project: alias_name,
protocol: 'ssh',
secret_token: secret_token
}
)
end
context "without premium license" do context "without premium license" do
context "project matches a project alias" do context "project matches a project alias" do
let(:project_alias) { create(:project_alias, project: project) }
before do before do
post( check_access_by_alias(project_alias.name)
api("/internal/allowed"),
params: {
action: "git-upload-pack",
key_id: key.id,
project: project_alias.name,
protocol: 'ssh',
secret_token: secret_token
}
)
end end
it "does not allow access because project can't be found" do it "does not allow access because project can't be found" do
...@@ -57,19 +60,8 @@ describe API::Internal do ...@@ -57,19 +60,8 @@ describe API::Internal do
end end
context "project matches a project alias" do context "project matches a project alias" do
let(:project_alias) { create(:project_alias, project: project) }
before do before do
post( check_access_by_alias(project_alias.name)
api("/internal/allowed"),
params: {
action: "git-upload-pack",
key_id: key.id,
project: project_alias.name,
protocol: 'ssh',
secret_token: secret_token
}
)
end end
it "allows access" do it "allows access" do
...@@ -79,16 +71,7 @@ describe API::Internal do ...@@ -79,16 +71,7 @@ describe API::Internal do
context "project doesn't match a project alias" do context "project doesn't match a project alias" do
before do before do
post( check_access_by_alias('some-project')
api("/internal/allowed"),
params: {
action: "git-upload-pack",
key_id: key.id,
project: "some-project",
protocol: 'ssh',
secret_token: secret_token
}
)
end end
it "does not allow access because project can't be found" do it "does not allow access because project can't be found" do
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe API::ProjectAliases, api: true do describe API::ProjectAliases, api: true do
set(:user) { create(:user) } let(:user) { create(:user) }
set(:admin) { create(:admin) } let(:admin) { create(:admin) }
context 'without premium license' do context 'without premium license' do
describe 'GET /project_aliases' do describe 'GET /project_aliases' do
...@@ -55,15 +55,9 @@ describe API::ProjectAliases, api: true do ...@@ -55,15 +55,9 @@ describe API::ProjectAliases, api: true do
end end
context 'with premium license' do context 'with premium license' do
before do shared_examples_for 'GitLab administrator only API endpoint' do
create(:license, plan: License::PREMIUM_PLAN)
end
describe 'GET /project_aliases' do
context 'anonymous user' do context 'anonymous user' do
before do let(:user) { nil }
get api('/project_aliases')
end
it 'returns 401' do it 'returns 401' do
expect(response).to have_gitlab_http_status(401) expect(response).to have_gitlab_http_status(401)
...@@ -71,23 +65,28 @@ describe API::ProjectAliases, api: true do ...@@ -71,23 +65,28 @@ describe API::ProjectAliases, api: true do
end end
context 'regular user' do context 'regular user' do
before do
get api('/project_aliases', user)
end
it 'returns 403' do it 'returns 403' do
expect(response).to have_gitlab_http_status(403) expect(response).to have_gitlab_http_status(403)
end end
end end
end
before do
create(:license, plan: License::PREMIUM_PLAN)
end
describe 'GET /project_aliases' do
before do
get api('/project_aliases', user)
end
it_behaves_like 'GitLab administrator only API endpoint'
context 'admin' do context 'admin' do
let(:user) { admin }
let!(:project_alias_1) { create(:project_alias) } let!(:project_alias_1) { create(:project_alias) }
let!(:project_alias_2) { create(:project_alias) } let!(:project_alias_2) { create(:project_alias) }
before do
get api('/project_aliases', admin)
end
it 'returns the project aliases list' do it 'returns the project aliases list' do
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project_aliases', dir: 'ee') expect(response).to match_response_schema('public_api/v4/project_aliases', dir: 'ee')
...@@ -97,33 +96,18 @@ describe API::ProjectAliases, api: true do ...@@ -97,33 +96,18 @@ describe API::ProjectAliases, api: true do
describe 'GET /project_aliases/:name' do describe 'GET /project_aliases/:name' do
let(:project_alias) { create(:project_alias) } let(:project_alias) { create(:project_alias) }
let(:alias_name) { project_alias.name }
context 'anonymous user' do before do
before do get api("/project_aliases/#{alias_name}", user)
get api("/project_aliases/#{project_alias.name}")
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end end
context 'regular user' do it_behaves_like 'GitLab administrator only API endpoint'
before do
get api("/project_aliases/#{project_alias.name}", user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do context 'admin' do
context 'existing project alias' do let(:user) { admin }
before do
get api("/project_aliases/#{project_alias.name}", admin)
end
context 'existing project alias' do
it 'returns the project alias' do it 'returns the project alias' do
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
expect(response).to match_response_schema('public_api/v4/project_alias', dir: 'ee') expect(response).to match_response_schema('public_api/v4/project_alias', dir: 'ee')
...@@ -131,9 +115,7 @@ describe API::ProjectAliases, api: true do ...@@ -131,9 +115,7 @@ describe API::ProjectAliases, api: true do
end end
context 'non-existent project alias' do context 'non-existent project alias' do
before do let(:alias_name) { 'some-project' }
get api("/project_aliases/some-project", admin)
end
it 'returns 404' do it 'returns 404' do
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(404)
...@@ -144,44 +126,26 @@ describe API::ProjectAliases, api: true do ...@@ -144,44 +126,26 @@ describe API::ProjectAliases, api: true do
describe 'POST /project_aliases' do describe 'POST /project_aliases' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:project_alias) { create(:project_alias) }
let(:alias_name) { project_alias.name }
context 'anonymous user' do before do
before do post api("/project_aliases", user), params: { project_id: project.id, name: alias_name }
post api("/project_aliases")
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end end
context 'regular user' do it_behaves_like 'GitLab administrator only API endpoint'
before do
post api("/project_aliases", user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do context 'admin' do
context 'existing project alias' do let(:user) { admin }
let(:project_alias) { create(:project_alias) }
before do
post api("/project_aliases", admin), params: { project_id: project.id, name: project_alias.name }
end
context 'existing project alias' do
it 'returns 400' do it 'returns 400' do
expect(response).to have_gitlab_http_status(400) expect(response).to have_gitlab_http_status(400)
end end
end end
context 'non-existent project alias' do context 'non-existent project alias' do
before do let(:alias_name) { 'some-project' }
post api("/project_aliases", admin), params: { project_id: project.id, name: 'some-project' }
end
it 'returns 200' do it 'returns 200' do
expect(response).to have_gitlab_http_status(201) expect(response).to have_gitlab_http_status(201)
...@@ -193,42 +157,25 @@ describe API::ProjectAliases, api: true do ...@@ -193,42 +157,25 @@ describe API::ProjectAliases, api: true do
describe 'DELETE /project_aliases/:name' do describe 'DELETE /project_aliases/:name' do
let(:project_alias) { create(:project_alias) } let(:project_alias) { create(:project_alias) }
let(:alias_name) { project_alias.name }
context 'anonymous user' do before do
before do delete api("/project_aliases/#{alias_name}", user)
delete api("/project_aliases/#{project_alias.name}")
end
it 'returns 401' do
expect(response).to have_gitlab_http_status(401)
end
end end
context 'regular user' do it_behaves_like 'GitLab administrator only API endpoint'
before do
delete api("/project_aliases/#{project_alias.name}", user)
end
it 'returns 403' do
expect(response).to have_gitlab_http_status(403)
end
end
context 'admin' do context 'admin' do
context 'existing project alias' do let(:user) { admin }
before do
delete api("/project_aliases/#{project_alias.name}", admin)
end
context 'existing project alias' do
it 'returns 204' do it 'returns 204' do
expect(response).to have_gitlab_http_status(204) expect(response).to have_gitlab_http_status(204)
end end
end end
context 'non-existent project alias' do context 'non-existent project alias' do
before do let(:alias_name) { 'some-project' }
delete api("/project_aliases/some-project", admin)
end
it 'returns 404' do it 'returns 404' do
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(404)
......
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