Skip to content

Commit

Permalink
[CLD-713] Update the Change Password flows to use npm package (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
vuhuucuong authored Jan 14, 2020
1 parent 2cf6070 commit 583971a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
31 changes: 25 additions & 6 deletions src/sagas/__tests__/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -112,24 +111,44 @@ 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('[email protected]').value).toEqual(
call(callChangePassword, { values: data, email: '[email protected]' })
call(changePassword, { password: '123', newPassword: '456', userName: '[email protected]' })
)
expect(clone.next({ message: 'SUCCESS' }).value).toEqual(
expect(clone.next('SUCCESS').value).toEqual(
put(
showNotificationMessage({
variant: 'info',
message: messages.CHANGE_SAVE_SUCCESSFULLY
})
)
)
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('[email protected]').value).toEqual(
call(changePassword, { password: '123', newPassword: '456', userName: '[email protected]' })
)
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
Expand Down
47 changes: 17 additions & 30 deletions src/sagas/settings.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<ChangePasswordParams>) {
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({
Expand Down

0 comments on commit 583971a

Please sign in to comment.