scinote-web/spec/requests/api/v1/users_controller_spec.rb

77 lines
2.7 KiB
Ruby
Raw Normal View History

2018-08-21 19:56:14 +08:00
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::V1::UsersController', type: :request do
before :all do
@user1 = create(:user)
@user2 = create(:user)
@user3 = create(:user)
2018-08-21 19:56:14 +08:00
@team1 = create(:team, created_by: @user1)
@team2 = create(:team, created_by: @user2)
@team3 = create(:team, created_by: @user3)
@owner_role = UserRole.find_by(name: I18n.t('user_roles.predefined.owner'))
create_user_assignment(@team1, @owner_role, @user1)
create_user_assignment(@team1, @owner_role, @user2)
create_user_assignment(@team2, @owner_role, @user2)
create_user_assignment(@team3, @owner_role, @user3)
2018-08-21 19:56:14 +08:00
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user1.id) }
end
describe 'GET users, #index' do
it 'When valid request, requested users are members of the team' do
hash_body = nil
get api_v1_team_users_path(team_id: @team1.id), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
pp hash_body[:data]
expect(hash_body[:data]).to match(
JSON.parse(
ActiveModelSerializers::SerializableResource
.new(@team1.users, each_serializer: Api::V1::UserSerializer)
.to_json
)['data']
)
end
it 'When invalid request, non existing team' do
hash_body = nil
get api_v1_team_users_path(team_id: -1), headers: @valid_headers
expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception
expect(hash_body['errors'][0]).to include('status': 404)
end
end
2018-08-21 19:56:14 +08:00
describe 'GET user, #show' do
it 'When valid request, requested user is member of the same teams' do
hash_body = nil
get api_v1_user_path(id: @user2.id), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
JSON.parse(
ActiveModelSerializers::SerializableResource
.new(@user2, serializer: Api::V1::UserSerializer)
.to_json
)['data']
2018-08-21 19:56:14 +08:00
)
end
it 'When invalid request, requested user is not member of the same teams' do
2018-08-21 19:56:14 +08:00
hash_body = nil
get api_v1_user_path(id: @user3.id), headers: @valid_headers
expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception
expect(hash_body['errors'][0]).to include('status': 403)
2018-08-21 19:56:14 +08:00
end
it 'When invalid request, non existing user' do
hash_body = nil
get api_v1_user_path(id: -1), headers: @valid_headers
2018-08-21 19:56:14 +08:00
expect(response).to have_http_status(403)
expect { hash_body = json }.not_to raise_exception
expect(hash_body['errors'][0]).to include('status': 403)
2018-08-21 19:56:14 +08:00
end
end
end