2019-09-30 05:06:31 -04:00
# frozen_string_literal: true
2017-09-05 05:30:16 -04:00
require 'spec_helper'
2020-06-24 02:09:01 -04:00
RSpec . describe GroupChildEntity do
2019-04-09 11:38:58 -04:00
include ExternalAuthorizationServiceHelpers
2017-10-11 08:39:23 -04:00
include Gitlab :: Routing . url_helpers
2022-02-11 13:18:58 -05:00
let_it_be ( :user ) { create ( :user ) }
2017-09-05 05:30:16 -04:00
let ( :request ) { double ( 'request' ) }
let ( :entity ) { described_class . new ( object , request : request ) }
2019-12-17 13:07:48 -05:00
2017-09-05 05:30:16 -04:00
subject ( :json ) { entity . as_json }
2017-09-05 10:18:24 -04:00
before do
allow ( request ) . to receive ( :current_user ) . and_return ( user )
2019-01-11 19:31:00 -05:00
stub_commonmark_sourcepos_disabled
2017-09-05 10:18:24 -04:00
end
shared_examples 'group child json' do
it 'renders json' do
is_expected . not_to be_nil
end
%w[ id
full_name
avatar_url
name
description
2017-12-21 16:15:50 -05:00
markdown_description
2017-09-05 10:18:24 -04:00
visibility
type
can_edit
visibility
2017-09-13 05:11:09 -04:00
permission
relative_path ] . each do | attribute |
2017-09-05 10:18:24 -04:00
it " includes #{ attribute } " do
expect ( json [ attribute . to_sym ] ) . to be_present
end
end
end
2017-09-05 05:30:16 -04:00
describe 'for a project' do
2017-10-05 04:38:05 -04:00
let ( :object ) do
2017-09-05 10:18:24 -04:00
create ( :project , :with_avatar ,
description : 'Awesomeness' )
end
before do
2018-07-11 10:36:08 -04:00
object . add_maintainer ( user )
2017-09-05 10:18:24 -04:00
end
2017-09-05 05:30:16 -04:00
it 'has the correct type' do
expect ( json [ :type ] ) . to eq ( 'project' )
end
2017-09-05 10:18:24 -04:00
it 'includes the star count' do
expect ( json [ :star_count ] ) . to be_present
end
2017-10-11 08:39:23 -04:00
it 'has the correct edit path' do
expect ( json [ :edit_path ] ) . to eq ( edit_project_path ( object ) )
end
2022-01-10 10:14:26 -05:00
it 'includes the last activity at' do
expect ( json [ :last_activity_at ] ) . to be_present
end
2017-09-05 10:18:24 -04:00
it_behaves_like 'group child json'
end
2019-07-24 05:20:54 -04:00
describe 'for a group' do
2017-12-21 16:15:50 -05:00
let ( :description ) { 'Awesomeness' }
2017-10-05 04:38:05 -04:00
let ( :object ) do
2017-09-05 10:18:24 -04:00
create ( :group , :nested , :with_avatar ,
2017-12-21 16:15:50 -05:00
description : description )
2017-09-05 10:18:24 -04:00
end
before do
object . add_owner ( user )
end
it 'has the correct type' do
expect ( json [ :type ] ) . to eq ( 'group' )
end
it 'counts projects and subgroups as children' do
create ( :project , namespace : object )
create ( :group , parent : object )
expect ( json [ :children_count ] ) . to eq ( 2 )
end
2021-08-24 14:10:53 -04:00
%w[ children_count leave_path parent_id number_users_with_delimiter project_count subgroup_count ] . each do | attribute |
2017-09-05 10:18:24 -04:00
it " includes #{ attribute } " do
expect ( json [ attribute . to_sym ] ) . to be_present
end
end
2017-09-12 04:51:34 -04:00
it 'allows an owner to leave when there is another one' do
object . add_owner ( create ( :user ) )
expect ( json [ :can_leave ] ) . to be_truthy
end
2022-02-11 13:18:58 -05:00
it 'allows an owner to delete the group' do
expect ( json [ :can_remove ] ) . to be_truthy
end
it 'allows admin to delete the group' , :enable_admin_mode do
allow ( request ) . to receive ( :current_user ) . and_return ( create ( :admin ) )
expect ( json [ :can_remove ] ) . to be_truthy
end
it 'disallows a maintainer to delete the group' do
object . add_maintainer ( user )
expect ( json [ :can_remove ] ) . to be_falsy
end
2017-10-11 08:39:23 -04:00
it 'has the correct edit path' do
expect ( json [ :edit_path ] ) . to eq ( edit_group_path ( object ) )
end
2017-12-21 16:15:50 -05:00
context 'emoji in description' do
let ( :description ) { ':smile:' }
it 'has the correct markdown_description' do
2019-01-11 19:31:00 -05:00
expect ( json [ :markdown_description ] ) . to eq ( '<p dir="auto"><gl-emoji title="smiling face with open mouth and smiling eyes" data-name="smile" data-unicode-version="6.0">😄</gl-emoji></p>' )
2017-12-21 16:15:50 -05:00
end
end
2017-09-05 10:18:24 -04:00
it_behaves_like 'group child json'
2017-09-05 05:30:16 -04:00
end
2019-04-09 11:38:58 -04:00
2021-08-24 14:10:53 -04:00
describe 'for a private group' do
let ( :object ) do
create ( :group , :private )
end
describe 'user is member of the group' do
before do
object . add_owner ( user )
end
it 'includes the counts' do
expect ( json . keys ) . to include ( * % i ( project_count subgroup_count ) )
end
end
describe 'user is not a member of the group' do
it 'does not include the counts' do
expect ( json . keys ) . not_to include ( * % i ( project_count subgroup_count ) )
end
end
describe 'user is only a member of a project in the group' do
let ( :project ) { create ( :project , namespace : object ) }
before do
project . add_guest ( user )
end
it 'does not include the counts' do
expect ( json . keys ) . not_to include ( * % i ( project_count subgroup_count ) )
end
end
end
2019-04-09 11:38:58 -04:00
describe 'for a project with external authorization enabled' do
let ( :object ) do
create ( :project , :with_avatar ,
description : 'Awesomeness' )
end
before do
enable_external_authorization_service_check
object . add_maintainer ( user )
end
it 'does not hit the external authorization service' do
expect ( :: Gitlab :: ExternalAuthorization ) . not_to receive ( :access_allowed? )
expect ( json [ :can_edit ] ) . to eq ( false )
end
end
2017-09-05 05:30:16 -04:00
end