|
9 | 9 | authenticateSucceeded, |
10 | 10 | fetch as fetchAction, |
11 | 11 | invalidateSession, |
12 | | - updateSession |
| 12 | + updateSession, |
| 13 | + invalidateSessionFailed |
13 | 14 | } from '../src/actions' |
14 | 15 | import { |
15 | 16 | failAuthenticator, |
@@ -238,6 +239,75 @@ describe('auth middleware', () => { |
238 | 239 | }) |
239 | 240 | }) |
240 | 241 |
|
| 242 | + describe('when invalidating', () => { |
| 243 | + it('calls authenticators invalidate', () => { |
| 244 | + const middleware = configureMiddleware(spiedAuthenticator) |
| 245 | + const mockStore = configureStore([middleware]) |
| 246 | + const data = { username: 'test', password: 'password' } |
| 247 | + const store = mockStore({ session: { authenticator: 'test', data } }) |
| 248 | + const invalidateAction = invalidateSession() |
| 249 | + |
| 250 | + store.dispatch(invalidateAction) |
| 251 | + expect(spiedAuthenticator.invalidate).toHaveBeenCalledWith(data) |
| 252 | + |
| 253 | + spiedAuthenticator.authenticate.mockClear() |
| 254 | + spiedAuthenticator.invalidate.mockClear() |
| 255 | + }) |
| 256 | + |
| 257 | + describe('when there is no session', () => { |
| 258 | + it('throws error', () => { |
| 259 | + const authenticator = createAuthenticator({ |
| 260 | + name: 'fake' |
| 261 | + }) |
| 262 | + const middleware = configureMiddleware(authenticator) |
| 263 | + const mockStore = configureStore([middleware]) |
| 264 | + const store = mockStore() |
| 265 | + const action = invalidateSession('not-real', {}) |
| 266 | + |
| 267 | + expect(() => store.dispatch(action)).toThrow( |
| 268 | + 'No session data to invalidate. Be sure you authenticate the ' + |
| 269 | + 'session before you try to invalidate it' |
| 270 | + ) |
| 271 | + }) |
| 272 | + }) |
| 273 | + |
| 274 | + describe('when redux store authenticator is not found', () => { |
| 275 | + it('returns a rejected promise and dispatches invalidate Session', async () => { |
| 276 | + const authenticator = createAuthenticator({ |
| 277 | + name: 'unmatchable' |
| 278 | + }) |
| 279 | + const middleware = configureMiddleware(authenticator) |
| 280 | + const mockStore = configureStore([middleware]) |
| 281 | + const store = mockStore({ session: { } }) |
| 282 | + await expect(store.dispatch(invalidateSession())).rejects.toEqual( |
| 283 | + new Error( |
| 284 | + 'No authenticated session. Be sure you authenticate the session ' + |
| 285 | + 'before you try to invalidate it' |
| 286 | + ) |
| 287 | + ) |
| 288 | + |
| 289 | + expect(store.getActions()).toContainEqual(invalidateSessionFailed()) |
| 290 | + }) |
| 291 | + }) |
| 292 | + |
| 293 | + describe('when there is no authenticator', () => { |
| 294 | + it('throws error', () => { |
| 295 | + const authenticator = createAuthenticator({ |
| 296 | + name: 'fake' |
| 297 | + }) |
| 298 | + const middleware = configureMiddleware(authenticator) |
| 299 | + const mockStore = configureStore([middleware]) |
| 300 | + const store = mockStore() |
| 301 | + const action = invalidateSession() |
| 302 | + |
| 303 | + expect(() => store.dispatch(action)).toThrow( |
| 304 | + 'No session data to invalidate. Be sure you authenticate the ' + |
| 305 | + 'session before you try to invalidate it' |
| 306 | + ) |
| 307 | + }) |
| 308 | + }) |
| 309 | + }) |
| 310 | + |
241 | 311 | describe('session restoration', () => { |
242 | 312 | it('hydrates session data from storage', () => { |
243 | 313 | const middleware = configureMiddleware() |
@@ -387,19 +457,21 @@ describe('auth middleware', () => { |
387 | 457 | }) |
388 | 458 |
|
389 | 459 | describe('when request returns 401 unauthorized', () => { |
390 | | - it('dispatches invalidateSession', async () => { |
| 460 | + it('calls authenticators invalidate', async () => { |
391 | 461 | fetch.mockResponse(JSON.stringify({ ok: true }), { status: 401 }) |
392 | | - const middleware = configureMiddleware() |
| 462 | + const middleware = configureMiddleware(spiedAuthenticator) |
393 | 463 | const mockStore = configureStore([middleware]) |
394 | 464 | const data = { token: '1235' } |
395 | | - const store = mockStore({ session: { data, isAuthenticated: true } }) |
396 | | - const invalidateAction = invalidateSession() |
| 465 | + const store = mockStore({ |
| 466 | + session: { data, isAuthenticated: true, authenticator: 'test' } |
| 467 | + }) |
397 | 468 |
|
398 | 469 | await store.dispatch(fetchAction('https://test.com')) |
399 | 470 |
|
400 | | - expect(store.getActions()).toEqual( |
401 | | - expect.arrayContaining([invalidateAction]) |
402 | | - ) |
| 471 | + expect(spiedAuthenticator.invalidate).toHaveBeenCalledWith(data) |
| 472 | + |
| 473 | + spiedAuthenticator.authenticate.mockClear() |
| 474 | + spiedAuthenticator.invalidate.mockClear() |
403 | 475 | }) |
404 | 476 |
|
405 | 477 | it('does not dispatch if not authenticated', async () => { |
|
0 commit comments