BigW Consortium Gitlab

fork_spec.rb 2.61 KB
Newer Older
1 2 3 4 5 6 7 8
require 'spec_helper'

describe API::API, api: true  do
  include ApiHelpers
  let(:user)  { create(:user) }
  let(:user2) { create(:user) }
  let(:user3) { create(:user) }
  let(:admin) { create(:admin) }
9 10 11 12 13 14 15 16

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

  let(:project_user2) do
    create(:project_member, user: user2, project: project, access_level: ProjectMember::GUEST)
  end
17 18 19 20 21 22 23 24

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

    context 'when authenticated' do
      it 'should fork if user has sufficient access to project' do
        post api("/projects/fork/#{project.id}", user2)
25 26 27 28 29 30
        expect(response.status).to eq(201)
        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)
31 32 33 34
      end

      it 'should fork if user is admin' do
        post api("/projects/fork/#{project.id}", admin)
35 36 37 38 39 40
        expect(response.status).to eq(201)
        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)
41 42 43 44
      end

      it 'should fail on missing project access for the project to fork' do
        post api("/projects/fork/#{project.id}", user3)
45 46
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 Project Not Found')
47 48 49 50
      end

      it 'should fail if forked project exists in the user namespace' do
        post api("/projects/fork/#{project.id}", user)
51 52 53
        expect(response.status).to eq(409)
        expect(json_response['message']['name']).to eq(['has already been taken'])
        expect(json_response['message']['path']).to eq(['has already been taken'])
54 55 56 57
      end

      it 'should fail if project to fork from does not exist' do
        post api('/projects/fork/424242', user)
58 59
        expect(response.status).to eq(404)
        expect(json_response['message']).to eq('404 Project Not Found')
60 61 62 63 64 65
      end
    end

    context 'when unauthenticated' do
      it 'should return authentication error' do
        post api("/projects/fork/#{project.id}")
66 67
        expect(response.status).to eq(401)
        expect(json_response['message']).to eq('401 Unauthorized')
68 69 70 71
      end
    end
  end
end