Skip to content

Commit

Permalink
refactoring: remove ProspectDTO, update frontend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loicguillois committed Jan 7, 2025
1 parent 85c67e4 commit 2f204a8
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 51 deletions.
11 changes: 8 additions & 3 deletions frontend/src/mocks/handlers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
NoteDTO,
OwnerDTO,
SignupLinkDTO,
UserDTO
UserDTO,
CeremaUser
} from '@zerologementvacant/models';
import {
genCampaignDTO,
Expand All @@ -22,7 +23,8 @@ import {
genHousingDTO,
genOwnerDTO,
genSenderDTO,
genUserDTO
genUserDTO,
genCeremaUser
} from '@zerologementvacant/models/fixtures';

const campaigns: CampaignDTO[] = Array.from({ length: 10 }, genCampaignDTO);
Expand All @@ -38,6 +40,8 @@ const drafts: DraftDTO[] = campaigns.map<DraftDTO>(() =>

const users: UserDTO[] = Array.from({ length: 10 }, () => genUserDTO());

const ceremaUsers: CeremaUser[] = Array.from({ length: 10 }, () => genCeremaUser());

const groups: GroupDTO[] = Array.from({ length: 5 }, () =>
genGroupDTO(faker.helpers.arrayElement(users))
);
Expand Down Expand Up @@ -129,7 +133,8 @@ const data = {
housingOwners,
owners,
signupLinks,
users
users,
ceremaUsers,
};

export default data;
4 changes: 3 additions & 1 deletion frontend/src/mocks/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { housingHandlers } from './housing-handlers';
import { noteHandlers } from './note-handlers';
import { ownerHandlers } from './owner-handlers';
import { signupLinksHandlers } from './signup-links-handlers';
import { userAccessHandlers } from './user-access-handlers';
import { userHandlers } from './user-handlers';

export const handlers: RequestHandler[] = [
Expand All @@ -25,5 +26,6 @@ export const handlers: RequestHandler[] = [
...noteHandlers,
...ownerHandlers,
...signupLinksHandlers,
...userHandlers
...userHandlers,
...userAccessHandlers
];
28 changes: 28 additions & 0 deletions frontend/src/mocks/handlers/signup-links-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@zerologementvacant/models';
import data from './data';
import config from '../../utils/config';
import { isPast } from 'date-fns';

export const signupLinksHandlers: RequestHandler[] = [
http.post<never, SignupLinkPayloadDTO, SignupLinkDTO>(
Expand All @@ -24,5 +25,32 @@ export const signupLinksHandlers: RequestHandler[] = [
status: constants.HTTP_STATUS_CREATED
});
}
),
http.get<never, SignupLinkDTO>(
`${config.apiEndpoint}/api/signup-links/:id`,
({ params }) => {
const { id } = params;
const signupLink = data.signupLinks.find((link) => link.id === id);

if (!signupLink) {
return HttpResponse.json({
name: 'SignupLinkMissingError',
message: `Signup ${id} link missing`
},
{ status: constants.HTTP_STATUS_NOT_FOUND });
}

if (isPast(signupLink.expiresAt)) {
return HttpResponse.json({
name: 'SignupLinkExpiredError',
message: `Signup link expired`
},
{ status: constants.HTTP_STATUS_GONE });
}

return HttpResponse.json(signupLink, {
status: constants.HTTP_STATUS_OK
});
}
)
];
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const AccountPasswordCreationView = () => {
) {
return <Navigate to="/inscription/impossible" />;
}
} else if(access === null) {
return <Navigate to="/inscription/impossible" />;
}

async function submit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';

import { ProspectDTO, SignupLinkDTO } from '@zerologementvacant/models';
import { SignupLinkDTO } from '@zerologementvacant/models';
import {
genEstablishmentDTO,
genProspectDTO,
genCeremaUser,
genSignupLinkDTO
} from '@zerologementvacant/models/fixtures';
import AccountPasswordCreationView from '../AccountPasswordCreationView';
Expand All @@ -17,7 +16,6 @@ import OnboardingModal from '../../../../components/modals/OnboardingModal/Onboa

describe('AccountPasswordCreationView', () => {
const user = userEvent.setup();
const establishment = genEstablishmentDTO();

function setup(link: SignupLinkDTO) {
const router = createMemoryRouter(
Expand Down Expand Up @@ -49,8 +47,9 @@ describe('AccountPasswordCreationView', () => {
}

it('should render', async () => {
const prospect = genProspectDTO(establishment);
const link = genSignupLinkDTO(prospect.email);
const ceremaUser = genCeremaUser();
data.ceremaUsers.push(ceremaUser);
const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -62,9 +61,10 @@ describe('AccountPasswordCreationView', () => {
});

it('should display an error if the link is expired', async () => {
const prospect = genProspectDTO(establishment);
const ceremaUser = genCeremaUser();
data.ceremaUsers.push(ceremaUser);
const link: SignupLinkDTO = {
...genSignupLinkDTO(prospect.email),
...genSignupLinkDTO(ceremaUser.email),
expiresAt: faker.date.past()
};
data.signupLinks.push(link);
Expand All @@ -75,11 +75,13 @@ describe('AccountPasswordCreationView', () => {
});

it("should be forbidden if one's establishment does not exist in ZLV", async () => {
const prospect: ProspectDTO = {
...genProspectDTO(establishment),
establishment: undefined
};
const link = genSignupLinkDTO(prospect.email);
const ceremaUser = genCeremaUser();
data.ceremaUsers.push({
...ceremaUser,
establishmentId: null
});

const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -89,11 +91,12 @@ describe('AccountPasswordCreationView', () => {
});

it('should be forbidden if one has no account', async () => {
const prospect: ProspectDTO = {
...genProspectDTO(establishment),
const ceremaUser = genCeremaUser();
data.ceremaUsers.push({
...ceremaUser,
hasCommitment: false
};
const link = genSignupLinkDTO(prospect.email);
});
const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -103,12 +106,14 @@ describe('AccountPasswordCreationView', () => {
});

it("should be forbidden if one's account is waiting for approval", async () => {
const prospect: ProspectDTO = {
...genProspectDTO(establishment),
const ceremaUser = genCeremaUser();
data.ceremaUsers.push({
...ceremaUser,
hasAccount: false,
hasCommitment: false
};
const link = genSignupLinkDTO(prospect.email);
});

const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -118,8 +123,9 @@ describe('AccountPasswordCreationView', () => {
});

it('should require a password of at least eight characters, one uppercase, one lowercase and one number', async () => {
const prospect = genProspectDTO(establishment);
const link = genSignupLinkDTO(prospect.email);
const ceremaUser = genCeremaUser();
data.ceremaUsers.push(ceremaUser);
const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -140,8 +146,9 @@ describe('AccountPasswordCreationView', () => {
});

it('should require to confirm the password', async () => {
const prospect = genProspectDTO(establishment);
const link = genSignupLinkDTO(prospect.email);
const ceremaUser = genCeremaUser();
data.ceremaUsers.push(ceremaUser);
const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -155,8 +162,9 @@ describe('AccountPasswordCreationView', () => {
});

it('should choose a password', async () => {
const prospect = genProspectDTO(establishment);
const link = genSignupLinkDTO(prospect.email);
const ceremaUser = genCeremaUser();
data.ceremaUsers.push(ceremaUser);
const link = genSignupLinkDTO(ceremaUser.email);
data.signupLinks.push(link);

setup(link);
Expand All @@ -170,7 +178,13 @@ describe('AccountPasswordCreationView', () => {
/^Confirmez votre mot de passe/i
);
await user.type(confirmationInput, password);
await user.keyboard('{Enter}');

const confirm = await screen.findByRole('button', {
name: /^Confirmer et créer mon compte/i
});
await user.click(confirm);

await new Promise(resolve => setTimeout(resolve, 2000));

const title = await screen.findByText(
/^Bienvenue sur Zéro Logement Vacant !/
Expand Down
8 changes: 0 additions & 8 deletions packages/models/src/ProspectDTO.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export * from './OwnerDTO';
export * from './OwnerKind';
export * from './OwnershipKind';
export * from './Pagination';
export * from './ProspectDTO';
export * from './RolesDTO';
export * from './RoomCount';
export * from './SenderDTO';
Expand Down
23 changes: 13 additions & 10 deletions packages/models/src/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { OwnerDTO } from '../OwnerDTO';
import { RolesDTO } from '../RolesDTO';
import { SenderDTO, SignatoryDTO } from '../SenderDTO';
import { UserDTO } from '../UserDTO';
import { CeremaUser } from '../CeremaUser';
import { Occupancy, OCCUPANCY_VALUES } from '../Occupancy';
import { HOUSING_KIND_VALUES } from '../HousingKind';
import { DatafoncierHousing } from '../DatafoncierHousing';
import { HOUSING_STATUS_VALUES } from '../HousingStatus';
import { FileUploadDTO } from '../FileUploadDTO';
import { HousingOwnerDTO } from '../HousingOwnerDTO';
import { SignupLinkDTO } from '../SignupLinkDTO';
import { ProspectDTO } from '../ProspectDTO';
import { EstablishmentDTO } from '../EstablishmentDTO';
import { ESTABLISHMENT_KIND_VALUES } from '../EstablishmentKind';
import { ESTABLISHMENT_SOURCE_VALUES } from '../EstablishmentSource';
Expand Down Expand Up @@ -346,15 +346,6 @@ export function genOwnerDTO(): OwnerDTO {
});
}

export function genProspectDTO(establishment: EstablishmentDTO): ProspectDTO {
return {
email: faker.internet.email(),
establishment: fp.pick(['id', 'siren'], establishment),
hasAccount: true,
hasCommitment: true
};
}

export function genSenderDTO(): SenderDTO {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
Expand Down Expand Up @@ -405,3 +396,15 @@ export function genUserDTO(role = RolesDTO.Usual): UserDTO {
disabled: false,
};
}

export function genCeremaUser(): CeremaUser {
return {
email: faker.internet.email(),
establishmentSiren: faker.string.numeric(9),
establishmentId: faker.string.uuid(),
cguValid: true,
hasAccount: true,
hasCommitment: true,
isValid: true,
};
}
1 change: 1 addition & 0 deletions server/src/controllers/signupLinkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function create(request: Request, response: Response) {
prospectEmail: email,
expiresAt: addHours(new Date(), SIGNUP_LINK_EXPIRATION)
};

await signupLinkRepository.insert(link);
await mailService.sendAccountActivationEmail(link.id, {
recipients: [email]
Expand Down

0 comments on commit 2f204a8

Please sign in to comment.