2019-09-30 08:06:01 -04:00
# frozen_string_literal: true
2019-08-29 03:56:52 -04:00
require 'spec_helper'
2017-03-23 04:43:34 -04:00
2020-06-24 05:08:32 -04:00
RSpec . describe UsersHelper do
2018-04-27 08:56:50 -04:00
include TermsHelper
2017-03-23 04:43:34 -04:00
let ( :user ) { create ( :user ) }
2020-01-13 19:08:14 -05:00
def filter_ee_badges ( badges )
badges . reject { | badge | badge [ :text ] == 'Is using seat' }
end
2022-02-02 13:15:35 -05:00
describe 'display_public_email?' do
let_it_be ( :user ) { create ( :user , :public_email ) }
subject { helper . display_public_email? ( user ) }
it { is_expected . to be true }
context 'when user public email is blank' do
let_it_be ( :user ) { create ( :user , public_email : '' ) }
it { is_expected . to be false }
end
end
2017-03-23 04:43:34 -04:00
describe '#user_link' do
subject { helper . user_link ( user ) }
it " links to the user's profile " do
is_expected . to include ( " href= \" #{ user_path ( user ) } \" " )
end
it " has the user's email as title " do
is_expected . to include ( " title= \" #{ user . email } \" " )
end
end
2017-12-11 09:21:06 -05:00
describe '#profile_tabs' do
subject ( :tabs ) { helper . profile_tabs }
before do
allow ( helper ) . to receive ( :current_user ) . and_return ( user )
allow ( helper ) . to receive ( :can? ) . and_return ( true )
end
2018-07-24 08:46:19 -04:00
context 'with public profile' do
it 'includes all the expected tabs' do
2019-02-05 06:49:58 -05:00
expect ( tabs ) . to include ( :activity , :groups , :contributed , :projects , :starred , :snippets )
2018-07-24 08:46:19 -04:00
end
end
context 'with private profile' do
before do
allow ( helper ) . to receive ( :can? ) . with ( user , :read_user_profile , nil ) . and_return ( false )
end
it 'is empty' do
expect ( tabs ) . to be_empty
end
2017-12-11 09:21:06 -05:00
end
end
2018-04-25 10:54:26 -04:00
2018-08-30 08:53:06 -04:00
describe '#user_internal_regex_data' do
using RSpec :: Parameterized :: TableSyntax
where ( :user_default_external , :user_default_internal_regex , :result ) do
false | nil | { user_internal_regex_pattern : nil , user_internal_regex_options : nil }
false | '' | { user_internal_regex_pattern : nil , user_internal_regex_options : nil }
false | 'mockRegexPattern' | { user_internal_regex_pattern : nil , user_internal_regex_options : nil }
true | nil | { user_internal_regex_pattern : nil , user_internal_regex_options : nil }
true | '' | { user_internal_regex_pattern : nil , user_internal_regex_options : nil }
2019-02-05 07:01:50 -05:00
true | 'mockRegexPattern' | { user_internal_regex_pattern : 'mockRegexPattern' , user_internal_regex_options : 'i' }
2018-08-30 08:53:06 -04:00
end
with_them do
before do
stub_application_setting ( user_default_external : user_default_external )
stub_application_setting ( user_default_internal_regex : user_default_internal_regex )
end
subject { helper . user_internal_regex_data }
it { is_expected . to eq ( result ) }
end
end
2018-04-25 10:54:26 -04:00
describe '#current_user_menu_items' do
subject ( :items ) { helper . current_user_menu_items }
before do
allow ( helper ) . to receive ( :current_user ) . and_return ( user )
allow ( helper ) . to receive ( :can? ) . and_return ( false )
end
2019-11-08 07:06:32 -05:00
after do
expect ( items ) . not_to include ( :start_trial )
end
2018-04-25 10:54:26 -04:00
it 'includes all default items' do
expect ( items ) . to include ( :help , :sign_out )
end
it 'includes the profile tab if the user can read themself' do
expect ( helper ) . to receive ( :can? ) . with ( user , :read_user , user ) { true }
expect ( items ) . to include ( :profile )
end
it 'includes the settings tab if the user can update themself' do
2020-04-06 08:10:44 -04:00
expect ( helper ) . to receive ( :can? ) . with ( user , :update_user , user ) { true }
2018-04-25 10:54:26 -04:00
2020-04-06 08:10:44 -04:00
expect ( items ) . to include ( :settings )
2018-04-25 10:54:26 -04:00
end
2018-04-27 08:56:50 -04:00
context 'when terms are enforced' do
before do
enforce_terms
end
it 'hides the profile and the settings tab' do
expect ( items ) . not_to include ( :settings , :profile , :help )
end
end
2018-04-25 10:54:26 -04:00
end
2019-01-07 06:54:41 -05:00
describe '#user_badges_in_admin_section' do
before do
allow ( helper ) . to receive ( :current_user ) . and_return ( user )
end
context 'with a blocked user' do
it " returns the blocked badge " do
blocked_user = create ( :user , state : 'blocked' )
badges = helper . user_badges_in_admin_section ( blocked_user )
2022-01-27 22:15:57 -05:00
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : " Blocked " , variant : " danger " ] )
2019-01-07 06:54:41 -05:00
end
end
2020-10-15 08:09:06 -04:00
context 'with a pending approval user' do
it 'returns the pending approval badge' do
blocked_pending_approval_user = create ( :user , :blocked_pending_approval )
badges = helper . user_badges_in_admin_section ( blocked_pending_approval_user )
2022-01-27 22:15:57 -05:00
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : 'Pending approval' , variant : 'info' ] )
2020-10-15 08:09:06 -04:00
end
end
2021-05-14 14:10:34 -04:00
context 'with a banned user' do
it 'returns the banned badge' do
banned_user = create ( :user , :banned )
badges = helper . user_badges_in_admin_section ( banned_user )
2022-01-27 22:15:57 -05:00
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : 'Banned' , variant : 'danger' ] )
2021-05-14 14:10:34 -04:00
end
end
2019-01-07 06:54:41 -05:00
context 'with an admin user' do
it " returns the admin badge " do
admin_user = create ( :admin )
badges = helper . user_badges_in_admin_section ( admin_user )
2022-01-27 22:15:57 -05:00
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : " Admin " , variant : " success " ] )
2019-01-07 06:54:41 -05:00
end
end
context 'with an external user' do
it 'returns the external badge' do
external_user = create ( :user , external : true )
badges = helper . user_badges_in_admin_section ( external_user )
2022-01-27 22:15:57 -05:00
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : " External " , variant : " secondary " ] )
2019-01-07 06:54:41 -05:00
end
end
context 'with the current user' do
it 'returns the "It\'s You" badge' do
badges = helper . user_badges_in_admin_section ( user )
2022-01-27 22:15:57 -05:00
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : " It's you! " , variant : " muted " ] )
2019-01-07 06:54:41 -05:00
end
end
context 'with an external blocked admin' do
it 'returns the blocked, admin and external badges' do
user = create ( :admin , state : 'blocked' , external : true )
badges = helper . user_badges_in_admin_section ( user )
2022-01-27 22:15:57 -05:00
expect ( badges ) . to match_array ( [
2019-01-07 06:54:41 -05:00
{ text : " Blocked " , variant : " danger " } ,
{ text : " Admin " , variant : " success " } ,
{ text : " External " , variant : " secondary " }
] )
end
end
2022-01-27 22:15:57 -05:00
context 'with a locked user' , time_travel_to : '2020-02-25 10:30:45 -0700' do
it 'returns the "Locked" badge' do
locked_user = create ( :user , locked_at : DateTime . parse ( '2020-02-25 10:30:00 -0700' ) )
badges = helper . user_badges_in_admin_section ( locked_user )
expect ( filter_ee_badges ( badges ) ) . to match_array ( [ text : " Locked " , variant : " warning " ] )
end
end
2019-01-07 06:54:41 -05:00
context 'get badges for normal user' do
it 'returns no badges' do
user = create ( :user )
badges = helper . user_badges_in_admin_section ( user )
2020-01-13 19:08:14 -05:00
expect ( filter_ee_badges ( badges ) ) . to be_empty
2019-01-07 06:54:41 -05:00
end
end
end
2020-03-16 08:09:12 -04:00
2020-10-15 08:09:06 -04:00
describe '#can_force_email_confirmation?' do
subject { helper . can_force_email_confirmation? ( user ) }
context 'for a user that is already confirmed' do
it { is_expected . to eq ( false ) }
end
context 'for a user that is not confirmed' do
let ( :user ) { create ( :user , :unconfirmed ) }
it { is_expected . to eq ( true ) }
end
end
2020-03-16 08:09:12 -04:00
describe '#work_information' do
2020-11-05 13:08:48 -05:00
let ( :with_schema_markup ) { false }
2020-03-16 08:09:12 -04:00
2020-11-05 13:08:48 -05:00
subject { helper . work_information ( user , with_schema_markup : with_schema_markup ) }
2020-03-16 08:09:12 -04:00
2020-11-05 13:08:48 -05:00
context 'when neither organization nor job_title are present' do
it { is_expected . to be_nil }
2020-03-16 08:09:12 -04:00
end
2020-11-05 13:08:48 -05:00
context 'when user parameter is nil' do
let ( :user ) { nil }
2020-03-16 08:09:12 -04:00
2020-11-05 13:08:48 -05:00
it { is_expected . to be_nil }
2020-03-16 08:09:12 -04:00
end
2020-11-05 13:08:48 -05:00
context 'without schema markup' do
context 'when both job_title and organization are present' do
let ( :user ) { build ( :user , organization : 'GitLab' , job_title : 'Frontend Engineer' ) }
2020-03-16 08:09:12 -04:00
2020-11-05 13:08:48 -05:00
it 'returns job title concatenated with organization' do
is_expected . to eq ( 'Frontend Engineer at GitLab' )
end
2020-03-16 08:09:12 -04:00
end
2020-11-05 13:08:48 -05:00
context 'when only organization is present' do
let ( :user ) { build ( :user , organization : 'GitLab' ) }
it " returns organization " do
is_expected . to eq ( 'GitLab' )
end
end
context 'when only job_title is present' do
let ( :user ) { build ( :user , job_title : 'Frontend Engineer' ) }
it 'returns job title' do
is_expected . to eq ( 'Frontend Engineer' )
end
end
2020-03-16 08:09:12 -04:00
end
2020-11-05 13:08:48 -05:00
context 'with schema markup' do
let ( :with_schema_markup ) { true }
2020-03-16 08:09:12 -04:00
2020-11-05 13:08:48 -05:00
context 'when both job_title and organization are present' do
let ( :user ) { build ( :user , organization : 'GitLab' , job_title : 'Frontend Engineer' ) }
it 'returns job title concatenated with organization' do
is_expected . to eq ( '<span itemprop="jobTitle">Frontend Engineer</span> at <span itemprop="worksFor">GitLab</span>' )
end
end
context 'when only organization is present' do
let ( :user ) { build ( :user , organization : 'GitLab' ) }
it " returns organization " do
is_expected . to eq ( '<span itemprop="worksFor">GitLab</span>' )
end
end
context 'when only job_title is present' do
let ( :user ) { build ( :user , job_title : 'Frontend Engineer' ) }
it 'returns job title' do
is_expected . to eq ( '<span itemprop="jobTitle">Frontend Engineer</span>' )
end
end
2020-03-16 08:09:12 -04:00
end
end
2020-11-26 07:09:48 -05:00
describe '#user_display_name' do
subject { helper . user_display_name ( user ) }
before do
stub_current_user ( nil )
end
context 'for a confirmed user' do
let ( :user ) { create ( :user ) }
before do
stub_profile_permission_allowed ( true )
end
it { is_expected . to eq ( user . name ) }
end
context 'for an unconfirmed user' do
let ( :user ) { create ( :user , :unconfirmed ) }
before do
stub_profile_permission_allowed ( false )
end
it { is_expected . to eq ( 'Unconfirmed user' ) }
context 'when current user is an admin' do
before do
admin_user = create ( :admin )
stub_current_user ( admin_user )
stub_profile_permission_allowed ( true , admin_user )
end
it { is_expected . to eq ( user . name ) }
end
context 'when the current user is self' do
before do
stub_current_user ( user )
stub_profile_permission_allowed ( true , user )
end
it { is_expected . to eq ( user . name ) }
end
end
context 'for a blocked user' do
let ( :user ) { create ( :user , :blocked ) }
it { is_expected . to eq ( 'Blocked user' ) }
end
def stub_current_user ( user )
allow ( helper ) . to receive ( :current_user ) . and_return ( user )
end
def stub_profile_permission_allowed ( allowed , current_user = nil )
2020-12-01 13:09:42 -05:00
allow ( helper ) . to receive ( :can? ) . with ( current_user , :read_user_profile , user ) . and_return ( allowed )
2020-11-26 07:09:48 -05:00
end
end
2020-12-09 07:09:42 -05:00
describe '#admin_users_data_attributes' do
subject ( :data ) { helper . admin_users_data_attributes ( [ user ] ) }
2021-01-11 22:10:47 -05:00
before do
allow ( helper ) . to receive ( :current_user ) . and_return ( user )
end
2020-12-09 07:09:42 -05:00
it 'users matches the serialized json' do
entity = double
expect_next_instance_of ( Admin :: UserSerializer ) do | instance |
2022-05-02 11:10:10 -04:00
expect ( instance ) . to receive ( :represent ) . with ( [ user ] , { current_user : user } ) . and_return ( entity )
2020-12-09 07:09:42 -05:00
end
expect ( entity ) . to receive ( :to_json ) . and_return ( " { \" username \" : \" admin \" } " )
expect ( data [ :users ] ) . to eq " { \" username \" : \" admin \" } "
end
it 'paths matches the schema' do
expect ( data [ :paths ] ) . to match_schema ( 'entities/admin_users_data_attributes_paths' )
end
end
2021-07-02 14:08:28 -04:00
describe '#confirm_user_data' do
confirm_admin_user_path = '/admin/users/root/confirm'
before do
allow ( helper ) . to receive ( :confirm_admin_user_path ) . with ( user ) . and_return ( confirm_admin_user_path )
end
subject ( :confirm_user_data ) { helper . confirm_user_data ( user ) }
it 'sets `path` key correctly' do
expect ( confirm_user_data [ :path ] ) . to eq ( confirm_admin_user_path )
end
it 'sets `modal_attributes` key to valid json' do
expect ( confirm_user_data [ :modal_attributes ] ) . to be_valid_json
end
context 'when `user.unconfirmed_email` is set' do
2021-11-03 20:12:36 -04:00
let ( :user ) { create ( :user , :unconfirmed , unconfirmed_email : 'foo@bar.com' ) }
2021-07-02 14:08:28 -04:00
it 'sets `modal_attributes.messageHtml` correctly' do
expect ( Gitlab :: Json . parse ( confirm_user_data [ :modal_attributes ] ) [ 'messageHtml' ] ) . to eq ( 'This user has an unconfirmed email address (foo@bar.com). You may force a confirmation.' )
end
end
context 'when `user.unconfirmed_email` is not set' do
it 'sets `modal_attributes.messageHtml` correctly' do
expect ( Gitlab :: Json . parse ( confirm_user_data [ :modal_attributes ] ) [ 'messageHtml' ] ) . to eq ( 'This user has an unconfirmed email address. You may force a confirmation.' )
end
end
end
2021-07-19 14:08:23 -04:00
describe '#admin_user_actions_data_attributes' do
subject ( :data ) { helper . admin_user_actions_data_attributes ( user ) }
before do
allow ( helper ) . to receive ( :current_user ) . and_return ( user )
allow ( Admin :: UserEntity ) . to receive ( :represent ) . and_call_original
end
it 'user matches the serialized json' do
expect ( data [ :user ] ) . to be_valid_json
expect ( Admin :: UserEntity ) . to have_received ( :represent ) . with ( user , hash_including ( { current_user : user } ) )
end
it 'paths matches the schema' do
expect ( data [ :paths ] ) . to match_schema ( 'entities/admin_users_data_attributes_paths' )
end
end
2017-03-23 04:43:34 -04:00
end