2020-03-29 11:47:27 +02:00
|
|
|
import React from 'react';
|
2020-04-05 23:56:43 +02:00
|
|
|
import ConnectionManager, {ERROR_TYPE} from "../../src/managers/ConnectionManager";
|
2020-03-30 23:44:06 +02:00
|
|
|
import * as SecureStore from 'expo-secure-store';
|
|
|
|
|
|
|
|
let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
|
2020-03-29 11:47:27 +02:00
|
|
|
|
|
|
|
const c = ConnectionManager.getInstance();
|
|
|
|
|
2020-03-31 10:57:53 +02:00
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
2020-03-31 14:21:01 +02:00
|
|
|
test('isLoggedIn yes', () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return 'token';
|
2020-03-31 14:21:01 +02:00
|
|
|
});
|
2020-04-04 16:24:26 +02:00
|
|
|
return expect(c.isLoggedIn()).toBe(true);
|
2020-03-31 14:21:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('isLoggedIn no', () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return null;
|
2020-03-31 14:21:01 +02:00
|
|
|
});
|
2020-04-04 16:24:26 +02:00
|
|
|
return expect(c.isLoggedIn()).toBe(false);
|
2020-03-31 14:21:01 +02:00
|
|
|
});
|
|
|
|
|
2020-03-31 10:57:53 +02:00
|
|
|
test('recoverLogin error crypto', () => {
|
|
|
|
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
return expect(c.recoverLogin()).rejects.toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('recoverLogin success crypto', () => {
|
|
|
|
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve('token1');
|
|
|
|
});
|
|
|
|
return expect(c.recoverLogin()).resolves.toBe('token1');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('saveLogin success', () => {
|
|
|
|
jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
return expect(c.saveLogin('email', 'token2')).resolves.toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('saveLogin error', () => {
|
|
|
|
jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
return expect(c.saveLogin('email', 'token3')).rejects.toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('recoverLogin error crypto with saved token', () => {
|
|
|
|
jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
return expect(c.recoverLogin()).resolves.toBe('token2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('recoverLogin success saved', () => {
|
|
|
|
return expect(c.recoverLogin()).resolves.toBe('token2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('isRequestResponseValid', () => {
|
|
|
|
let json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 0,
|
2020-03-31 10:57:53 +02:00
|
|
|
data: {}
|
|
|
|
};
|
2020-04-06 18:32:10 +02:00
|
|
|
expect(c.isResponseValid(json)).toBeTrue();
|
2020-03-31 10:57:53 +02:00
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 1,
|
2020-03-31 10:57:53 +02:00
|
|
|
data: {}
|
|
|
|
};
|
2020-04-06 18:32:10 +02:00
|
|
|
expect(c.isResponseValid(json)).toBeTrue();
|
2020-03-31 10:57:53 +02:00
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 50,
|
|
|
|
data: {}
|
|
|
|
};
|
|
|
|
expect(c.isResponseValid(json)).toBeTrue();
|
|
|
|
json = {
|
|
|
|
error: 50,
|
2020-03-31 10:57:53 +02:00
|
|
|
data: {truc: 'machin'}
|
|
|
|
};
|
2020-04-06 18:32:10 +02:00
|
|
|
expect(c.isResponseValid(json)).toBeTrue();
|
2020-03-31 10:57:53 +02:00
|
|
|
json = {
|
|
|
|
message: 'coucou'
|
|
|
|
};
|
2020-04-06 18:32:10 +02:00
|
|
|
expect(c.isResponseValid(json)).toBeFalse();
|
2020-03-31 10:57:53 +02:00
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 'coucou',
|
|
|
|
data: {truc: 'machin'}
|
2020-03-31 10:57:53 +02:00
|
|
|
};
|
2020-04-06 18:32:10 +02:00
|
|
|
expect(c.isResponseValid(json)).toBeFalse();
|
2020-03-31 10:57:53 +02:00
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 0,
|
|
|
|
data: 'coucou'
|
2020-03-31 10:57:53 +02:00
|
|
|
};
|
2020-04-06 18:32:10 +02:00
|
|
|
expect(c.isResponseValid(json)).toBeFalse();
|
|
|
|
json = {
|
|
|
|
error: 0,
|
|
|
|
};
|
|
|
|
expect(c.isResponseValid(json)).toBeFalse();
|
2020-03-31 10:57:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("isConnectionResponseValid", () => {
|
|
|
|
let json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 0,
|
|
|
|
data: {token: 'token'}
|
2020-03-31 10:57:53 +02:00
|
|
|
};
|
|
|
|
expect(c.isConnectionResponseValid(json)).toBeTrue();
|
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 2,
|
|
|
|
data: {}
|
2020-04-04 16:24:26 +02:00
|
|
|
};
|
|
|
|
expect(c.isConnectionResponseValid(json)).toBeTrue();
|
2020-03-31 10:57:53 +02:00
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 0,
|
|
|
|
data: {token: ''}
|
2020-03-31 10:57:53 +02:00
|
|
|
};
|
|
|
|
expect(c.isConnectionResponseValid(json)).toBeFalse();
|
|
|
|
json = {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: 'prout',
|
|
|
|
data: {token: ''}
|
2020-03-31 10:57:53 +02:00
|
|
|
};
|
|
|
|
expect(c.isConnectionResponseValid(json)).toBeFalse();
|
|
|
|
});
|
|
|
|
|
2020-03-29 11:47:27 +02:00
|
|
|
test("connect bad credentials", () => {
|
2020-03-30 23:44:06 +02:00
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
|
|
|
return {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: ERROR_TYPE.BAD_CREDENTIALS,
|
|
|
|
data: {}
|
|
|
|
};
|
2020-03-30 23:44:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return expect(c.connect('email', 'password'))
|
2020-03-29 11:47:27 +02:00
|
|
|
.rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("connect good credentials", () => {
|
2020-03-30 23:44:06 +02:00
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
2020-03-31 10:57:53 +02:00
|
|
|
return {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: ERROR_TYPE.SUCCESS,
|
|
|
|
data: {token: 'token'}
|
|
|
|
};
|
2020-03-30 23:44:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
2020-03-31 14:21:01 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
|
2020-03-30 23:44:06 +02:00
|
|
|
return Promise.resolve(true);
|
|
|
|
});
|
|
|
|
return expect(c.connect('email', 'password')).resolves.toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2020-04-04 16:24:26 +02:00
|
|
|
test("connect good credentials no consent", () => {
|
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
|
|
|
return {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: ERROR_TYPE.NO_CONSENT,
|
|
|
|
data: {}
|
|
|
|
};
|
2020-04-04 16:24:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return expect(c.connect('email', 'password'))
|
|
|
|
.rejects.toBe(ERROR_TYPE.NO_CONSENT);
|
|
|
|
});
|
|
|
|
|
2020-03-30 23:44:06 +02:00
|
|
|
test("connect good credentials, fail save token", () => {
|
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
2020-03-31 10:57:53 +02:00
|
|
|
return {
|
2020-04-06 18:32:10 +02:00
|
|
|
error: ERROR_TYPE.SUCCESS,
|
|
|
|
data: {token: 'token'}
|
|
|
|
};
|
2020-03-30 23:44:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
2020-03-31 14:21:01 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
|
2020-03-30 23:44:06 +02:00
|
|
|
return Promise.reject(false);
|
|
|
|
});
|
2020-04-06 18:32:10 +02:00
|
|
|
return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.UNKNOWN);
|
2020-03-30 23:44:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("connect connection error", () => {
|
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
return expect(c.connect('email', 'password'))
|
|
|
|
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("connect bogus response 1", () => {
|
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
2020-03-31 10:57:53 +02:00
|
|
|
return {
|
2020-03-30 23:44:06 +02:00
|
|
|
thing: true,
|
|
|
|
wrong: '',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
|
|
|
return expect(c.connect('email', 'password'))
|
|
|
|
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
|
|
|
|
});
|
|
|
|
|
2020-03-31 10:57:53 +02:00
|
|
|
|
|
|
|
test("authenticatedRequest success", () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return 'token';
|
2020-03-31 10:57:53 +02:00
|
|
|
});
|
2020-03-30 23:44:06 +02:00
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
2020-04-06 18:32:10 +02:00
|
|
|
return {
|
|
|
|
error: ERROR_TYPE.SUCCESS,
|
|
|
|
data: {coucou: 'toi'}
|
|
|
|
};
|
2020-03-30 23:44:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
2020-03-31 10:57:53 +02:00
|
|
|
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
|
|
|
|
.resolves.toStrictEqual({coucou: 'toi'});
|
2020-03-30 23:44:06 +02:00
|
|
|
});
|
|
|
|
|
2020-03-31 10:57:53 +02:00
|
|
|
test("authenticatedRequest error wrong token", () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return 'token';
|
2020-03-31 10:57:53 +02:00
|
|
|
});
|
2020-03-30 23:44:06 +02:00
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
2020-04-06 18:32:10 +02:00
|
|
|
return {
|
|
|
|
error: ERROR_TYPE.BAD_TOKEN,
|
|
|
|
data: {}
|
|
|
|
};
|
2020-03-30 23:44:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
2020-03-31 10:57:53 +02:00
|
|
|
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
|
2020-04-06 18:32:10 +02:00
|
|
|
.rejects.toBe(ERROR_TYPE.BAD_TOKEN);
|
2020-03-30 23:44:06 +02:00
|
|
|
});
|
|
|
|
|
2020-03-31 10:57:53 +02:00
|
|
|
test("authenticatedRequest error bogus response", () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return 'token';
|
2020-03-31 10:57:53 +02:00
|
|
|
});
|
2020-03-30 23:44:06 +02:00
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
json: () => {
|
2020-04-06 18:32:10 +02:00
|
|
|
return {
|
|
|
|
error: ERROR_TYPE.SUCCESS,
|
|
|
|
};
|
2020-03-30 23:44:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
});
|
2020-03-31 10:57:53 +02:00
|
|
|
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
|
2020-03-30 23:44:06 +02:00
|
|
|
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
|
|
|
|
});
|
|
|
|
|
2020-03-31 10:57:53 +02:00
|
|
|
test("authenticatedRequest connection error", () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return 'token';
|
2020-03-31 10:57:53 +02:00
|
|
|
});
|
|
|
|
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
|
|
|
|
return Promise.reject()
|
|
|
|
});
|
|
|
|
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
|
|
|
|
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("authenticatedRequest error no token", () => {
|
2020-04-04 16:24:26 +02:00
|
|
|
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
|
|
|
|
return null;
|
2020-03-31 10:57:53 +02:00
|
|
|
});
|
|
|
|
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
|
2020-04-06 18:32:10 +02:00
|
|
|
.rejects.toBe(ERROR_TYPE.UNKNOWN);
|
2020-03-29 11:47:27 +02:00
|
|
|
});
|