Skip to content

Commit

Permalink
test(order): cancel order integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 21, 2024
1 parent d597b62 commit c10071f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/module/tutor-service/tutorService.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,22 @@ export class TutorServiceService {
}
}

async getOrders(serviceId: string) {
try {
const ordersSnapshot = await this.firestore
.collection("orders")
.where("tutorServiceId", "==", serviceId)
.get();

return ordersSnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
}));
} catch (error) {
throw new Error(`Failed to get orders: ${error}`);
}
}

async validateTutorServiceOwnership(tutorId: string, serviceId: string) {
try {
const tutorServiceRef = this.firestore
Expand Down
62 changes: 61 additions & 1 deletion tests/integration/order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TutorServiceService } from "@/module/tutor-service/tutorService.service
import { faker } from "@faker-js/faker";
import { login } from "@tests/helpers/client.helper";
import supertest from "supertest";
import { describe, expect, test } from "vitest";
import { afterAll, describe, expect, test } from "vitest";

const tsService = new TutorServiceService({ firestore });

Expand Down Expand Up @@ -33,6 +33,15 @@ describe("Order a service", async () => {

const services = await tsService.getTutorServices();

afterAll(async () => {
const snapshot = await firestore.collection("orders").get();
const batch = firestore.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
});

test("Learner can order a service", async () => {
const availability = await tsService.getTutorServiceAvailability(
services[0].id,
Expand Down Expand Up @@ -74,3 +83,54 @@ describe("Order a service", async () => {
);
});
});

describe("Cancel an order", async () => {
const services = await tsService.getTutorServices();
const randomService = faker.helpers.arrayElement(services);

const { userId, idToken: learnerIdToken } = await registerLearner();

test("Tutor can cancel an order", async () => {
const tutorId = await firestore
.collection("tutor_services")
.doc(randomService.id)
.get()
.then(async (doc) => {
const ref = doc.data()?.tutorId;
return ref.id;
});
const tutorIdToken = await login(tutorId);

// Create an order
const availability = await tsService.getTutorServiceAvailability(
randomService.id,
);

await supertest(app)
.post("/api/v1/orders")
.set("Authorization", `Bearer ${learnerIdToken}`)
.send({
learnerId: userId,
tutorServiceId: randomService.id,
sessionTime: availability[0],
totalHours: 1,
notes: "I want to learn more",
})
.expect(201);

// Cancel the order
const orders = await tsService.getOrders(randomService.id);
const order = orders[0];
await supertest(app)
.post(`/api/v1/orders/${order.id}/cancel`)
.set("Authorization", `Bearer ${tutorIdToken}`)
.expect(200);
});

test("Learner cannot cancel an order", async () => {
await supertest(app)
.post(`/api/v1/orders/x/cancel`)
.set("Authorization", `Bearer ${learnerIdToken}`)
.expect(403);
});
});

0 comments on commit c10071f

Please sign in to comment.