77 */
88namespace OCA \Files_External \Tests \Controller ;
99
10+ use OC \Settings \AuthorizedGroupMapper ;
1011use OCA \Files_External \Controller \AjaxController ;
1112use OCA \Files_External \Lib \Auth \Password \GlobalAuth ;
1213use OCA \Files_External \Lib \Auth \PublicKey \RSA ;
14+ use OCA \Files_External \Settings \Admin ;
1315use OCP \AppFramework \Http \JSONResponse ;
16+ use OCP \IGroup ;
1417use OCP \IGroupManager ;
1518use OCP \IL10N ;
1619use OCP \IRequest ;
@@ -28,6 +31,7 @@ class AjaxControllerTest extends TestCase {
2831 private IGroupManager &MockObject $ groupManager ;
2932 private IUserManager &MockObject $ userManager ;
3033 private IL10N &MockObject $ l10n ;
34+ private AuthorizedGroupMapper &MockObject $ authorizedGroupMapper ;
3135 private AjaxController $ ajaxController ;
3236
3337 protected function setUp (): void {
@@ -38,6 +42,7 @@ protected function setUp(): void {
3842 $ this ->groupManager = $ this ->createMock (IGroupManager::class);
3943 $ this ->userManager = $ this ->createMock (IUserManager::class);
4044 $ this ->l10n = $ this ->createMock (IL10N ::class);
45+ $ this ->authorizedGroupMapper = $ this ->createMock (AuthorizedGroupMapper::class);
4146
4247 $ this ->ajaxController = new AjaxController (
4348 'files_external ' ,
@@ -48,6 +53,7 @@ protected function setUp(): void {
4853 $ this ->groupManager ,
4954 $ this ->userManager ,
5055 $ this ->l10n ,
56+ $ this ->authorizedGroupMapper ,
5157 );
5258
5359 $ this ->l10n ->expects ($ this ->any ())
@@ -62,6 +68,50 @@ protected function setUp(): void {
6268 parent ::setUp ();
6369 }
6470
71+ public function testGetApplicableEntitiesReturnsGroupsAndUsers (): void {
72+ $ group = $ this ->createMock (IGroup::class);
73+ $ group ->method ('getGID ' )->willReturn ('group1 ' );
74+ $ group ->method ('getDisplayName ' )->willReturn ('Group One ' );
75+
76+ $ user = $ this ->createMock (IUser::class);
77+ $ user ->method ('getUID ' )->willReturn ('user1 ' );
78+ $ user ->method ('getDisplayName ' )->willReturn ('User One ' );
79+
80+ $ this ->groupManager
81+ ->expects ($ this ->once ())
82+ ->method ('search ' )
83+ ->with ('test ' , 10 , 0 )
84+ ->willReturn ([$ group ]);
85+ $ this ->userManager
86+ ->expects ($ this ->once ())
87+ ->method ('searchDisplayName ' )
88+ ->with ('test ' , 10 , 0 )
89+ ->willReturn ([$ user ]);
90+
91+ $ response = $ this ->ajaxController ->getApplicableEntities ('test ' , 10 , 0 );
92+ $ this ->assertSame (200 , $ response ->getStatus ());
93+ $ this ->assertSame (['group1 ' => 'Group One ' ], $ response ->getData ()['groups ' ]);
94+ $ this ->assertSame (['user1 ' => 'User One ' ], $ response ->getData ()['users ' ]);
95+ }
96+
97+ public function testGetApplicableEntitiesWithNoResults (): void {
98+ $ this ->groupManager
99+ ->expects ($ this ->once ())
100+ ->method ('search ' )
101+ ->with ('' , null , null )
102+ ->willReturn ([]);
103+ $ this ->userManager
104+ ->expects ($ this ->once ())
105+ ->method ('searchDisplayName ' )
106+ ->with ('' , null , null )
107+ ->willReturn ([]);
108+
109+ $ response = $ this ->ajaxController ->getApplicableEntities ();
110+ $ this ->assertSame (200 , $ response ->getStatus ());
111+ $ this ->assertSame ([], $ response ->getData ()['groups ' ]);
112+ $ this ->assertSame ([], $ response ->getData ()['users ' ]);
113+ }
114+
65115 public function testGetSshKeys (): void {
66116 $ this ->rsa
67117 ->expects ($ this ->once ())
@@ -153,4 +203,91 @@ public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void {
153203 $ this ->assertSame ($ response ->getStatus (), 403 );
154204 $ this ->assertSame ('Permission denied ' , $ response ->getData ()['message ' ]);
155205 }
206+
207+ public function testSaveGlobalCredentialsAsAdminForGlobal (): void {
208+ $ user = $ this ->createMock (IUser::class);
209+ $ user ->method ('getUID ' )->willReturn ('MyAdminUid ' );
210+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
211+ $ this ->groupManager
212+ ->expects ($ this ->once ())
213+ ->method ('isAdmin ' )
214+ ->with ('MyAdminUid ' )
215+ ->willReturn (true );
216+ $ this ->authorizedGroupMapper
217+ ->expects ($ this ->never ())
218+ ->method ('findAllClassesForUser ' );
219+ $ this ->globalAuth
220+ ->expects ($ this ->once ())
221+ ->method ('saveAuth ' )
222+ ->with ('' , 'test ' , 'password ' );
223+
224+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('' , 'test ' , 'password ' );
225+ $ this ->assertSame (200 , $ response ->getStatus ());
226+ }
227+
228+ public function testSaveGlobalCredentialsAsDelegatedAdminForGlobal (): void {
229+ $ user = $ this ->createMock (IUser::class);
230+ $ user ->method ('getUID ' )->willReturn ('DelegatedUid ' );
231+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
232+ $ this ->groupManager
233+ ->expects ($ this ->once ())
234+ ->method ('isAdmin ' )
235+ ->with ('DelegatedUid ' )
236+ ->willReturn (false );
237+ $ this ->authorizedGroupMapper
238+ ->expects ($ this ->once ())
239+ ->method ('findAllClassesForUser ' )
240+ ->with ($ user )
241+ ->willReturn ([Admin::class]);
242+ $ this ->globalAuth
243+ ->expects ($ this ->once ())
244+ ->method ('saveAuth ' )
245+ ->with ('' , 'test ' , 'password ' );
246+
247+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('' , 'test ' , 'password ' );
248+ $ this ->assertSame (200 , $ response ->getStatus ());
249+ }
250+
251+ public function testSaveGlobalCredentialsAsDelegatedAdminForAnotherUser (): void {
252+ // Delegated admins may only set global (uid='') credentials, not impersonate other users.
253+ $ user = $ this ->createMock (IUser::class);
254+ $ user ->method ('getUID ' )->willReturn ('DelegatedUid ' );
255+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
256+ $ this ->groupManager
257+ ->expects ($ this ->never ())
258+ ->method ('isAdmin ' );
259+ $ this ->authorizedGroupMapper
260+ ->expects ($ this ->never ())
261+ ->method ('findAllClassesForUser ' );
262+ $ this ->globalAuth
263+ ->expects ($ this ->never ())
264+ ->method ('saveAuth ' );
265+
266+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('OtherUserUid ' , 'test ' , 'password ' );
267+ $ this ->assertSame (403 , $ response ->getStatus ());
268+ $ this ->assertSame ('Permission denied ' , $ response ->getData ()['message ' ]);
269+ }
270+
271+ public function testSaveGlobalCredentialsAsNormalUserForGlobal (): void {
272+ $ user = $ this ->createMock (IUser::class);
273+ $ user ->method ('getUID ' )->willReturn ('NormalUid ' );
274+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
275+ $ this ->groupManager
276+ ->expects ($ this ->once ())
277+ ->method ('isAdmin ' )
278+ ->with ('NormalUid ' )
279+ ->willReturn (false );
280+ $ this ->authorizedGroupMapper
281+ ->expects ($ this ->once ())
282+ ->method ('findAllClassesForUser ' )
283+ ->with ($ user )
284+ ->willReturn ([]);
285+ $ this ->globalAuth
286+ ->expects ($ this ->never ())
287+ ->method ('saveAuth ' );
288+
289+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('' , 'test ' , 'password ' );
290+ $ this ->assertSame (403 , $ response ->getStatus ());
291+ $ this ->assertSame ('Permission denied ' , $ response ->getData ()['message ' ]);
292+ }
156293}
0 commit comments