Application Android et IOS pour l'amicale des élèves
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 5.4KB

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