Skip to content

Commit 91397fe

Browse files
committed
test(policy): align service expectations with strict typed values
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 0359265 commit 91397fe

1 file changed

Lines changed: 60 additions & 2 deletions

File tree

tests/php/Unit/Service/Policy/PolicyServiceTest.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\Libresign\Service\Policy\Model\PolicyLayer;
1212
use OCA\Libresign\Service\Policy\Model\ResolvedPolicy;
1313
use OCA\Libresign\Service\Policy\PolicyService;
14+
use OCA\Libresign\Service\Policy\Provider\DocMdp\DocMdpPolicy;
1415
use OCA\Libresign\Service\Policy\Provider\Signature\SignatureFlowPolicy;
1516
use OCA\Libresign\Service\Policy\Runtime\PolicyContextFactory;
1617
use OCA\Libresign\Service\Policy\Runtime\PolicyRegistry;
@@ -40,12 +41,69 @@ protected function setUp(): void {
4041
$container = $this->createMock(ContainerInterface::class);
4142
$container
4243
->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+
});
4551
$this->registry = new PolicyRegistry($container);
4652
$this->contextFactory = new PolicyContextFactory($this->userManager, $this->groupManager, $this->userSession);
4753
}
4854

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+
49107
public function testResolveForUserIdBuildsContextWithGroupsAndRequestOverride(): void {
50108
$user = $this->createMock(IUser::class);
51109
$this->userManager

0 commit comments

Comments
 (0)