Skip to content

Commit

Permalink
[SDK-2172] Add SDK metrics to all API calls (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck authored Dec 2, 2020
1 parent 0cafccb commit 7a25483
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 85 deletions.
138 changes: 99 additions & 39 deletions __tests__/Auth0Client/getTokenSilently.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
} from '../constants';

import { releaseLockSpy } from '../../__mocks__/browser-tabs-lock';
import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';

jest.mock('unfetch');
jest.mock('es-cookie');
Expand Down Expand Up @@ -179,13 +180,19 @@ describe('Auth0Client', () => {

await getTokenSilently(auth0);

assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);
});

it('calls the token endpoint with the correct params when using refresh tokens', async () => {
Expand All @@ -201,12 +208,18 @@ describe('Auth0Client', () => {
ignoreCache: true
});

assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
grant_type: 'refresh_token',
refresh_token: TEST_REFRESH_TOKEN
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
grant_type: 'refresh_token',
refresh_token: TEST_REFRESH_TOKEN
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);
});

it('calls the token endpoint with the correct params when passing redirect uri and using refresh tokens', async () => {
Expand All @@ -224,12 +237,18 @@ describe('Auth0Client', () => {
ignoreCache: true
});

assertPost('https://auth0_domain/oauth/token', {
redirect_uri,
client_id: TEST_CLIENT_ID,
grant_type: 'refresh_token',
refresh_token: TEST_REFRESH_TOKEN
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri,
client_id: TEST_CLIENT_ID,
grant_type: 'refresh_token',
refresh_token: TEST_REFRESH_TOKEN
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);
});

it('calls the token endpoint with the correct params when not providing any redirect uri and using refresh tokens', async () => {
Expand All @@ -247,12 +266,18 @@ describe('Auth0Client', () => {
ignoreCache: true
});

assertPost('https://auth0_domain/oauth/token', {
redirect_uri: 'http://localhost',
client_id: TEST_CLIENT_ID,
grant_type: 'refresh_token',
refresh_token: TEST_REFRESH_TOKEN
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: 'http://localhost',
client_id: TEST_CLIENT_ID,
grant_type: 'refresh_token',
refresh_token: TEST_REFRESH_TOKEN
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);
});

it('calls the token endpoint with the correct timeout when using refresh tokens', async () => {
Expand Down Expand Up @@ -351,6 +376,7 @@ describe('Auth0Client', () => {

jest.spyOn(<any>utils, 'runIframe').mockResolvedValue({
access_token: TEST_ACCESS_TOKEN,
code: TEST_CODE,
state: TEST_STATE
});

Expand All @@ -359,6 +385,19 @@ describe('Auth0Client', () => {
await getTokenSilently(auth0);

expectToHaveBeenCalledWithAuth0ClientParam(utils.runIframe, auth0Client);
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
},
{
'Auth0-Client': btoa(JSON.stringify(auth0Client))
}
);
});

it('refreshes the token when cache available without access token', async () => {
Expand Down Expand Up @@ -458,6 +497,9 @@ describe('Auth0Client', () => {
redirect_uri: TEST_REDIRECT_URI,
refresh_token: TEST_REFRESH_TOKEN
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
},
1
);

Expand All @@ -474,13 +516,19 @@ describe('Auth0Client', () => {

await loginWithRedirect(auth0);

assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);

mockFetch.mockResolvedValueOnce(
fetchResponse(true, {
Expand All @@ -501,6 +549,9 @@ describe('Auth0Client', () => {
redirect_uri: TEST_REDIRECT_URI,
refresh_token: TEST_REFRESH_TOKEN
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
},
1
);

Expand All @@ -519,13 +570,19 @@ describe('Auth0Client', () => {

await loginWithRedirect(auth0);

assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);

const access_token = await getTokenSilently(auth0, { ignoreCache: true });

Expand All @@ -537,6 +594,9 @@ describe('Auth0Client', () => {
redirect_uri: TEST_REDIRECT_URI,
refresh_token: TEST_REFRESH_TOKEN
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
},
1
);

Expand Down
7 changes: 6 additions & 1 deletion __tests__/Auth0Client/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ const authorizationResponse: AuthenticationResult = {
};

export const assertPostFn = mockFetch => {
return (url, body, callNum = 0) => {
return (url, body, headers = null, callNum = 0) => {
const [actualUrl, opts] = mockFetch.mock.calls[callNum];
expect(url).toEqual(actualUrl);
expect(body).toEqual(JSON.parse(opts.body));
if (headers) {
Object.keys(headers).forEach(header =>
expect(headers[header]).toEqual(opts.headers[header])
);
}
};
};

Expand Down
46 changes: 31 additions & 15 deletions __tests__/Auth0Client/loginWithPopup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ import {
TEST_STATE
} from '../constants';

import { DEFAULT_POPUP_CONFIG_OPTIONS } from '../../src/constants';
import {
DEFAULT_AUTH0_CLIENT,
DEFAULT_POPUP_CONFIG_OPTIONS
} from '../../src/constants';
import version from '../../src/version';

jest.mock('unfetch');
jest.mock('es-cookie');
Expand Down Expand Up @@ -236,13 +240,19 @@ describe('Auth0Client', () => {
await loginWithPopup(auth0);

expect(mockWindow.open).toHaveBeenCalled();
assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: 'my_code'
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: 'my_code'
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);
});

it('uses default config', async () => {
Expand Down Expand Up @@ -313,13 +323,19 @@ describe('Auth0Client', () => {
await loginWithPopup(auth0, {}, { popup });

expect(mockWindow.open).not.toHaveBeenCalled();
assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: 'my_code'
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: 'my_code'
},
{
'Auth0-Client': btoa(JSON.stringify(DEFAULT_AUTH0_CLIENT))
}
);
});

it('opens popup with custom auth0Client', async () => {
Expand Down
26 changes: 19 additions & 7 deletions __tests__/Auth0Client/loginWithRedirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
TEST_SCOPES,
TEST_STATE
} from '../constants';
import version from '../../src/version';

jest.mock('unfetch');
jest.mock('es-cookie');
Expand Down Expand Up @@ -115,13 +116,24 @@ describe('Auth0Client', () => {
code_challenge_method: 'S256'
});

assertPost('https://auth0_domain/oauth/token', {
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
});
assertPost(
'https://auth0_domain/oauth/token',
{
redirect_uri: TEST_REDIRECT_URI,
client_id: TEST_CLIENT_ID,
code_verifier: TEST_CODE_VERIFIER,
grant_type: 'authorization_code',
code: TEST_CODE
},
{
'Auth0-Client': btoa(
JSON.stringify({
name: 'auth0-spa-js',
version: version
})
)
}
);
});

it('should log the user in using different default scope', async () => {
Expand Down
Loading

0 comments on commit 7a25483

Please sign in to comment.