2019-03-28 06:24:08 -04:00
# frozen_string_literal: true
2015-03-12 17:51:41 -04:00
require 'spec_helper'
2020-06-24 05:08:32 -04:00
RSpec . describe GroupsHelper do
2017-06-20 17:35:55 -04:00
include ApplicationHelper
2017-10-02 07:35:01 -04:00
describe 'group_icon_url' do
it 'returns an url for the avatar' do
2018-06-06 17:26:00 -04:00
avatar_file_path = File . join ( 'spec' , 'fixtures' , 'banana_sample.gif' )
2017-10-02 07:35:01 -04:00
group = create ( :group )
group . avatar = fixture_file_upload ( avatar_file_path )
group . save!
expect ( group_icon_url ( group . path ) . to_s )
2017-11-09 10:40:41 -05:00
. to match ( group . avatar . url )
2015-03-12 17:51:41 -04:00
end
2016-07-25 14:16:19 -04:00
it 'gives default avatar_icon when no avatar is present' do
2015-03-12 17:51:41 -04:00
group = create ( :group )
2017-10-02 07:35:01 -04:00
expect ( group_icon_url ( group . path ) ) . to match_asset_path ( 'group_avatar.png' )
2015-03-12 17:51:41 -04:00
end
end
2016-09-06 12:48:00 -04:00
2021-03-12 07:09:33 -05:00
describe 'group_dependency_proxy_url' do
it 'converts uppercase letters to lowercase' do
group = create ( :group , path : 'GroupWithUPPERcaseLetters' )
expect ( group_dependency_proxy_url ( group ) ) . to end_with ( " /groupwithuppercaseletters #{ DependencyProxy :: URL_SUFFIX } " )
end
end
2016-09-06 12:48:00 -04:00
describe 'group_lfs_status' do
let ( :group ) { create ( :group ) }
2017-08-02 15:55:11 -04:00
let! ( :project ) { create ( :project , namespace_id : group . id ) }
2016-09-06 12:48:00 -04:00
before do
allow ( Gitlab . config . lfs ) . to receive ( :enabled ) . and_return ( true )
end
context 'only one project in group' do
before do
group . update_attribute ( :lfs_enabled , true )
end
it 'returns all projects as enabled' do
expect ( group_lfs_status ( group ) ) . to include ( 'Enabled for all projects' )
end
it 'returns all projects as disabled' do
project . update_attribute ( :lfs_enabled , false )
expect ( group_lfs_status ( group ) ) . to include ( 'Enabled for 0 out of 1 project' )
end
end
context 'more than one project in group' do
before do
2017-08-02 15:55:11 -04:00
create ( :project , namespace_id : group . id )
2016-09-06 12:48:00 -04:00
end
context 'LFS enabled in group' do
before do
group . update_attribute ( :lfs_enabled , true )
end
it 'returns both projects as enabled' do
expect ( group_lfs_status ( group ) ) . to include ( 'Enabled for all projects' )
end
it 'returns only one as enabled' do
project . update_attribute ( :lfs_enabled , false )
expect ( group_lfs_status ( group ) ) . to include ( 'Enabled for 1 out of 2 projects' )
end
end
context 'LFS disabled in group' do
before do
group . update_attribute ( :lfs_enabled , false )
end
it 'returns both projects as disabled' do
expect ( group_lfs_status ( group ) ) . to include ( 'Disabled for all projects' )
end
it 'returns only one as disabled' do
project . update_attribute ( :lfs_enabled , true )
expect ( group_lfs_status ( group ) ) . to include ( 'Disabled for 1 out of 2 projects' )
end
end
end
end
2017-06-20 17:35:55 -04:00
2019-07-24 05:20:54 -04:00
describe 'group_title' do
2020-11-11 19:09:44 -05:00
let_it_be ( :group ) { create ( :group ) }
let_it_be ( :nested_group ) { create ( :group , parent : group ) }
let_it_be ( :deep_nested_group ) { create ( :group , parent : nested_group ) }
let_it_be ( :very_deep_nested_group ) { create ( :group , parent : deep_nested_group ) }
subject { helper . group_title ( very_deep_nested_group ) }
2017-06-20 17:35:55 -04:00
it 'outputs the groups in the correct order' do
2020-11-11 19:09:44 -05:00
expect ( subject )
2018-01-27 00:35:53 -05:00
. to match ( %r{ <li style="text-indent: 16px;"><a.*> #{ deep_nested_group . name } .*</li>.*<a.*> #{ very_deep_nested_group . name } </a> }m )
2017-06-20 17:35:55 -04:00
end
2020-11-11 19:09:44 -05:00
it 'enqueues the elements in the breadcrumb schema list' do
expect ( helper ) . to receive ( :push_to_schema_breadcrumb ) . with ( group . name , group_path ( group ) )
expect ( helper ) . to receive ( :push_to_schema_breadcrumb ) . with ( nested_group . name , group_path ( nested_group ) )
expect ( helper ) . to receive ( :push_to_schema_breadcrumb ) . with ( deep_nested_group . name , group_path ( deep_nested_group ) )
expect ( helper ) . to receive ( :push_to_schema_breadcrumb ) . with ( very_deep_nested_group . name , group_path ( very_deep_nested_group ) )
subject
end
2017-06-20 17:35:55 -04:00
end
2017-09-05 20:02:11 -04:00
# rubocop:disable Layout/SpaceBeforeComma
2019-07-24 05:20:54 -04:00
describe '#share_with_group_lock_help_text' do
2017-09-05 20:02:11 -04:00
let! ( :root_group ) { create ( :group ) }
let! ( :subgroup ) { create ( :group , parent : root_group ) }
let! ( :sub_subgroup ) { create ( :group , parent : subgroup ) }
let ( :root_owner ) { create ( :user ) }
let ( :sub_owner ) { create ( :user ) }
let ( :sub_sub_owner ) { create ( :user ) }
let ( :possible_help_texts ) do
{
default_help : " This setting will be applied to all subgroups unless overridden by a group owner " ,
2018-01-27 00:35:53 -05:00
ancestor_locked_but_you_can_override : %r{ This setting is applied on <a .+>.+</a> \ . You can override the setting or .+ } ,
2017-09-06 14:31:45 -04:00
ancestor_locked_so_ask_the_owner : / This setting is applied on .+ \ . To share projects in this group with another group, ask the owner to override the setting or remove the share with group lock from .+ / ,
2017-09-06 02:49:57 -04:00
ancestor_locked_and_has_been_overridden : / This setting is applied on .+ and has been overridden on this subgroup /
2017-09-05 20:02:11 -04:00
}
end
2020-08-10 23:11:00 -04:00
2017-09-05 20:02:11 -04:00
let ( :possible_linked_ancestors ) do
{
root_group : root_group ,
subgroup : subgroup
}
end
2020-08-10 23:11:00 -04:00
2017-09-05 20:02:11 -04:00
let ( :users ) do
{
root_owner : root_owner ,
sub_owner : sub_owner ,
sub_sub_owner : sub_sub_owner
}
end
2019-12-17 13:07:48 -05:00
2017-09-05 20:02:11 -04:00
subject { helper . share_with_group_lock_help_text ( sub_subgroup ) }
2017-09-06 14:31:45 -04:00
where ( :root_share_with_group_locked , :subgroup_share_with_group_locked , :sub_subgroup_share_with_group_locked , :current_user , :help_text , :linked_ancestor ) do
2017-09-05 20:02:11 -04:00
[
[ false , false , false , :root_owner , :default_help , nil ] ,
[ false , false , false , :sub_owner , :default_help , nil ] ,
[ false , false , false , :sub_sub_owner , :default_help , nil ] ,
[ false , false , true , :root_owner , :default_help , nil ] ,
[ false , false , true , :sub_owner , :default_help , nil ] ,
[ false , false , true , :sub_sub_owner , :default_help , nil ] ,
[ false , true , false , :root_owner , :ancestor_locked_and_has_been_overridden , :subgroup ] ,
[ false , true , false , :sub_owner , :ancestor_locked_and_has_been_overridden , :subgroup ] ,
[ false , true , false , :sub_sub_owner , :ancestor_locked_and_has_been_overridden , :subgroup ] ,
[ false , true , true , :root_owner , :ancestor_locked_but_you_can_override , :subgroup ] ,
[ false , true , true , :sub_owner , :ancestor_locked_but_you_can_override , :subgroup ] ,
[ false , true , true , :sub_sub_owner , :ancestor_locked_so_ask_the_owner , :subgroup ] ,
[ true , false , false , :root_owner , :default_help , nil ] ,
[ true , false , false , :sub_owner , :default_help , nil ] ,
[ true , false , false , :sub_sub_owner , :default_help , nil ] ,
[ true , false , true , :root_owner , :default_help , nil ] ,
[ true , false , true , :sub_owner , :default_help , nil ] ,
[ true , false , true , :sub_sub_owner , :default_help , nil ] ,
[ true , true , false , :root_owner , :ancestor_locked_and_has_been_overridden , :root_group ] ,
[ true , true , false , :sub_owner , :ancestor_locked_and_has_been_overridden , :root_group ] ,
[ true , true , false , :sub_sub_owner , :ancestor_locked_and_has_been_overridden , :root_group ] ,
[ true , true , true , :root_owner , :ancestor_locked_but_you_can_override , :root_group ] ,
[ true , true , true , :sub_owner , :ancestor_locked_so_ask_the_owner , :root_group ] ,
[ true , true , true , :sub_sub_owner , :ancestor_locked_so_ask_the_owner , :root_group ]
]
end
with_them do
before do
root_group . add_owner ( root_owner )
subgroup . add_owner ( sub_owner )
sub_subgroup . add_owner ( sub_sub_owner )
2017-09-06 14:31:45 -04:00
root_group . update_column ( :share_with_group_lock , true ) if root_share_with_group_locked
subgroup . update_column ( :share_with_group_lock , true ) if subgroup_share_with_group_locked
sub_subgroup . update_column ( :share_with_group_lock , true ) if sub_subgroup_share_with_group_locked
2017-09-05 20:02:11 -04:00
allow ( helper ) . to receive ( :current_user ) . and_return ( users [ current_user ] )
allow ( helper ) . to receive ( :can? )
. with ( users [ current_user ] , :change_share_with_group_lock , subgroup )
. and_return ( Ability . allowed? ( users [ current_user ] , :change_share_with_group_lock , subgroup ) )
2017-09-06 02:49:57 -04:00
ancestor = possible_linked_ancestors [ linked_ancestor ]
if ancestor
allow ( helper ) . to receive ( :can? )
. with ( users [ current_user ] , :read_group , ancestor )
. and_return ( Ability . allowed? ( users [ current_user ] , :read_group , ancestor ) )
allow ( helper ) . to receive ( :can? )
. with ( users [ current_user ] , :admin_group , ancestor )
. and_return ( Ability . allowed? ( users [ current_user ] , :admin_group , ancestor ) )
end
2017-09-05 20:02:11 -04:00
end
it 'has the correct help text with correct ancestor links' do
expect ( subject ) . to match ( possible_help_texts [ help_text ] )
expect ( subject ) . to match ( possible_linked_ancestors [ linked_ancestor ] . name ) unless help_text == :default_help
end
end
end
2017-12-11 09:21:06 -05:00
2019-10-17 08:07:33 -04:00
describe '#group_container_registry_nav' do
let ( :group ) { create ( :group , :public ) }
let ( :user ) { create ( :user ) }
2019-12-17 13:07:48 -05:00
2019-10-17 08:07:33 -04:00
before do
stub_container_registry_config ( enabled : true )
allow ( helper ) . to receive ( :current_user ) { user }
allow ( helper ) . to receive ( :can? ) . with ( user , :read_container_image , group ) { true }
helper . instance_variable_set ( :@group , group )
end
subject { helper . group_container_registry_nav? }
context 'when container registry is enabled' do
it { is_expected . to be_truthy }
it 'is disabled for guest' do
allow ( helper ) . to receive ( :can? ) . with ( user , :read_container_image , group ) { false }
expect ( subject ) . to be false
end
end
context 'when container registry is not enabled' do
before do
stub_container_registry_config ( enabled : false )
end
it { is_expected . to be_falsy }
it 'is disabled for guests' do
allow ( helper ) . to receive ( :can? ) . with ( user , :read_container_image , group ) { false }
expect ( subject ) . to be false
end
end
end
2017-12-11 09:21:06 -05:00
describe '#group_sidebar_links' do
let ( :group ) { create ( :group , :public ) }
let ( :user ) { create ( :user ) }
2019-12-17 13:07:48 -05:00
2017-12-11 09:21:06 -05:00
before do
2018-07-03 08:08:46 -04:00
group . add_owner ( user )
2017-12-11 09:21:06 -05:00
allow ( helper ) . to receive ( :current_user ) { user }
2018-07-03 08:08:46 -04:00
allow ( helper ) . to receive ( :can? ) { | * args | Ability . allowed? ( * args ) }
2017-12-11 09:21:06 -05:00
helper . instance_variable_set ( :@group , group )
end
it 'returns all the expected links' do
links = [
:overview , :activity , :issues , :labels , :milestones , :merge_requests ,
:group_members , :settings
]
expect ( helper . group_sidebar_links ) . to include ( * links )
end
it 'includes settings when the user can admin the group' do
expect ( helper ) . to receive ( :current_user ) { user }
expect ( helper ) . to receive ( :can? ) . with ( user , :admin_group , group ) { false }
expect ( helper . group_sidebar_links ) . not_to include ( :settings )
end
it 'excludes cross project features when the user cannot read cross project' do
cross_project_features = [ :activity , :issues , :labels , :milestones ,
:merge_requests ]
2018-07-03 08:08:46 -04:00
allow ( Ability ) . to receive ( :allowed? ) . and_call_original
cross_project_features . each do | feature |
expect ( Ability ) . to receive ( :allowed? ) . with ( user , " read_group_ #{ feature } " . to_sym , group ) { false }
end
2017-12-11 09:21:06 -05:00
expect ( helper . group_sidebar_links ) . not_to include ( * cross_project_features )
end
end
2019-04-02 02:49:11 -04:00
2019-07-24 05:20:54 -04:00
describe 'parent_group_options' do
2019-04-02 02:49:11 -04:00
let ( :current_user ) { create ( :user ) }
let ( :group ) { create ( :group , name : 'group' ) }
let ( :group2 ) { create ( :group , name : 'group2' ) }
before do
group . add_owner ( current_user )
group2 . add_owner ( current_user )
end
it 'includes explicitly owned groups except self' do
expect ( parent_group_options ( group2 ) ) . to eq ( [ { id : group . id , text : group . human_name } ] . to_json )
end
it 'excludes parent group' do
subgroup = create ( :group , parent : group2 )
expect ( parent_group_options ( subgroup ) ) . to eq ( [ { id : group . id , text : group . human_name } ] . to_json )
end
it 'includes subgroups with inherited ownership' do
subgroup = create ( :group , parent : group )
expect ( parent_group_options ( group2 ) ) . to eq ( [ { id : group . id , text : group . human_name } , { id : subgroup . id , text : subgroup . human_name } ] . to_json )
end
it 'excludes own subgroups' do
create ( :group , parent : group2 )
expect ( parent_group_options ( group2 ) ) . to eq ( [ { id : group . id , text : group . human_name } ] . to_json )
end
end
2019-08-17 01:56:48 -04:00
describe '#can_disable_group_emails?' do
let ( :current_user ) { create ( :user ) }
let ( :group ) { create ( :group , name : 'group' ) }
let ( :subgroup ) { create ( :group , name : 'subgroup' , parent : group ) }
before do
allow ( helper ) . to receive ( :current_user ) { current_user }
end
it 'returns true for the group owner' do
allow ( helper ) . to receive ( :can? ) . with ( current_user , :set_emails_disabled , group ) { true }
expect ( helper . can_disable_group_emails? ( group ) ) . to be_truthy
end
it 'returns false for anyone else' do
allow ( helper ) . to receive ( :can? ) . with ( current_user , :set_emails_disabled , group ) { false }
expect ( helper . can_disable_group_emails? ( group ) ) . to be_falsey
end
context 'when subgroups' do
before do
allow ( helper ) . to receive ( :can? ) . with ( current_user , :set_emails_disabled , subgroup ) { true }
end
it 'returns false if parent group is disabling emails' do
allow ( group ) . to receive ( :emails_disabled? ) . and_return ( true )
expect ( helper . can_disable_group_emails? ( subgroup ) ) . to be_falsey
end
it 'returns true if parent group is not disabling emails' do
allow ( group ) . to receive ( :emails_disabled? ) . and_return ( false )
expect ( helper . can_disable_group_emails? ( subgroup ) ) . to be_truthy
end
end
end
2020-04-24 05:09:44 -04:00
describe '#can_update_default_branch_protection?' do
let ( :current_user ) { create ( :user ) }
let ( :group ) { create ( :group ) }
subject { helper . can_update_default_branch_protection? ( group ) }
before do
allow ( helper ) . to receive ( :current_user ) { current_user }
end
context 'for users who can update default branch protection of the group' do
before do
allow ( helper ) . to receive ( :can? ) . with ( current_user , :update_default_branch_protection , group ) { true }
end
it { is_expected . to be_truthy }
end
context 'for users who cannot update default branch protection of the group' do
before do
allow ( helper ) . to receive ( :can? ) . with ( current_user , :update_default_branch_protection , group ) { false }
end
it { is_expected . to be_falsey }
end
end
2020-09-03 23:08:22 -04:00
2020-10-28 11:08:49 -04:00
describe '#show_thanks_for_purchase_banner?' do
subject { helper . show_thanks_for_purchase_banner? }
it 'returns true with purchased_quantity present in params' do
allow ( controller ) . to receive ( :params ) { { purchased_quantity : '1' } }
is_expected . to be_truthy
end
it 'returns false with purchased_quantity not present in params' do
is_expected . to be_falsey
end
it 'returns false with purchased_quantity is empty in params' do
allow ( controller ) . to receive ( :params ) { { purchased_quantity : '' } }
is_expected . to be_falsey
end
end
2020-09-03 23:08:22 -04:00
describe '#show_invite_banner?' do
let_it_be ( :current_user ) { create ( :user ) }
let_it_be_with_refind ( :group ) { create ( :group ) }
let_it_be ( :users ) { [ current_user , create ( :user ) ] }
subject { helper . show_invite_banner? ( group ) }
before do
allow ( helper ) . to receive ( :current_user ) { current_user }
allow ( helper ) . to receive ( :can? ) . with ( current_user , :admin_group , group ) . and_return ( can_admin_group )
stub_feature_flags ( invite_your_teammates_banner_a : feature_enabled_flag )
users . take ( group_members_count ) . each { | user | group . add_guest ( user ) }
end
using RSpec :: Parameterized :: TableSyntax
where ( :feature_enabled_flag , :can_admin_group , :group_members_count , :expected_result ) do
true | true | 1 | true
true | false | 1 | false
false | true | 1 | false
false | false | 1 | false
true | true | 2 | false
true | false | 2 | false
false | true | 2 | false
false | false | 2 | false
end
with_them do
context 'when the group was just created' do
before do
flash [ :notice ] = " Group #{ group . name } was successfully created "
end
it { is_expected . to be_falsey }
end
context 'when no flash message' do
it 'returns the expected result' do
expect ( subject ) . to eq ( expected_result )
end
end
end
end
2021-01-20 19:11:07 -05:00
describe '#group_open_issues_count' do
let_it_be ( :current_user ) { create ( :user ) }
let_it_be ( :group ) { create ( :group , :public ) }
let_it_be ( :count_service ) { Groups :: OpenIssuesCountService }
before do
allow ( helper ) . to receive ( :current_user ) { current_user }
end
2021-03-15 14:09:05 -04:00
it 'returns count value from cache' do
allow_next_instance_of ( count_service ) do | service |
allow ( service ) . to receive ( :count ) . and_return ( 2500 )
2021-01-20 19:11:07 -05:00
end
2021-03-15 14:09:05 -04:00
expect ( helper . group_open_issues_count ( group ) ) . to eq ( '2.5k' )
2021-01-20 19:11:07 -05:00
end
context 'when cached_sidebar_open_issues_count feature flag is disabled' do
before do
stub_feature_flags ( cached_sidebar_open_issues_count : false )
end
it 'returns not cached issues count' do
allow ( helper ) . to receive ( :group_issues_count ) . and_return ( 2500 )
expect ( helper . group_open_issues_count ( group ) ) . to eq ( '2,500' )
end
end
end
describe '#cached_open_group_issues_count' do
let_it_be ( :current_user ) { create ( :user ) }
let_it_be ( :group ) { create ( :group , name : 'group' ) }
let_it_be ( :count_service ) { Groups :: OpenIssuesCountService }
before do
allow ( helper ) . to receive ( :current_user ) { current_user }
end
it 'returns all digits for count value under 1000' do
allow_next_instance_of ( count_service ) do | service |
allow ( service ) . to receive ( :count ) . and_return ( 999 )
end
expect ( helper . cached_open_group_issues_count ( group ) ) . to eq ( '999' )
end
it 'returns truncated digits for count value over 1000' do
allow_next_instance_of ( count_service ) do | service |
allow ( service ) . to receive ( :count ) . and_return ( 2300 )
end
expect ( helper . cached_open_group_issues_count ( group ) ) . to eq ( '2.3k' )
end
it 'returns truncated digits for count value over 10000' do
allow_next_instance_of ( count_service ) do | service |
allow ( service ) . to receive ( :count ) . and_return ( 12560 )
end
expect ( helper . cached_open_group_issues_count ( group ) ) . to eq ( '12.6k' )
end
it 'returns truncated digits for count value over 100000' do
allow_next_instance_of ( count_service ) do | service |
allow ( service ) . to receive ( :count ) . and_return ( 112560 )
end
expect ( helper . cached_open_group_issues_count ( group ) ) . to eq ( '112.6k' )
end
end
2015-03-12 17:51:41 -04:00
end