Refactor auth reducer tests, add tests for VERIFY_CREDENTIALS_FAIL

This commit is contained in:
Alex Gleason 2021-03-24 15:01:10 -05:00
parent 79c9c46a9a
commit 886ab93c70
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 98 additions and 110 deletions

View File

@ -1,124 +1,111 @@
import reducer from '../auth'; import reducer from '../auth';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import { Map as ImmutableMap, fromJS } from 'immutable';
import * as actions from 'soapbox/actions/auth'; import {
// import app from 'soapbox/__fixtures__/app.json'; AUTH_APP_CREATED,
import user from 'soapbox/__fixtures__/user.json'; AUTH_LOGGED_IN,
VERIFY_CREDENTIALS_FAIL,
} from 'soapbox/actions/auth';
describe('auth reducer', () => { describe('auth reducer', () => {
it('should return the initial state', () => { it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(ImmutableMap({ expect(reducer(undefined, {})).toEqual(ImmutableMap({
app: ImmutableMap(), app: ImmutableMap(),
user: ImmutableMap(), users: ImmutableMap(),
tokens: ImmutableList(), tokens: ImmutableMap(),
me: null,
})); }));
}); });
it('should handle AUTH_APP_CREATED', () => { describe('AUTH_APP_CREATED', () => {
const state = ImmutableMap({ }); it('should copy in the app', () => {
const auth = { const token = { token_type: 'Bearer', access_token: 'ABCDEFG' };
auth: { const action = { type: AUTH_APP_CREATED, app: token };
app: {
vapid_key: 'BHczIFh4Wn3Q_7wDgehaB8Ti3Uu8BoyOgXxkOVuEJRuEqxtd9TAno8K9ycz4myiQ1ruiyVfG6xT1JLeXtpxDzUs', const result = reducer(undefined, action);
token_type: 'Bearer', const expected = fromJS(token);
client_secret: 'HU6RGO4284Edr4zucuWmn8OFjcpVtMsoXJU0-8tpwRM',
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', expect(result.get('app')).toEqual(expected);
created_at: 1594050270, });
name: 'SoapboxFE_2020-07-06T15:43:31.989Z', });
client_id: 'Q0A2r_9ZcEORMenj9kuDRQc3UVL8ypQRoNJ6XQHWJU8',
expires_in: 600, describe('AUTH_LOGGED_IN', () => {
scope: 'read write follow push admin', it('should import the token', () => {
refresh_token: 'aydRA4eragIhavCdAyg6QQnDJmiMbdc-oEBvHYcW_PQ', const token = { token_type: 'Bearer', access_token: 'ABCDEFG' };
website: null, const action = { type: AUTH_LOGGED_IN, token };
id: '113',
access_token: 'pbXS8HkoWodrAt_QE1NENcwqigxgWr3P1RIQCKMN0Os', const result = reducer(undefined, action);
}, const expected = fromJS({ 'ABCDEFG': token });
user: {
access_token: 'UVBP2e17b4pTpb_h8fImIm3F5a66IBVb-JkyZHs4gLE', expect(result.get('tokens')).toEqual(expected);
expires_in: 600, });
me: 'https://social.teci.world/users/curtis',
refresh_token: 'c2DpbVxYZBJDogNn-VBNFES72yXPNUYQCv0CrXGOplY', it('should merge the token with existing state', () => {
scope: 'read write follow push admin', const state = fromJS({
token_type: 'Bearer', tokens: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' } },
}, });
tokens: [],
}, const expected = fromJS({
}; 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' },
'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' },
});
const action = { const action = {
type: actions.AUTH_APP_CREATED, type: AUTH_LOGGED_IN,
app: auth, token: { token_type: 'Bearer', access_token: 'HIJKLMN' },
}; };
expect(reducer(state, action).toJS()).toMatchObject({
app: auth, const result = reducer(state, action);
expect(result.get('tokens')).toEqual(expected);
}); });
}); });
// Fails with TypeError: cannot read property merge of undefined describe('VERIFY_CREDENTIALS_FAIL', () => {
// it('should handle the Action AUTH_APP_AUTHORIZED', () => { it('should delete the failed token', () => {
// const state = ImmutableMap({ const state = fromJS({
// auth: { tokens: {
// app: { 'ABCDEFG': { token_type: 'Bearer', access_token: 'ABCDEFG' },
// vapid_key: 'oldVapidKey', 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' },
// 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, const expected = fromJS({
user: user, 'HIJKLMN': { token_type: 'Bearer', access_token: 'HIJKLMN' },
};
expect(reducer(state, action).toJS()).toMatchObject({
user: user,
});
}); });
it('should handle the Action AUTH_LOGGED_OUT', () => { const action = { type: VERIFY_CREDENTIALS_FAIL, token: 'ABCDEFG' };
const state = ImmutableMap({ const result = reducer(state, action);
user: { expect(result.get('tokens')).toEqual(expected);
access_token: 'UVBP2e17b4pTpb_h8fImIm3F5a66IBVb-JkyZHs4gLE', });
expires_in: 600,
me: 'https://social.teci.world/users/curtis', it('should delete any users associated with the failed token', () => {
refresh_token: 'c2DpbVxYZBJDogNn-VBNFES72yXPNUYQCv0CrXGOplY', const state = fromJS({
scope: 'read write follow push admin', users: {
token_type: 'Bearer', '1234': { id: '1234', access_token: 'ABCDEFG' },
'5678': { id: '5678', access_token: 'HIJKLMN' },
}, },
}); });
const action = {
type: actions.AUTH_LOGGED_OUT, const expected = fromJS({
}; '5678': { id: '5678', access_token: 'HIJKLMN' },
expect(reducer(state, action).toJS()).toMatchObject({
user: {},
});
}); });
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');
});
});
}); });

View File

@ -11,6 +11,7 @@ import { Map as ImmutableMap, fromJS } from 'immutable';
const defaultState = ImmutableMap({ const defaultState = ImmutableMap({
app: ImmutableMap(), app: ImmutableMap(),
users: ImmutableMap(),
tokens: ImmutableMap(), tokens: ImmutableMap(),
me: null, me: null,
}); });