debian-mirror-gitlab/spec/requests/api/project_members_spec.rb

153 lines
5.7 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
describe API::API, api: true do
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) }
2015-04-26 12:48:37 +05:30
let(:project_member) { create(:project_member, user: user, project: project, access_level: ProjectMember::MASTER) }
let(:project_member2) { create(:project_member, user: user3, project: project, access_level: ProjectMember::DEVELOPER) }
2014-09-02 18:07:02 +05:30
describe "GET /projects/:id/members" do
2015-04-26 12:48:37 +05:30
before { project_member }
before { project_member2 }
2014-09-02 18:07:02 +05:30
it "should return project team members" do
get api("/projects/#{project.id}/members", user)
2015-04-26 12:48:37 +05:30
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
2014-09-02 18:07:02 +05:30
end
it "finds team members with query string" do
get api("/projects/#{project.id}/members", user), query: user.username
2015-04-26 12:48:37 +05:30
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)
2014-09-02 18:07:02 +05:30
end
it "should return a 404 error if id not found" do
get api("/projects/9999/members", user)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(404)
2014-09-02 18:07:02 +05:30
end
end
describe "GET /projects/:id/members/:user_id" do
2015-04-26 12:48:37 +05:30
before { project_member }
2014-09-02 18:07:02 +05:30
it "should return project team member" do
get api("/projects/#{project.id}/members/#{user.id}", user)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(200)
expect(json_response['username']).to eq(user.username)
expect(json_response['access_level']).to eq(ProjectMember::MASTER)
2014-09-02 18:07:02 +05:30
end
it "should return a 404 error if user id not found" do
get api("/projects/#{project.id}/members/1234", user)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(404)
2014-09-02 18:07:02 +05:30
end
end
describe "POST /projects/:id/members" do
it "should add user to project team" do
2015-09-11 14:41:01 +05:30
expect do
post api("/projects/#{project.id}/members", user), user_id: user2.id, access_level: ProjectMember::DEVELOPER
end.to change { ProjectMember.count }.by(1)
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(201)
expect(json_response['username']).to eq(user2.username)
expect(json_response['access_level']).to eq(ProjectMember::DEVELOPER)
2014-09-02 18:07:02 +05:30
end
it "should return a 201 status if user is already project member" do
2015-09-11 14:41:01 +05:30
post api("/projects/#{project.id}/members", user),
user_id: user2.id,
access_level: ProjectMember::DEVELOPER
expect do
post api("/projects/#{project.id}/members", user), user_id: user2.id, access_level: ProjectMember::DEVELOPER
end.not_to change { ProjectMember.count }
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(201)
expect(json_response['username']).to eq(user2.username)
expect(json_response['access_level']).to eq(ProjectMember::DEVELOPER)
2014-09-02 18:07:02 +05:30
end
it "should return a 400 error when user id is not given" do
2015-04-26 12:48:37 +05:30
post api("/projects/#{project.id}/members", user), access_level: ProjectMember::MASTER
expect(response.status).to eq(400)
2014-09-02 18:07:02 +05:30
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
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(400)
2014-09-02 18:07:02 +05:30
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
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(422)
2014-09-02 18:07:02 +05:30
end
end
describe "PUT /projects/:id/members/:user_id" do
2015-04-26 12:48:37 +05:30
before { project_member2 }
2014-09-02 18:07:02 +05:30
it "should update project team member" do
2015-04-26 12:48:37 +05:30
put api("/projects/#{project.id}/members/#{user3.id}", user), access_level: ProjectMember::MASTER
expect(response.status).to eq(200)
expect(json_response['username']).to eq(user3.username)
expect(json_response['access_level']).to eq(ProjectMember::MASTER)
2014-09-02 18:07:02 +05:30
end
it "should return a 404 error if user_id is not found" do
2015-04-26 12:48:37 +05:30
put api("/projects/#{project.id}/members/1234", user), access_level: ProjectMember::MASTER
expect(response.status).to eq(404)
2014-09-02 18:07:02 +05:30
end
it "should return a 400 error when access level is not given" do
put api("/projects/#{project.id}/members/#{user3.id}", user)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(400)
2014-09-02 18:07:02 +05:30
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
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(422)
2014-09-02 18:07:02 +05:30
end
end
describe "DELETE /projects/:id/members/:user_id" do
2015-04-26 12:48:37 +05:30
before { project_member }
before { project_member2 }
2014-09-02 18:07:02 +05:30
it "should remove user from project team" do
2015-09-11 14:41:01 +05:30
expect do
2014-09-02 18:07:02 +05:30
delete api("/projects/#{project.id}/members/#{user3.id}", user)
2015-09-11 14:41:01 +05:30
end.to change { ProjectMember.count }.by(-1)
2014-09-02 18:07:02 +05:30
end
it "should return 200 if team member is not part of a project" do
delete api("/projects/#{project.id}/members/#{user3.id}", user)
2015-09-11 14:41:01 +05:30
expect do
2014-09-02 18:07:02 +05:30
delete api("/projects/#{project.id}/members/#{user3.id}", user)
2015-09-11 14:41:01 +05:30
end.to_not change { ProjectMember.count }
2014-09-02 18:07:02 +05:30
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)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(200)
2014-09-02 18:07:02 +05:30
end
it "should return 200 OK when the user was not member" do
2015-09-11 14:41:01 +05:30
expect do
2014-09-02 18:07:02 +05:30
delete api("/projects/#{project.id}/members/1000000", user)
2015-09-11 14:41:01 +05:30
end.to change { ProjectMember.count }.by(0)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(200)
expect(json_response['message']).to eq("Access revoked")
expect(json_response['id']).to eq(1000000)
2014-09-02 18:07:02 +05:30
end
end
end