From 583971ac7e6cf34a56d6e628e071a59b384afb65 Mon Sep 17 00:00:00 2001 From: Cuong Vu Date: Tue, 14 Jan 2020 10:33:29 +0700 Subject: [PATCH] [CLD-713] Update the Change Password flows to use npm package (#266) --- src/sagas/__tests__/settings.ts | 31 +++++++++++++++++----- src/sagas/settings.ts | 47 ++++++++++++--------------------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/sagas/__tests__/settings.ts b/src/sagas/__tests__/settings.ts index 23d6a442d0..27b43fb2df 100644 --- a/src/sagas/__tests__/settings.ts +++ b/src/sagas/__tests__/settings.ts @@ -7,8 +7,7 @@ import settingsSagas, { developerPasswordChange, developerPasswordChangeListen, fetchDeveloperInfo, - updateDeveloperInfo, - callChangePassword + updateDeveloperInfo } from '../settings' import { Action } from '@/types/core' import ActionTypes from '@/constants/action-types' @@ -20,7 +19,7 @@ import { errorThrownServer } from '@/actions/error' import errorMessages from '@/constants/error-messages' import messages from '@/constants/messages' import { developerStub } from '../__stubs__/developer' -import { removeSession } from '@reapit/cognito-auth' +import { removeSession, changePassword } from '@reapit/cognito-auth' import { history } from '../../core/router' import Routes from '@/constants/routes' import { authLogoutSuccess } from '@/actions/auth' @@ -112,12 +111,13 @@ describe('settings', () => { }) expect(gen.next().value).toEqual(put(settingShowLoading(true))) expect(gen.next().value).toEqual(select(selectDeveloperEmail)) + it('should call API success', () => { const clone = gen.clone() expect(clone.next('abc@gmail.com').value).toEqual( - call(callChangePassword, { values: data, email: 'abc@gmail.com' }) + call(changePassword, { password: '123', newPassword: '456', userName: 'abc@gmail.com' }) ) - expect(clone.next({ message: 'SUCCESS' }).value).toEqual( + expect(clone.next('SUCCESS').value).toEqual( put( showNotificationMessage({ variant: 'info', @@ -125,11 +125,30 @@ describe('settings', () => { }) ) ) - expect(clone.next({ message: 'SUCCESS' }).value).toEqual(call(removeSession)) + expect(clone.next().value).toEqual(call(removeSession)) expect(clone.next().value).toEqual(put(authLogoutSuccess())) expect(clone.next().value).toEqual(history.replace(`${Routes.DEVELOPER_LOGIN}?isChangePasswordSuccess=1`)) expect(clone.next().value).toEqual(put(settingShowLoading(false))) + expect(clone.next().done).toEqual(true) + }) + + it('should fail if API response !== "SUCCESS" ', () => { + const clone = gen.clone() + expect(clone.next('abc@gmail.com').value).toEqual( + call(changePassword, { password: '123', newPassword: '456', userName: 'abc@gmail.com' }) + ) + expect(clone.next('FAIL').value).toEqual( + put( + errorThrownServer({ + type: 'SERVER', + message: errorMessages.DEFAULT_SERVER_ERROR + }) + ) + ) + expect(clone.next().value).toEqual(put(settingShowLoading(false))) + expect(clone.next().done).toEqual(true) }) + it('should call API fail', () => { const clone = gen.clone() // @ts-ignore diff --git a/src/sagas/settings.ts b/src/sagas/settings.ts index eb3de42069..e2456762e1 100644 --- a/src/sagas/settings.ts +++ b/src/sagas/settings.ts @@ -1,6 +1,6 @@ import { put, fork, all, call, takeLatest, select } from '@redux-saga/core/effects' import { fetcher } from '@reapit/elements' -import { removeSession } from '@reapit/cognito-auth' +import { removeSession, changePassword } from '@reapit/cognito-auth' import { Action } from '@/types/core' import ActionTypes from '@/constants/action-types' import errorMessages from '@/constants/error-messages' @@ -101,40 +101,27 @@ export type CallChangePasswordParams = { email: string } -export const callChangePassword = async ({ values, email }: CallChangePasswordParams) => { - const CHANGE_PASSWORD_URL = '/password/change' - const response = await fetcher({ - url: CHANGE_PASSWORD_URL, - api: process.env.COGNITO_API_BASE_URL as string, - method: 'POST', - headers: MARKETPLACE_HEADERS, - body: { - password: values.currentPassword, - newPassword: values.password, - userName: email - } - }) - return response -} - export const developerPasswordChange = function*({ data }: Action) { yield put(settingShowLoading(true)) try { const email = yield select(selectDeveloperEmail) - const response = yield call(callChangePassword, { values: data, email }) - const isCallAPISuccess = response.message === 'SUCCESS' - if (isCallAPISuccess) { - yield put( - showNotificationMessage({ - variant: 'info', - message: messages.CHANGE_SAVE_SUCCESSFULLY - }) - ) - const SUCCESS_ALERT_LOGIN_PAGE = `${Routes.DEVELOPER_LOGIN}?isChangePasswordSuccess=1` - yield call(removeSession) - yield put(authLogoutSuccess()) - yield history.replace(SUCCESS_ALERT_LOGIN_PAGE) + /* rename for compatible reason */ + const { currentPassword: password, password: newPassword, confirmPassword: newPasswordConfirm } = data + const response = yield call(changePassword, { userName: email, password, newPassword }) + const isCallAPISuccess = response === 'SUCCESS' + if (!isCallAPISuccess) { + throw new Error('Server error') } + yield put( + showNotificationMessage({ + variant: 'info', + message: messages.CHANGE_SAVE_SUCCESSFULLY + }) + ) + const SUCCESS_ALERT_LOGIN_PAGE = `${Routes.DEVELOPER_LOGIN}?isChangePasswordSuccess=1` + yield call(removeSession) + yield put(authLogoutSuccess()) + yield history.replace(SUCCESS_ALERT_LOGIN_PAGE) } catch (error) { yield put( errorThrownServer({