Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
5805562
chore: CU-17tkuw66na0 stubs for failing spec tests
andriikachur Dec 3, 2026
6c42c44
test: CU-17tkuw66na0 failing spec tests
andriikachur Dec 3, 2026
08c38f5
test: CU-17tkuw66na0 align tests with approved spec decisions
andriikachur Dec 3, 2026
00c86fd
feat: CU-17tkuw66na0 email tickets of linked requesters visible to th…
andriikachur Dec 3, 2026
3fdfa16
fix: CU-17tkuw66na0 drop unused findByTicketNumberAndOwnerMachineId
andriikachur Dec 3, 2026
c8a3d3a
fix: CU-17tkuw66na0 extract call arguments and named predicates
andriikachur Dec 3, 2026
d8c982f
Merge remote-tracking branch 'origin/main' into feature/CU-17tkuw66na0
andriikachur Dec 4, 2026
2ded54b
Merge remote-tracking branch 'origin/main' into feature/CU-17tkuw66na0
andriikachur Dec 4, 2026
4a6093c
test: CU-17tkuw66na0 device visibility kept when search and activity …
andriikachur Dec 4, 2026
424403d
fix: CU-17tkuw66na0 combine the device visibility union with the sear…
andriikachur Dec 4, 2026
a5f3ba3
test: CU-17tkuw66na0 device owner scope built from the machine's stor…
andriikachur Dec 4, 2026
0863099
refactor: CU-17tkuw66na0 owner scope from the machine's stored direct…
andriikachur Dec 4, 2026
c0b69d5
test: CU-17tkuw66na0 requester index also covers the creation source
andriikachur Dec 4, 2026
266ca40
perf: CU-17tkuw66na0 requester index covers the creation source of em…
andriikachur Dec 4, 2026
42dc4a6
Merge remote-tracking branch 'origin/main' into feature/CU-17tkuw66na0
andriikachur Dec 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
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.AuthPrincipal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.stereotype.Component;

import java.util.Set;

import static com.openframe.api.util.AuthPrincipalUtils.isAgent;
import static org.springframework.util.StringUtils.hasText;

Expand All @@ -21,6 +25,7 @@
public class ClientOwnerScopeResolver {

private final MachineRepository machineRepository;
private final ObjectProvider<ClientRequesterAccounts> requesterAccounts;

// Only AGENT tokens are owner-scoped; admins read everything.
public boolean isOwnerScoped(AuthPrincipal principal) {
Expand All @@ -44,8 +49,13 @@ public ClientOwnerScope forMachine(String machineId) {
Machine machine = machineRepository.findByMachineId(machineId).orElse(null);
if (machine == null || machine.getOrganizationId() == null) {
log.debug("No current organization for machine {}; owner scope falls back to machine only", machineId);
return new ClientOwnerScope(machineId, null, null);
}
return new ClientOwnerScope(machineId, machine.getOrganizationId(), machine.getOrganizationAssignedAt());
Set<String> accountIds = requesterAccountIdsOf(machine);
return ClientOwnerScope.of(machineId, machine, accountIds);
}

private Set<String> requesterAccountIdsOf(Machine machine) {
ClientRequesterAccounts accounts = requesterAccounts.getIfAvailable();
return accounts == null ? Set.of() : accounts.accountIdsOf(machine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ public class TicketService {
private final AuditedMethodRunner auditedMethodRunner;
private final AuditRecorder auditRecorder;

/**
* Cursor-paginated ticket listing. Cursors are raw ticket ids. AGENT principals only see the
* tickets their machine owns under its current organization ({@link ClientOwnerScope}).
*/
public CountedGenericQueryResult<Ticket> getTickets(AuthPrincipal principal,
TicketFilterInput filter,
CursorPaginationCriteria pagination,
Expand Down Expand Up @@ -186,8 +182,7 @@ public Optional<Ticket> getTicket(AuthPrincipal principal, @NotBlank String tick
return ticketRepository.findById(ticketId);
}
ClientOwnerScope scope = clientOwnerScopeResolver.scopeOf(principal);
return ticketRepository.findByIdAndOwnerMachineId(ticketId, scope.getMachineId())
Comment thread
andriikachur marked this conversation as resolved.
Resolved
.filter(ticket -> scope.allows(ticket.getOwner(), ticket.getCreatedAt()));
return ticketRepository.findById(ticketId).filter(scope::allows);
}

public Optional<Ticket> getTicketByNumber(AuthPrincipal principal, Integer ticketNumber) {
Expand All @@ -197,8 +192,7 @@ public Optional<Ticket> getTicketByNumber(AuthPrincipal principal, Integer ticke
return ticketRepository.findByTicketNumber(ticketNumber);
}
ClientOwnerScope scope = clientOwnerScopeResolver.scopeOf(principal);
return ticketRepository.findByTicketNumberAndOwnerMachineId(ticketNumber, scope.getMachineId())
.filter(ticket -> scope.allows(ticket.getOwner(), ticket.getCreatedAt()));
return ticketRepository.findByTicketNumber(ticketNumber).filter(scope::allows);
}

public Optional<Ticket> findByIdOrNumber(AuthPrincipal principal, String ticketId, Integer ticketNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -11,9 +12,12 @@
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;
Expand All @@ -26,8 +30,12 @@ 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;

Expand Down Expand Up @@ -96,4 +104,55 @@ void forMachine_blankMachineId_rejected() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import com.openframe.api.service.device.ClientOwnerScopeResolver;
import com.openframe.data.document.device.Machine;
import com.openframe.data.document.organization.Organization;
import com.openframe.data.document.ticket.AdminTicketOwner;
import com.openframe.data.document.ticket.ClientTicketOwner;
import com.openframe.data.document.ticket.EmailTicketOwner;
import com.openframe.data.document.ticket.Ticket;
import com.openframe.data.document.ticket.TicketCreationSource;
import com.openframe.data.document.ticket.TicketRequester;
import com.openframe.data.document.ticket.TicketRequesterMatch;
import com.openframe.data.model.ClientOwnerScope;
import com.openframe.data.repository.device.MachineRepository;
import com.openframe.data.repository.organization.OrganizationRepository;
Expand All @@ -21,6 +26,7 @@

import java.time.Instant;
import java.util.Optional;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -38,6 +44,8 @@ class TicketServiceClientOwnerScopeTest {
private static final AuthPrincipal AGENT = AuthPrincipal.builder().machineId("machine-1").actorType(ActorType.AGENT).build();
private static final AuthPrincipal ADMIN = AuthPrincipal.builder().id("admin-1").actorType(ActorType.ADMIN).build();
private static final ClientOwnerScope UNDER_ORG_B = new ClientOwnerScope("machine-1", "org-b", null);
private static final ClientOwnerScope LINKED_UNDER_ORG_B =
new ClientOwnerScope("machine-1", "org-b", null, Set.of("account-1"));

@Mock private TicketRepository ticketRepository;
@Mock private TicketNumberService ticketNumberService;
Expand All @@ -60,7 +68,7 @@ void getTicket_agentAndTicketCreatedUnderCurrentOrganization_found() {
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(UNDER_ORG_B);
Ticket ticket = ownedBy("machine-1", "org-b");
when(ticketRepository.findByIdAndOwnerMachineId("t-1", "machine-1")).thenReturn(Optional.of(ticket));
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(ticket));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");
Expand All @@ -74,7 +82,7 @@ void getTicket_agentAndTicketCreatedUnderPreviousOrganization_notFound() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(UNDER_ORG_B);
when(ticketRepository.findByIdAndOwnerMachineId("t-1", "machine-1"))
when(ticketRepository.findById("t-1"))
.thenReturn(Optional.of(ownedBy("machine-1", "org-a")));

// execution
Expand All @@ -90,7 +98,7 @@ void getTicket_agentAndTicketWithoutSnapshot_stillFound() {
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(UNDER_ORG_B);
Ticket legacy = ownedBy("machine-1", null);
when(ticketRepository.findByIdAndOwnerMachineId("t-1", "machine-1")).thenReturn(Optional.of(legacy));
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(legacy));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");
Expand All @@ -107,7 +115,7 @@ void getTicket_agentAndTicketFromBeforeTheCurrentEnrollment_notFound() {
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(new ClientOwnerScope("machine-1", "org-b", assignedAt));
Ticket older = ownedBy("machine-1", "org-b");
older.setCreatedAt(assignedAt.minusSeconds(1));
when(ticketRepository.findByIdAndOwnerMachineId("t-1", "machine-1")).thenReturn(Optional.of(older));
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(older));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");
Expand All @@ -124,7 +132,7 @@ void getTicketByNumber_agentAndTicketFromBeforeTheCurrentEnrollment_notFound() {
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(new ClientOwnerScope("machine-1", "org-b", assignedAt));
Ticket older = ownedBy("machine-1", "org-b");
older.setCreatedAt(assignedAt.minusSeconds(1));
when(ticketRepository.findByTicketNumberAndOwnerMachineId(7, "machine-1")).thenReturn(Optional.of(older));
when(ticketRepository.findByTicketNumber(7)).thenReturn(Optional.of(older));

// execution
Optional<Ticket> found = service.getTicketByNumber(AGENT, 7);
Expand All @@ -138,7 +146,7 @@ void getTicketByNumber_agentAndTicketCreatedUnderPreviousOrganization_notFound()
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(UNDER_ORG_B);
when(ticketRepository.findByTicketNumberAndOwnerMachineId(42, "machine-1"))
when(ticketRepository.findByTicketNumber(42))
.thenReturn(Optional.of(ownedBy("machine-1", "org-a")));

// execution
Expand Down Expand Up @@ -187,6 +195,125 @@ void createTicketFromDialog_agent_ownerSnapshotsTheMachinesCurrentOrganization()
verify(ticketRepository).save(created);
}

@Test
void getTicket_agentVisibleEmailTicket_present() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
Ticket ticket = emailTicketOf("account-1");
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(ticket));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");

// verifications
assertThat(found).contains(ticket);
}

@Test
void getTicket_agentInvisibleEmailTicket_empty() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(emailTicketOf("account-2")));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");

// verifications
assertThat(found).isEmpty();
verify(ticketRepository).findById("t-1");
}

@Test
void getTicketByNumber_agentVisibleEmailTicket_present() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
Ticket ticket = emailTicketOf("account-1");
when(ticketRepository.findByTicketNumber(42)).thenReturn(Optional.of(ticket));

// execution
Optional<Ticket> found = service.getTicketByNumber(AGENT, 42);

// verifications
assertThat(found).contains(ticket);
}

@Test
void getTicket_agentAndOtherMachinesTicket_notFound() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(ownedBy("machine-2", "org-b")));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");

// verifications
assertThat(found).isEmpty();
}

@Test
void getTicketByNumber_agentAndOtherMachinesTicket_notFound() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
when(ticketRepository.findByTicketNumber(42)).thenReturn(Optional.of(ownedBy("machine-2", "org-b")));

// execution
Optional<Ticket> found = service.getTicketByNumber(AGENT, 42);

// verifications
assertThat(found).isEmpty();
}

@Test
void getTicket_agentAndAdminOwnedTicket_notFound() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
Ticket adminTicket = Ticket.builder().id("t-1").owner(new AdminTicketOwner("admin-1")).build();
when(ticketRepository.findById("t-1")).thenReturn(Optional.of(adminTicket));

// execution
Optional<Ticket> found = service.getTicket(AGENT, "t-1");

// verifications
assertThat(found).isEmpty();
}

@Test
void getTicketByNumber_agentInvisibleEmailTicket_empty() {
// setup
when(clientOwnerScopeResolver.isOwnerScoped(AGENT)).thenReturn(true);
when(clientOwnerScopeResolver.scopeOf(AGENT)).thenReturn(LINKED_UNDER_ORG_B);
when(ticketRepository.findByTicketNumber(42)).thenReturn(Optional.of(emailTicketOf("account-2")));

// execution
Optional<Ticket> found = service.getTicketByNumber(AGENT, 42);

// verifications
assertThat(found).isEmpty();
verify(ticketRepository).findByTicketNumber(42);
}

private static Ticket emailTicketOf(String accountId) {
TicketRequester requester = TicketRequester.builder()
.accountId(accountId)
.matchedBy(TicketRequesterMatch.EMAIL)
.matchedAddress("ann@acme.com")
.build();
return Ticket.builder()
.id("t-1")
.ticketNumber(42)
.creationSource(TicketCreationSource.EMAIL)
.owner(new EmailTicketOwner("ann@acme.com"))
.requester(requester)
.organizationId("org-a")
.build();
}

private static Ticket ownedBy(String machineId, String organizationId) {
return Ticket.builder().id("t-1").owner(new ClientTicketOwner(machineId, organizationId)).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
@CompoundIndex(name = "tenant_creation_source", def = "{'tenantId': 1, 'creationSource': 1}"),
// Rule 3 of the inbound email pipeline's Match: the email tickets of a sender with a normalised subject.
@CompoundIndex(name = "tenant_email_sender_subject",
def = "{'tenantId': 1, 'emailOrigin.senderAddress': 1, 'emailOrigin.subjectKey': 1}")
def = "{'tenantId': 1, 'emailOrigin.senderAddress': 1, 'emailOrigin.subjectKey': 1}"),
@CompoundIndex(name = "tenant_requester_account_source",
def = "{'tenantId': 1, 'requester.accountId': 1, 'creationSource': 1}")
})
public class Ticket implements TenantScoped {
@Id
Expand Down
Loading
Loading