Repository navigation
Expand file tree
/
Copy pathClientOwnerScopeResolverTest.java
More file actions
158 lines (129 loc) · 6.36 KB
/
Copy pathClientOwnerScopeResolverTest.java
File metadata and controls
158 lines (129 loc) · 6.36 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.openframe.api.service.device;
import com.openframe.core.exception.ForbiddenException;
import com.openframe.data.document.device.Machine;
import com.openframe.data.model.ClientOwnerScope;
import com.openframe.data.model.ClientRequesterAccounts;
import com.openframe.data.repository.device.MachineRepository;
import com.openframe.security.authentication.ActorType;
import com.openframe.security.authentication.AuthPrincipal;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.support.StaticListableBeanFactory;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class ClientOwnerScopeResolverTest {
private static final AuthPrincipal AGENT = AuthPrincipal.builder().machineId("machine-1").actorType(ActorType.AGENT).build();
private static final AuthPrincipal AGENT_WITHOUT_MACHINE = AuthPrincipal.builder().id("agent").actorType(ActorType.AGENT).build();
private static final AuthPrincipal ADMIN = AuthPrincipal.builder().id("admin-1").actorType(ActorType.ADMIN).build();
private static final Instant ASSIGNED_AT = Instant.parse("2026-10-20T10:00:00Z");
private static final Set<String> LINKED_ACCOUNT_IDS = Set.of("account-1", "account-2");
@Mock private MachineRepository machineRepository;
@Mock private ObjectProvider<ClientRequesterAccounts> requesterAccountsProvider;
@Mock private ClientRequesterAccounts requesterAccounts;
@InjectMocks private ClientOwnerScopeResolver resolver;
@Test
void scopeOf_agent_carriesTheMachinesCurrentOrganization() {
// setup
Machine machine = new Machine();
machine.setMachineId("machine-1");
machine.setOrganizationId("org-b");
machine.setOrganizationAssignedAt(Instant.parse("2026-10-20T10:00:00Z"));
when(machineRepository.findByMachineId("machine-1")).thenReturn(Optional.of(machine));
// execution
ClientOwnerScope scope = resolver.scopeOf(AGENT);
// verifications
assertThat(scope).isEqualTo(new ClientOwnerScope("machine-1", "org-b", Instant.parse("2026-10-20T10:00:00Z")));
}
@Test
void scopeOf_agentOfUnknownMachine_scopeWithoutOrganization() {
// setup
when(machineRepository.findByMachineId("machine-1")).thenReturn(Optional.empty());
// execution
ClientOwnerScope scope = resolver.scopeOf(AGENT);
// verifications
assertThat(scope).isEqualTo(new ClientOwnerScope("machine-1", null, null));
}
// A null machine would match every owner without a machineId, i.e. every admin item.
@Test
void scopeOf_agentWithoutMachineId_forbidden() {
// execution + verifications
assertThatThrownBy(() -> resolver.scopeOf(AGENT_WITHOUT_MACHINE)).isInstanceOf(ForbiddenException.class);
verifyNoInteractions(machineRepository);
}
@Test
void isOwnerScoped_onlyForAgents() {
// execution + verifications
assertThat(resolver.isOwnerScoped(AGENT)).isTrue();
assertThat(resolver.isOwnerScoped(ADMIN)).isFalse();
verifyNoInteractions(machineRepository);
}
@Test
void forMachine_knownMachine_scopeCarriesItsCurrentOrganization() {
// setup
Machine machine = new Machine();
machine.setMachineId("machine-1");
machine.setOrganizationId("org-b");
when(machineRepository.findByMachineId("machine-1")).thenReturn(Optional.of(machine));
// execution
ClientOwnerScope scope = resolver.forMachine("machine-1");
// verifications
assertThat(scope).isEqualTo(new ClientOwnerScope("machine-1", "org-b", null));
}
@Test
void forMachine_blankMachineId_rejected() {
// execution + verifications
assertThatThrownBy(() -> resolver.forMachine(" ")).isInstanceOf(IllegalArgumentException.class);
verifyNoInteractions(machineRepository);
}
@Test
void scopeOf_withSpi_fillsAccountIdsFromTheMachine() {
// setup
Machine machine = machineUnderOrgB();
when(machineRepository.findByMachineId("machine-1")).thenReturn(Optional.of(machine));
when(requesterAccountsProvider.getIfAvailable()).thenReturn(requesterAccounts);
when(requesterAccounts.accountIdsOf(machine)).thenReturn(LINKED_ACCOUNT_IDS);
// execution
ClientOwnerScope scope = resolver.scopeOf(AGENT);
// verifications
assertThat(scope).isEqualTo(new ClientOwnerScope("machine-1", "org-b", ASSIGNED_AT, LINKED_ACCOUNT_IDS));
}
@Test
void scopeOf_withoutSpi_emptyAccountIds() {
// setup
ObjectProvider<ClientRequesterAccounts> noSpi = new StaticListableBeanFactory().getBeanProvider(ClientRequesterAccounts.class);
ClientOwnerScopeResolver resolverWithoutSpi = new ClientOwnerScopeResolver(machineRepository, noSpi);
when(machineRepository.findByMachineId("machine-1")).thenReturn(Optional.of(machineUnderOrgB()));
// execution
ClientOwnerScope scope = resolverWithoutSpi.scopeOf(AGENT);
// verifications
assertThat(scope).isEqualTo(new ClientOwnerScope("machine-1", "org-b", ASSIGNED_AT, Set.of()));
}
@Test
void forMachine_unknownMachine_asksSpiWithoutMachine() {
// setup
when(machineRepository.findByMachineId("machine-1")).thenReturn(Optional.empty());
when(requesterAccountsProvider.getIfAvailable()).thenReturn(requesterAccounts);
when(requesterAccounts.accountIdsOf(null)).thenReturn(Set.of("account-1"));
// execution
ClientOwnerScope scope = resolver.forMachine("machine-1");
// verifications
assertThat(scope).isEqualTo(new ClientOwnerScope("machine-1", null, null, Set.of("account-1")));
}
private static Machine machineUnderOrgB() {
Machine machine = new Machine();
machine.setMachineId("machine-1");
machine.setOrganizationId("org-b");
machine.setOrganizationAssignedAt(ASSIGNED_AT);
return machine;
}
}