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