99namespace OCA \Libresign \Tests \Unit \Service \Policy \Runtime ;
1010
1111use OCA \Libresign \Service \Policy \Runtime \PolicyContextFactory ;
12+ use OCP \Group \ISubAdmin ;
1213use OCP \IGroupManager ;
1314use OCP \IUser ;
1415use OCP \IUserManager ;
1920final class PolicyContextFactoryTest extends TestCase {
2021 private IUserManager &MockObject $ userManager ;
2122 private IGroupManager &MockObject $ groupManager ;
23+ private ISubAdmin &MockObject $ subAdmin ;
2224 private IUserSession &MockObject $ userSession ;
2325
2426 protected function setUp (): void {
2527 parent ::setUp ();
2628 $ this ->userManager = $ this ->createMock (IUserManager::class);
2729 $ this ->groupManager = $ this ->createMock (IGroupManager::class);
30+ $ this ->subAdmin = $ this ->createMock (ISubAdmin::class);
2831 $ this ->userSession = $ this ->createMock (IUserSession::class);
2932 }
3033
@@ -42,6 +45,46 @@ public function testForCurrentUserUsesSessionUser(): void {
4245 $ this ->assertSame (['finance ' ], $ context ->getGroups ());
4346 $ this ->assertSame (['signature_flow ' => 'parallel ' ], $ context ->getRequestOverrides ());
4447 $ this ->assertSame (['type ' => 'group ' , 'id ' => 'finance ' ], $ context ->getActiveContext ());
48+ $ this ->assertSame ([
49+ 'canManageSystemPolicies ' => false ,
50+ 'canManageGroupPolicies ' => false ,
51+ ], $ context ->getActorCapabilities ());
52+ }
53+
54+ public function testForCurrentUserMarksSystemAdminCapabilities (): void {
55+ $ user = $ this ->createMock (IUser::class);
56+ $ user ->method ('getUID ' )->willReturn ('admin ' );
57+
58+ $ this ->userSession ->expects ($ this ->once ())->method ('getUser ' )->willReturn ($ user );
59+ $ this ->groupManager ->expects ($ this ->once ())->method ('getUserGroupIds ' )->with ($ user )->willReturn ([]);
60+ $ this ->groupManager ->expects ($ this ->once ())->method ('isAdmin ' )->with ('admin ' )->willReturn (true );
61+ $ this ->subAdmin ->expects ($ this ->never ())->method ('isSubAdmin ' );
62+
63+ $ factory = $ this ->getFactory ();
64+ $ context = $ factory ->forCurrentUser ();
65+
66+ $ this ->assertSame ([
67+ 'canManageSystemPolicies ' => true ,
68+ 'canManageGroupPolicies ' => true ,
69+ ], $ context ->getActorCapabilities ());
70+ }
71+
72+ public function testForCurrentUserMarksSubAdminGroupCapabilities (): void {
73+ $ user = $ this ->createMock (IUser::class);
74+ $ user ->method ('getUID ' )->willReturn ('manager ' );
75+
76+ $ this ->userSession ->expects ($ this ->once ())->method ('getUser ' )->willReturn ($ user );
77+ $ this ->groupManager ->expects ($ this ->once ())->method ('getUserGroupIds ' )->with ($ user )->willReturn (['finance ' ]);
78+ $ this ->groupManager ->expects ($ this ->once ())->method ('isAdmin ' )->with ('manager ' )->willReturn (false );
79+ $ this ->subAdmin ->expects ($ this ->once ())->method ('isSubAdmin ' )->with ($ user )->willReturn (true );
80+
81+ $ factory = $ this ->getFactory ();
82+ $ context = $ factory ->forCurrentUser ();
83+
84+ $ this ->assertSame ([
85+ 'canManageSystemPolicies ' => false ,
86+ 'canManageGroupPolicies ' => true ,
87+ ], $ context ->getActorCapabilities ());
4588 }
4689
4790 public function testForUserIdLoadsUserWhenAvailable (): void {
@@ -68,6 +111,6 @@ public function testForUserIdKeepsUserIdWithoutGroupsWhenUserDoesNotExist(): voi
68111 }
69112
70113 private function getFactory (): PolicyContextFactory {
71- return new PolicyContextFactory ($ this ->userManager , $ this ->groupManager , $ this ->userSession );
114+ return new PolicyContextFactory ($ this ->userManager , $ this ->groupManager , $ this ->subAdmin , $ this -> userSession );
72115 }
73116}
0 commit comments