1
1
import { navItems , routes } from '../../src/index'
2
2
import { Ability } from '@ownclouders/web-client'
3
+ import { createTestingPinia } from '@ownclouders/web-test-helpers'
3
4
import { mock } from 'vitest-mock-extended'
5
+ import * as pkg from '@ownclouders/web-pkg'
6
+ import { AuthService } from '../../../web-runtime/src/services/auth/authService'
7
+
8
+ const mockRequireAcr = vi . fn ( )
9
+ vi . spyOn ( pkg , 'useAuthService' ) . mockReturnValue ( mock < AuthService > ( { requireAcr : mockRequireAcr } ) )
4
10
5
11
const getAbilityMock = ( hasPermission : boolean ) => mock < Ability > ( { can : ( ) => hasPermission } )
6
12
7
13
describe ( 'admin settings index' , ( ) => {
14
+ beforeEach ( ( ) => {
15
+ createTestingPinia ( {
16
+ initialState : {
17
+ capabilities : {
18
+ capabilities : { auth : { mfa : { enabled : true , levelnames : [ 'advanced' ] } } }
19
+ }
20
+ }
21
+ } )
22
+ } )
23
+
8
24
describe ( 'navItems' , ( ) => {
9
25
describe ( 'general' , ( ) => {
10
26
it . each ( [ true , false ] ) ( 'should be enabled according to the permissions' , ( enabled ) => {
@@ -64,11 +80,11 @@ describe('admin settings index', () => {
64
80
} ) ,
65
81
redirect : { name : 'admin-settings-groups' }
66
82
}
67
- ] ) ( 'redirects "/general" with sufficient permissions' , ( { can, redirect } ) => {
83
+ ] ) ( 'redirects "/general" with sufficient permissions' , async ( { can, redirect } ) => {
68
84
const ability = mock < Ability > ( { can } )
69
85
const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/general' )
70
86
const nextMock = vi . fn ( )
71
- ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
87
+ await ( route . beforeEnter as any ) ( { } , { } , nextMock )
72
88
const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
73
89
expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
74
90
} )
@@ -84,11 +100,11 @@ describe('admin settings index', () => {
84
100
} ) ,
85
101
redirect : { name : 'admin-settings-spaces' }
86
102
}
87
- ] ) ( 'redirects "/users" with sufficient permissions' , ( { can, redirect } ) => {
103
+ ] ) ( 'redirects "/users" with sufficient permissions' , async ( { can, redirect } ) => {
88
104
const ability = mock < Ability > ( { can } )
89
105
const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/users' )
90
106
const nextMock = vi . fn ( )
91
- ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
107
+ await ( route . beforeEnter as any ) ( { } , { } , nextMock )
92
108
const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
93
109
expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
94
110
} )
@@ -104,11 +120,11 @@ describe('admin settings index', () => {
104
120
} ) ,
105
121
redirect : { name : 'admin-settings-general' }
106
122
}
107
- ] ) ( 'redirects "/groups" with sufficient permissions' , ( { can, redirect } ) => {
123
+ ] ) ( 'redirects "/groups" with sufficient permissions' , async ( { can, redirect } ) => {
108
124
const ability = mock < Ability > ( { can } )
109
125
const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/groups' )
110
126
const nextMock = vi . fn ( )
111
- ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
127
+ await ( route . beforeEnter as any ) ( { } , { } , nextMock )
112
128
const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
113
129
expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
114
130
} )
@@ -124,24 +140,56 @@ describe('admin settings index', () => {
124
140
} ) ,
125
141
redirect : { name : 'admin-settings-users' }
126
142
}
127
- ] ) ( 'redirects "/spaces" with sufficient permissions' , ( { can, redirect } ) => {
143
+ ] ) ( 'redirects "/spaces" with sufficient permissions' , async ( { can, redirect } ) => {
128
144
const ability = mock < Ability > ( { can } )
129
145
const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === '/spaces' )
130
146
const nextMock = vi . fn ( )
131
- ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
147
+ await ( route . beforeEnter as any ) ( { } , { } , nextMock )
132
148
const args = [ ...( redirect ? [ redirect ] : [ ] ) ]
133
149
expect ( nextMock ) . toHaveBeenCalledWith ( ...args )
134
150
} )
135
151
it . each ( [ '/general' , '/users' , '/groups' , '/spaces' ] ) (
136
152
'should throw an error if permissions are insufficient' ,
137
- ( path ) => {
153
+ async ( path ) => {
138
154
const ability = mock < Ability > ( { can : vi . fn ( ( ) => false ) } )
139
155
const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === path )
140
156
const nextMock = vi . fn ( )
141
- expect ( ( ) => {
142
- ; ( route . beforeEnter as any ) ( { } , { } , nextMock )
143
- } ) . toThrowError ( 'Insufficient permissions' )
157
+ await expect ( ( ) => ( route . beforeEnter as any ) ( { } , { } , nextMock ) ) . rejects . toThrowError (
158
+ 'Insufficient permissions'
159
+ )
144
160
}
145
161
)
162
+
163
+ describe ( 'requireAcr' , ( ) => {
164
+ it . each ( [ '/general' , '/users' , '/groups' , '/spaces' ] ) (
165
+ 'should call requireAcr if MFA is enabled when path is %s' ,
166
+ async ( path ) => {
167
+ const ability = mock < Ability > ( { can : vi . fn ( ( ) => true ) } )
168
+ const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === path )
169
+ await ( route . beforeEnter as any ) ( { fullPath : path } , { } , vi . fn ( ) )
170
+ expect ( mockRequireAcr ) . toHaveBeenCalledWith ( 'advanced' , path )
171
+ }
172
+ )
173
+ } )
174
+
175
+ describe ( 'requireAcr' , ( ) => {
176
+ it . each ( [ '/general' , '/users' , '/groups' , '/spaces' ] ) (
177
+ 'should not call requireAcr if MFA is disabled when path is %s' ,
178
+ async ( path ) => {
179
+ createTestingPinia ( {
180
+ initialState : {
181
+ capabilities : {
182
+ capabilities : { auth : { mfa : { enabled : false , levelnames : [ 'advanced' ] } } }
183
+ }
184
+ }
185
+ } )
186
+
187
+ const ability = mock < Ability > ( { can : vi . fn ( ( ) => true ) } )
188
+ const route = routes ( { $ability : ability } ) . find ( ( n ) => n . path === path )
189
+ await ( route . beforeEnter as any ) ( { } , { } , vi . fn ( ) )
190
+ expect ( mockRequireAcr ) . not . toHaveBeenCalled ( )
191
+ }
192
+ )
193
+ } )
146
194
} )
147
195
} )
0 commit comments