From 886ab93c70ff552ed0e0ec3a980f7f0a7f9b573b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 24 Mar 2021 15:01:10 -0500 Subject: [PATCH] Refactor auth reducer tests, add tests for VERIFY_CREDENTIALS_FAIL --- app/soapbox/reducers/__tests__/auth-test.js | 207 +++++++++----------- app/soapbox/reducers/auth.js | 1 + 2 files changed, 98 insertions(+), 110 deletions(-) diff --git a/app/soapbox/reducers/__tests__/auth-test.js b/app/soapbox/reducers/__tests__/auth-test.js index acbf77ffb..381a871a3 100644 --- a/app/soapbox/reducers/__tests__/auth-test.js +++ b/app/soapbox/reducers/__tests__/auth-test.js @@ -1,124 +1,111 @@ import reducer from '../auth'; -import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; -import * as actions from 'soapbox/actions/auth'; -// import app from 'soapbox/__fixtures__/app.json'; -import user from 'soapbox/__fixtures__/user.json'; +import { Map as ImmutableMap, fromJS } from 'immutable'; +import { + AUTH_APP_CREATED, + AUTH_LOGGED_IN, + VERIFY_CREDENTIALS_FAIL, +} from 'soapbox/actions/auth'; describe('auth reducer', () => { it('should return the initial state', () => { expect(reducer(undefined, {})).toEqual(ImmutableMap({ app: ImmutableMap(), - user: ImmutableMap(), - tokens: ImmutableList(), + users: ImmutableMap(), + tokens: ImmutableMap(), + me: null, })); }); - it('should handle AUTH_APP_CREATED', () => { - const state = ImmutableMap({ }); - const auth = { - auth: { - app: { - vapid_key: 'BHczIFh4Wn3Q_7wDgehaB8Ti3Uu8BoyOgXxkOVuEJRuEqxtd9TAno8K9ycz4myiQ1ruiyVfG6xT1JLeXtpxDzUs', - token_type: 'Bearer', - client_secret: 'HU6RGO4284Edr4zucuWmn8OFjcpVtMsoXJU0-8tpwRM', - redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', - created_at: 1594050270, - name: 'SoapboxFE_2020-07-06T15:43:31.989Z', - client_id: 'Q0A2r_9ZcEORMenj9kuDRQc3UVL8ypQRoNJ6XQHWJU8', - expires_in: 600, - scope: 'read write follow push admin', - refresh_token: 'aydRA4eragIhavCdAyg6QQnDJmiMbdc-oEBvHYcW_PQ', - website: null, - id: '113', - access_token: 'pbXS8HkoWodrAt_QE1NENcwqigxgWr3P1RIQCKMN0Os', + describe('AUTH_APP_CREATED', () => { + it('should copy in the app', () => { + const token = { token_type: 'Bearer', access_token: 'ABCDEFG' }; + const action = { type: AUTH_APP_CREATED, app: token }; + + const result = reducer(undefined, action); + const expected = fromJS(token); + + expect(result.get('app')).toEqual(expected); + }); + }); + + describe('AUTH_LOGGED_IN', () => { + it('should import the token', () => { + const token = { token_type: 'Bearer', access_token: 'ABCDEFG' }; + const action = { type: AUTH_LOGGED_IN, token }; + + const result = reducer(undefined, action); + const expected = fromJS({ 'ABCDEFG': token }); + + expect(result.get('tokens')).toEqual(expected); + }); + + it('should merge the token with existing state', () => { + const state = fromJS({ + tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' } }, + }); + + const expected = fromJS({ + 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' }, + 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' }, + }); + + const action = { + type: AUTH_LOGGED_IN, + token: { token_type: 'Bearer', access_token: 'HIJKLMN' }, + }; + + const result = reducer(state, action); + expect(result.get('tokens')).toEqual(expected); + }); + }); + + describe('VERIFY_CREDENTIALS_FAIL', () => { + it('should delete the failed token', () => { + const state = fromJS({ + tokens: { + 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' }, + 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' }, }, - user: { - access_token: 'UVBP2e17b4pTpb_h8fImIm3F5a66IBVb-JkyZHs4gLE', - expires_in: 600, - me: 'https://social.teci.world/users/curtis', - refresh_token: 'c2DpbVxYZBJDogNn-VBNFES72yXPNUYQCv0CrXGOplY', - scope: 'read write follow push admin', - token_type: 'Bearer', + }); + + const expected = fromJS({ + 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' }, + }); + + const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' }; + const result = reducer(state, action); + expect(result.get('tokens')).toEqual(expected); + }); + + it('should delete any users associated with the failed token', () => { + const state = fromJS({ + users: { + '1234': { id: '1234', access_token: 'ABCDEFG' }, + '5678': { id: '5678', access_token: 'HIJKLMN' }, }, - tokens: [], - }, - }; - const action = { - type: actions.AUTH_APP_CREATED, - app: auth, - }; - expect(reducer(state, action).toJS()).toMatchObject({ - app: auth, + }); + + const expected = fromJS({ + '5678': { id: '5678', access_token: 'HIJKLMN' }, + }); + + const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' }; + const result = reducer(state, action); + expect(result.get('users')).toEqual(expected); + }); + + it('should reassign `me` to the next in line', () => { + const state = fromJS({ + me: '1234', + users: { + '1234': { id: '1234', access_token: 'ABCDEFG' }, + '5678': { id: '5678', access_token: 'HIJKLMN' }, + }, + }); + + const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' }; + const result = reducer(state, action); + expect(result.get('me')).toEqual('5678'); }); }); - - // Fails with TypeError: cannot read property merge of undefined - // it('should handle the Action AUTH_APP_AUTHORIZED', () => { - // const state = ImmutableMap({ - // auth: { - // app: { - // vapid_key: 'oldVapidKey', - // token_type: 'Bearer', - // client_secret: 'oldClientSecret', - // redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', - // created_at: 1594764335, - // name: 'SoapboxFE_2020-07-14T22:05:17.054Z', - // client_id: 'bjiy8AxGKXXesfZcyp_iN-uQVE6Cnl03efWoSdOPh9M', - // expires_in: 600, - // scope: 'read write follow push admin', - // refresh_token: 'oldRefreshToken', - // website: null, - // id: '134', - // access_token: 'oldAccessToken', - // }, - // }, - // }); - // const action = { - // type: actions.AUTH_APP_AUTHORIZED, - // app: app, - // }; - // expect(reducer(state, action).toJS()).toMatchObject({ - // app: app, - // }); - // }); - - it('should handle the Action AUTH_LOGGED_IN', () => { - const state = ImmutableMap({ - user: { - access_token: 'UVBP2e17b4pTpb_h8fImIm3F5a66IBVb-JkyZHs4gLE', - expires_in: 600, - me: 'https://social.teci.world/users/curtis', - refresh_token: 'c2DpbVxYZBJDogNn-VBNFES72yXPNUYQCv0CrXGOplY', - scope: 'read write follow push admin', - token_type: 'Bearer', - }, - }); - const action = { - type: actions.AUTH_LOGGED_IN, - user: user, - }; - expect(reducer(state, action).toJS()).toMatchObject({ - user: user, - }); - }); - - it('should handle the Action AUTH_LOGGED_OUT', () => { - const state = ImmutableMap({ - user: { - access_token: 'UVBP2e17b4pTpb_h8fImIm3F5a66IBVb-JkyZHs4gLE', - expires_in: 600, - me: 'https://social.teci.world/users/curtis', - refresh_token: 'c2DpbVxYZBJDogNn-VBNFES72yXPNUYQCv0CrXGOplY', - scope: 'read write follow push admin', - token_type: 'Bearer', - }, - }); - const action = { - type: actions.AUTH_LOGGED_OUT, - }; - expect(reducer(state, action).toJS()).toMatchObject({ - user: {}, - }); - }); - }); diff --git a/app/soapbox/reducers/auth.js b/app/soapbox/reducers/auth.js index ef099bd4e..671ef0d0a 100644 --- a/app/soapbox/reducers/auth.js +++ b/app/soapbox/reducers/auth.js @@ -11,6 +11,7 @@ import { Map as ImmutableMap, fromJS } from 'immutable'; const defaultState = ImmutableMap({ app: ImmutableMap(), + users: ImmutableMap(), tokens: ImmutableMap(), me: null, });