This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
/
AnyPermissionCheckTest.java
72 lines (54 loc) · 2.78 KB
/
AnyPermissionCheckTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package keywhiz.service.permission;
import java.util.Collections;
import java.util.List;
import keywhiz.service.permissions.Action;
import keywhiz.service.permissions.AnyPermissionCheck;
import keywhiz.service.permissions.PermissionCheck;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
public class AnyPermissionCheckTest {
@Rule public MockitoRule mockito = MockitoJUnit.rule();
@Mock private PermissionCheck delegate1;
@Mock private PermissionCheck delegate2;
private static Object source;
private static Object target;
@Test public void isAllowedReturnsFalseWithEmptyCheckList() {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(Collections.emptyList());
assertFalse(anyPermissionCheck.isAllowed(source, Action.ADD, target));
}
@Test public void isAllowedReturnsFalseWhenAllDelegatesReturnFalse() {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(createDelegatesList(false, false));
assertFalse(anyPermissionCheck.isAllowed(source, Action.ADD, target));
}
@Test public void isAllowedReturnsTrueWhenOneDelegateReturnsTrue() {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(createDelegatesList(true, false));
assertTrue(anyPermissionCheck.isAllowed(source, Action.ADD, target));
}
@Test public void checkAllowedOrThrowThrowsExceptionWithEmptyCheckList() {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(Collections.emptyList());
assertThatThrownBy(() -> anyPermissionCheck.checkAllowedOrThrow(source, Action.ADD, target))
.isInstanceOf(RuntimeException.class);
}
@Test public void checkAllowedOrThrowThrowsExceptionWhenAllDelegatesReturnFalse() {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(createDelegatesList(false, false));
assertThatThrownBy(() -> anyPermissionCheck.checkAllowedOrThrow(source, Action.ADD, target))
.isInstanceOf(RuntimeException.class);
}
@Test public void checkAllowedOrThrowReturnsVoidWhenOneDelegateReturnsTrue() {
PermissionCheck anyPermissionCheck = new AnyPermissionCheck(createDelegatesList(true, false));
anyPermissionCheck.checkAllowedOrThrow(source, Action.ADD, target);
}
private List<PermissionCheck> createDelegatesList(boolean delegate1Allowed, boolean delegate2Allowed) {
when(delegate1.isAllowed(any(), any(), any())).thenReturn(delegate1Allowed);
when(delegate2.isAllowed(any(), any(), any())).thenReturn(delegate2Allowed);
return List.of(delegate1, delegate2);
}
}