Skip to content

Commit

Permalink
[#36] Add tests to authAdapter and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
liamstevens111 committed Mar 7, 2023
1 parent b539037 commit 6cba1d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
40 changes: 27 additions & 13 deletions src/adapters/authAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
import nock from 'nock';

import AuthAdapter from './authAdapter';
import AuthAdapter, { commonParams } from './authAdapter';

/* eslint-disable camelcase */
const testCredentials = {
const mockLoginCredentials = {
email: '[email protected]',
password: 'password123',
};

const commonLoginParams = {
grant_type: 'password',
client_id: process.env.REACT_APP_API_CLIENT_ID,
client_secret: process.env.REACT_APP_API_CLIENT_SECRET,
};
/* eslint-enable camelcase */

describe('AuthAdapter', () => {
afterAll(() => {
nock.cleanAll();
nock.restore();
});

describe('login', () => {
describe('loginWithEmailPassword', () => {
test('The login endpoint is called with credientials from the request', async () => {
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`)
.defaultReplyHeaders({
'access-control-allow-origin': '*',
'access-control-allow-credentials': 'true',
})
.post('/oauth/token', {
...testCredentials,
...commonLoginParams,
...mockLoginCredentials,
...commonParams,
grant_type: 'password',
})
.reply(200);

expect(scope.isDone()).toBe(false);
await AuthAdapter.loginWithEmailPassword({ ...mockLoginCredentials });
expect(scope.isDone()).toBe(true);
});
});

describe('loginWithRefreshToken', () => {
test('The refresh token endpoint is called with refresh token from the request', async () => {
const scope = nock(`${process.env.REACT_APP_API_ENDPOINT}`)
.defaultReplyHeaders({
'access-control-allow-origin': '*',
'access-control-allow-credentials': 'true',
})
.post('/oauth/token', {
refresh_token: 'refresh_token',
...commonParams,
grant_type: 'refresh_token',
})
.reply(200);

expect(scope.isDone()).toBe(false);
await AuthAdapter.loginWithEmailPassword({ ...testCredentials });
await AuthAdapter.loginWithRefreshToken('refresh_token');
expect(scope.isDone()).toBe(true);
});
});
Expand Down
16 changes: 10 additions & 6 deletions src/adapters/authAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ type LoginAuthType = {
password: string;
};

/* eslint-disable camelcase */
export const commonParams = {
client_id: process.env.REACT_APP_API_CLIENT_ID,
client_secret: process.env.REACT_APP_API_CLIENT_SECRET,
};
/* eslint-enable camelcase */
class AuthAdapter extends BaseAdapter {
static loginWithEmailPassword(params: LoginAuthType) {
static loginWithEmailPassword(authParams: LoginAuthType) {
/* eslint-disable camelcase */
const requestParams = {
...params,
...commonParams,
...authParams,
grant_type: 'password',
client_id: process.env.REACT_APP_API_CLIENT_ID,
client_secret: process.env.REACT_APP_API_CLIENT_SECRET,
};
/* eslint-enable camelcase */

Expand All @@ -22,10 +27,9 @@ class AuthAdapter extends BaseAdapter {
static loginWithRefreshToken(refreshToken: string) {
/* eslint-disable camelcase */
const requestParams = {
...commonParams,
refresh_token: refreshToken,
grant_type: 'refresh_token',
client_id: process.env.REACT_APP_API_CLIENT_ID,
client_secret: process.env.REACT_APP_API_CLIENT_SECRET,
};
/* eslint-enable camelcase */

Expand Down
16 changes: 15 additions & 1 deletion src/helpers/userToken.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setToken, getToken } from './userToken';
import { setToken, getToken, clearToken } from './userToken';

/* eslint-disable camelcase */
describe('setToken', () => {
Expand Down Expand Up @@ -26,4 +26,18 @@ describe('getToken', () => {
expect(getToken()).toEqual({});
});
});

describe('clearToken', () => {
test('Given an existing UserToken in LocalStorage, removes the value', () => {
const testToken = { access_token: 'access_token', refresh_token: 'refesh_token' };

localStorage.setItem('UserToken', JSON.stringify(testToken));

expect(JSON.parse(localStorage.getItem('UserToken') as string)).toStrictEqual(testToken);

clearToken();

expect(JSON.parse(localStorage.getItem('UserToken') as string)).toBeNull();
});
});
/* eslint-enable camelcase */

0 comments on commit 6cba1d1

Please sign in to comment.