From 6cba1d16b7a9707ed3c9f66f0c9abc6fd1032201 Mon Sep 17 00:00:00 2001 From: Liam Stevens <8955671+liamstevens111@users.noreply.github.com> Date: Fri, 24 Feb 2023 11:33:18 +0700 Subject: [PATCH] [#36] Add tests to authAdapter and refactor --- src/adapters/authAdapter.test.ts | 40 +++++++++++++++++++++----------- src/adapters/authAdapter.ts | 16 ++++++++----- src/helpers/userToken.test.ts | 16 ++++++++++++- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/adapters/authAdapter.test.ts b/src/adapters/authAdapter.test.ts index 8ae0953..d83ae88 100644 --- a/src/adapters/authAdapter.test.ts +++ b/src/adapters/authAdapter.test.ts @@ -1,27 +1,20 @@ import nock from 'nock'; -import AuthAdapter from './authAdapter'; +import AuthAdapter, { commonParams } from './authAdapter'; /* eslint-disable camelcase */ -const testCredentials = { +const mockLoginCredentials = { email: 'testemail@gmail.com', 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({ @@ -29,13 +22,34 @@ describe('AuthAdapter', () => { '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); }); }); diff --git a/src/adapters/authAdapter.ts b/src/adapters/authAdapter.ts index 447d936..9b646f7 100644 --- a/src/adapters/authAdapter.ts +++ b/src/adapters/authAdapter.ts @@ -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 */ @@ -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 */ diff --git a/src/helpers/userToken.test.ts b/src/helpers/userToken.test.ts index d99bf01..04bcf72 100644 --- a/src/helpers/userToken.test.ts +++ b/src/helpers/userToken.test.ts @@ -1,4 +1,4 @@ -import { setToken, getToken } from './userToken'; +import { setToken, getToken, clearToken } from './userToken'; /* eslint-disable camelcase */ describe('setToken', () => { @@ -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 */