application-amicale/__tests__/managers/ConnectionManager.test.js

211 lines
6.3 KiB
JavaScript
Raw Normal View History

jest.mock('react-native-keychain');
2020-03-29 11:47:27 +02:00
import React from 'react';
import ConnectionManager from "../../src/managers/ConnectionManager";
import {ERROR_TYPE} from "../../src/utils/WebData";
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();
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
});
test("isConnectionResponseValid", () => {
let json = {
error: 0,
data: {token: 'token'}
};
expect(c.isConnectionResponseValid(json)).toBeTrue();
json = {
error: 2,
data: {}
2020-04-04 16:24:26 +02:00
};
expect(c.isConnectionResponseValid(json)).toBeTrue();
json = {
error: 0,
data: {token: ''}
};
expect(c.isConnectionResponseValid(json)).toBeFalse();
json = {
error: 'prout',
data: {token: ''}
};
expect(c.isConnectionResponseValid(json)).toBeFalse();
});
2020-03-29 11:47:27 +02:00
test("connect bad credentials", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.BAD_CREDENTIALS,
data: {}
};
},
})
});
return expect(c.connect('email', 'password'))
2020-03-29 11:47:27 +02:00
.rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS);
});
test("connect good credentials", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.SUCCESS,
data: {token: 'token'}
};
},
})
});
2020-03-31 14:21:01 +02:00
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
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 {
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);
});
test("connect good credentials, fail save token", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.SUCCESS,
data: {token: 'token'}
};
},
})
});
2020-03-31 14:21:01 +02:00
jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
return Promise.reject(false);
});
return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.UNKNOWN);
});
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: () => {
return {
thing: true,
wrong: '',
}
},
})
});
return expect(c.connect('email', 'password'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
});
test("authenticatedRequest success", () => {
2020-04-04 16:24:26 +02:00
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return 'token';
});
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.SUCCESS,
data: {coucou: 'toi'}
};
},
})
});
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.resolves.toStrictEqual({coucou: 'toi'});
});
test("authenticatedRequest error wrong token", () => {
2020-04-04 16:24:26 +02:00
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return 'token';
});
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.BAD_TOKEN,
data: {}
};
},
})
});
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.rejects.toBe(ERROR_TYPE.BAD_TOKEN);
});
test("authenticatedRequest error bogus response", () => {
2020-04-04 16:24:26 +02:00
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return 'token';
});
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
error: ERROR_TYPE.SUCCESS,
};
},
})
});
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
});
test("authenticatedRequest connection error", () => {
2020-04-04 16:24:26 +02:00
jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
return 'token';
});
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;
});
return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
.rejects.toBe(ERROR_TYPE.UNKNOWN);
2020-03-29 11:47:27 +02:00
});