2019-07-25 01:24:42 -04:00
# frozen_string_literal: true
2017-12-24 07:03:38 -05:00
require 'spec_helper'
2020-09-17 23:09:28 -04:00
RSpec . describe 'Group or Project invitations' , :aggregate_failures do
2021-04-29 17:10:03 -04:00
let_it_be ( :owner ) { create ( :user , name : 'John Doe' ) }
let_it_be ( :group ) { create ( :group , name : 'Owned' ) }
let_it_be ( :project ) { create ( :project , :repository , namespace : group ) }
2020-07-07 08:09:16 -04:00
let ( :user ) { create ( :user , email : 'user@example.com' ) }
2018-05-17 05:19:47 -04:00
let ( :group_invite ) { group . group_members . invite . last }
2017-12-24 07:03:38 -05:00
before do
2020-11-16 10:09:23 -05:00
stub_application_setting ( require_admin_approval_after_user_signup : false )
2018-07-11 10:36:08 -04:00
project . add_maintainer ( owner )
2020-07-07 08:09:16 -04:00
group . add_owner ( owner )
2017-12-24 07:03:38 -05:00
group . add_developer ( 'user@example.com' , owner )
2018-05-17 05:19:47 -04:00
group_invite . generate_invite_token!
end
2019-08-01 11:26:54 -04:00
def confirm_email ( new_user )
2018-05-17 05:19:47 -04:00
new_user_token = User . find_by_email ( new_user . email ) . confirmation_token
visit user_confirmation_path ( confirmation_token : new_user_token )
end
def fill_in_sign_up_form ( new_user )
2020-10-07 17:08:21 -04:00
fill_in 'new_user_first_name' , with : new_user . first_name
fill_in 'new_user_last_name' , with : new_user . last_name
2020-07-07 08:09:16 -04:00
fill_in 'new_user_username' , with : new_user . username
fill_in 'new_user_email' , with : new_user . email
fill_in 'new_user_password' , with : new_user . password
click_button 'Register'
2018-05-17 05:19:47 -04:00
end
def fill_in_sign_in_form ( user )
fill_in 'user_login' , with : user . email
fill_in 'user_password' , with : user . password
check 'user_remember_me'
click_button 'Sign in'
2017-12-24 07:03:38 -05:00
end
2020-09-10 11:09:10 -04:00
def fill_in_welcome_form
select 'Software Developer' , from : 'user_role'
click_button 'Get started!'
end
2017-12-24 07:03:38 -05:00
context 'when signed out' do
before do
2018-05-17 05:19:47 -04:00
visit invite_path ( group_invite . raw_invite_token )
2017-12-24 07:03:38 -05:00
end
it 'renders sign in page with sign in notice' do
2021-03-30 14:10:47 -04:00
expect ( current_path ) . to eq ( new_user_registration_path )
expect ( page ) . to have_content ( 'To accept this invitation, create an account or sign in' )
2017-12-24 07:03:38 -05:00
end
2020-08-10 14:09:54 -04:00
it 'pre-fills the "Username or email" field on the sign in box with the invite_email from the invite' do
2021-03-30 14:10:47 -04:00
click_link 'Sign in'
2020-08-10 14:09:54 -04:00
expect ( find_field ( 'Username or email' ) . value ) . to eq ( group_invite . invite_email )
end
it 'pre-fills the Email field on the sign up box with the invite_email from the invite' do
expect ( find_field ( 'Email' ) . value ) . to eq ( group_invite . invite_email )
end
2021-04-22 05:09:45 -04:00
it 'sign in, grants access and redirects to group activity page' do
2021-03-30 14:10:47 -04:00
click_link 'Sign in'
2018-05-17 05:19:47 -04:00
fill_in_sign_in_form ( user )
2017-12-24 07:03:38 -05:00
2021-04-22 05:09:45 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2017-12-24 07:03:38 -05:00
end
end
2020-07-07 08:09:16 -04:00
context 'when signed in as an existing member' do
2017-12-24 07:03:38 -05:00
before do
sign_in ( owner )
end
it 'shows message user already a member' do
2018-05-17 05:19:47 -04:00
visit invite_path ( group_invite . raw_invite_token )
2020-07-28 20:09:37 -04:00
expect ( page ) . to have_link ( owner . name , href : user_url ( owner ) )
2017-12-24 07:03:38 -05:00
expect ( page ) . to have_content ( 'However, you are already a member of this group.' )
end
end
2021-03-30 14:10:47 -04:00
context 'when inviting an unregistered user' do
2018-05-17 05:19:47 -04:00
let ( :new_user ) { build_stubbed ( :user ) }
let ( :invite_email ) { new_user . email }
2020-09-30 08:09:53 -04:00
let ( :group_invite ) { create ( :group_member , :invited , group : group , invite_email : invite_email , created_by : owner ) }
2021-04-29 17:10:03 -04:00
let ( :send_email_confirmation ) { true }
before do
stub_application_setting ( send_user_confirmation_email : send_email_confirmation )
end
2018-05-17 05:19:47 -04:00
2021-03-30 14:10:47 -04:00
context 'when registering using invitation email' do
2020-07-07 08:09:16 -04:00
before do
2021-04-29 17:10:03 -04:00
visit invite_path ( group_invite . raw_invite_token , invite_type : Members :: InviteEmailExperiment :: INVITE_TYPE )
2018-05-17 05:19:47 -04:00
end
2021-03-30 14:10:47 -04:00
context 'with admin approval required enabled' do
2020-11-16 10:09:23 -05:00
before do
stub_application_setting ( require_admin_approval_after_user_signup : true )
end
it 'does not sign the user in' do
fill_in_sign_up_form ( new_user )
expect ( current_path ) . to eq ( new_user_session_path )
expect ( page ) . to have_content ( 'You have signed up successfully. However, we could not sign you in because your account is awaiting approval from your GitLab administrator' )
end
end
2020-07-07 08:09:16 -04:00
context 'email confirmation disabled' do
let ( :send_email_confirmation ) { false }
2018-05-17 05:19:47 -04:00
2021-04-22 05:09:45 -04:00
it 'signs up and redirects to the most recent membership activity page with all the projects/groups invitations automatically accepted' do
2018-05-17 05:19:47 -04:00
fill_in_sign_up_form ( new_user )
2020-09-10 11:09:10 -04:00
fill_in_welcome_form
2018-05-17 05:19:47 -04:00
2021-04-22 05:09:45 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
expect ( page ) . to have_content ( 'You have been granted Owner access to group Owned.' )
2020-03-11 20:09:34 -04:00
end
2020-07-07 08:09:16 -04:00
context 'the user sign-up using a different email address' do
let ( :invite_email ) { build_stubbed ( :user ) . email }
2019-08-01 11:26:54 -04:00
2021-04-22 05:09:45 -04:00
it 'signs up and redirects to the activity page' do
2020-07-07 08:09:16 -04:00
fill_in_sign_up_form ( new_user )
2020-09-10 11:09:10 -04:00
fill_in_welcome_form
2020-03-11 20:09:34 -04:00
2021-04-22 05:09:45 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2020-07-07 08:09:16 -04:00
end
2019-08-01 11:26:54 -04:00
end
end
2020-07-07 08:09:16 -04:00
context 'email confirmation enabled' do
2021-04-29 17:10:03 -04:00
context 'with members/invite_email experiment' , :experiment do
it 'tracks the accepted invite' do
expect ( experiment ( 'members/invite_email' ) ) . to track ( :accepted )
. with_context ( actor : group_invite )
. on_next_instance
fill_in_sign_up_form ( new_user )
end
end
2018-05-17 05:19:47 -04:00
2020-03-11 20:09:34 -04:00
context 'when soft email confirmation is not enabled' do
before do
allow ( User ) . to receive ( :allow_unconfirmed_access_for ) . and_return 0
end
2021-04-27 23:09:58 -04:00
it 'signs up and redirects to the group activity page with all the project/groups invitation automatically accepted' do
2020-03-11 20:09:34 -04:00
fill_in_sign_up_form ( new_user )
2020-09-10 11:09:10 -04:00
fill_in_welcome_form
2020-03-11 20:09:34 -04:00
2021-04-27 23:09:58 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2020-03-11 20:09:34 -04:00
end
2019-08-01 11:26:54 -04:00
end
2020-03-11 20:09:34 -04:00
context 'when soft email confirmation is enabled' do
before do
allow ( User ) . to receive ( :allow_unconfirmed_access_for ) . and_return 2 . days
end
2019-08-01 11:26:54 -04:00
2021-04-27 23:09:58 -04:00
it 'signs up and redirects to to the group activity page with all the project/groups invitation automatically accepted' do
2020-03-11 20:09:34 -04:00
fill_in_sign_up_form ( new_user )
2020-09-10 11:09:10 -04:00
fill_in_welcome_form
2020-07-07 08:09:16 -04:00
2021-04-27 23:09:58 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2020-07-07 08:09:16 -04:00
end
end
context 'the user sign-up using a different email address' do
let ( :invite_email ) { build_stubbed ( :user ) . email }
context 'when soft email confirmation is not enabled' do
before do
stub_feature_flags ( soft_email_confirmation : false )
allow ( User ) . to receive ( :allow_unconfirmed_access_for ) . and_return 0
end
2021-04-27 23:09:58 -04:00
it 'signs up and redirects to the group activity page' do
2020-07-07 08:09:16 -04:00
fill_in_sign_up_form ( new_user )
confirm_email ( new_user )
fill_in_sign_in_form ( new_user )
2020-09-10 11:09:10 -04:00
fill_in_welcome_form
2020-07-07 08:09:16 -04:00
2021-04-22 05:09:45 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2020-07-07 08:09:16 -04:00
end
end
context 'when soft email confirmation is enabled' do
before do
stub_feature_flags ( soft_email_confirmation : true )
allow ( User ) . to receive ( :allow_unconfirmed_access_for ) . and_return 2 . days
end
2021-04-27 23:09:58 -04:00
it 'signs up and redirects to the group activity page' do
2020-07-07 08:09:16 -04:00
fill_in_sign_up_form ( new_user )
2020-09-10 11:09:10 -04:00
fill_in_welcome_form
2020-07-07 08:09:16 -04:00
2021-04-22 05:09:45 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2020-07-07 08:09:16 -04:00
end
2020-03-11 20:09:34 -04:00
end
2018-05-17 05:19:47 -04:00
end
end
end
2020-07-07 08:09:16 -04:00
context 'when declining the invitation' do
2020-09-30 08:09:53 -04:00
context 'as an existing user' do
let ( :group_invite ) { create ( :group_member , user : user , group : group , created_by : owner ) }
context 'when signed in' do
before do
sign_in ( user )
visit decline_invite_path ( group_invite . raw_invite_token )
end
it 'declines application and redirects to dashboard' do
expect ( current_path ) . to eq ( dashboard_projects_path )
expect ( page ) . to have_content ( 'You have declined the invitation to join group Owned.' )
expect { group_invite . reload } . to raise_error ActiveRecord :: RecordNotFound
end
2020-07-07 08:09:16 -04:00
end
2020-09-30 08:09:53 -04:00
context 'when signed out' do
before do
visit decline_invite_path ( group_invite . raw_invite_token )
end
2020-07-28 20:09:37 -04:00
2020-09-30 08:09:53 -04:00
it 'declines application and redirects to sign in page' do
expect ( current_path ) . to eq ( new_user_session_path )
expect ( page ) . to have_content ( 'You have declined the invitation to join group Owned.' )
expect { group_invite . reload } . to raise_error ActiveRecord :: RecordNotFound
end
2020-07-07 08:09:16 -04:00
end
end
2020-09-30 08:09:53 -04:00
context 'as a non-existing user' do
2020-07-07 08:09:16 -04:00
before do
visit decline_invite_path ( group_invite . raw_invite_token )
end
2020-09-30 08:09:53 -04:00
it 'declines application and shows a decline page' do
expect ( current_path ) . to eq ( decline_invite_path ( group_invite . raw_invite_token ) )
expect ( page ) . to have_content ( 'You successfully declined the invitation' )
2020-07-28 20:09:37 -04:00
expect { group_invite . reload } . to raise_error ActiveRecord :: RecordNotFound
2020-07-07 08:09:16 -04:00
end
end
end
2021-04-22 05:09:45 -04:00
context 'when accepting the invitation as an existing user' do
2020-07-07 08:09:16 -04:00
before do
sign_in ( user )
visit invite_path ( group_invite . raw_invite_token )
end
2021-04-27 23:09:58 -04:00
it 'grants access and redirects to the group activity page' do
2020-07-28 20:09:37 -04:00
expect ( group . users . include? ( user ) ) . to be false
2020-07-07 08:09:16 -04:00
page . click_link 'Accept invitation'
2020-07-28 20:09:37 -04:00
2021-04-22 05:09:45 -04:00
expect ( current_path ) . to eq ( activity_group_path ( group ) )
2020-07-07 08:09:16 -04:00
expect ( page ) . to have_content ( 'You have been granted Owner access to group Owned.' )
2020-07-28 20:09:37 -04:00
expect ( group . users . include? ( user ) ) . to be true
2020-07-07 08:09:16 -04:00
end
end
2018-05-17 05:19:47 -04:00
end
2017-12-24 07:03:38 -05:00
end