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

Test de schedule mentorias #105

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
149 changes: 149 additions & 0 deletions src/__tests__/api/scheduleMentoria.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { describe, it, expect, beforeEach, beforeAll } from "vitest";
import { mockEmailer } from "./mocks/emailer";
import {
createMocks,
RequestMethod,
createRequest,
createResponse,
} from "node-mocks-http";
import type { NextApiRequest, NextApiResponse } from "next";
import scheduleMentoriaHandler from "@/pages/api/mentoria/schedule";
import { prisma } from "@/lib/prisma";
import { hashPassword } from "@/lib/hashPassword";
import {
cleanParticipation,
insertAndCheckSuccessfullyDummyParticipation,
validOfmi,
} from "./upsertParticipationUtils";
import { ParticipationRole } from "@prisma/client";

type ApiRequest = NextApiRequest & ReturnType<typeof createRequest>;
type APiResponse = NextApiResponse & ReturnType<typeof createResponse>;

const dummyContestantEmail = "[email protected]";
const dummyVolunteerEmail = "[email protected]";

beforeAll(async () => {
// ofmi is Needed
await prisma.ofmi.upsert({
where: { edition: validOfmi.edition },
update: {
...validOfmi,
},
create: {
...validOfmi,
},
});
// Upsert the valid user Auth
await prisma.userAuth.upsert({
where: { email: dummyContestantEmail },
update: {},
create: { email: dummyContestantEmail, password: hashPassword("pass") },
});
await prisma.userAuth.upsert({
where: { email: dummyVolunteerEmail },
update: {},
create: { email: dummyVolunteerEmail, password: hashPassword("pass") },
});
});

beforeEach(async () => {
// Clean mentoría
await prisma.mentoria.deleteMany({
where: {
contestantParticipant: {
Participation: {
every: { user: { UserAuth: { email: dummyContestantEmail } } },
},
},
},
});
await cleanParticipation(dummyContestantEmail);
await cleanParticipation(dummyVolunteerEmail);

// Remover contestant participation
mockEmailer.resetMock();
});

describe("/api/mentoria/schedule API Endpoint", () => {
function mockRequestResponse({
method = "POST",
body,
}: {
method?: RequestMethod;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body: any;
}): {
req: ApiRequest;
res: APiResponse;
} {
const { req, res } = createMocks<ApiRequest, APiResponse>({
method,
headers: {
"Content-Type": "application/json",
},
body: body,
});
return { req, res };
}

async function createParticipations(): Promise<{
contestantParticipationId: string;
volunteerParticipationId: string;
}> {
const contestant = (
await insertAndCheckSuccessfullyDummyParticipation(
dummyContestantEmail,
ParticipationRole.CONTESTANT,
)
)._getJSONData()["participation"];
const contestantParticipationId = contestant.contestantParticipationId;
expect(contestantParticipationId.length).greaterThanOrEqual(1);
const volunteer = (
await insertAndCheckSuccessfullyDummyParticipation(
dummyContestantEmail,
ParticipationRole.VOLUNTEER,
)
)._getJSONData()["participation"];
const volunteerParticipationId = volunteer.volunteerParticipationId;
expect(volunteerParticipationId.length).greaterThanOrEqual(1);
return { contestantParticipationId, volunteerParticipationId };
}

it("should return a successful response", async () => {
// Create contestant and volunteer participation
const { contestantParticipationId, volunteerParticipationId } =
await createParticipations();

const meetingTime = new Date("2024-11-24").toISOString();
const { req, res } = mockRequestResponse({
body: {
contestantParticipantId: contestantParticipationId,
volunteerAuthId: "3232",
volunteerParticipationId,
meetingTimeOpt: meetingTime,
calendlyPayload: {
event: { uri: "event_uri" },
invitee: { uri: "invitee_uri" },
},
},
});
await scheduleMentoriaHandler(req, res);

expect(res.statusCode).toBe(200);
expect(res.getHeaders()).toEqual({ "content-type": "application/json" });

// Check update in DB
const participationModel = await prisma.mentoria.findUnique({
where: {
volunteerParticipationId_contestantParticipantId_meetingTime: {
contestantParticipantId: contestantParticipationId,
volunteerParticipationId: volunteerParticipationId,
meetingTime: meetingTime,
},
},
});

expect(participationModel).not.toBeNull();
});
});
90 changes: 26 additions & 64 deletions src/__tests__/api/upsertParticipation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@ import { emailReg } from "@/lib/validators";
import { prisma } from "@/lib/prisma";
import { hashPassword } from "@/lib/hashPassword";
import { toISOStringReg } from "@/lib/validators/date";
import { ParticipationRole } from "@prisma/client";
import {
cleanParticipation,
insertAndCheckSuccessfullyDummyParticipation,
validMailingAddressInput,
validOfmi,
validUserInput,
validUserParticipationInput,
} from "./upsertParticipationUtils";

type ApiRequest = NextApiRequest & ReturnType<typeof createRequest>;
type APiResponse = NextApiResponse & ReturnType<typeof createResponse>;

const dummyEmail = "[email protected]";
const validOfmi = {
edition: 1,
birthDateRequirement: new Date("2005-07-01"),
year: 2024,
registrationOpenTime: new Date("2024-07-07"),
registrationCloseTime: new Date("2050-08-08"),
};
const validRole: ParticipationRole = "CONTESTANT";

beforeAll(async () => {
// ofmi is Needed
Expand All @@ -47,16 +46,7 @@ beforeAll(async () => {
});

beforeEach(async () => {
// Remove contestant participation of dummy email
await prisma.contestantParticipation.deleteMany({
where: {
Participation: { every: { user: { UserAuth: { email: dummyEmail } } } },
},
});
// Remove participation of dummy email
await prisma.participation.deleteMany({
where: { user: { UserAuth: { email: dummyEmail } } },
});
await cleanParticipation(dummyEmail);
// Remover contestant participation
mockEmailer.resetMock();
});
Expand All @@ -83,43 +73,11 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
return { req, res };
}

const validMailingAddressInput = {
street: "Calle",
externalNumber: "#8Bis",
zipcode: "01234",
country: "MEX",
state: "Aguascalientes",
municipality: "Aguascalientes",
locality: "Aguascalientes",
phone: "5511223344",
references: "Hasta el fondo",
};

const validUserInput = {
email: dummyEmail,
firstName: "Juan Carlos",
lastName: "Sigler Priego",
preferredName: "Juanito",
birthDate: new Date("2006-11-24").toISOString(),
pronouns: "HE",
governmentId: "HEGG061124MVZRRL02",
shirtSize: "M",
shirtStyle: "STRAIGHT",
mailingAddress: validMailingAddressInput,
};

const validUserParticipationInput = {
role: validRole,
schoolName: "Colegio Carol Baur",
schoolStage: "HIGH",
schoolGrade: 3,
schoolCountry: "MEX",
schoolState: "Aguascalientes",
};
const validUser = validUserInput(dummyEmail);

const validRequest = {
ofmiEdition: 1,
user: validUserInput,
ofmiEdition: validOfmi.edition,
user: validUser,
userParticipation: validUserParticipationInput,
};

Expand All @@ -144,11 +102,15 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
expect(participationModel).not.toBeNull();
});

it("should register volunteer", async () => {
await insertAndCheckSuccessfullyDummyParticipation(dummyEmail, "VOLUNTEER");
});

it("should update", async () => {
const { req, res } = mockRequestResponse({ body: validRequest });
await upsertParticipationHandler(req, res);
expect(res.statusCode).toBe(201);
expect(res.getHeaders()).toEqual({ "content-type": "application/json" });
const res = await insertAndCheckSuccessfullyDummyParticipation(
dummyEmail,
"CONTESTANT",
);
const participation = res._getJSONData()["participation"];

const newFirstName = "Other Name";
Expand All @@ -158,7 +120,7 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
body: {
...validRequest,
user: {
...validUserInput,
...validUser,
firstName: newFirstName,
mailingAddress: {
...validMailingAddressInput,
Expand Down Expand Up @@ -278,7 +240,7 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
body: {
...validRequest,
user: {
...validUserInput,
...validUser,
email: "juanito.omegaup.com",
},
},
Expand All @@ -297,7 +259,7 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
body: {
...validRequest,
user: {
...validUserInput,
...validUser,
email: "[email protected]",
},
},
Expand All @@ -317,7 +279,7 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
body: {
...validRequest,
user: {
...validUserInput,
...validUser,
birthDate: "0006-12-12",
},
},
Expand All @@ -336,7 +298,7 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
body: {
...validRequest,
user: {
...validUserInput,
...validUser,
birthDate: new Date("2008-12-12").toISOString(),
},
},
Expand All @@ -356,7 +318,7 @@ describe("/api/ofmi/registerParticipation API Endpoint", () => {
body: {
...validRequest,
user: {
...validUserInput,
...validUser,
birthDate: new Date("2004-12-12").toISOString(),
governmentId: "PELJ041212HDFXXX04",
},
Expand Down
Loading