Skip to content

Commit

Permalink
fix: add member password routes (#824)
Browse files Browse the repository at this point in the history
* fix: add member password routes

* fix: add tests

* fix: update from comments
  • Loading branch information
spaenleh authored Jul 31, 2024
1 parent 3b4a955 commit 5d2f744
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 20 deletions.
22 changes: 13 additions & 9 deletions src/member/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import {
buildGetMemberStorageRoute,
buildGetMembersByEmailRoute,
buildGetMembersByIdRoute,
buildPatchMemberPasswordRoute,
buildPatchMemberRoute,
buildPostMemberEmailUpdateRoute,
buildUpdateMemberPasswordRoute,
buildPostMemberPasswordRoute,
buildUploadAvatarRoute,
} from './routes.js';

Expand Down Expand Up @@ -137,18 +138,21 @@ export const deleteCurrentMember = async ({
.then(({ data }) => data),
);

export const createPassword = async (
payload: { password: Password },
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
axios
.post<void>(`${API_HOST}/${buildPostMemberPasswordRoute()}`, payload)
.then((data) => data);

export const updatePassword = async (
payload: { password: Password; currentPassword: Password },
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
verifyAuthentication(() =>
axios
.patch<void>(`${API_HOST}/${buildUpdateMemberPasswordRoute()}`, {
password: payload.password,
currentPassword: payload.currentPassword,
})
.then(({ data }) => data),
);
axios
.patch<void>(`${API_HOST}/${buildPatchMemberPasswordRoute()}`, payload)
.then((data) => data);

export const uploadAvatar = async (
{
Expand Down
73 changes: 66 additions & 7 deletions src/member/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { memberKeys } from '../keys.js';
import { SIGN_OUT_ROUTE } from '../routes.js';
import {
buildDeleteMemberRoute,
buildPatchMemberPasswordRoute,
buildPatchMemberRoute,
buildUpdateMemberPasswordRoute,
buildPostMemberPasswordRoute,
buildUploadAvatarRoute,
} from './routes.js';
import { updatePasswordRoutine, uploadAvatarRoutine } from './routines.js';
Expand Down Expand Up @@ -346,20 +347,17 @@ describe('Member Mutations', () => {
});

describe('useUpdatePassword', () => {
const route = `/${buildUpdateMemberPasswordRoute()}`;
const route = `/${buildPatchMemberPasswordRoute()}`;
const mutation = mutations.useUpdatePassword;
const password = 'ASDasd123';
const currentPassword = 'ASDasd123';
const name = 'myName';
const id = 'myId';
const email = '[email protected]';

it(`Update password`, async () => {
const endpoints = [
{
route,
response: { email, id, name },
statusCode: StatusCodes.OK,
response: {},
statusCode: StatusCodes.NO_CONTENT,
method: HttpMethod.Patch,
},
];
Expand Down Expand Up @@ -409,4 +407,65 @@ describe('Member Mutations', () => {
);
});
});

describe('useCreatePassword', () => {
const route = `/${buildPostMemberPasswordRoute()}`;
const mutation = mutations.useCreatePassword;
const password = 'ASDasd123';

it(`Update password`, async () => {
const endpoints = [
{
route,
response: {},
statusCode: StatusCodes.NO_CONTENT,
method: HttpMethod.Post,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
mockedMutation.mutate({ password });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith({
type: updatePasswordRoutine.SUCCESS,
payload: { message: SUCCESS_MESSAGES.UPDATE_PASSWORD },
});
});

it(`Unauthorized`, async () => {
const endpoints = [
{
route,
response: UNAUTHORIZED_RESPONSE,
method: HttpMethod.Post,
statusCode: StatusCodes.UNAUTHORIZED,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
mockedMutation.mutate({ password });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith(
expect.objectContaining({
type: updatePasswordRoutine.FAILURE,
}),
);
});
});
});
30 changes: 28 additions & 2 deletions src/member/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ export default (queryConfig: QueryClientConfig) => {
);
};

/** mutation to update member password. suppose only you can edit yourself
/**
* Mutation to update the member password
* @param {Password} password new password that user wants to set
* @param {Password} currentPassword current password already stored
* @param {Password} currentPassword current password already stored, needs to match old password
*/
const useUpdatePassword = () =>
useMutation(
Expand All @@ -194,6 +195,30 @@ export default (queryConfig: QueryClientConfig) => {
},
);

/**
* Mutation to create a member password
* @param {Password} password new password to set on current member
*/
const useCreatePassword = () =>
useMutation(
(payload: { password: Password }) =>
Api.createPassword(payload, queryConfig),
{
onSuccess: () => {
notifier?.({
type: updatePasswordRoutine.SUCCESS,
payload: { message: SUCCESS_MESSAGES.UPDATE_PASSWORD },
});
},
onError: (error: Error) => {
notifier?.({
type: updatePasswordRoutine.FAILURE,
payload: { error },
});
},
},
);

const useUpdateMemberEmail = () =>
useMutation((newEmail: string) => Api.updateEmail(newEmail, queryConfig), {
onSuccess: () => {
Expand Down Expand Up @@ -235,6 +260,7 @@ export default (queryConfig: QueryClientConfig) => {
useUploadAvatar,
useEditMember,
useUpdatePassword,
useCreatePassword,
useUpdateMemberEmail,
useValidateEmailUpdate,
};
Expand Down
4 changes: 2 additions & 2 deletions src/member/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const buildPatchMemberRoute = (id: UUID) => `${MEMBERS_ROUTE}/${id}`;
export const buildDeleteMemberRoute = (id: UUID) => `${MEMBERS_ROUTE}/${id}`;

// Password
export const buildUpdateMemberPasswordRoute = () =>
`${MEMBERS_ROUTE}/update-password`;
export const buildPostMemberPasswordRoute = () => `password`;
export const buildPatchMemberPasswordRoute = () => `password`;

// Storage
export const buildGetMemberStorageRoute = () =>
Expand Down

0 comments on commit 5d2f744

Please sign in to comment.