-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathget-access-token.test.ts
More file actions
221 lines (193 loc) · 6.99 KB
/
get-access-token.test.ts
File metadata and controls
221 lines (193 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { NextRequest, NextResponse } from "next/server.js";
import * as jose from "jose";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi
} from "vitest";
import { SessionData, TokenSet } from "../types/index.js";
import { Auth0Client } from "./client.js";
// Basic constants for testing
const DEFAULT = {
domain: "https://op.example.com",
clientId: "test-client-id",
clientSecret: "test-client-secret",
appBaseUrl: "https://example.org",
secret: "test-secret-long-enough-for-hs256-test-secret-long-enough-for-hs256",
alg: "RS256",
sub: "test-sub",
sid: "test-sid",
scope: "openid profile email offline_access"
};
const initialTokenSetBase = {
accessToken: "test-access-token",
refreshToken: "test-refresh-token",
idToken: "test-id-token",
scope: DEFAULT.scope
};
const authClientConfig = {
domain: DEFAULT.domain,
clientId: DEFAULT.clientId,
clientSecret: DEFAULT.clientSecret,
appBaseUrl: DEFAULT.appBaseUrl,
secret: DEFAULT.secret
};
// msw server setup
let keyPair: jose.GenerateKeyPairResult;
const refreshedAccessToken = "msw-refreshed-access-token";
const refreshedRefreshToken = "msw-refreshed-refresh-token";
const refreshedExpiresIn = 3600;
const issuer = DEFAULT.domain;
const audience = DEFAULT.clientId;
const handlers = [
// OIDC Discovery Endpoint
http.get(`${DEFAULT.domain}/.well-known/openid-configuration`, () => {
return HttpResponse.json({
issuer: issuer,
token_endpoint: `${DEFAULT.domain}/oauth/token`,
jwks_uri: `${DEFAULT.domain}/.well-known/jwks.json`
});
}),
// JWKS Endpoint
http.get(`${DEFAULT.domain}/.well-known/jwks.json`, async () => {
const jwk = await jose.exportJWK(keyPair.publicKey);
return HttpResponse.json({ keys: [jwk] });
}),
// Token Endpoint (for refresh token grant)
http.post(
`${DEFAULT.domain}/oauth/token`,
async ({ request }: { request: Request }) => {
const body = await request.formData();
if (
body.get("grant_type") === "refresh_token" &&
body.get("refresh_token")
) {
// Generate a new ID token for the refreshed set
const newIdToken = await new jose.SignJWT({
sid: DEFAULT.sid,
sub: DEFAULT.sub,
auth_time: Math.floor(Date.now() / 1000),
nonce: "nonce-value" // Example nonce
})
.setProtectedHeader({ alg: DEFAULT.alg })
.setIssuer(issuer)
.setAudience(audience)
.setIssuedAt()
.setExpirationTime("1h")
.sign(keyPair.privateKey);
return HttpResponse.json({
access_token: refreshedAccessToken,
refresh_token: refreshedRefreshToken,
id_token: newIdToken,
token_type: "Bearer",
expires_in: refreshedExpiresIn,
scope: DEFAULT.scope // Assuming scope doesn't change on refresh
});
}
// Fallback for unexpected grant types or errors
return HttpResponse.json(
{ error: "invalid_grant", error_description: "Unsupported grant type" },
{ status: 400 }
);
}
)
];
const server = setupServer(...handlers);
beforeAll(async () => {
keyPair = await jose.generateKeyPair(DEFAULT.alg);
server.listen({ onUnhandledRequest: "error" });
});
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
/**
* Creates initial session data for tests.
*/
function createInitialSession(): SessionData {
// Use a VALID (non-expired) initial token
const initialExpiresAt = Math.floor(Date.now() / 1000) + 3600; // Expires in 1 hour
const initialTokenSet: TokenSet = {
...initialTokenSetBase, // Spread the base token set from the new constant
expiresAt: initialExpiresAt // Add the dynamic expiration time
};
const initialSession: SessionData = {
user: { sub: DEFAULT.sub },
tokenSet: initialTokenSet,
internal: { sid: DEFAULT.sid, createdAt: Date.now() / 1000 }
};
return initialSession;
}
describe("Auth0Client - getAccessToken", () => {
let mockSaveToSession: ReturnType<typeof vi.spyOn>;
let auth0Client: Auth0Client;
beforeEach(async () => {
// Instantiate Auth0Client normally, it will use intercepted fetch
auth0Client = new Auth0Client(authClientConfig);
// Mock saveToSession to avoid cookie/request context issues
mockSaveToSession = vi
.spyOn(Auth0Client.prototype as any, "saveToSession")
.mockResolvedValue(undefined); // Mock successful save
});
afterEach(() => {
vi.restoreAllMocks(); // Restore mocks after each test
});
/**
* Test Case: Pages Router Overload - getAccessToken(req, res, options)
* Verifies that when called with req/res objects and refresh: true (with a valid token),
* it refreshes the token.
*/
it("should refresh token and save session for pages-router overload when refresh is true (with valid token)", async () => {
const initialSession = createInitialSession();
// Mock getSession specifically for this test
vi.spyOn(Auth0Client.prototype as any, "getSession").mockResolvedValue(
initialSession
);
// Pages router overload requires req/res objects
const mockReq = new NextRequest(`https://${DEFAULT.appBaseUrl}/api/test`);
const mockRes = new NextResponse();
// --- Execution ---
const result = await auth0Client.getAccessToken(mockReq, mockRes, {
refresh: true
});
// --- Assertions ---
expect(result).not.toBeNull();
expect(result?.token).toBe(refreshedAccessToken); // From msw handler
// Check if expiresAt is close to the expected value based on the mock server response.
// We use toBeCloseTo to account for minor timing differences between the client
// calculating the expiration and the test assertion running.
const expectedExpiresAtRough =
Math.floor(Date.now() / 1000) + refreshedExpiresIn;
// The '0' precision checks for equality at the integer second level.
expect(result?.expiresAt).toBeCloseTo(expectedExpiresAtRough, 0);
expect(mockSaveToSession).toHaveBeenCalledOnce();
});
/**
* Test Case: App Router Overload - getAccessToken(options)
* Verifies that when called without req/res objects and refresh: true (with a valid token),
* it refreshes the token.
*/
it("should refresh token for app-router overload when refresh is true (with valid token)", async () => {
const initialSession = createInitialSession();
// Mock getSession specifically for this test
vi.spyOn(Auth0Client.prototype as any, "getSession").mockResolvedValue(
initialSession
);
// --- Execution ---
const result = await auth0Client.getAccessToken({
refresh: true
});
// --- Assertions ---
expect(result).not.toBeNull();
expect(result?.token).toBe(refreshedAccessToken);
const expectedExpiresAtRough =
Math.floor(Date.now() / 1000) + refreshedExpiresIn;
expect(result?.expiresAt).toBeCloseTo(expectedExpiresAtRough, 0);
expect(mockSaveToSession).toHaveBeenCalledOnce();
});
});