Improved connection manager and added new tests

This commit is contained in:
Arnaud Vergnet 2020-03-30 23:44:06 +02:00
parent 0b19915a62
commit 5aa3afd383
3 changed files with 170 additions and 15 deletions

View file

@ -1,15 +1,143 @@
import React from 'react'; import React from 'react';
import ConnectionManager, {ERROR_TYPE} from "../../managers/ConnectionManager"; import ConnectionManager, {ERROR_TYPE} from "../../managers/ConnectionManager";
import * as SecureStore from 'expo-secure-store';
let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
const fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
const c = ConnectionManager.getInstance(); const c = ConnectionManager.getInstance();
test("connect bad credentials", () => { test("connect bad credentials", () => {
return expect(c.connect('truc', 'chose')) jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
state: false,
message: 'Adresse mail ou mot de passe incorrect',
token: ''
}
},
})
});
return expect(c.connect('email', 'password'))
.rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS); .rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS);
}); });
test("connect good credentials", () => { test("connect good credentials", () => {
return expect(c.connect('vergnet@etud.insa-toulouse.fr', 'Coucoù512')) jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
.resolves.toBe('test'); return Promise.resolve({
json: () => {
return {
state: true,
message: 'Connexion confirmée',
token: 'token'
}
},
})
});
jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
return Promise.resolve(true);
});
return expect(c.connect('email', 'password')).resolves.toBeTruthy();
});
test("connect good credentials, fail save token", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
state: true,
message: 'Connexion confirmée',
token: 'token'
}
},
})
});
jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
return Promise.reject(false);
});
return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.SAVE_TOKEN);
});
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("connect bogus response 2", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
state: true,
message: '',
}
},
})
});
return expect(c.connect('email', 'password'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
});
test("connect bogus response 3", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
state: false,
message: '',
}
},
})
});
return expect(c.connect('email', 'password'))
.rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS);
});
test("connect bogus response 4", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
message: '',
token: '',
}
},
})
});
return expect(c.connect('email', 'password'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
});
test("connect bogus response 5", () => {
jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
return Promise.resolve({
json: () => {
return {
state: true,
message: 'Connexion confirmée',
token: ''
}
},
})
});
return expect(c.connect('email', 'password'))
.rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
}); });

View file

@ -1,8 +1,11 @@
// @flow // @flow
import * as SecureStore from 'expo-secure-store';
export const ERROR_TYPE = { export const ERROR_TYPE = {
BAD_CREDENTIALS: 0, BAD_CREDENTIALS: 0,
CONNECTION_ERROR: 1 CONNECTION_ERROR: 1,
SAVE_TOKEN: 2,
}; };
const AUTH_URL = "https://www.amicale-insat.fr/api/password"; const AUTH_URL = "https://www.amicale-insat.fr/api/password";
@ -27,6 +30,20 @@ export default class ConnectionManager {
ConnectionManager.instance; ConnectionManager.instance;
} }
async saveLogin(email: string, token: string) {
this.#token = token;
this.#email = email;
return new Promise((resolve, reject) => {
SecureStore.setItemAsync('token', token)
.then(() => {
resolve(true);
})
.catch(error => {
reject(false);
});
});
}
async connect(email: string, password: string) { async connect(email: string, password: string) {
let data = { let data = {
email: email, email: email,
@ -42,24 +59,31 @@ export default class ConnectionManager {
body: JSON.stringify(data) body: JSON.stringify(data)
}).then(async (response) => response.json()) }).then(async (response) => response.json())
.then((data) => { .then((data) => {
console.log(data); if (this.isResponseValid(data)) {
if (this.isResponseValid(data)) if (data.state) {
resolve({success: data.success, token: data.token}); this.saveLogin(email, data.token)
else .then(() => {
reject(ERROR_TYPE.BAD_CREDENTIALS); resolve(true);
})
.catch(() => {
reject(ERROR_TYPE.SAVE_TOKEN);
});
} else
reject(ERROR_TYPE.BAD_CREDENTIALS);
} else
reject(ERROR_TYPE.CONNECTION_ERROR);
}) })
.catch((error) => { .catch((error) => {
console.log(error);
reject(ERROR_TYPE.CONNECTION_ERROR); reject(ERROR_TYPE.CONNECTION_ERROR);
}); });
}); });
} }
isResponseValid(response: Object) { isResponseValid(response: Object) {
return response !== undefined let valid = response !== undefined && response.state !== undefined;
&& response.success !== undefined if (valid && response.state)
&& response.success valid = valid && response.token !== undefined && response.token !== '';
&& response.token !== undefined; return valid;
} }
} }

View file

@ -12,6 +12,9 @@
}, },
"jest": { "jest": {
"preset": "react-native", "preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element|@react-native-community|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base)"
],
"setupFilesAfterEnv": [ "setupFilesAfterEnv": [
"jest-extended" "jest-extended"
] ]