Skip to content

Commit 2b7110d

Browse files
update tests
1 parent eeb53f6 commit 2b7110d

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

EXAMPLES.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,6 @@ export async function GET() {
541541

542542
// Use the refreshed token
543543
// ...
544-
545-
return Response.json({ token, expiresAt });
546544
} catch (error) {
547545
console.error('Error getting access token:', error);
548546
return Response.json({ error: 'Failed to get access token' }, { status: 500 });
@@ -571,8 +569,6 @@ export default withApiAuthRequired(async function handler(
571569

572570
// Use the refreshed token
573571
// ...
574-
575-
res.status(200).json({ token, expiresAt });
576572
} catch (error: any) {
577573
console.error('Error getting access token:', error);
578574
res.status(error.status || 500).json({ error: error.message });
@@ -581,7 +577,7 @@ export default withApiAuthRequired(async function handler(
581577
```
582578

583579
By setting `{ refresh: true }`, you instruct the SDK to bypass the standard expiration check and request a new access token from the identity provider using the refresh token (if available and valid). The new token set (including the potentially updated access token, refresh token, and expiration time) will be saved back into the session automatically.
584-
This will in turn, also update the `id_token` field of `tokenset` in the session.
580+
This will in turn, update the `access_token`, `id_token` and `expires_at` fields of `tokenset` in the session.
585581

586582
## `<Auth0Provider />`
587583

src/server/client.test.ts

Lines changed: 89 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22

3+
import { AuthClient } from "./auth-client"; // Import the actual class for spyOn
34
import { Auth0Client } from "./client.js";
45

6+
// Define ENV_VARS at the top level for broader scope
7+
const ENV_VARS = {
8+
DOMAIN: "AUTH0_DOMAIN",
9+
CLIENT_ID: "AUTH0_CLIENT_ID",
10+
CLIENT_SECRET: "AUTH0_CLIENT_SECRET",
11+
CLIENT_ASSERTION_SIGNING_KEY: "AUTH0_CLIENT_ASSERTION_SIGNING_KEY",
12+
APP_BASE_URL: "APP_BASE_URL",
13+
SECRET: "AUTH0_SECRET",
14+
SCOPE: "AUTH0_SCOPE"
15+
};
16+
517
describe("Auth0Client", () => {
618
// Store original env vars
719
const originalEnv = { ...process.env };
820

9-
// Define correct environment variable names
10-
const ENV_VARS = {
11-
DOMAIN: "AUTH0_DOMAIN",
12-
CLIENT_ID: "AUTH0_CLIENT_ID",
13-
CLIENT_SECRET: "AUTH0_CLIENT_SECRET",
14-
CLIENT_ASSERTION_SIGNING_KEY: "AUTH0_CLIENT_ASSERTION_SIGNING_KEY",
15-
APP_BASE_URL: "APP_BASE_URL",
16-
SECRET: "AUTH0_SECRET",
17-
SCOPE: "AUTH0_SCOPE"
18-
};
19-
2021
// Clear env vars before each test
2122
beforeEach(() => {
2223
vi.resetModules();
@@ -112,3 +113,80 @@ describe("Auth0Client", () => {
112113
});
113114
});
114115
});
116+
117+
describe("Auth0Client getAccessToken", () => {
118+
const setupClient = () => {
119+
// Set required environment variables
120+
process.env[ENV_VARS.DOMAIN] = "test.auth0.com";
121+
process.env[ENV_VARS.CLIENT_ID] = "test_client_id";
122+
process.env[ENV_VARS.CLIENT_SECRET] = "test_client_secret";
123+
process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.test";
124+
process.env[ENV_VARS.SECRET] = "test_secret_string_at_least_32_bytes";
125+
return new Auth0Client();
126+
};
127+
128+
beforeEach(() => {
129+
// Reset mocks before each test
130+
vi.clearAllMocks();
131+
// Restore spyOn mocks
132+
vi.restoreAllMocks();
133+
});
134+
135+
it("should call getTokenSet with forceRefresh=true when refresh option is true", async () => {
136+
const client = setupClient();
137+
138+
// Define mock session data first
139+
const mockSession = {
140+
user: { sub: "user123" },
141+
tokenSet: {
142+
accessToken: "initial_at",
143+
idToken: "initial_idt",
144+
refreshToken: "initial_rt",
145+
scope: "openid profile",
146+
expiresAt: Math.floor(Date.now() / 1000) + 3600 // Not expired
147+
},
148+
internal: { sid: "sid123", createdAt: Date.now() / 1000 }
149+
};
150+
const refreshedTokenSet = {
151+
accessToken: "refreshed_at",
152+
idToken: "refreshed_idt",
153+
refreshToken: "rotated_rt",
154+
scope: "openid profile",
155+
expiresAt: Math.floor(Date.now() / 1000) + 7200
156+
};
157+
158+
// Mock getSession directly on the Auth0Client prototype
159+
vi.spyOn(Auth0Client.prototype, "getSession").mockResolvedValue(
160+
mockSession
161+
);
162+
163+
// Mock getTokenSet directly on the AuthClient prototype
164+
const getTokenSetSpy = vi
165+
.spyOn(AuthClient.prototype, "getTokenSet")
166+
.mockResolvedValue([null, refreshedTokenSet]);
167+
168+
const result = await client.getAccessToken({ refresh: true });
169+
170+
// Verify session was checked (by checking our mock of getSession)
171+
expect(Auth0Client.prototype.getSession).toHaveBeenCalledTimes(1);
172+
173+
// Verify the spy on getTokenSet was called
174+
expect(getTokenSetSpy).toHaveBeenCalledTimes(1);
175+
expect(getTokenSetSpy).toHaveBeenCalledWith(
176+
mockSession.tokenSet, // The initial token set from session
177+
true // forceRefresh flag
178+
);
179+
180+
// Verify the refreshed token is returned
181+
expect(result).toEqual({
182+
token: refreshedTokenSet.accessToken,
183+
scope: refreshedTokenSet.scope,
184+
expiresAt: refreshedTokenSet.expiresAt
185+
});
186+
187+
// Restore the spy after the test
188+
getTokenSetSpy.mockRestore();
189+
});
190+
191+
// Add other tests for getAccessToken: no session, no refresh token, expired token, etc.
192+
});

0 commit comments

Comments
 (0)