BigW Consortium Gitlab

project_members_spec.rb 5.67 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::API, api: true  do
4 5 6 7 8
  include ApiHelpers
  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let(:user3) { create(:user) }
  let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
9 10
  let(:project_member) { create(:project_member, :master, user: user, project: project) }
  let(:project_member2) { create(:project_member, :developer, user: user3, project: project) }
11 12

  describe "GET /projects/:id/members" do
13 14
    before { project_member }
    before { project_member2 }
15 16 17

    it "should return project team members" do
      get api("/projects/#{project.id}/members", user)
18 19 20 21
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.count).to eq(2)
      expect(json_response.map { |u| u['username'] }).to include user.username
22 23 24 25
    end

    it "finds team members with query string" do
      get api("/projects/#{project.id}/members", user), query: user.username
26 27 28 29
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
      expect(json_response.count).to eq(1)
      expect(json_response.first['username']).to eq(user.username)
30 31 32 33
    end

    it "should return a 404 error if id not found" do
      get api("/projects/9999/members", user)
34
      expect(response.status).to eq(404)
35 36 37 38
    end
  end

  describe "GET /projects/:id/members/:user_id" do
39
    before { project_member }
40 41 42

    it "should return project team member" do
      get api("/projects/#{project.id}/members/#{user.id}", user)
43 44 45
      expect(response.status).to eq(200)
      expect(json_response['username']).to eq(user.username)
      expect(json_response['access_level']).to eq(ProjectMember::MASTER)
46 47 48 49
    end

    it "should return a 404 error if user id not found" do
      get api("/projects/#{project.id}/members/1234", user)
50
      expect(response.status).to eq(404)
51 52 53 54 55
    end
  end

  describe "POST /projects/:id/members" do
    it "should add user to project team" do
56 57 58
      expect do
        post api("/projects/#{project.id}/members", user), user_id: user2.id, access_level: ProjectMember::DEVELOPER
      end.to change { ProjectMember.count }.by(1)
59

60 61 62
      expect(response.status).to eq(201)
      expect(json_response['username']).to eq(user2.username)
      expect(json_response['access_level']).to eq(ProjectMember::DEVELOPER)
63 64 65
    end

    it "should return a 201 status if user is already project member" do
66 67 68
      post api("/projects/#{project.id}/members", user),
           user_id: user2.id,
           access_level: ProjectMember::DEVELOPER
69 70 71
      expect do
        post api("/projects/#{project.id}/members", user), user_id: user2.id, access_level: ProjectMember::DEVELOPER
      end.not_to change { ProjectMember.count }
72

73 74 75
      expect(response.status).to eq(201)
      expect(json_response['username']).to eq(user2.username)
      expect(json_response['access_level']).to eq(ProjectMember::DEVELOPER)
76 77 78
    end

    it "should return a 400 error when user id is not given" do
79
      post api("/projects/#{project.id}/members", user), access_level: ProjectMember::MASTER
80
      expect(response.status).to eq(400)
81 82 83 84
    end

    it "should return a 400 error when access level is not given" do
      post api("/projects/#{project.id}/members", user), user_id: user2.id
85
      expect(response.status).to eq(400)
86 87 88 89
    end

    it "should return a 422 error when access level is not known" do
      post api("/projects/#{project.id}/members", user), user_id: user2.id, access_level: 1234
90
      expect(response.status).to eq(422)
91 92 93 94
    end
  end

  describe "PUT /projects/:id/members/:user_id" do
95
    before { project_member2 }
96 97

    it "should update project team member" do
98
      put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: ProjectMember::MASTER
99 100 101
      expect(response.status).to eq(200)
      expect(json_response['username']).to eq(user3.username)
      expect(json_response['access_level']).to eq(ProjectMember::MASTER)
102 103 104
    end

    it "should return a 404 error if user_id is not found" do
105
      put api("/projects/#{project.id}/members/1234", user), access_level: ProjectMember::MASTER
106
      expect(response.status).to eq(404)
107 108 109 110
    end

    it "should return a 400 error when access level is not given" do
      put api("/projects/#{project.id}/members/#{user3.id}", user)
111
      expect(response.status).to eq(400)
112 113 114 115
    end

    it "should return a 422 error when access level is not known" do
      put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: 123
116
      expect(response.status).to eq(422)
117 118 119 120
    end
  end

  describe "DELETE /projects/:id/members/:user_id" do
121 122
    before { project_member }
    before { project_member2 }
123 124

    it "should remove user from project team" do
125
      expect do
126
        delete api("/projects/#{project.id}/members/#{user3.id}", user)
127
      end.to change { ProjectMember.count }.by(-1)
128 129 130 131
    end

    it "should return 200 if team member is not part of a project" do
      delete api("/projects/#{project.id}/members/#{user3.id}", user)
132
      expect do
133
        delete api("/projects/#{project.id}/members/#{user3.id}", user)
134
      end.to_not change { ProjectMember.count }
135 136 137 138 139
    end

    it "should return 200 if team member already removed" do
      delete api("/projects/#{project.id}/members/#{user3.id}", user)
      delete api("/projects/#{project.id}/members/#{user3.id}", user)
140
      expect(response.status).to eq(200)
141 142 143
    end

    it "should return 200 OK when the user was not member" do
144
      expect do
145
        delete api("/projects/#{project.id}/members/1000000", user)
146
      end.to change { ProjectMember.count }.by(0)
147 148 149
      expect(response.status).to eq(200)
      expect(json_response['message']).to eq("Access revoked")
      expect(json_response['id']).to eq(1000000)
150 151 152
    end
  end
end