Skip to content

Commit

Permalink
feat: add mobile sign up hooks (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh authored Jul 21, 2023
1 parent e451953 commit 6e83809
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/api/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import configureAxios, { verifyAuthentication } from './axios';
import {
MOBILE_SIGN_IN_ROUTE,
MOBILE_SIGN_IN_WITH_PASSWORD_ROUTE,
MOBILE_SIGN_UP_ROUTE,
SIGN_IN_ROUTE,
SIGN_IN_WITH_PASSWORD_ROUTE,
SIGN_OUT_ROUTE,
Expand Down Expand Up @@ -58,6 +59,11 @@ export const mobileSignInWithPassword = async (
.then(({ data }) => data);

export const signUp = async (
payload: { name: string; email: string },
payload: { name: string; email: string; captcha: string },
{ API_HOST }: QueryClientConfig,
): Promise<void> => axios.post(`${API_HOST}/${SIGN_UP_ROUTE}`, payload);

export const mobileSignUp = async (
payload: { name: string; email: string; challenge: string; captcha: string },
{ API_HOST }: QueryClientConfig,
): Promise<void> => axios.post(`${API_HOST}/${MOBILE_SIGN_UP_ROUTE}`, payload);
1 change: 1 addition & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const buildDownloadItemThumbnailRoute = ({
)}`;

export const GET_CURRENT_MEMBER_ROUTE = `${MEMBERS_ROUTE}/current`;
export const MOBILE_SIGN_UP_ROUTE = `m/register`;
export const MOBILE_SIGN_IN_ROUTE = `m/login`;
export const MOBILE_SIGN_IN_WITH_PASSWORD_ROUTE = `m/login-password`;
export const SIGN_IN_ROUTE = `login`;
Expand Down
180 changes: 180 additions & 0 deletions src/mutations/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { SUCCESS_MESSAGES } from '@graasp/translations';
import { OK_RESPONSE, UNAUTHORIZED_RESPONSE } from '../../test/constants';
import { mockMutation, setUpTest, waitForMutation } from '../../test/utils';
import {
MOBILE_SIGN_IN_ROUTE,
MOBILE_SIGN_IN_WITH_PASSWORD_ROUTE,
SIGN_IN_ROUTE,
SIGN_IN_WITH_PASSWORD_ROUTE,
SIGN_OUT_ROUTE,
Expand Down Expand Up @@ -48,6 +50,7 @@ jest.spyOn(Cookies, 'get').mockReturnValue({ session: 'somesession' });

const captcha = 'captcha';
const email = '[email protected]';
const challenge = '1234';

describe('Authentication Mutations', () => {
const mockedNotifier = jest.fn();
Expand Down Expand Up @@ -115,6 +118,61 @@ describe('Authentication Mutations', () => {
});
});

describe('useMobileSignIn', () => {
const route = `/${MOBILE_SIGN_IN_ROUTE}`;
const mutation = mutations.useMobileSignIn;

it(`Sign in`, async () => {
const endpoints = [
{ route, response: OK_RESPONSE, method: HttpMethod.POST },
];

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

await act(async () => {
await mockedMutation.mutate({ email, captcha, challenge });
await waitForMutation();
});

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

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 () => {
await mockedMutation.mutate({ email, captcha });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith(
expect.objectContaining({
type: signInRoutine.FAILURE,
}),
);
});
});

describe('useSignInWithPassword', () => {
const route = `/${SIGN_IN_WITH_PASSWORD_ROUTE}`;
const mutation = mutations.useSignInWithPassword;
Expand Down Expand Up @@ -182,6 +240,73 @@ describe('Authentication Mutations', () => {
});
});

describe('useMobileSignInWithPassword', () => {
const route = `/${MOBILE_SIGN_IN_WITH_PASSWORD_ROUTE}`;
const mutation = mutations.useMobileSignInWithPassword;
const password = 'password';
const link = 'mylink';

it(`Sign in with password`, async () => {
const endpoints = [
{
route,
response: { resource: link },
statusCode: StatusCodes.SEE_OTHER,
method: HttpMethod.POST,
},
];
// set random data in cache
queryClient.setQueryData(CURRENT_MEMBER_KEY, 'somevalue');

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

await act(async () => {
await mockedMutation.mutate({ email, password, captcha, challenge });
await waitForMutation();
});

// verify cache keys
expect(queryClient.getQueryData(CURRENT_MEMBER_KEY)).toBeFalsy();

expect(mockedNotifier).toHaveBeenCalledWith({
type: signInWithPasswordRoutine.SUCCESS,
payload: { message: SUCCESS_MESSAGES.SIGN_IN_WITH_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 () => {
await mockedMutation.mutate({ email, captcha });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith(
expect.objectContaining({
type: signInWithPasswordRoutine.FAILURE,
}),
);
});
});

describe('useUpdatePassword', () => {
const route = `/${buildUpdateMemberPasswordRoute()}`;
const mutation = mutations.useUpdatePassword;
Expand Down Expand Up @@ -301,6 +426,61 @@ describe('Authentication Mutations', () => {
);
});
});
describe('useMobileSignUp', () => {
const route = `/${SIGN_UP_ROUTE}`;
const mutation = mutations.useMobileSignUp;
const name = 'name';

it(`Sign up`, async () => {
const endpoints = [
{ route, response: OK_RESPONSE, method: HttpMethod.POST },
];

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

await act(async () => {
await mockedMutation.mutate({ email, name, captcha, challenge });
await waitForMutation();
});

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

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 () => {
await mockedMutation.mutate({ email, name, captcha });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith(
expect.objectContaining({
type: signUpRoutine.FAILURE,
}),
);
});
});

describe('useSignOut', () => {
const route = `/${SIGN_OUT_ROUTE}`;
Expand Down
25 changes: 25 additions & 0 deletions src/mutations/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ export default (queryConfig: QueryClientConfig) => {
},
);

const useMobileSignUp = () =>
useMutation(
(payload: {
name: string;
email: string;
challenge: string;
captcha: string;
}) => Api.mobileSignUp(payload, queryConfig),
{
onSuccess: () => {
notifier?.({
type: signUpRoutine.SUCCESS,
payload: { message: SUCCESS_MESSAGES.SIGN_UP },
});
},
onError: (error: Error) => {
notifier?.({
type: signUpRoutine.FAILURE,
payload: { error },
});
},
},
);

const useSignOut = () => {
const queryClient = useQueryClient();
return useMutation((_currentMemberId: UUID) => Api.signOut(queryConfig), {
Expand Down Expand Up @@ -227,6 +251,7 @@ export default (queryConfig: QueryClientConfig) => {
useSignInWithPassword,
useSignOut,
useSignUp,
useMobileSignUp,
useUpdatePassword,
useMobileSignIn,
useMobileSignInWithPassword,
Expand Down

0 comments on commit 6e83809

Please sign in to comment.