BigW Consortium Gitlab

fork_spec.rb 4.67 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers
  let(:user)  { create(:user) }
6
  let(:user2) { create(:user) }
7 8
  let(:user3) { create(:user) }
  let(:admin) { create(:admin) }
9
  let(:group) { create(:group) }
10 11 12 13 14
  let(:group2) do
    group = create(:group, name: 'group2_name')
    group.add_owner(user2)
    group
  end
15 16 17 18 19 20

  let(:project) do
    create(:project, creator_id: user.id, namespace: user.namespace)
  end

  let(:project_user2) do
21
    create(:project_member, :reporter, user: user2, project: project)
22
  end
23 24 25 26 27 28

  describe 'POST /projects/fork/:id' do
    before { project_user2 }
    before { user3 }

    context 'when authenticated' do
29
      it 'forks if user has sufficient access to project' do
30
        post api("/projects/fork/#{project.id}", user2)
31

32
        expect(response).to have_http_status(201)
33 34 35 36 37
        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)
38 39
      end

40
      it 'forks if user is admin' do
41
        post api("/projects/fork/#{project.id}", admin)
42

43
        expect(response).to have_http_status(201)
44 45 46 47 48
        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)
49 50
      end

51
      it 'fails on missing project access for the project to fork' do
52
        post api("/projects/fork/#{project.id}", user3)
53

54
        expect(response).to have_http_status(404)
55
        expect(json_response['message']).to eq('404 Project Not Found')
56 57
      end

58
      it 'fails if forked project exists in the user namespace' do
59
        post api("/projects/fork/#{project.id}", user)
60

61
        expect(response).to have_http_status(409)
62 63
        expect(json_response['message']['name']).to eq(['has already been taken'])
        expect(json_response['message']['path']).to eq(['has already been taken'])
64 65
      end

66
      it 'fails if project to fork from does not exist' do
67
        post api('/projects/fork/424242', user)
68

69
        expect(response).to have_http_status(404)
70
        expect(json_response['message']).to eq('404 Project Not Found')
71
      end
72 73

      it 'forks with explicit own user namespace id' do
74 75
        post api("/projects/fork/#{project.id}", user2), namespace: user2.namespace.id

76 77 78 79 80
        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
81 82
        post api("/projects/fork/#{project.id}", user2), namespace: user2.username

83 84 85 86 87
        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
88 89
        post api("/projects/fork/#{project.id}", admin), namespace: user2.username

90 91 92 93 94
        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
95 96
        post api("/projects/fork/#{project.id}", user2), namespace: admin.namespace.id

97
        expect(response).to have_http_status(404)
98 99 100
      end

      it 'fails if trying to fork to non-existent namespace' do
101 102
        post api("/projects/fork/#{project.id}", user2), namespace: 42424242

103 104 105 106 107
        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
108 109
        post api("/projects/fork/#{project.id}", user2), namespace: group2.name

110 111 112 113 114
        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
115 116
        post api("/projects/fork/#{project.id}", user2), namespace: group.name

117
        expect(response).to have_http_status(404)
118 119 120
      end

      it 'forks to not owned group when admin' do
121 122
        post api("/projects/fork/#{project.id}", admin), namespace: group.name

123 124 125
        expect(response).to have_http_status(201)
        expect(json_response['namespace']['name']).to eq(group.name)
      end
126 127 128
    end

    context 'when unauthenticated' do
129
      it 'returns authentication error' do
130
        post api("/projects/fork/#{project.id}")
131

132
        expect(response).to have_http_status(401)
133
        expect(json_response['message']).to eq('401 Unauthorized')
134 135 136 137
      end
    end
  end
end