Skip to content

test: cover the auth paths the design decisions depend on - #1

Open
NidaKarim wants to merge 3 commits into
mainfrom
test/auth-coverage-gaps
Open

NidaKarim wants to merge 3 commits into
mainfrom
test/auth-coverage-gaps

Conversation

@NidaKarim

Copy link
Copy Markdown
Owner

Found while reviewing coverage on the auth layer after submission. No production code changes — tests only.

The gap

JwtStrategy had 0% unit coverage, and more importantly the behaviour that its design exists for was untested anywhere:

validate() re-reads the user from the database rather than trusting the token claims, so a deleted or demoted user loses access on the next request rather than whenever their token happens to expire.

That is the main argument for accepting a per-request database read — and nothing demonstrated it.

What's added

Unit — src/auth/strategies/jwt.strategy.spec.ts

  • looks the user up by the token subject
  • returns the shape that becomes request.user, without passwordHash
  • trusts the database role over the token claim
  • rejects a token whose user no longer exists

E2E — test/services.e2e-spec.ts

  • GET /auth/me returns the caller (endpoint previously had no coverage)
  • a still-unexpired token whose user has been deleted is rejected
  • a role change takes effect without re-issuing the token

That last one logs in as a viewer, gets 403 on a write, promotes the user to admin in the database, then reuses the same token — which still carries "role":"viewer" — and gets 201.

Coverage

File Before After
jwt.strategy.ts 0% 100%
auth layer overall 36% 49%

jwt-auth.guard.ts remains at 0% unit coverage. Its @Public() branch is exercised end to end by every login, so I left it rather than write a test that only restates the guard's implementation.

Suites

45 unit (was 41) · 59 e2e (was 55). Lint clean.

Nida Ali added 3 commits August 28, 2026 01:48
JwtStrategy had no unit coverage, and the behaviour the database
re-read exists for -- a deleted or demoted user losing access on the
next request rather than at token expiry -- was untested end to end.

- unit: JwtStrategy.validate lookup, returned shape, database role
  winning over the token claim, and rejection when the user is gone
- e2e: GET /auth/me, a still-unexpired token whose user was deleted,
  and a role change taking effect without re-issuing the token
The role-change test relied on a comment to establish that the token
still said "viewer". Decode and assert it, so a 201 can only mean the
guard used the freshly-read database role.
The create conflict tests passed a payload with no versions, so the
23505 they mocked could not have occurred for that input -- service
names carry no unique constraint. The payload now repeats a version
name, which is the only unique constraint a create can trip, and the
message is asserted so it stays pointed at versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant