-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -734,11 +734,71 @@ describe('Auth0 Remix Server', () => { | |
describe('when the access token is valid', () => { | ||
beforeEach(() => { | ||
vi.mocked(getCredentials).mockResolvedValueOnce({ | ||
accessToken: 'test-access-token' | ||
accessToken: 'test-access-token', | ||
expiresAt: 66669420 | ||
} as never); | ||
vi.mocked(jose.jwtVerify).mockResolvedValue({} as never); | ||
}); | ||
|
||
describe('and there is a user cache in place', () => { | ||
beforeEach<LocalTestContext>((context) => { | ||
context.authOptions.profileCacheGet = vi.fn(); | ||
}); | ||
|
||
it<LocalTestContext>('returns the cached result', async ({ authOptions }) => { | ||
vi.mocked(authOptions.profileCacheGet!).mockResolvedValue({ | ||
email: '[email protected]' | ||
} as never); | ||
|
||
const request = new Request('https://it-doesnt-matter.com'); | ||
|
||
const authorizer = new Auth0RemixServer(authOptions); | ||
const actual = await authorizer.getUser(request, {}); | ||
|
||
expect(actual.email).toEqual('[email protected]'); | ||
expect(authOptions.profileCacheGet).toHaveBeenCalledWith('test-access-token'); | ||
}); | ||
|
||
describe('when the cache misses', () => { | ||
it<LocalTestContext>('calls out to auth0', async ({ authOptions }) => { | ||
authOptions.profileCacheSet = vi.fn(); | ||
vi.mocked(authOptions.profileCacheGet!).mockRejectedValueOnce(new Error('Cache miss')); | ||
|
||
authOptions.session = { | ||
store: 'sessionStore', | ||
key: 'sessionKey', | ||
strategy: SessionStrategy.Browser | ||
} as never; | ||
|
||
const user = { | ||
name: 'test-user', | ||
first_name: 'test-first-name' | ||
}; | ||
|
||
vi.mocked(fetch).mockResolvedValue({ | ||
ok: true, | ||
json: () => Promise.resolve(user) | ||
} as never); | ||
|
||
const request = new Request('https://it-doesnt-matter.com'); | ||
|
||
const authorizer = new Auth0RemixServer(authOptions); | ||
const actual = await authorizer.getUser(request, {}); | ||
|
||
expect(actual.firstName).toEqual('test-first-name'); | ||
expect(actual.name).toEqual('test-user'); | ||
|
||
const fetchParams = vi.mocked(fetch).mock.calls[0]; | ||
expect(fetchParams[0]).toEqual('https://test.domain.com/userinfo'); | ||
|
||
expect(authOptions.profileCacheSet).toHaveBeenCalledWith('test-access-token', { | ||
firstName: 'test-first-name', | ||
name: 'test-user' | ||
}, 66669420); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('and the user profile fetch succeeds', () => { | ||
it<LocalTestContext>('returns the user', async ({ authOptions }) => { | ||
authOptions.session = { | ||
|
This file contains 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