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 ;
1416use OCP \IGroupManager ;
1517use OCP \IL10N ;
@@ -28,6 +30,7 @@ class AjaxControllerTest extends TestCase {
2830 private IGroupManager &MockObject $ groupManager ;
2931 private IUserManager &MockObject $ userManager ;
3032 private IL10N &MockObject $ l10n ;
33+ private AuthorizedGroupMapper &MockObject $ authorizedGroupMapper ;
3134 private AjaxController $ ajaxController ;
3235
3336 protected function setUp (): void {
@@ -38,6 +41,7 @@ protected function setUp(): void {
3841 $ this ->groupManager = $ this ->createMock (IGroupManager::class);
3942 $ this ->userManager = $ this ->createMock (IUserManager::class);
4043 $ this ->l10n = $ this ->createMock (IL10N ::class);
44+ $ this ->authorizedGroupMapper = $ this ->createMock (AuthorizedGroupMapper::class);
4145
4246 $ this ->ajaxController = new AjaxController (
4347 'files_external ' ,
@@ -48,6 +52,7 @@ protected function setUp(): void {
4852 $ this ->groupManager ,
4953 $ this ->userManager ,
5054 $ this ->l10n ,
55+ $ this ->authorizedGroupMapper ,
5156 );
5257
5358 $ this ->l10n ->expects ($ this ->any ())
@@ -153,4 +158,91 @@ public function testSaveGlobalCredentialsAsNormalUserForAnotherUser(): void {
153158 $ this ->assertSame ($ response ->getStatus (), 403 );
154159 $ this ->assertSame ('Permission denied ' , $ response ->getData ()['message ' ]);
155160 }
161+
162+ public function testSaveGlobalCredentialsAsAdminForGlobal (): void {
163+ $ user = $ this ->createMock (IUser::class);
164+ $ user ->method ('getUID ' )->willReturn ('MyAdminUid ' );
165+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
166+ $ this ->groupManager
167+ ->expects ($ this ->once ())
168+ ->method ('isAdmin ' )
169+ ->with ('MyAdminUid ' )
170+ ->willReturn (true );
171+ $ this ->authorizedGroupMapper
172+ ->expects ($ this ->never ())
173+ ->method ('findAllClassesForUser ' );
174+ $ this ->globalAuth
175+ ->expects ($ this ->once ())
176+ ->method ('saveAuth ' )
177+ ->with ('' , 'test ' , 'password ' );
178+
179+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('' , 'test ' , 'password ' );
180+ $ this ->assertSame (200 , $ response ->getStatus ());
181+ }
182+
183+ public function testSaveGlobalCredentialsAsDelegatedAdminForGlobal (): void {
184+ $ user = $ this ->createMock (IUser::class);
185+ $ user ->method ('getUID ' )->willReturn ('DelegatedUid ' );
186+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
187+ $ this ->groupManager
188+ ->expects ($ this ->once ())
189+ ->method ('isAdmin ' )
190+ ->with ('DelegatedUid ' )
191+ ->willReturn (false );
192+ $ this ->authorizedGroupMapper
193+ ->expects ($ this ->once ())
194+ ->method ('findAllClassesForUser ' )
195+ ->with ($ user )
196+ ->willReturn ([Admin::class]);
197+ $ this ->globalAuth
198+ ->expects ($ this ->once ())
199+ ->method ('saveAuth ' )
200+ ->with ('' , 'test ' , 'password ' );
201+
202+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('' , 'test ' , 'password ' );
203+ $ this ->assertSame (200 , $ response ->getStatus ());
204+ }
205+
206+ public function testSaveGlobalCredentialsAsDelegatedAdminForAnotherUser (): void {
207+ // Delegated admins may only set global (uid='') credentials, not impersonate other users.
208+ $ user = $ this ->createMock (IUser::class);
209+ $ user ->method ('getUID ' )->willReturn ('DelegatedUid ' );
210+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
211+ $ this ->groupManager
212+ ->expects ($ this ->never ())
213+ ->method ('isAdmin ' );
214+ $ this ->authorizedGroupMapper
215+ ->expects ($ this ->never ())
216+ ->method ('findAllClassesForUser ' );
217+ $ this ->globalAuth
218+ ->expects ($ this ->never ())
219+ ->method ('saveAuth ' );
220+
221+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('OtherUserUid ' , 'test ' , 'password ' );
222+ $ this ->assertSame (403 , $ response ->getStatus ());
223+ $ this ->assertSame ('Permission denied ' , $ response ->getData ()['message ' ]);
224+ }
225+
226+ public function testSaveGlobalCredentialsAsNormalUserForGlobal (): void {
227+ $ user = $ this ->createMock (IUser::class);
228+ $ user ->method ('getUID ' )->willReturn ('NormalUid ' );
229+ $ this ->userSession ->method ('getUser ' )->willReturn ($ user );
230+ $ this ->groupManager
231+ ->expects ($ this ->once ())
232+ ->method ('isAdmin ' )
233+ ->with ('NormalUid ' )
234+ ->willReturn (false );
235+ $ this ->authorizedGroupMapper
236+ ->expects ($ this ->once ())
237+ ->method ('findAllClassesForUser ' )
238+ ->with ($ user )
239+ ->willReturn ([]);
240+ $ this ->globalAuth
241+ ->expects ($ this ->never ())
242+ ->method ('saveAuth ' );
243+
244+ $ response = $ this ->ajaxController ->saveGlobalCredentials ('' , 'test ' , 'password ' );
245+ $ this ->assertSame (403 , $ response ->getStatus ());
246+ $ this ->assertSame ('Permission denied ' , $ response ->getData ()['message ' ]);
247+ }
156248}
0 commit comments