Skip to content

test: add authentication library tests#148

Merged
jakebromberg merged 5 commits intomainfrom
test/03-auth-lib
Mar 18, 2026
Merged

test: add authentication library tests#148
jakebromberg merged 5 commits intomainfrom
test/03-auth-lib

Conversation

@jakebromberg
Copy link
Copy Markdown
Member

@jakebromberg jakebromberg commented Feb 5, 2026

Closes #248

Summary

  • Add tests for auth client configuration and getJWTToken / getBaseURL behavior
  • Add tests for server-client configuration and header-based URL construction
  • Add focused test files for server-side auth: session management, RBAC, user completeness, and user mapping
  • Add tests for auth type guards (isAuthenticated, isIncomplete, isPasswordReset) and mapRoleToAuthorization
  • Add tests for auth utilities (betterAuthSessionToAuthenticationData, toUserFromBetterAuthJWT)
  • Add tests for session management (authentication slice)
  • Add centralized BetterAuth session fixtures to test-utils

Non-test changes

  • Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames) from types.ts
  • Remove dead Cognito import from authenticationHooks.ts
  • Delete obsolete api.test.ts (source file was already removed)

Follow-up

  • Verify-email route testing tracked separately

Test plan

  • npx vitest run lib/__tests__/features/authentication/ lib/__tests__/features/session.test.ts -- 192 tests passing across 10 files

Part 3 of 26

Note: E2E tests depend on #271 and WXYC/Backend-Service#223. E2E will pass once those merge.

Note: Depends on #146 (test utilities) — both modify lib/test-utils/fixtures.ts. Merge #146 first.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Feb 5, 2026

Deploying wxyc-dj with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0b5c713
Status:🚫  Build failed.

View logs

@jakebromberg jakebromberg marked this pull request as ready for review February 6, 2026 00:55
Copy link
Copy Markdown
Collaborator

@JacksonMeade JacksonMeade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the tests here are focused, clean and nice. Others, I feel, are testing the better-auth library more than our own usage of it, and that is not our responsibility to test. There are also some nits about naming and file separation, and I wish the PR gave a more comprehensive overview of the changes made here, especially where files have been modified.


afterEach(() => {
process.env = originalEnv;
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by this environment switching - what does this do? It appears to re-serialize the environment into itself

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this was confusing. The process.env = { ...originalEnv } in beforeEach was unnecessary since afterEach already restores it. Removed in c4a99a3.

]),
})
);
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests appear to be testing better-auth's behavior, not our implementations of it. Are these tests necessary?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e., shouldn't we have tests that verify that the necessary plugins for the backend are present in the actual client that gets created, rather than one that gets mocked?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Replaced the mock-shape tests (authClient export checks, plugin factory call assertions, fetchOptions verification) with a single test that verifies the captured config has the expected structure: 3 plugins, credentials include, and a defined baseURL. The getJWTToken and getBaseURL tests stay since those exercise our actual logic.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The new single config test does exactly this - it captures the config object passed to createAuthClient and asserts on the plugin list, credentials, and baseURL. It verifies what we configure, not what better-auth does with it.

expect(capturedConfig?.baseURL).toBe("http://localhost:3001/auth");
});
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, these tests need to be reevaluated for whether they test the library or the code. Further, the most breakable part of our implementation with the server-client is the verify-email route, which requires special session forwarding due to the JWT + admin client disconnect on using baseUrl. So we should simplify the client tests to be better scoped to our usages of those clients.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Removed the mock-shape blocks (serverAuthClient export, plugin configuration, weak base URL configuration). Replaced with a single config verification test. Kept the getBaseURL header-based URL construction tests since those exercise our actual branching logic. Verify-email route testing is noted as a follow-up in the updated PR description.

expect(mockRedirect).not.toHaveBeenCalled();
});
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are, I think, the real meat of what needs to be tested in the role system. They don't so much test server utilities as role-based-access-control and good session carryover. They also rely on mocking, while I think we need to really look at redirect behavior. These tests are good, but should be better separated out into different files that describe their actual tested behavior.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on all counts. Split into four focused files: server-session.test.ts (getServerSession + requireAuth), rbac.test.ts (checkRole + requireRole), user-completeness.test.ts (isUserIncomplete + getIncompleteUserAttributes), and user-mapping.test.ts (getUserFromSession). The file names now describe the tested behavior rather than the source module.

const data: AuthenticatedUser = {
user: {
username: "testuser",
email: "test@example.com",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests should rely on the central repo of mock users rather than being on their own.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. All inline mock user objects replaced with createTestAuthenticatedUser(), createTestIncompleteUser(), and createTestPasswordResetUser() from test-utils.

])('should map "%s" to SM (full access)', (role, expected) => {
expect(mapRoleToAuthorization(role)).toBe(expected);
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clean

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

});

it("should map 'custom:dj-name' to 'djName'", () => {
expect(djAttributeNames["custom:dj-name"]).toBe("djName");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectation carries over from cognito and is no longer relevant. So I think this points to a need to perform a clean-up re:cognito-based code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the djAttributeNames and modifiableAttributeNames tests, and also removed those Cognito-era exports from types.ts itself along with the dead import in authenticationHooks.ts.

expect(result).toEqual({});
expect(result.authority).toBe(Authorization.NO);
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cleans up this code very nicely and tests real functionality. However, I wish the PR description had better informed me of these changes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. Updated the PR description to call out the non-test file changes (Cognito removal from types.ts and authenticationHooks.ts, api.test.ts deletion) and note verify-email route testing as a follow-up.

expect(runtime).toBe("edge");
});
});
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tests

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

jakebromberg pushed a commit that referenced this pull request Feb 11, 2026
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
jakebromberg pushed a commit that referenced this pull request Feb 11, 2026
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
jakebromberg pushed a commit that referenced this pull request Feb 26, 2026
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
jakebromberg pushed a commit that referenced this pull request Mar 2, 2026
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
jakebromberg pushed a commit that referenced this pull request Mar 9, 2026
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
Jake Bromberg added 5 commits March 18, 2026 14:37
- Add tests for auth client functionality
- Add tests for server-client auth
- Add tests for server utilities
- Add tests for auth types validation
- Add tests for auth utilities
- Add tests for session management
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
- Make checkRole/requireRole/getUserFromSession tests async (these
  functions use getUserAuthority which fetches from APP_ORGANIZATION)
- Add organization-utils mock to rbac and user-mapping tests
- Add organizationClient to server-client plugin mock
- Update plugin assertions to include organization plugin
- Simplify server-client getBaseURL tests to match main's env-only impl
- Fix createTestSessionWithRole to provide all required BetterAuthSession user fields instead of only { role }
- Fix session.test.ts: mock react cache function, mock organization-utils and authentication types, replace invalid runtime export test with module exports test
- Fix client.test.ts: add vi.resetModules() in beforeEach to prevent JWT token cache leaking between tests, update fetch-count test to clear cache between invocations
- Fix server-utils.ts: isUserIncomplete and getIncompleteUserAttributes now check both realName and djName (matching utilities.ts behavior and user-completeness.test.ts expectations)
- Update server-utils.test.ts to match corrected djName-checking behavior
@jakebromberg jakebromberg merged commit f542444 into main Mar 18, 2026
3 of 5 checks passed
jakebromberg pushed a commit that referenced this pull request Mar 18, 2026
Remove mock-shape tests that only verify mocks return what they were
told. Replace with single configuration verification tests using
captured config objects.

Split monolithic server-utils.test.ts (705 lines) into four focused
files: server-session, rbac, user-completeness, and user-mapping.

Remove Cognito-era exports (djAttributeNames, modifiableAttributeNames)
from types.ts and dead import from authenticationHooks.ts.

Centralize test data using BetterAuth session fixtures in test-utils.
Remove over-explaining comments throughout test files.
@jakebromberg jakebromberg deleted the test/03-auth-lib branch March 18, 2026 21:46
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.

Add tests for authentication library

2 participants