Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionManager.test.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React from 'react';
  2. import ConnectionManager from "../../src/managers/ConnectionManager";
  3. import {ERROR_TYPE} from "../../src/utils/WebData";
  4. import * as SecureStore from 'expo-secure-store';
  5. let fetch = require('isomorphic-fetch'); // fetch is not implemented in nodeJS but in react-native
  6. const c = ConnectionManager.getInstance();
  7. afterEach(() => {
  8. jest.restoreAllMocks();
  9. });
  10. test('isLoggedIn yes', () => {
  11. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  12. return 'token';
  13. });
  14. return expect(c.isLoggedIn()).toBe(true);
  15. });
  16. test('isLoggedIn no', () => {
  17. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  18. return null;
  19. });
  20. return expect(c.isLoggedIn()).toBe(false);
  21. });
  22. test('recoverLogin error crypto', () => {
  23. jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
  24. return Promise.reject();
  25. });
  26. return expect(c.recoverLogin()).rejects.toBe(false);
  27. });
  28. test('recoverLogin success crypto', () => {
  29. jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
  30. return Promise.resolve('token1');
  31. });
  32. return expect(c.recoverLogin()).resolves.toBe('token1');
  33. });
  34. test('saveLogin success', () => {
  35. jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
  36. return Promise.resolve();
  37. });
  38. return expect(c.saveLogin('email', 'token2')).resolves.toBeTruthy();
  39. });
  40. test('saveLogin error', () => {
  41. jest.spyOn(SecureStore, 'setItemAsync').mockImplementationOnce(() => {
  42. return Promise.reject();
  43. });
  44. return expect(c.saveLogin('email', 'token3')).rejects.toBeFalsy();
  45. });
  46. test('recoverLogin error crypto with saved token', () => {
  47. jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
  48. return Promise.reject();
  49. });
  50. return expect(c.recoverLogin()).resolves.toBe('token2');
  51. });
  52. test('recoverLogin success saved', () => {
  53. return expect(c.recoverLogin()).resolves.toBe('token2');
  54. });
  55. test("isConnectionResponseValid", () => {
  56. let json = {
  57. error: 0,
  58. data: {token: 'token'}
  59. };
  60. expect(c.isConnectionResponseValid(json)).toBeTrue();
  61. json = {
  62. error: 2,
  63. data: {}
  64. };
  65. expect(c.isConnectionResponseValid(json)).toBeTrue();
  66. json = {
  67. error: 0,
  68. data: {token: ''}
  69. };
  70. expect(c.isConnectionResponseValid(json)).toBeFalse();
  71. json = {
  72. error: 'prout',
  73. data: {token: ''}
  74. };
  75. expect(c.isConnectionResponseValid(json)).toBeFalse();
  76. });
  77. test("connect bad credentials", () => {
  78. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  79. return Promise.resolve({
  80. json: () => {
  81. return {
  82. error: ERROR_TYPE.BAD_CREDENTIALS,
  83. data: {}
  84. };
  85. },
  86. })
  87. });
  88. return expect(c.connect('email', 'password'))
  89. .rejects.toBe(ERROR_TYPE.BAD_CREDENTIALS);
  90. });
  91. test("connect good credentials", () => {
  92. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  93. return Promise.resolve({
  94. json: () => {
  95. return {
  96. error: ERROR_TYPE.SUCCESS,
  97. data: {token: 'token'}
  98. };
  99. },
  100. })
  101. });
  102. jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
  103. return Promise.resolve(true);
  104. });
  105. return expect(c.connect('email', 'password')).resolves.toBeTruthy();
  106. });
  107. test("connect good credentials no consent", () => {
  108. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  109. return Promise.resolve({
  110. json: () => {
  111. return {
  112. error: ERROR_TYPE.NO_CONSENT,
  113. data: {}
  114. };
  115. },
  116. })
  117. });
  118. return expect(c.connect('email', 'password'))
  119. .rejects.toBe(ERROR_TYPE.NO_CONSENT);
  120. });
  121. test("connect good credentials, fail save token", () => {
  122. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  123. return Promise.resolve({
  124. json: () => {
  125. return {
  126. error: ERROR_TYPE.SUCCESS,
  127. data: {token: 'token'}
  128. };
  129. },
  130. })
  131. });
  132. jest.spyOn(ConnectionManager.prototype, 'saveLogin').mockImplementationOnce(() => {
  133. return Promise.reject(false);
  134. });
  135. return expect(c.connect('email', 'password')).rejects.toBe(ERROR_TYPE.UNKNOWN);
  136. });
  137. test("connect connection error", () => {
  138. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  139. return Promise.reject();
  140. });
  141. return expect(c.connect('email', 'password'))
  142. .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
  143. });
  144. test("connect bogus response 1", () => {
  145. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  146. return Promise.resolve({
  147. json: () => {
  148. return {
  149. thing: true,
  150. wrong: '',
  151. }
  152. },
  153. })
  154. });
  155. return expect(c.connect('email', 'password'))
  156. .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
  157. });
  158. test("authenticatedRequest success", () => {
  159. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  160. return 'token';
  161. });
  162. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  163. return Promise.resolve({
  164. json: () => {
  165. return {
  166. error: ERROR_TYPE.SUCCESS,
  167. data: {coucou: 'toi'}
  168. };
  169. },
  170. })
  171. });
  172. return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
  173. .resolves.toStrictEqual({coucou: 'toi'});
  174. });
  175. test("authenticatedRequest error wrong token", () => {
  176. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  177. return 'token';
  178. });
  179. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  180. return Promise.resolve({
  181. json: () => {
  182. return {
  183. error: ERROR_TYPE.BAD_TOKEN,
  184. data: {}
  185. };
  186. },
  187. })
  188. });
  189. return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
  190. .rejects.toBe(ERROR_TYPE.BAD_TOKEN);
  191. });
  192. test("authenticatedRequest error bogus response", () => {
  193. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  194. return 'token';
  195. });
  196. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  197. return Promise.resolve({
  198. json: () => {
  199. return {
  200. error: ERROR_TYPE.SUCCESS,
  201. };
  202. },
  203. })
  204. });
  205. return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
  206. .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
  207. });
  208. test("authenticatedRequest connection error", () => {
  209. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  210. return 'token';
  211. });
  212. jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
  213. return Promise.reject()
  214. });
  215. return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
  216. .rejects.toBe(ERROR_TYPE.CONNECTION_ERROR);
  217. });
  218. test("authenticatedRequest error no token", () => {
  219. jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
  220. return null;
  221. });
  222. return expect(c.authenticatedRequest('https://www.amicale-insat.fr/api/token/check'))
  223. .rejects.toBe(ERROR_TYPE.UNKNOWN);
  224. });