mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-05 14:41:53 +08:00
Merge pull request #802 from mlorb/ml-sci-1619
Write unit tests for invite users [SCI-1619]
This commit is contained in:
commit
1af27d9361
8 changed files with 194 additions and 10 deletions
1
Gemfile
1
Gemfile
|
@ -90,6 +90,7 @@ group :development, :test do
|
||||||
gem 'pry-byebug'
|
gem 'pry-byebug'
|
||||||
gem 'pry-rails'
|
gem 'pry-rails'
|
||||||
gem 'factory_girl_rails'
|
gem 'factory_girl_rails'
|
||||||
|
gem 'rails-controller-testing'
|
||||||
gem 'rspec-rails'
|
gem 'rspec-rails'
|
||||||
gem 'better_errors'
|
gem 'better_errors'
|
||||||
gem 'binding_of_caller'
|
gem 'binding_of_caller'
|
||||||
|
|
|
@ -318,6 +318,10 @@ GEM
|
||||||
bundler (>= 1.3.0, < 2.0)
|
bundler (>= 1.3.0, < 2.0)
|
||||||
railties (= 5.1.1)
|
railties (= 5.1.1)
|
||||||
sprockets-rails (>= 2.0.0)
|
sprockets-rails (>= 2.0.0)
|
||||||
|
rails-controller-testing (1.0.2)
|
||||||
|
actionpack (~> 5.x, >= 5.0.1)
|
||||||
|
actionview (~> 5.x, >= 5.0.1)
|
||||||
|
activesupport (~> 5.x)
|
||||||
rails-dom-testing (2.0.3)
|
rails-dom-testing (2.0.3)
|
||||||
activesupport (>= 4.2.0)
|
activesupport (>= 4.2.0)
|
||||||
nokogiri (>= 1.6)
|
nokogiri (>= 1.6)
|
||||||
|
@ -525,6 +529,7 @@ DEPENDENCIES
|
||||||
pry-rails
|
pry-rails
|
||||||
puma
|
puma
|
||||||
rails (= 5.1.1)
|
rails (= 5.1.1)
|
||||||
|
rails-controller-testing
|
||||||
rails_12factor
|
rails_12factor
|
||||||
rails_autolink (~> 1.1, >= 1.1.6)
|
rails_autolink (~> 1.1, >= 1.1.6)
|
||||||
recaptcha
|
recaptcha
|
||||||
|
|
|
@ -13,6 +13,8 @@ module ClientApi
|
||||||
success_response(invite_results)
|
success_response(invite_results)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def success_response(invite_results)
|
def success_response(invite_results)
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.json do
|
format.json do
|
||||||
|
@ -23,8 +25,6 @@ module ClientApi
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def check_invite_users_permission
|
def check_invite_users_permission
|
||||||
@team = Team.find_by_id(params[:team_id])
|
@team = Team.find_by_id(params[:team_id])
|
||||||
if @team && !is_admin_of_team(@team)
|
if @team && !is_admin_of_team(@team)
|
||||||
|
|
|
@ -5,15 +5,15 @@ module ClientApi
|
||||||
|
|
||||||
def initialize(args)
|
def initialize(args)
|
||||||
@user = args[:user]
|
@user = args[:user]
|
||||||
@emails = args[:emails].map(&:downcase)
|
@emails = args[:emails]
|
||||||
@team = args[:team]
|
@team = args[:team]
|
||||||
@role = args[:role]
|
@role = args[:role]
|
||||||
|
|
||||||
raise ClientApi::CustomInvitationsError unless @team && @role &&
|
unless @role && UserTeam.roles.keys.include?(@role) &&
|
||||||
@emails && @emails.present?
|
@emails && @emails.present?
|
||||||
if @role && !UserTeam.roles.keys.include?(@role)
|
|
||||||
raise ClientApi::CustomInvitationsError
|
raise ClientApi::CustomInvitationsError
|
||||||
end
|
end
|
||||||
|
@emails = @emails.map(&:downcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
def invitation
|
def invitation
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
"eslint-plugin-jsx-a11y": "^6.0.2",
|
"eslint-plugin-jsx-a11y": "^6.0.2",
|
||||||
"eslint-plugin-prettier": "^2.1.2",
|
"eslint-plugin-prettier": "^2.1.2",
|
||||||
"eslint-plugin-react": "^7.1.0",
|
"eslint-plugin-react": "^7.1.0",
|
||||||
"prettier": "^1.5.3",
|
"prettier": "^1.7.0",
|
||||||
"webpack-dev-server": "^2.5.1"
|
"webpack-dev-server": "^2.5.1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe ClientApi::Users::InvitationsController, type: :controller do
|
||||||
|
login_user
|
||||||
|
let(:user_one) { User.first }
|
||||||
|
let(:team_one) { create :team }
|
||||||
|
let(:emails_one) { Array.new(3) { Faker::Internet.email } }
|
||||||
|
|
||||||
|
describe '#invite_users' do
|
||||||
|
it 'returns HTTP success if users were invited' do
|
||||||
|
post :invite_users, params: { user_role: 'normal_user',
|
||||||
|
emails: emails_one },
|
||||||
|
format: :json
|
||||||
|
expect(response).to be_success
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(response).to render_template('client_api/users/invite_users')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns HTTP success if users were invited to team' do
|
||||||
|
create :user_team, team: team_one, user: user_one
|
||||||
|
post :invite_users, params: { team_id: team_one.id,
|
||||||
|
user_role: 'normal_user',
|
||||||
|
emails: emails_one },
|
||||||
|
format: :json
|
||||||
|
expect(response).to be_success
|
||||||
|
expect(response).to have_http_status(:ok)
|
||||||
|
expect(response).to render_template('client_api/users/invite_users')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns HTTP unprocessable_entity if users can\'t be invited to team' do
|
||||||
|
post :invite_users, params: { team_id: team_one.id,
|
||||||
|
user_role: 'normal_user',
|
||||||
|
emails: emails_one },
|
||||||
|
format: :json
|
||||||
|
expect(response).to_not be_success
|
||||||
|
expect(response).to have_http_status(:unprocessable_entity)
|
||||||
|
expect(response.content_type).to eq 'application/json'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
138
spec/services/client_api/invitations_service_spec.rb
Normal file
138
spec/services/client_api/invitations_service_spec.rb
Normal file
|
@ -0,0 +1,138 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe ClientApi::InvitationsService do
|
||||||
|
let(:team_one) { create :team }
|
||||||
|
let(:user_one) { create :user, email: Faker::Internet.email }
|
||||||
|
let(:emails_one) { Array.new(3) { Faker::Internet.email } }
|
||||||
|
|
||||||
|
it 'raises an ClientApi::CustomInvitationsError if ' \
|
||||||
|
'role is not assigned' do
|
||||||
|
expect {
|
||||||
|
ClientApi::InvitationsService.new(user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
emails: emails_one)
|
||||||
|
}.to raise_error(ClientApi::CustomInvitationsError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises an ClientApi::CustomInvitationsError if ' \
|
||||||
|
'emails are not assigned' do
|
||||||
|
expect {
|
||||||
|
ClientApi::InvitationsService.new(user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
role: 'normal_user')
|
||||||
|
}.to raise_error(ClientApi::CustomInvitationsError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises an ClientApi::CustomInvitationsError if ' \
|
||||||
|
'emails are not present' do
|
||||||
|
expect {
|
||||||
|
ClientApi::InvitationsService.new(user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: [])
|
||||||
|
}.to raise_error(ClientApi::CustomInvitationsError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises an ClientApi::CustomInvitationsError if ' \
|
||||||
|
'role is not included in UserTeam.roles' do
|
||||||
|
expect {
|
||||||
|
ClientApi::InvitationsService.new(user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
role: 'abnormal_user',
|
||||||
|
emails: emails_one)
|
||||||
|
}.to raise_error(ClientApi::CustomInvitationsError)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#invitation' do
|
||||||
|
it 'returns too many emails response if invite users limit is exceeded' do
|
||||||
|
emails_exceeded = Array.new(Constants::INVITE_USERS_LIMIT + 1) do
|
||||||
|
Faker::Internet.email
|
||||||
|
end
|
||||||
|
invitation_service = ClientApi::InvitationsService.new(
|
||||||
|
user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: emails_exceeded
|
||||||
|
)
|
||||||
|
result = invitation_service.invitation
|
||||||
|
expect(result.last[:status]).to eq :too_many_emails
|
||||||
|
expect(result.count).to eq Constants::INVITE_USERS_LIMIT + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when user is new' do
|
||||||
|
it 'returns invalid response if invited user is not valid' do
|
||||||
|
invitation_service = ClientApi::InvitationsService.new(
|
||||||
|
user: user_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: ['banana.man']
|
||||||
|
)
|
||||||
|
result_status = invitation_service.invitation.last[:status]
|
||||||
|
expect(result_status).to eq :user_invalid
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'invites new user' do
|
||||||
|
invitation_service = ClientApi::InvitationsService.new(
|
||||||
|
user: user_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: ['new@banana.net']
|
||||||
|
)
|
||||||
|
result = invitation_service.invitation
|
||||||
|
expect(result.last[:status]).to eq :user_created
|
||||||
|
# test functions result
|
||||||
|
expect(result.last[:user].email).to eq 'new@banana.net'
|
||||||
|
expect(result.last[:user].invited_by).to eq user_one
|
||||||
|
# test in database
|
||||||
|
expect(User.last.email).to eq 'new@banana.net'
|
||||||
|
expect(User.last.invited_by).to eq user_one
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates user-team relation and notification if team present' do
|
||||||
|
invitation_service = ClientApi::InvitationsService.new(
|
||||||
|
user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: ['new@banana.net']
|
||||||
|
)
|
||||||
|
result_status = invitation_service.invitation.last[:status]
|
||||||
|
expect(result_status).to eq :user_created_invited_to_team
|
||||||
|
expect(User.find_by_email('new@banana.net').teams).to include team_one
|
||||||
|
expect(Notification.last.users.last[:email]).to eq 'new@banana.net'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when user already exists' do
|
||||||
|
let(:user_two) { create :user, email: Faker::Internet.email }
|
||||||
|
let(:service_one) do
|
||||||
|
ClientApi::InvitationsService.new(user: user_one,
|
||||||
|
team: team_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: [user_two.email])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns information, that user already exists' do
|
||||||
|
invitation_service = ClientApi::InvitationsService.new(
|
||||||
|
user: user_one,
|
||||||
|
role: 'normal_user',
|
||||||
|
emails: [user_two.email]
|
||||||
|
)
|
||||||
|
result_status = invitation_service.invitation.last[:status]
|
||||||
|
expect(result_status).to eq :user_exists_unconfirmed
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns user exists in team response if team present ' \
|
||||||
|
'and user already part of the team' do
|
||||||
|
create :user_team, team: team_one, user: user_two
|
||||||
|
result_status = service_one.invitation.last[:status]
|
||||||
|
expect(result_status).to eq :user_exists_and_in_team_unconfirmed
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates user-team relation and notification if team present ' \
|
||||||
|
'and user not part of the team' do
|
||||||
|
result_status = service_one.invitation.last[:status]
|
||||||
|
expect(result_status).to eq :user_exists_invited_to_team_unconfirmed
|
||||||
|
expect(User.find_by_email(user_two.email).teams).to include team_one
|
||||||
|
expect(Notification.last.users.last[:email]).to eq user_two.email
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -4518,9 +4518,9 @@ preserve@^0.2.0:
|
||||||
version "0.2.0"
|
version "0.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
|
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
|
||||||
|
|
||||||
prettier@^1.5.3:
|
prettier@^1.7.0:
|
||||||
version "1.5.3"
|
version "1.7.0"
|
||||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.5.3.tgz#59dadc683345ec6b88f88b94ed4ae7e1da394bfe"
|
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.0.tgz#47481588f41f7c90f63938feb202ac82554e7150"
|
||||||
|
|
||||||
prettysize@^0.1.0:
|
prettysize@^0.1.0:
|
||||||
version "0.1.0"
|
version "0.1.0"
|
||||||
|
|
Loading…
Reference in a new issue