Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredperreault-okta committed Aug 13, 2024
1 parent f0072e2 commit 021171e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
5 changes: 4 additions & 1 deletion test/spec/oidc/endpoints/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('token endpoint', function() {
var endpoint = '/oauth2/v1/token';
var codeVerifier = 'superfake';
var authorizationCode = 'notreal';
var extraParams = { foo: 'bar' };

util.itMakesCorrectRequestResponse({
title: 'requests a token',
Expand All @@ -55,7 +56,8 @@ describe('token endpoint', function() {
data: {
client_id: CLIENT_ID,
grant_type: 'authorization_code',
redirect_uri: REDIRECT_URI
redirect_uri: REDIRECT_URI,
extraParams: extraParams
},
headers: {
'Accept': 'application/json',
Expand All @@ -81,6 +83,7 @@ describe('token endpoint', function() {
redirectUri: REDIRECT_URI,
authorizationCode: authorizationCode,
codeVerifier: codeVerifier,
extraParams: extraParams
}, {
tokenUrl: ISSUER + endpoint
});
Expand Down
11 changes: 9 additions & 2 deletions test/spec/oidc/exchangeCodeForTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ describe('exchangeCodeForTokens', () => {
(postToTokenEndpoint as jest.Mock).mockResolvedValue(oauthResponse);
(handleOAuthResponse as jest.Mock).mockResolvedValue(tokenResponse);
const acrValues = 'foo';
const tokenParams = { ...testParams, acrValues };
const tokenParams = {
...testParams,
acrValues,
extraParams: {
foo: 'bar'
}
};
const urls = getOAuthUrls(sdk);
await exchangeCodeForTokens(sdk, tokenParams);
expect(postToTokenEndpoint).toHaveBeenCalledWith(sdk, testParams, urls);
Expand All @@ -62,7 +68,8 @@ describe('exchangeCodeForTokens', () => {
redirectUri: testParams.redirectUri,
responseType: ['token', 'id_token'],
scopes: ['openid', 'email'],
acrValues
acrValues,
extraParams: tokenParams.extraParams
}, oauthResponse, urls);
});

Expand Down
6 changes: 4 additions & 2 deletions test/spec/oidc/parseFromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe('token.parseFromUrl', function() {
oktaAuthArgs: {
pkce: false,
responseMode: 'query',
responseType: ['code']
responseType: ['code'],
extraParams: { foo: 'bar' }
},
searchMock: '?code=fake' +
'&state=' + oauthUtil.mockedState,
Expand All @@ -65,7 +66,8 @@ describe('token.parseFromUrl', function() {
tokenUrl: 'https://auth-js-test.okta.com/oauth2/v1/token',
authorizeUrl: 'https://auth-js-test.okta.com/oauth2/v1/authorize',
userinfoUrl: 'https://auth-js-test.okta.com/oauth2/v1/userinfo'
}
},
extraParams: { foo: 'bar' }
},
expectedResp: {
code: 'fake',
Expand Down
17 changes: 17 additions & 0 deletions test/spec/oidc/renewTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,23 @@ describe('token.renewTokens', function() {
});
});

it('extraParams', async () => {
const { sdk, accessToken, idToken, getWihoutPromptResponse, withoutPromptSpy } = testContext;
accessToken.extraParams = { foo: 'bar' };
const tokens = { fake: true };
getWihoutPromptResponse.tokens = tokens;
const res = await renewTokens(sdk, {});
expect(res).toBe(tokens);
expect(withoutPromptSpy).toHaveBeenCalledWith(sdk, {
authorizeUrl: accessToken.authorizeUrl,
issuer: idToken.issuer,
responseType: ['token', 'id_token'],
scopes: accessToken.scopes,
userinfoUrl: accessToken.userinfoUrl,
extraParams: { foo: 'bar' }
});
});

});

});
10 changes: 8 additions & 2 deletions test/spec/oidc/util/handleOAuthResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,23 @@ describe('handleOAuthResponse', () => {
expect(res.tokens.refreshToken!.refreshToken).toBe('foo');
});
it('returns all tokens from the response', async () => {
const tokenParams: TokenParams = { responseType: ['token', 'id_token', 'refresh_token'], dpop: true };
const tokenParams: TokenParams = {
responseType: ['token', 'id_token', 'refresh_token'],
dpop: true,
extraParams: { foo: 'bar' }
};
const oauthRes = { id_token: 'foo', access_token: 'blar', refresh_token: 'bloo', token_type: 'DPoP' };
const res = await handleOAuthResponse(sdk, tokenParams, oauthRes, undefined as unknown as CustomUrls);
expect(res.tokens).toBeTruthy();
expect(res.tokens.accessToken).toBeTruthy();
expect(res.tokens.accessToken!.accessToken).toBe('blar');
expect(res.tokens.accessToken!.extraParams).toEqual({ foo: 'bar' });
expect(res.tokens.idToken).toBeTruthy();
expect(res.tokens.idToken!.idToken).toBe('foo');
expect(res.tokens.idToken!.extraParams).toEqual({ foo: 'bar' });
expect(res.tokens.refreshToken).toBeTruthy();
expect(res.tokens.refreshToken!.refreshToken).toBe('bloo');
expect(res.tokens.refreshToken!.extraParams).toEqual({ foo: 'bar' });
});
it('prefers "scope" value from endpoint response over method parameter', async () => {
const tokenParams: TokenParams = { responseType: ['token', 'id_token', 'refresh_token'], scopes: ['profile'] };
Expand All @@ -78,7 +85,6 @@ describe('handleOAuthResponse', () => {
expect(res.tokens.idToken!.scopes).toEqual(['openid', 'offline_access']);
expect(res.tokens.refreshToken!.scopes).toEqual(['openid', 'offline_access']);
});

describe('errors', () => {
it('does not throw if response contains only "error" without "error_description"', async () => {
let errorThrown = false;
Expand Down

0 comments on commit 021171e

Please sign in to comment.