From 9e390d3404dfcdc859d957554a394daa2ce2dcc3 Mon Sep 17 00:00:00 2001 From: seotts Date: Tue, 7 Sep 2021 13:28:20 -0400 Subject: [PATCH] Unit tests for transfer error states --- test/unit/redux/studio-member-actions.test.js | 101 ++++++++++++++---- 1 file changed, 81 insertions(+), 20 deletions(-) diff --git a/test/unit/redux/studio-member-actions.test.js b/test/unit/redux/studio-member-actions.test.js index c5a3dea64..a76da4849 100644 --- a/test/unit/redux/studio-member-actions.test.js +++ b/test/unit/redux/studio-member-actions.test.js @@ -293,7 +293,7 @@ describe('inviteCurator', () => { }); test('error because of rate limit', async () => { api.mockImplementation((opts, callback) => { - callback(null, null, {statusCode: 429}); + callback(null, {}, {statusCode: 429}); }); await expect(store.dispatch(inviteCurator('user2'))) .rejects.toBe(Errors.RATE_LIMIT); @@ -406,26 +406,87 @@ describe('acceptInvitation', () => { expect(state.studio.invited).toBe(true); expect(state.studio.curator).toBe(false); }); +}); - describe('transferHost', () => { - beforeEach(() => { - store = configureStore(reducers, { - ...initialState, - studio: { - id: 123123, - managers: 3 - } - }); - }); - - test('transfers the host on success', async () => { - api.mockImplementation((opts, callback) => { - callback(null, {}, {statusCode: 200}); - }); - await store.dispatch(transferHost('password', 'newHostName', 'newHostId')); - const state = store.getState(); - expect(api.mock.calls[0][0].uri).toBe('/studios/123123/transfer/newHostName'); - expect(state.studio.owner).toBe('newHostId'); +describe('transferHost', () => { + beforeEach(() => { + store = configureStore(reducers, { + ...initialState, + studio: { + id: 123123, + managers: 3, + owner: 'oldHost' + } }); }); + + test('transfers the host on success', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {}, {statusCode: 200}); + }); + await store.dispatch(transferHost('password', 'newHostName', 'newHostId')); + const state = store.getState(); + expect(api.mock.calls[0][0].uri).toBe('/studios/123123/transfer/newHostName'); + expect(state.studio.owner).toBe('newHostId'); + }); + + test('error because of permissions issue', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {}, {statusCode: 403}); + }); + await expect(store.dispatch(transferHost('password', 'newHostName', 'newHostId'))) + .rejects.toBe(Errors.PERMISSION); + const state = store.getState(); + expect(state.studio.owner).toBe('oldHost'); + }); + + test('error because of authorization issue', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {}, {statusCode: 401}); + }); + await expect(store.dispatch(transferHost('password', 'newHostName', 'newHostId'))) + .rejects.toBe(Errors.PERMISSION); + const state = store.getState(); + expect(state.studio.owner).toBe('oldHost'); + }); + + test('error because of issue with new host', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {}, {statusCode: 409}); + }); + await expect(store.dispatch(transferHost('password', 'newHostName', 'newHostId'))) + .rejects.toBe(Errors.CANNOT_BE_HOST); + const state = store.getState(); + expect(state.studio.owner).toBe('oldHost'); + }); + + test('error because of incorrect password', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {status: 'error', message: 'password incorrect'}, {statusCode: 401}); + }); + await expect(store.dispatch(transferHost('password', 'newHostName', 'newHostId'))) + .rejects.toBe(Errors.PASSWORD); + const state = store.getState(); + expect(state.studio.owner).toBe('oldHost'); + }); + + test('error because of too many password attempts', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {status: 'error', message: 'try again later'}, {statusCode: 429}); + }); + await expect(store.dispatch(transferHost('password', 'newHostName', 'newHostId'))) + .rejects.toBe(Errors.PASSWORD_ATTEMPT_LIMIT); + const state = store.getState(); + expect(state.studio.owner).toBe('oldHost'); + }); + + test('error because of rate limit', async () => { + api.mockImplementation((opts, callback) => { + callback(null, {}, {statusCode: 429}); + }); + await expect(store.dispatch(transferHost('password', 'newHostName', 'newHostId'))) + .rejects.toBe(Errors.RATE_LIMIT); + const state = store.getState(); + expect(state.studio.owner).toBe('oldHost'); + }); });