2021-01-17 18:26:56 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Team Members API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
2022-09-02 17:39:29 +00:00
|
|
|
let(:account_2) { create(:account) }
|
2021-01-17 18:26:56 +00:00
|
|
|
let!(:team) { create(:team, account: account) }
|
|
|
|
|
|
|
|
describe 'GET /api/v1/accounts/{account.id}/teams/{team_id}/team_members' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'returns all the teams' do
|
|
|
|
create(:team_member, team: team, user: agent)
|
|
|
|
get "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(JSON.parse(response.body).first['id']).to eq(agent.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/accounts/{account.id}/teams/{team_id}/team_members' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'returns unathorized for agent' do
|
|
|
|
params = { user_id: agent.id }
|
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
2021-01-29 07:04:52 +00:00
|
|
|
it 'add a new team members when its administrator' do
|
|
|
|
user_ids = (1..5).map { create(:user, account: account, role: :agent).id }
|
|
|
|
params = { user_ids: user_ids }
|
2021-12-21 17:18:01 +00:00
|
|
|
# have a team member added already
|
|
|
|
create(:team_member, team: team, user: User.find(user_ids.first))
|
2021-01-17 18:26:56 +00:00
|
|
|
|
|
|
|
post "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
params: params,
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2021-12-21 17:18:01 +00:00
|
|
|
expect(json_response.count).to eq(user_ids.count - 1)
|
2021-01-17 18:26:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/teams/{team_id}/team_members' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'return unauthorized for agent' do
|
|
|
|
params = { user_id: agent.id }
|
|
|
|
delete "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
2021-01-29 07:04:52 +00:00
|
|
|
it 'destroys the team members when its administrator' do
|
|
|
|
user_ids = (1..5).map { create(:user, account: account, role: :agent).id }
|
2021-12-21 17:18:01 +00:00
|
|
|
user_ids.each { |id| create(:team_member, team: team, user: User.find(id)) }
|
|
|
|
params = { user_ids: user_ids.first(3) }
|
2021-01-29 07:04:52 +00:00
|
|
|
|
2021-12-21 17:18:01 +00:00
|
|
|
delete "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
2021-01-17 18:26:56 +00:00
|
|
|
params: params,
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-12-21 17:18:01 +00:00
|
|
|
expect(team.team_members.count).to eq(2)
|
2021-01-17 18:26:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-09 13:51:31 +00:00
|
|
|
|
|
|
|
describe 'PATCH /api/v1/accounts/{account.id}/teams/{team_id}/team_members' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
patch "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
2022-09-02 17:39:29 +00:00
|
|
|
let(:agent_2) { create(:user, account: account_2, role: :agent) }
|
2021-02-09 13:51:31 +00:00
|
|
|
let(:administrator) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'return unauthorized for agent' do
|
|
|
|
params = { user_id: agent.id }
|
|
|
|
patch "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
params: params,
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the team members when its administrator' do
|
|
|
|
user_ids = (1..5).map { create(:user, account: account, role: :agent).id }
|
|
|
|
params = { user_ids: user_ids }
|
|
|
|
|
|
|
|
patch "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
params: params,
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response.count).to eq(user_ids.count)
|
|
|
|
end
|
2022-09-02 17:39:29 +00:00
|
|
|
|
|
|
|
it 'ignores the user ids when its not a valid account user id' do
|
|
|
|
params = { user_ids: [agent_2.id] }
|
|
|
|
|
|
|
|
patch "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
|
|
|
|
params: params,
|
|
|
|
headers: administrator.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['error']).to eq('Invalid User IDs')
|
|
|
|
end
|
2021-02-09 13:51:31 +00:00
|
|
|
end
|
|
|
|
end
|
2021-01-17 18:26:56 +00:00
|
|
|
end
|