123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- import React from 'react';
- import ConnectionManager from "../../src/managers/ConnectionManager";
- import {ERROR_TYPE} from "../../src/utils/WebData";
- import * as SecureStore from 'expo-secure-store';
-
- let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
-
- const c = ConnectionManager.getInstance();
-
- afterEach(() => {
- jest.restoreAllMocks();
- });
-
- test('isLoggedIn yes', () => {
- jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
- return 'token';
- });
- return expect(c.isLoggedIn()).toBe(true);
- });
-
- test('isLoggedIn no', () => {
- jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
- return null;
- });
- return expect(c.isLoggedIn()).toBe(false);
- });
-
- 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("isConnectionResponseValid", () => {
- let json = {
- error: 0,
- data: {token: 'token'}
- };
- expect(c.isConnectionResponseValid(json)).toBeTrue();
- json = {
- error: 2,
- data: {}
- };
- 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();
- });
-
- 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'))
- .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'}
- };
- },
- })
- });
- jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
- return Promise.resolve(true);
- });
- return expect(c.connect('email', 'password')).resolves.toBeTruthy();
- });
-
- test("connect good credentials no consent", () => {
- jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
- return Promise.resolve({
- json: () => {
- return {
- error: ERROR_TYPE.NO_CONSENT,
- data: {}
- };
- },
- })
- });
- 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'}
- };
- },
- })
- });
- 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", () => {
- 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", () => {
- 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", () => {
- 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", () => {
- 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", () => {
- 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);
- });
|