|
11 | 11 | use OCA\Libresign\Service\Policy\Model\PolicyLayer; |
12 | 12 | use OCA\Libresign\Service\Policy\Model\ResolvedPolicy; |
13 | 13 | use OCA\Libresign\Service\Policy\PolicyService; |
| 14 | +use OCA\Libresign\Service\Policy\Provider\DocMdp\DocMdpPolicy; |
14 | 15 | use OCA\Libresign\Service\Policy\Provider\Signature\SignatureFlowPolicy; |
15 | 16 | use OCA\Libresign\Service\Policy\Runtime\PolicyContextFactory; |
16 | 17 | use OCA\Libresign\Service\Policy\Runtime\PolicyRegistry; |
@@ -40,12 +41,69 @@ protected function setUp(): void { |
40 | 41 | $container = $this->createMock(ContainerInterface::class); |
41 | 42 | $container |
42 | 43 | ->method('get') |
43 | | - ->with(SignatureFlowPolicy::class) |
44 | | - ->willReturn(new SignatureFlowPolicy()); |
| 44 | + ->willReturnCallback(static function (string $class): object { |
| 45 | + return match ($class) { |
| 46 | + SignatureFlowPolicy::class => new SignatureFlowPolicy(), |
| 47 | + DocMdpPolicy::class => new DocMdpPolicy(), |
| 48 | + default => throw new \RuntimeException('Unexpected provider class: ' . $class), |
| 49 | + }; |
| 50 | + }); |
45 | 51 | $this->registry = new PolicyRegistry($container); |
46 | 52 | $this->contextFactory = new PolicyContextFactory($this->userManager, $this->groupManager, $this->userSession); |
47 | 53 | } |
48 | 54 |
|
| 55 | + public function testResolveForUserIdUsesDocMdpGroupPolicyWhenSystemAllowsOverride(): void { |
| 56 | + $user = $this->createMock(IUser::class); |
| 57 | + $this->userManager |
| 58 | + ->expects($this->once()) |
| 59 | + ->method('get') |
| 60 | + ->with('john') |
| 61 | + ->willReturn($user); |
| 62 | + |
| 63 | + $this->groupManager |
| 64 | + ->expects($this->once()) |
| 65 | + ->method('getUserGroupIds') |
| 66 | + ->with($user) |
| 67 | + ->willReturn(['finance']); |
| 68 | + |
| 69 | + $this->source |
| 70 | + ->method('loadSystemPolicy') |
| 71 | + ->with(DocMdpPolicy::KEY) |
| 72 | + ->willReturn((new PolicyLayer()) |
| 73 | + ->setScope('system') |
| 74 | + ->setValue(0) |
| 75 | + ->setAllowChildOverride(true) |
| 76 | + ->setVisibleToChild(true)); |
| 77 | + |
| 78 | + $this->source |
| 79 | + ->method('loadGroupPolicies') |
| 80 | + ->with(DocMdpPolicy::KEY, $this->callback(static function ($context): bool { |
| 81 | + return $context->getUserId() === 'john' && $context->getGroups() === ['finance']; |
| 82 | + })) |
| 83 | + ->willReturn([(new PolicyLayer()) |
| 84 | + ->setScope('group') |
| 85 | + ->setValue(2) |
| 86 | + ->setAllowChildOverride(false) |
| 87 | + ->setVisibleToChild(true) |
| 88 | + ->setAllowedValues([2])]); |
| 89 | + |
| 90 | + $this->source->method('loadCirclePolicies')->willReturn([]); |
| 91 | + $this->source->method('loadUserPreference')->willReturn(null); |
| 92 | + $this->source->method('loadRequestOverride')->willReturn(null); |
| 93 | + |
| 94 | + $service = new PolicyService( |
| 95 | + $this->contextFactory, |
| 96 | + $this->source, |
| 97 | + $this->registry, |
| 98 | + ); |
| 99 | + |
| 100 | + $resolved = $service->resolveForUserId(DocMdpPolicy::KEY, 'john'); |
| 101 | + |
| 102 | + $this->assertSame(2, $resolved->getEffectiveValue()); |
| 103 | + $this->assertSame('group', $resolved->getSourceScope()); |
| 104 | + $this->assertFalse($resolved->canUseAsRequestOverride()); |
| 105 | + } |
| 106 | + |
49 | 107 | public function testResolveForUserIdBuildsContextWithGroupsAndRequestOverride(): void { |
50 | 108 | $user = $this->createMock(IUser::class); |
51 | 109 | $this->userManager |
|
0 commit comments