Skip to content

Commit 3f07bdb

Browse files
committed
shrink tests
1 parent f0ea31c commit 3f07bdb

File tree

2 files changed

+8
-172
lines changed

2 files changed

+8
-172
lines changed

src/server/auth/handlers/token.test.ts

Lines changed: 7 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,14 @@ describe('Token Handler', () => {
264264
});
265265

266266
it('returns tokens for valid code exchange', async () => {
267+
const mockExchangeCode = jest.spyOn(mockProvider, 'exchangeAuthorizationCode');
267268
const response = await supertest(app)
268269
.post('/token')
269270
.type('form')
270271
.send({
271272
client_id: 'valid-client',
272273
client_secret: 'valid-secret',
274+
resource: 'https://api.example.com/resource',
273275
grant_type: 'authorization_code',
274276
code: 'valid_code',
275277
code_verifier: 'valid_verifier'
@@ -280,24 +282,6 @@ describe('Token Handler', () => {
280282
expect(response.body.token_type).toBe('bearer');
281283
expect(response.body.expires_in).toBe(3600);
282284
expect(response.body.refresh_token).toBe('mock_refresh_token');
283-
});
284-
285-
it('accepts and passes resource parameter to provider', async () => {
286-
const mockExchangeCode = jest.spyOn(mockProvider, 'exchangeAuthorizationCode');
287-
288-
const response = await supertest(app)
289-
.post('/token')
290-
.type('form')
291-
.send({
292-
client_id: 'valid-client',
293-
client_secret: 'valid-secret',
294-
grant_type: 'authorization_code',
295-
code: 'valid_code',
296-
code_verifier: 'valid_verifier',
297-
resource: 'https://api.example.com/resource'
298-
});
299-
300-
expect(response.status).toBe(200);
301285
expect(mockExchangeCode).toHaveBeenCalledWith(
302286
validClient,
303287
'valid_code',
@@ -465,12 +449,14 @@ describe('Token Handler', () => {
465449
});
466450

467451
it('returns new tokens for valid refresh token', async () => {
452+
const mockExchangeRefresh = jest.spyOn(mockProvider, 'exchangeRefreshToken');
468453
const response = await supertest(app)
469454
.post('/token')
470455
.type('form')
471456
.send({
472457
client_id: 'valid-client',
473458
client_secret: 'valid-secret',
459+
resource: 'https://api.example.com/resource',
474460
grant_type: 'refresh_token',
475461
refresh_token: 'valid_refresh_token'
476462
});
@@ -480,39 +466,6 @@ describe('Token Handler', () => {
480466
expect(response.body.token_type).toBe('bearer');
481467
expect(response.body.expires_in).toBe(3600);
482468
expect(response.body.refresh_token).toBe('new_mock_refresh_token');
483-
});
484-
485-
it('respects requested scopes on refresh', async () => {
486-
const response = await supertest(app)
487-
.post('/token')
488-
.type('form')
489-
.send({
490-
client_id: 'valid-client',
491-
client_secret: 'valid-secret',
492-
grant_type: 'refresh_token',
493-
refresh_token: 'valid_refresh_token',
494-
scope: 'profile email'
495-
});
496-
497-
expect(response.status).toBe(200);
498-
expect(response.body.scope).toBe('profile email');
499-
});
500-
501-
it('accepts and passes resource parameter to provider on refresh', async () => {
502-
const mockExchangeRefresh = jest.spyOn(mockProvider, 'exchangeRefreshToken');
503-
504-
const response = await supertest(app)
505-
.post('/token')
506-
.type('form')
507-
.send({
508-
client_id: 'valid-client',
509-
client_secret: 'valid-secret',
510-
grant_type: 'refresh_token',
511-
refresh_token: 'valid_refresh_token',
512-
resource: 'https://api.example.com/resource'
513-
});
514-
515-
expect(response.status).toBe(200);
516469
expect(mockExchangeRefresh).toHaveBeenCalledWith(
517470
validClient,
518471
'valid_refresh_token',
@@ -521,48 +474,7 @@ describe('Token Handler', () => {
521474
);
522475
});
523476

524-
it('rejects invalid resource parameter (non-URL) on refresh', async () => {
525-
const response = await supertest(app)
526-
.post('/token')
527-
.type('form')
528-
.send({
529-
client_id: 'valid-client',
530-
client_secret: 'valid-secret',
531-
grant_type: 'refresh_token',
532-
refresh_token: 'valid_refresh_token',
533-
resource: 'not-a-url'
534-
});
535-
536-
expect(response.status).toBe(400);
537-
expect(response.body.error).toBe('invalid_request');
538-
expect(response.body.error_description).toContain('resource');
539-
});
540-
541-
it('handles refresh token exchange without resource parameter', async () => {
542-
const mockExchangeRefresh = jest.spyOn(mockProvider, 'exchangeRefreshToken');
543-
544-
const response = await supertest(app)
545-
.post('/token')
546-
.type('form')
547-
.send({
548-
client_id: 'valid-client',
549-
client_secret: 'valid-secret',
550-
grant_type: 'refresh_token',
551-
refresh_token: 'valid_refresh_token'
552-
});
553-
554-
expect(response.status).toBe(200);
555-
expect(mockExchangeRefresh).toHaveBeenCalledWith(
556-
validClient,
557-
'valid_refresh_token',
558-
undefined, // scopes
559-
undefined // resource parameter
560-
);
561-
});
562-
563-
it('passes resource with scopes on refresh', async () => {
564-
const mockExchangeRefresh = jest.spyOn(mockProvider, 'exchangeRefreshToken');
565-
477+
it('respects requested scopes on refresh', async () => {
566478
const response = await supertest(app)
567479
.post('/token')
568480
.type('form')
@@ -571,17 +483,11 @@ describe('Token Handler', () => {
571483
client_secret: 'valid-secret',
572484
grant_type: 'refresh_token',
573485
refresh_token: 'valid_refresh_token',
574-
scope: 'profile email',
575-
resource: 'https://api.example.com/resource'
486+
scope: 'profile email'
576487
});
577488

578489
expect(response.status).toBe(200);
579-
expect(mockExchangeRefresh).toHaveBeenCalledWith(
580-
validClient,
581-
'valid_refresh_token',
582-
['profile', 'email'], // scopes
583-
new URL('https://api.example.com/resource') // resource parameter
584-
);
490+
expect(response.body.scope).toBe('profile email');
585491
});
586492
});
587493

src/server/auth/providers/proxyProvider.test.ts

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe("Proxy OAuth Server Provider", () => {
8888
codeChallenge: "test-challenge",
8989
state: "test-state",
9090
scopes: ["read", "write"],
91+
resource: new URL('https://api.example.com/resource'),
9192
},
9293
mockResponse
9394
);
@@ -100,52 +101,10 @@ describe("Proxy OAuth Server Provider", () => {
100101
expectedUrl.searchParams.set("code_challenge_method", "S256");
101102
expectedUrl.searchParams.set("state", "test-state");
102103
expectedUrl.searchParams.set("scope", "read write");
103-
104-
expect(mockResponse.redirect).toHaveBeenCalledWith(expectedUrl.toString());
105-
});
106-
107-
it('includes resource parameter in authorization redirect', async () => {
108-
await provider.authorize(
109-
validClient,
110-
{
111-
redirectUri: 'https://example.com/callback',
112-
codeChallenge: 'test-challenge',
113-
state: 'test-state',
114-
scopes: ['read', 'write'],
115-
resource: new URL('https://api.example.com/resource')
116-
},
117-
mockResponse
118-
);
119-
120-
const expectedUrl = new URL('https://auth.example.com/authorize');
121-
expectedUrl.searchParams.set('client_id', 'test-client');
122-
expectedUrl.searchParams.set('response_type', 'code');
123-
expectedUrl.searchParams.set('redirect_uri', 'https://example.com/callback');
124-
expectedUrl.searchParams.set('code_challenge', 'test-challenge');
125-
expectedUrl.searchParams.set('code_challenge_method', 'S256');
126-
expectedUrl.searchParams.set('state', 'test-state');
127-
expectedUrl.searchParams.set('scope', 'read write');
128104
expectedUrl.searchParams.set('resource', 'https://api.example.com/resource');
129105

130106
expect(mockResponse.redirect).toHaveBeenCalledWith(expectedUrl.toString());
131107
});
132-
133-
it('handles authorization without resource parameter', async () => {
134-
await provider.authorize(
135-
validClient,
136-
{
137-
redirectUri: 'https://example.com/callback',
138-
codeChallenge: 'test-challenge',
139-
state: 'test-state',
140-
scopes: ['read']
141-
},
142-
mockResponse
143-
);
144-
145-
const redirectUrl = (mockResponse.redirect as jest.Mock).mock.calls[0][0];
146-
const url = new URL(redirectUrl);
147-
expect(url.searchParams.has('resource')).toBe(false);
148-
});
149108
});
150109

151110
describe("token exchange", () => {
@@ -282,35 +241,6 @@ describe("Proxy OAuth Server Provider", () => {
282241
);
283242
expect(tokens).toEqual(mockTokenResponse);
284243
});
285-
286-
it('handles refresh token exchange without resource parameter', async () => {
287-
const tokens = await provider.exchangeRefreshToken(
288-
validClient,
289-
'test-refresh-token',
290-
['read']
291-
);
292-
293-
const fetchCall = (global.fetch as jest.Mock).mock.calls[0];
294-
const body = fetchCall[1].body as string;
295-
expect(body).not.toContain('resource=');
296-
expect(tokens).toEqual(mockTokenResponse);
297-
});
298-
299-
it('includes both scope and resource parameters in refresh', async () => {
300-
const tokens = await provider.exchangeRefreshToken(
301-
validClient,
302-
'test-refresh-token',
303-
['profile', 'email'],
304-
new URL('https://api.example.com/resource')
305-
);
306-
307-
const fetchCall = (global.fetch as jest.Mock).mock.calls[0];
308-
const body = fetchCall[1].body as string;
309-
expect(body).toContain('scope=profile+email');
310-
expect(body).toContain('resource=' + encodeURIComponent('https://api.example.com/resource'));
311-
expect(tokens).toEqual(mockTokenResponse);
312-
});
313-
314244
});
315245

316246
describe("client registration", () => {

0 commit comments

Comments
 (0)