From cacfb2862c81005f0c18018e4d4b19ab0be46f5d Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Sat, 4 Apr 2020 16:24:26 +0200 Subject: [PATCH] Updated unit tests --- __tests__/managers/ConnectionManager.test.js | 60 +++++++++++++------- managers/ConnectionManager.js | 16 +++--- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/__tests__/managers/ConnectionManager.test.js b/__tests__/managers/ConnectionManager.test.js index d854559..dcc1c28 100644 --- a/__tests__/managers/ConnectionManager.test.js +++ b/__tests__/managers/ConnectionManager.test.js @@ -11,20 +11,19 @@ afterEach(() => { }); test('isLoggedIn yes', () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.resolve(true); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return 'token'; }); - return expect(c.isLoggedIn()).resolves.toBe(true); + return expect(c.isLoggedIn()).toBe(true); }); test('isLoggedIn no', () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.reject(false); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return null; }); - return expect(c.isLoggedIn()).rejects.toBe(false); + return expect(c.isLoggedIn()).toBe(false); }); - test('recoverLogin error crypto', () => { jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => { return Promise.reject(); @@ -111,7 +110,12 @@ test("isConnectionResponseValid", () => { state: false, }; expect(c.isConnectionResponseValid(json)).toBeTrue(); - + json = { + state: false, + message: 'Adresse mail ou mot de passe incorrect', + token: '' + }; + expect(c.isConnectionResponseValid(json)).toBeTrue(); json = { state: true, message: 'Connexion confirmée', @@ -143,7 +147,6 @@ test("isConnectionResponseValid", () => { expect(c.isConnectionResponseValid(json)).toBeFalse(); }); - test("connect bad credentials", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ @@ -178,6 +181,25 @@ test("connect good credentials", () => { return expect(c.connect('email', 'password')).resolves.toBeTruthy(); }); +test("connect good credentials no consent", () => { + jest.spyOn(global, 'fetch').mockImplementationOnce(() => { + return Promise.resolve({ + json: () => { + return { + state: false, + message: 'pas de consent', + token: '', + data: { + consent: false, + } + } + }, + }) + }); + return expect(c.connect('email', 'password')) + .rejects.toBe(ERROR_TYPE.NO_CONSENT); +}); + test("connect good credentials, fail save token", () => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ @@ -221,8 +243,8 @@ test("connect bogus response 1", () => { test("authenticatedRequest success", () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.resolve('token'); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return 'token'; }); jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ @@ -236,8 +258,8 @@ test("authenticatedRequest success", () => { }); test("authenticatedRequest error wrong token", () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.resolve('token'); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return 'token'; }); jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ @@ -251,8 +273,8 @@ test("authenticatedRequest error wrong token", () => { }); test("authenticatedRequest error bogus response", () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.resolve('token'); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return 'token'; }); jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ @@ -266,8 +288,8 @@ test("authenticatedRequest error bogus response", () => { }); test("authenticatedRequest connection error", () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.resolve('token'); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return 'token'; }); jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.reject() @@ -277,8 +299,8 @@ test("authenticatedRequest connection error", () => { }); test("authenticatedRequest error no token", () => { - jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { - return Promise.reject(false); + jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => { + return null; }); jest.spyOn(global, 'fetch').mockImplementationOnce(() => { return Promise.resolve({ diff --git a/managers/ConnectionManager.js b/managers/ConnectionManager.js index e7eb80e..5965450 100644 --- a/managers/ConnectionManager.js +++ b/managers/ConnectionManager.js @@ -18,8 +18,6 @@ export default class ConnectionManager { #email: string; #token: string | null; - loginCallback: Function; - listeners: Array; constructor() { @@ -54,8 +52,8 @@ export default class ConnectionManager { async recoverLogin() { return new Promise((resolve, reject) => { - if (this.#token !== null) - resolve(this.#token); + if (this.getToken() !== null) + resolve(this.getToken()); else { SecureStore.getItemAsync('token') .then((token) => { @@ -74,7 +72,7 @@ export default class ConnectionManager { } isLoggedIn() { - return this.#token !== null; + return this.getToken() !== null; } async saveLogin(email: string, token: string) { @@ -130,7 +128,9 @@ export default class ConnectionManager { .catch(() => { reject(ERROR_TYPE.SAVE_TOKEN); }); - } else if (data.data.consent !== undefined && !data.data.consent) + } else if (data.data !== undefined + && data.data.consent !== undefined + && !data.data.consent) reject(ERROR_TYPE.NO_CONSENT); else reject(ERROR_TYPE.BAD_CREDENTIALS); @@ -170,14 +170,14 @@ export default class ConnectionManager { async authenticatedRequest(url: string) { return new Promise((resolve, reject) => { - if (this.#token !== null) { + if (this.getToken() !== null) { fetch(url, { method: 'POST', headers: new Headers({ 'Accept': 'application/json', 'Content-Type': 'application/json', }), - body: JSON.stringify({token: this.#token}) + body: JSON.stringify({token: this.getToken()}) }).then(async (response) => response.json()) .then((data) => { if (this.isRequestResponseValid(data)) {