@@ -45,85 +45,103 @@ describe('admin settings index', () => {
4545 } )
4646 describe ( 'routes' , ( ) => {
4747 describe ( 'default-route "/"' , ( ) => {
48- it ( 'should redirect to general if permission given ' , ( ) => {
48+ it ( 'should redirect to general' , ( ) => {
4949 const ability = mock < Ability > ( )
5050 ability . can . mockReturnValueOnce ( true )
5151 const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/' )
5252 expect ( ( route . redirect as any ) ( ) . name ) . toEqual ( 'admin-settings-general' )
5353 } )
54- it ( 'should redirect to user management if permission given' , ( ) => {
55- const ability = mock < Ability > ( )
56- ability . can . mockReturnValueOnce ( false )
57- ability . can . mockReturnValueOnce ( true )
58- const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/' )
59- expect ( ( route . redirect as any ) ( ) . name ) . toEqual ( 'admin-settings-users' )
60- } )
61- it ( 'should redirect to group management if permission given' , ( ) => {
62- const ability = mock < Ability > ( )
63- ability . can . mockReturnValueOnce ( false )
64- ability . can . mockReturnValueOnce ( false )
65- ability . can . mockReturnValueOnce ( true )
66- const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/' )
67- expect ( ( route . redirect as any ) ( ) . name ) . toEqual ( 'admin-settings-groups' )
68- } )
69- it ( 'should redirect to space management if permission given' , ( ) => {
70- const ability = mock < Ability > ( )
71- ability . can . mockReturnValueOnce ( false )
72- ability . can . mockReturnValueOnce ( false )
73- ability . can . mockReturnValueOnce ( false )
74- ability . can . mockReturnValueOnce ( true )
75- const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/' )
76- expect ( ( route . redirect as any ) ( ) . name ) . toEqual ( 'admin-settings-spaces' )
77- } )
78- it ( 'should throw an error if permissions are insufficient' , ( ) => {
79- const ability = mock < Ability > ( )
80- ability . can . mockReturnValue ( false )
81- expect ( routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/' ) . redirect ) . toThrow ( )
82- } )
8354 } )
8455 it . each ( [
85- { can : true , redirect : null } ,
86- { can : false , redirect : { path : '/' } }
56+ { can : vi . fn ( ( ) => true ) , redirect : null } ,
57+ {
58+ can : vi . fn ( ( _ , subject ) => {
59+ if ( subject === 'Group' ) {
60+ return true
61+ }
62+
63+ return false
64+ } ) ,
65+ redirect : { name : 'admin-settings-groups' }
66+ }
8767 ] ) ( 'redirects "/general" with sufficient permissions' , ( { can, redirect } ) => {
88- const ability = mock < Ability > ( { can : vi . fn ( ( ) => can ) } )
68+ const ability = mock < Ability > ( { can } )
8969 const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/general' )
9070 const nextMock = vi . fn ( )
9171 ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
9272 const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
9373 expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
9474 } )
9575 it . each ( [
96- { can : true , redirect : null } ,
97- { can : false , redirect : { path : '/' } }
76+ { can : vi . fn ( ( ) => true ) , redirect : null } ,
77+ {
78+ can : vi . fn ( ( _ , subject ) => {
79+ if ( subject === 'Drive' ) {
80+ return true
81+ }
82+
83+ return false
84+ } ) ,
85+ redirect : { name : 'admin-settings-spaces' }
86+ }
9887 ] ) ( 'redirects "/users" with sufficient permissions' , ( { can, redirect } ) => {
99- const ability = mock < Ability > ( { can : vi . fn ( ( ) => can ) } )
88+ const ability = mock < Ability > ( { can } )
10089 const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/users' )
10190 const nextMock = vi . fn ( )
10291 ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
10392 const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
10493 expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
10594 } )
10695 it . each ( [
107- { can : true , redirect : null } ,
108- { can : false , redirect : { path : '/' } }
96+ { can : vi . fn ( ( ) => true ) , redirect : null } ,
97+ {
98+ can : vi . fn ( ( _ , subject ) => {
99+ if ( subject === 'Setting' ) {
100+ return true
101+ }
102+
103+ return false
104+ } ) ,
105+ redirect : { name : 'admin-settings-general' }
106+ }
109107 ] ) ( 'redirects "/groups" with sufficient permissions' , ( { can, redirect } ) => {
110- const ability = mock < Ability > ( { can : vi . fn ( ( ) => can ) } )
108+ const ability = mock < Ability > ( { can } )
111109 const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/groups' )
112110 const nextMock = vi . fn ( )
113111 ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
114112 const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
115113 expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
116114 } )
117115 it . each ( [
118- { can : true , redirect : null } ,
119- { can : false , redirect : { path : '/' } }
116+ { can : vi . fn ( ( ) => true ) , redirect : null } ,
117+ {
118+ can : vi . fn ( ( _ , subject ) => {
119+ if ( subject === 'Account' ) {
120+ return true
121+ }
122+
123+ return false
124+ } ) ,
125+ redirect : { name : 'admin-settings-users' }
126+ }
120127 ] ) ( 'redirects "/spaces" with sufficient permissions' , ( { can, redirect } ) => {
121- const ability = mock < Ability > ( { can : vi . fn ( ( ) => can ) } )
128+ const ability = mock < Ability > ( { can } )
122129 const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/spaces' )
123130 const nextMock = vi . fn ( )
124131 ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
125132 const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
126133 expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
127134 } )
135+ it . each ( [ '/general' , '/users' , '/groups' , '/spaces' ] ) (
136+ 'should throw an error if permissions are insufficient' ,
137+ ( path ) => {
138+ const ability = mock < Ability > ( { can : vi . fn ( ( ) => false ) } )
139+ const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === path )
140+ const nextMock = vi . fn ( )
141+ expect ( ( ) => {
142+ ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
143+ } ) . toThrowError ( 'Insufficient permissions' )
144+ }
145+ )
128146 } )
129147} )
0 commit comments