Browse Source

Updated unit tests

Arnaud Vergnet 4 years ago
parent
commit
cacfb2862c
2 changed files with 49 additions and 27 deletions
  1. 41
    19
      __tests__/managers/ConnectionManager.test.js
  2. 8
    8
      managers/ConnectionManager.js

+ 41
- 19
__tests__/managers/ConnectionManager.test.js View File

@@ -11,20 +11,19 @@ afterEach(() => {
11 11
 });
12 12
 
13 13
 test('isLoggedIn yes', () => {
14
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
15
-        return Promise.resolve(true);
14
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
15
+        return 'token';
16 16
     });
17
-    return expect(c.isLoggedIn()).resolves.toBe(true);
17
+    return expect(c.isLoggedIn()).toBe(true);
18 18
 });
19 19
 
20 20
 test('isLoggedIn no', () => {
21
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
22
-        return Promise.reject(false);
21
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
22
+        return null;
23 23
     });
24
-    return expect(c.isLoggedIn()).rejects.toBe(false);
24
+    return expect(c.isLoggedIn()).toBe(false);
25 25
 });
26 26
 
27
-
28 27
 test('recoverLogin error crypto', () => {
29 28
     jest.spyOn(SecureStore, 'getItemAsync').mockImplementationOnce(() => {
30 29
         return Promise.reject();
@@ -111,7 +110,12 @@ test("isConnectionResponseValid", () => {
111 110
         state: false,
112 111
     };
113 112
     expect(c.isConnectionResponseValid(json)).toBeTrue();
114
-
113
+    json = {
114
+        state: false,
115
+        message: 'Adresse mail ou mot de passe incorrect',
116
+        token: ''
117
+    };
118
+    expect(c.isConnectionResponseValid(json)).toBeTrue();
115 119
     json = {
116 120
         state: true,
117 121
         message: 'Connexion confirmée',
@@ -143,7 +147,6 @@ test("isConnectionResponseValid", () => {
143 147
     expect(c.isConnectionResponseValid(json)).toBeFalse();
144 148
 });
145 149
 
146
-
147 150
 test("connect bad credentials", () => {
148 151
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
149 152
         return Promise.resolve({
@@ -178,6 +181,25 @@ test("connect good credentials", () => {
178 181
     return expect(c.connect('email', 'password')).resolves.toBeTruthy();
179 182
 });
180 183
 
184
+test("connect good credentials no consent", () => {
185
+    jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
186
+        return Promise.resolve({
187
+            json: () => {
188
+                return {
189
+                    state: false,
190
+                    message: 'pas de consent',
191
+                    token: '',
192
+                    data: {
193
+                        consent: false,
194
+                    }
195
+                }
196
+            },
197
+        })
198
+    });
199
+    return expect(c.connect('email', 'password'))
200
+        .rejects.toBe(ERROR_TYPE.NO_CONSENT);
201
+});
202
+
181 203
 test("connect good credentials, fail save token", () => {
182 204
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
183 205
         return Promise.resolve({
@@ -221,8 +243,8 @@ test("connect bogus response 1", () => {
221 243
 
222 244
 
223 245
 test("authenticatedRequest success", () => {
224
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
225
-        return Promise.resolve('token');
246
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
247
+        return 'token';
226 248
     });
227 249
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
228 250
         return Promise.resolve({
@@ -236,8 +258,8 @@ test("authenticatedRequest success", () => {
236 258
 });
237 259
 
238 260
 test("authenticatedRequest error wrong token", () => {
239
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
240
-        return Promise.resolve('token');
261
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
262
+        return 'token';
241 263
     });
242 264
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
243 265
         return Promise.resolve({
@@ -251,8 +273,8 @@ test("authenticatedRequest error wrong token", () => {
251 273
 });
252 274
 
253 275
 test("authenticatedRequest error bogus response", () => {
254
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
255
-        return Promise.resolve('token');
276
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
277
+        return 'token';
256 278
     });
257 279
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
258 280
         return Promise.resolve({
@@ -266,8 +288,8 @@ test("authenticatedRequest error bogus response", () => {
266 288
 });
267 289
 
268 290
 test("authenticatedRequest connection error", () => {
269
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
270
-        return Promise.resolve('token');
291
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
292
+        return 'token';
271 293
     });
272 294
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
273 295
         return Promise.reject()
@@ -277,8 +299,8 @@ test("authenticatedRequest connection error", () => {
277 299
 });
278 300
 
279 301
 test("authenticatedRequest error no token", () => {
280
-    jest.spyOn(ConnectionManager.prototype, 'recoverLogin').mockImplementationOnce(() => {
281
-        return Promise.reject(false);
302
+    jest.spyOn(ConnectionManager.prototype, 'getToken').mockImplementationOnce(() => {
303
+        return null;
282 304
     });
283 305
     jest.spyOn(global, 'fetch').mockImplementationOnce(() => {
284 306
         return Promise.resolve({

+ 8
- 8
managers/ConnectionManager.js View File

@@ -18,8 +18,6 @@ export default class ConnectionManager {
18 18
     #email: string;
19 19
     #token: string | null;
20 20
 
21
-    loginCallback: Function;
22
-
23 21
     listeners: Array<Function>;
24 22
 
25 23
     constructor() {
@@ -54,8 +52,8 @@ export default class ConnectionManager {
54 52
 
55 53
     async recoverLogin() {
56 54
         return new Promise((resolve, reject) => {
57
-            if (this.#token !== null)
58
-                resolve(this.#token);
55
+            if (this.getToken() !== null)
56
+                resolve(this.getToken());
59 57
             else {
60 58
                 SecureStore.getItemAsync('token')
61 59
                     .then((token) => {
@@ -74,7 +72,7 @@ export default class ConnectionManager {
74 72
     }
75 73
 
76 74
     isLoggedIn() {
77
-        return this.#token !== null;
75
+        return this.getToken() !== null;
78 76
     }
79 77
 
80 78
     async saveLogin(email: string, token: string) {
@@ -130,7 +128,9 @@ export default class ConnectionManager {
130 128
                                 .catch(() => {
131 129
                                     reject(ERROR_TYPE.SAVE_TOKEN);
132 130
                                 });
133
-                        } else if (data.data.consent !== undefined && !data.data.consent)
131
+                        } else if (data.data !== undefined
132
+                            && data.data.consent !== undefined
133
+                            && !data.data.consent)
134 134
                             reject(ERROR_TYPE.NO_CONSENT);
135 135
                         else
136 136
                             reject(ERROR_TYPE.BAD_CREDENTIALS);
@@ -170,14 +170,14 @@ export default class ConnectionManager {
170 170
 
171 171
     async authenticatedRequest(url: string) {
172 172
         return new Promise((resolve, reject) => {
173
-            if (this.#token !== null) {
173
+            if (this.getToken() !== null) {
174 174
                 fetch(url, {
175 175
                     method: 'POST',
176 176
                     headers: new Headers({
177 177
                         'Accept': 'application/json',
178 178
                         'Content-Type': 'application/json',
179 179
                     }),
180
-                    body: JSON.stringify({token: this.#token})
180
+                    body: JSON.stringify({token: this.getToken()})
181 181
                 }).then(async (response) => response.json())
182 182
                     .then((data) => {
183 183
                         if (this.isRequestResponseValid(data)) {

Loading…
Cancel
Save