-
Notifications
You must be signed in to change notification settings - Fork 326
fix(auth): gate activity notes and last-scan data server-side #2807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
109d028
fix(auth): gate activity notes and last-scan data server-side
3b8ab9a
fix(auth): resolve the entity before the note gate
beba8f9
Merge branch 'main' into fix/activity-and-scan-authz
DonKoko 89f7577
Merge remote-tracking branch 'origin/main' into fix/activity-and-scan…
b23151d
test(auth): move route tests out of app/routes and fix their spelling
7dc17ea
Merge remote-tracking branch 'origin/fix/activity-and-scan-authz' int…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
apps/webapp/app/modules/scan/last-scan-for-viewer.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // @vitest-environment node | ||
| /** | ||
| * Authorization tests for `getLastScanForViewer`. | ||
| * | ||
| * The asset overview loader used to fetch and return the parsed last scan | ||
| * unconditionally, and the component hid `<ScanDetails>` behind a client-side | ||
| * `scan:read` check. The parsed payload carries the scanner's display name and | ||
| * EMAIL, the scan's GPS COORDINATES and the device user-agent, so for BASE and | ||
| * SELF_SERVICE (both `scan: []`) that was PII sitting in the page payload, | ||
| * hidden only by React. | ||
| * | ||
| * These tests drive the real `Role2PermissionMap` through the real | ||
| * `hasPermission`, so they assert the actual matrix rather than a restatement | ||
| * of it. Only the DB read is mocked. | ||
| * | ||
| * @see {@link file://./service.server.ts} | ||
| */ | ||
| import { OrganizationRoles } from "@prisma/client"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const { scanFindFirst } = vi.hoisted(() => ({ scanFindFirst: vi.fn() })); | ||
|
|
||
| // why: the only external dependency. `hasPermission` and `parseScanData` run | ||
| // for real so the gate is tested end to end against the live matrix. | ||
| vi.mock("~/database/db.server", () => ({ | ||
| db: { scan: { findFirst: scanFindFirst } }, | ||
| })); | ||
|
|
||
| import { getLastScanForViewer } from "./service.server"; | ||
|
|
||
| /** A scan row shaped as `getScanByQrId` returns it, carrying real PII. */ | ||
| const scanRow = { | ||
| id: "scan-1", | ||
| userId: "user-9", | ||
| latitude: "52.3676", | ||
| longitude: "4.9041", | ||
| userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)", | ||
| manuallyGenerated: false, | ||
| createdAt: new Date("2026-07-01T10:00:00Z"), | ||
| qr: { id: "qr-1", organizationId: "org-1" }, | ||
| user: { | ||
| id: "user-9", | ||
| firstName: "Dana", | ||
| lastName: "Reeves", | ||
| displayName: "Dana Reeves", | ||
| email: "dana@example.com", | ||
| userOrganizations: [{ organizationId: "org-1" }], | ||
| }, | ||
| }; | ||
|
|
||
| function args(roles: OrganizationRoles[]) { | ||
| return { | ||
| qrId: "qr-1", | ||
| userId: "user-1", | ||
| organizationId: "org-1", | ||
| roles, | ||
| }; | ||
| } | ||
|
|
||
| describe("getLastScanForViewer", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| scanFindFirst.mockResolvedValue(scanRow); | ||
| }); | ||
|
|
||
| it.each([OrganizationRoles.BASE, OrganizationRoles.SELF_SERVICE])( | ||
| "returns null for %s and never reads the scan row", | ||
| async (role) => { | ||
| const result = await getLastScanForViewer(args([role])); | ||
|
|
||
| expect(result).toBeNull(); | ||
| // Not merely omitted from the response — never fetched at all. | ||
| expect(scanFindFirst).not.toHaveBeenCalled(); | ||
| } | ||
| ); | ||
|
|
||
| it.each([OrganizationRoles.ADMIN, OrganizationRoles.OWNER])( | ||
| "returns the parsed scan for %s", | ||
| async (role) => { | ||
| const result = await getLastScanForViewer(args([role])); | ||
|
|
||
| expect(scanFindFirst).toHaveBeenCalledTimes(1); | ||
| expect(result).toEqual( | ||
| expect.objectContaining({ | ||
| scannedBy: "Dana Reeves(dana@example.com)", | ||
| coordinates: "52.3676, 4.9041", | ||
| }) | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| it("returns null without touching the DB when the asset has no QR code", async () => { | ||
| const result = await getLastScanForViewer({ | ||
| ...args([OrganizationRoles.OWNER]), | ||
| qrId: undefined, | ||
| }); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(scanFindFirst).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns null when the asset has a QR but has never been scanned", async () => { | ||
| scanFindFirst.mockResolvedValue(null); | ||
|
|
||
| const result = await getLastScanForViewer(args([OrganizationRoles.OWNER])); | ||
|
|
||
| // Same shape an unauthorized viewer gets, so callers need no special case. | ||
| expect(result).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.