fix(qr): inactive answer for malformed codes, undated events, switched-off badge, reset-pending sessions
A code of the wrong shape now shows "no longer active" instead of failing, My QR codes skips events with no end date and marks switched-off codes, and a session waiting on a password reset can still open a QR page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
c6c220265f
commit
d532bdcda4
10 files changed
+59
-6
No files matched your search
@@ -493,6 +493,7 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
|
||||
QrLoadFailed: "Could not load registration codes.",
|
||||
QrSaveFailed: "Could not change the code.",
|
||||
QrMyTitle: "My QR codes",
|
||||
QrSwitchedOff: "Switched off",
|
||||
QrMyNone: "You have no registration codes yet.",
|
||||
PrivNotice: "Privacy notice",
|
||||
PrivSave: "Save notice",
|
||||
|
||||
@@ -25,7 +25,7 @@ export function guardDecision(pathname: string, session: SessionState | null): s
|
||||
return isPublicPath(pathname) ? null : "/login";
|
||||
}
|
||||
// A pending reset confines the session to the set-password form (/login stays reachable).
|
||||
if (session?.resetRequired && pathname !== "/reset-password" && pathname !== "/login") {
|
||||
if (session?.resetRequired && !/^\/r\/[^/]+$/.test(pathname) && pathname !== "/reset-password" && pathname !== "/login") {
|
||||
return "/reset-password";
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -26,6 +26,14 @@ describe("My QR codes", () => {
|
||||
expect(screen.getByRole("button", { name: "Download PNG" })).toBeTruthy();
|
||||
});
|
||||
|
||||
it("marks a switched-off code", async () => {
|
||||
mocks.getMyLinks.mockResolvedValue([
|
||||
{ eventUuid: "u1", eventName: "Expo 2026", startDate: "2026-12-01", endDate: "2026-12-02", location: "KLCC", token: "t1", enabled: false },
|
||||
]);
|
||||
render(<MyQrPage />);
|
||||
await screen.findByText("Switched off");
|
||||
});
|
||||
|
||||
it("shows an empty state", async () => {
|
||||
mocks.getMyLinks.mockResolvedValue([]);
|
||||
render(<MyQrPage />);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { PageHeader } from "@/app/components/ui/page-header";
|
||||
import { Badge } from "@/app/components/ui/badge";
|
||||
import { EmptyState, ErrorState } from "@/app/components/ui/states";
|
||||
import { QrCodeItem } from "@/app/components/registration/QrCodeItem";
|
||||
import { getMyLinks, type MyLink } from "@/app/lib/services/adminRegistration";
|
||||
@@ -28,6 +29,7 @@ export default function MyQrPage() {
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{l.startDate} - {l.endDate} | {l.location}
|
||||
</p>
|
||||
{!l.enabled && <Badge variant="warning">{t("QrSwitchedOff")}</Badge>}
|
||||
<QrCodeItem label={l.eventName} eventName={l.eventName} token={l.token} enabled={l.enabled} />
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { guardDecision } from "./app/lib/routeGuard";
|
||||
import { middleware } from "./middleware";
|
||||
|
||||
// add-qr-self-registration: the QR landing page and its proxy are reachable signed out; admin paths still are not.
|
||||
@@ -24,3 +25,12 @@ describe("middleware: public registration paths", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("routeGuard: /r/<token> with a pending password reset", () => {
|
||||
it("stays reachable; admin paths still go to /reset-password", () => {
|
||||
const s = { present: true, resetRequired: true };
|
||||
expect(guardDecision("/r/abc123", s)).toBeNull();
|
||||
expect(guardDecision("/events", s)).toBe("/reset-password");
|
||||
expect(guardDecision("/r", s)).toBe("/reset-password");
|
||||
});
|
||||
});
|
||||
+4
@@ -138,7 +138,11 @@ public class PublicRegistrationService {
|
||||
}
|
||||
}
|
||||
|
||||
/** The gateway lets only this token shape through unsigned; anything else would come back 401 and surface as a 500. */
|
||||
private static final java.util.regex.Pattern TOKEN_SHAPE = java.util.regex.Pattern.compile("[A-Za-z0-9_-]{22}");
|
||||
|
||||
private Open open(String token) {
|
||||
if (token == null || !TOKEN_SHAPE.matcher(token).matches()) throw inactive();
|
||||
Map<String, Object> link = found(() -> rest.getForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_REGISTRATION_LINK_FIND_BY_TOKEN), MAP, token).getBody());
|
||||
if (link == null || !Boolean.TRUE.equals(link.get("enabled")) || !(link.get("eventUuid") instanceof Number uuid)) throw inactive();
|
||||
CompanyEvent e = found(() -> rest.getForEntity(url(RestApiCounterServiceConstants.API_REST_COUNTER_EVENT_FIND_BY_UUID), EVENT, uuid.longValue()).getBody());
|
||||
|
||||
+1
-1
@@ -43,7 +43,7 @@ class PublicRegistrationSalespersonTest {
|
||||
private static final String FIND_BY_ID = "/user/findById/{id}";
|
||||
private static final long EV = 3312726793093644288L;
|
||||
private static final long COMPANY = 5L;
|
||||
private static final String TOKEN = "abc123";
|
||||
private static final String TOKEN = "abcDEF123_-abcDEF123_-";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
+11
-1
@@ -36,6 +36,7 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
@@ -51,7 +52,7 @@ class PublicRegistrationServiceTest {
|
||||
private static final String REGISTER = "/counter/registration/register";
|
||||
private static final long EV = 3312726793093644288L;
|
||||
private static final long COMPANY = 5L;
|
||||
private static final String TOKEN = "abc123";
|
||||
private static final String TOKEN = "abcDEF123_-abcDEF123_-";
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private RedisTemplate<String, Object> redis;
|
||||
@@ -142,6 +143,15 @@ class PublicRegistrationServiceTest {
|
||||
assertInactive(() -> service.page("nope"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void page_tokenShapeTheGatewayRefuses_inactive_withoutBackendCall() {
|
||||
for (String bad : List.of("x", "short-link", TOKEN.substring(1), TOKEN + "A", TOKEN.substring(1) + "!")) {
|
||||
assertInactive(() -> service.page(bad));
|
||||
assertInactive(() -> service.register(bad, ok()));
|
||||
}
|
||||
verifyNoInteractions(rest);
|
||||
}
|
||||
|
||||
@Test
|
||||
void page_switchedOffLink_inactive() {
|
||||
stubLink(false, 21L);
|
||||
|
||||
+3
-3
@@ -77,11 +77,11 @@ public class RegistrationLinkService {
|
||||
.map(EventSalesperson::getEventUuid).toList();
|
||||
if (uuids.isEmpty()) return List.of();
|
||||
return eventRepository.findByCompanyIdAndUuidIn(req.companyId(), uuids).stream()
|
||||
.filter(e -> e.getEnabled() == GeneralEnum.FlagYesNo.Y && !e.getDateEnd().toLocalDate().isBefore(today))
|
||||
.sorted(Comparator.comparing(Event::getDateStart))
|
||||
.filter(e -> e.getEnabled() == GeneralEnum.FlagYesNo.Y && e.getDateEnd() != null && !e.getDateEnd().toLocalDate().isBefore(today))
|
||||
.sorted(Comparator.comparing(Event::getDateStart, Comparator.nullsLast(Comparator.naturalOrder())))
|
||||
.map(e -> {
|
||||
RegistrationLink l = personal(req.companyId(), e.getUuid(), req.userId());
|
||||
return new MineRow(String.valueOf(e.getUuid()), e.getName(), e.getDateStart().toLocalDate().toString(),
|
||||
return new MineRow(String.valueOf(e.getUuid()), e.getName(), e.getDateStart() == null ? null : e.getDateStart().toLocalDate().toString(),
|
||||
e.getDateEnd().toLocalDate().toString(), e.getLocation(), l.getToken(), l.isEnabled());
|
||||
}).toList();
|
||||
}
|
||||
|
||||
+18
@@ -213,6 +213,24 @@ class RegistrationLinkServiceTest {
|
||||
verify(linkRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mineSkipsUndatedEndAndSortsUndatedStartLast() {
|
||||
EventSalesperson r1 = new EventSalesperson(); r1.setEventUuid(1L);
|
||||
EventSalesperson r2 = new EventSalesperson(); r2.setEventUuid(2L);
|
||||
EventSalesperson r3 = new EventSalesperson(); r3.setEventUuid(3L);
|
||||
when(eventSalespersonRepository.findByCompanyIdAndUserId(COMPANY, 7L)).thenReturn(List.of(r1, r2, r3));
|
||||
Event noStart = event(1L, COMPANY, "NoStart", null, LocalDateTime.of(2026, 12, 12, 18, 0), GeneralEnum.FlagYesNo.Y);
|
||||
Event noEnd = event(2L, COMPANY, "NoEnd", LocalDateTime.of(2026, 12, 3, 9, 0), null, GeneralEnum.FlagYesNo.Y);
|
||||
Event dated = event(3L, COMPANY, "Dated", LocalDateTime.of(2026, 12, 5, 9, 0), LocalDateTime.of(2026, 12, 6, 9, 0), GeneralEnum.FlagYesNo.Y);
|
||||
when(eventRepository.findByCompanyIdAndUuidIn(eq(COMPANY), anyCollection())).thenReturn(List.of(noStart, noEnd, dated));
|
||||
when(linkRepository.findFirstByEventUuidAndSalespersonUserIdOrderByIdAsc(anyLong(), eq(7L))).thenReturn(Optional.of(link(7L, "T", true)));
|
||||
|
||||
List<RegistrationLinkService.MineRow> out = service.mine(new RegistrationLinkService.MineRequest(COMPANY, 7L, "2026-12-01"));
|
||||
|
||||
assertThat(out).extracting(RegistrationLinkService.MineRow::eventName).containsExactly("Dated", "NoStart");
|
||||
assertThat(out.get(1).startDate()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void mineListsOnlyEnabledNotEndedTeamEventsOrderedByStartAndCreatesMissingLinks() {
|
||||
EventSalesperson r1 = new EventSalesperson(); r1.setEventUuid(1L);
|
||||
|
||||
Reference in new issue
Block a user