Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate Account Settings #4498

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import isEqual from 'lodash/isEqual.js';

import { ConsoleLogger as Logger } from '@aws-amplify/core/internals/utils';
import {
changePassword,
ValidatorOptions,
getDefaultConfirmPasswordValidators,
getDefaultPasswordValidators,
getLogger,
runFieldValidators,
} from '@aws-amplify/ui';

Expand All @@ -18,7 +18,7 @@ import { ChangePasswordProps, ValidateParams } from './types';
import DEFAULTS from './defaults';
import { defaultChangePasswordDisplayText } from '../utils';

const logger = new Logger('ChangePassword');
const logger = getLogger('ChangePassword');

const getIsDisabled = (
formValues: FormValues,
Expand Down Expand Up @@ -173,7 +173,7 @@ function ChangePassword({
setErrorMessage(null);
}

changePassword({ user, currentPassword, newPassword })
changePassword({ currentPassword, newPassword })
.then(() => {
// notify success to the parent
onSuccess?.();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ describe('ChangePassword', () => {
fireEvent.submit(submitButton);

expect(changePasswordSpy).toBeCalledWith({
user,
currentPassword: 'oldpassword',
newPassword: 'newpassword',
});
Expand Down Expand Up @@ -343,7 +342,6 @@ describe('ChangePassword', () => {
fireEvent.submit(submitButton);

expect(changePasswordSpy).toBeCalledWith({
user,
currentPassword: 'oldpassword',
newPassword: 'newpassword',
});
Expand Down
34 changes: 15 additions & 19 deletions packages/ui/src/helpers/accountSettings/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
import * as Auth from '@aws-amplify/auth';
import * as Auth from 'aws-amplify/auth';

import { changePassword, deleteUser } from '../utils';
import { AmplifyUser } from '../../../types';

// mock `aws-amplify` to prevent logging auth errors during test runs
jest.mock('aws-amplify');

const changePasswordSpy = jest.spyOn(Auth, 'changePassword');
const changePasswordSpy = jest.spyOn(Auth, 'updatePassword');
const deleteUserSpy = jest.spyOn(Auth, 'deleteUser');

describe('changePassword', () => {
const user = { username: 'testuser' } as AmplifyUser;
const currentPassword = 'oldpassword';
const newPassword = 'newpassword';

it('should resolve if Auth.changePassword is successful', async () => {
changePasswordSpy.mockResolvedValue('SUCCESS');
it('should resolve if Auth.updatePassword is successful', async () => {
changePasswordSpy.mockResolvedValue();

await expect(
changePassword({ user, currentPassword, newPassword })
changePassword({ currentPassword, newPassword })
).resolves.toBeUndefined();

expect(changePasswordSpy).toHaveBeenCalledWith(
user,
currentPassword,
newPassword
);
expect(changePasswordSpy).toHaveBeenCalledWith({
newPassword: newPassword,
oldPassword: currentPassword,
});
});

it('should reject with error if Auth.changePassword fails', async () => {
it('should reject with error if Auth.updatePassword fails', async () => {
const error = new Error('change password failed');
changePasswordSpy.mockRejectedValue(error);

await expect(
changePassword({ user, currentPassword, newPassword })
changePassword({ currentPassword, newPassword })
).rejects.toEqual(error);

expect(changePasswordSpy).toHaveBeenCalledWith(
user,
currentPassword,
newPassword
);
expect(changePasswordSpy).toHaveBeenCalledWith({
newPassword: newPassword,
oldPassword: currentPassword,
});
});
});

Expand Down
21 changes: 8 additions & 13 deletions packages/ui/src/helpers/accountSettings/utils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import * as Auth from '@aws-amplify/auth';
import { updatePassword, deleteUser as deleteAuthUser } from 'aws-amplify/auth';

import { AmplifyUser } from '../../types';
import { getLogger } from '../utils';

const logger = getLogger('Auth');

type ChangePasswordInput = {
user: AmplifyUser;
currentPassword: string;
newPassword: string;
};

export const changePassword = async ({
user: _,
currentPassword,
newPassword,
}: ChangePasswordInput): Promise<void> => {
try {
logger.debug('calling Auth.changePassword');
logger.debug('calling Auth.updatePassword');
/**
* Auth.changePassword returns `Promise<"SUCCESS">`. We're not interested
* Auth.updatePassword returns `Promise<"SUCCESS">`. We're not interested
* in its resolved string value, so we just return Promise.resolve() on success.
*/
// await Auth.changePassword(user, currentPassword, newPassword);
const input: Auth.UpdatePasswordInput = {
await updatePassword({
oldPassword: currentPassword,
newPassword,
};
await Auth.updatePassword(input);
logger.debug('Auth.changePassword was successful');
});
logger.debug('Auth.updatePassword was successful');
return Promise.resolve();
} catch (e) {
logger.debug('Auth.changePassword failed with error', e);
logger.debug('Auth.updatePassword failed with error', e);
return Promise.reject(e);
}
};
Expand All @@ -40,7 +35,7 @@ export const deleteUser = async () => {
try {
logger.debug('calling Auth.deleteUser');
await Promise.resolve();
// await Auth.deleteUser();
await deleteAuthUser();
logger.debug('Auth.deleteUser was successful');
return Promise.resolve();
} catch (e) {
Expand Down
100 changes: 50 additions & 50 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.