2014-10-01 04:20:40 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe API::API, api: true do
|
|
|
|
include ApiHelpers
|
|
|
|
let(:user) { create(:user) }
|
2016-09-06 17:51:08 -04:00
|
|
|
let(:user2) { create(:user) }
|
2014-10-01 04:20:40 -04:00
|
|
|
let(:user3) { create(:user) }
|
|
|
|
let(:admin) { create(:admin) }
|
2016-09-06 17:51:08 -04:00
|
|
|
let(:group) { create(:group) }
|
2016-09-05 20:31:06 -04:00
|
|
|
let(:group2) do
|
|
|
|
group = create(:group, name: 'group2_name')
|
|
|
|
group.add_owner(user2)
|
|
|
|
group
|
|
|
|
end
|
2015-06-22 16:00:54 -04:00
|
|
|
|
|
|
|
let(:project) do
|
|
|
|
create(:project, creator_id: user.id, namespace: user.namespace)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:project_user2) do
|
2016-09-19 14:28:41 -04:00
|
|
|
create(:project_member, :reporter, user: user2, project: project)
|
2015-06-22 16:00:54 -04:00
|
|
|
end
|
2014-10-01 04:20:40 -04:00
|
|
|
|
|
|
|
describe 'POST /projects/fork/:id' do
|
|
|
|
before { project_user2 }
|
|
|
|
before { user3 }
|
|
|
|
|
|
|
|
context 'when authenticated' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'forks if user has sufficient access to project' do
|
2014-10-01 04:20:40 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2)
|
2016-09-06 04:52:42 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(201)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['name']).to eq(project.name)
|
|
|
|
expect(json_response['path']).to eq(project.path)
|
|
|
|
expect(json_response['owner']['id']).to eq(user2.id)
|
|
|
|
expect(json_response['namespace']['id']).to eq(user2.namespace.id)
|
|
|
|
expect(json_response['forked_from_project']['id']).to eq(project.id)
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'forks if user is admin' do
|
2014-10-01 04:20:40 -04:00
|
|
|
post api("/projects/fork/#{project.id}", admin)
|
2016-09-06 04:52:42 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(201)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['name']).to eq(project.name)
|
|
|
|
expect(json_response['path']).to eq(project.path)
|
|
|
|
expect(json_response['owner']['id']).to eq(admin.id)
|
|
|
|
expect(json_response['namespace']['id']).to eq(admin.namespace.id)
|
|
|
|
expect(json_response['forked_from_project']['id']).to eq(project.id)
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'fails on missing project access for the project to fork' do
|
2014-10-01 04:20:40 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user3)
|
2016-09-06 04:52:42 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['message']).to eq('404 Project Not Found')
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'fails if forked project exists in the user namespace' do
|
2014-10-01 04:20:40 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user)
|
2016-09-06 04:52:42 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(409)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['message']['name']).to eq(['has already been taken'])
|
|
|
|
expect(json_response['message']['path']).to eq(['has already been taken'])
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'fails if project to fork from does not exist' do
|
2014-10-01 04:20:40 -04:00
|
|
|
post api('/projects/fork/424242', user)
|
2016-09-06 04:52:42 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['message']).to eq('404 Project Not Found')
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
2016-09-05 20:31:06 -04:00
|
|
|
|
|
|
|
it 'forks with explicit own user namespace id' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2), namespace: user2.namespace.id
|
|
|
|
|
2016-09-05 20:31:06 -04:00
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['owner']['id']).to eq(user2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forks with explicit own user name as namespace' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2), namespace: user2.username
|
|
|
|
|
2016-09-05 20:31:06 -04:00
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['owner']['id']).to eq(user2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forks to another user when admin' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", admin), namespace: user2.username
|
|
|
|
|
2016-09-05 20:31:06 -04:00
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['owner']['id']).to eq(user2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if trying to fork to another user when not admin' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2), namespace: admin.namespace.id
|
|
|
|
|
2016-09-21 10:40:46 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2016-09-05 20:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails if trying to fork to non-existent namespace' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2), namespace: 42424242
|
|
|
|
|
2016-09-05 20:31:06 -04:00
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
expect(json_response['message']).to eq('404 Target Namespace Not Found')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forks to owned group' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2), namespace: group2.name
|
|
|
|
|
2016-09-05 20:31:06 -04:00
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['namespace']['name']).to eq(group2.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails to fork to not owned group' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", user2), namespace: group.name
|
|
|
|
|
2016-09-21 10:40:46 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2016-09-05 20:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'forks to not owned group when admin' do
|
2016-09-06 04:52:42 -04:00
|
|
|
post api("/projects/fork/#{project.id}", admin), namespace: group.name
|
|
|
|
|
2016-09-05 20:31:06 -04:00
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response['namespace']['name']).to eq(group.name)
|
|
|
|
end
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when unauthenticated' do
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'returns authentication error' do
|
2014-10-01 04:20:40 -04:00
|
|
|
post api("/projects/fork/#{project.id}")
|
2016-09-06 04:52:42 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(401)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['message']).to eq('401 Unauthorized')
|
2014-10-01 04:20:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|