Updated unit tests

This commit is contained in:
Arnaud Vergnet 2020-04-04 16:24:26 +02:00
parent 17016b6452
commit cacfb2862c
2 changed files with 49 additions and 27 deletions

View file

@ -11,20 +11,19 @@ afterEach(() => {
}); });
test('isLoggedIn yes', () => { test('isLoggedIn yes', () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.resolve(true); return 'token';
}); });
return expect(c.isLoggedIn()).resolves.toBe(true); return expect(c.isLoggedIn()).toBe(true);
}); });
test('isLoggedIn no', () => { test('isLoggedIn no', () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.reject(false); return null;
}); });
return expect(c.isLoggedIn()).rejects.toBe(false); return expect(c.isLoggedIn()).toBe(false);
}); });
test('recoverLogin error crypto', () => { test('recoverLogin error crypto', () => {
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => { jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
return Promise.reject(); return Promise.reject();
@ -111,7 +110,12 @@ test("isConnectionResponseValid", () => {
state: false, state: false,
}; };
expect(c.isConnectionResponseValid(json)).toBeTrue(); expect(c.isConnectionResponseValid(json)).toBeTrue();
json = {
state: false,
message: 'Adresse mail ou mot de passe incorrect',
token: ''
};
expect(c.isConnectionResponseValid(json)).toBeTrue();
json = { json = {
state: true, state: true,
message: 'Connexion confirmée', message: 'Connexion confirmée',
@ -143,7 +147,6 @@ test("isConnectionResponseValid", () => {
expect(c.isConnectionResponseValid(json)).toBeFalse(); expect(c.isConnectionResponseValid(json)).toBeFalse();
}); });
test("connect bad credentials", () => { test("connect bad credentials", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
@ -178,6 +181,25 @@ test("connect good credentials", () => {
return expect(c.connect('email', 'password')).resolves.toBeTruthy(); 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", () => { test("connect good credentials, fail save token", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
@ -221,8 +243,8 @@ test("connect bogus response 1", () => {
test("authenticatedRequest success", () => { test("authenticatedRequest success", () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.resolve('token'); return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
@ -236,8 +258,8 @@ test("authenticatedRequest success", () => {
}); });
test("authenticatedRequest error wrong token", () => { test("authenticatedRequest error wrong token", () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.resolve('token'); return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
@ -251,8 +273,8 @@ test("authenticatedRequest error wrong token", () => {
}); });
test("authenticatedRequest error bogus response", () => { test("authenticatedRequest error bogus response", () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.resolve('token'); return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({
@ -266,8 +288,8 @@ test("authenticatedRequest error bogus response", () => {
}); });
test("authenticatedRequest connection error", () => { test("authenticatedRequest connection error", () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.resolve('token'); return 'token';
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.reject() return Promise.reject()
@ -277,8 +299,8 @@ test("authenticatedRequest connection error", () => {
}); });
test("authenticatedRequest error no token", () => { test("authenticatedRequest error no token", () => {
jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => { jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return Promise.reject(false); return null;
}); });
jest.spyOn(global, 'fetch').mockImplementationOnce(() => { jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({ return Promise.resolve({

View file

@ -18,8 +18,6 @@ export default class ConnectionManager {
#email: string; #email: string;
#token: string | null; #token: string | null;
loginCallback: Function;
listeners: Array<Function>; listeners: Array<Function>;
constructor() { constructor() {
@ -54,8 +52,8 @@ export default class ConnectionManager {
async recoverLogin() { async recoverLogin() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.#token !== null) if (this.getToken() !== null)
resolve(this.#token); resolve(this.getToken());
else { else {
SecureStore.getItemAsync('token') SecureStore.getItemAsync('token')
.then((token) => { .then((token) => {
@ -74,7 +72,7 @@ export default class ConnectionManager {
} }
isLoggedIn() { isLoggedIn() {
return this.#token !== null; return this.getToken() !== null;
} }
async saveLogin(email: string, token: string) { async saveLogin(email: string, token: string) {
@ -130,7 +128,9 @@ export default class ConnectionManager {
.catch(() => { .catch(() => {
reject(ERROR_TYPE.SAVE_TOKEN); 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); reject(ERROR_TYPE.NO_CONSENT);
else else
reject(ERROR_TYPE.BAD_CREDENTIALS); reject(ERROR_TYPE.BAD_CREDENTIALS);
@ -170,14 +170,14 @@ export default class ConnectionManager {
async authenticatedRequest(url: string) { async authenticatedRequest(url: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.#token !== null) { if (this.getToken() !== null) {
fetch(url, { fetch(url, {
method: 'POST', method: 'POST',
headers: new Headers({ headers: new Headers({
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}), }),
body: JSON.stringify({token: this.#token}) body: JSON.stringify({token: this.getToken()})
}).then(async (response) => response.json()) }).then(async (response) => response.json())
.then((data) => { .then((data) => {
if (this.isRequestResponseValid(data)) { if (this.isRequestResponseValid(data)) {