| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/dev/aipossible/aipossible-server/test/ |
Upload File : |
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import request from "supertest";
import { AppModule } from "../src/app.module";
import { PrismaService } from "../src/common/prisma/prisma.service";
import { BlockType } from "../src/authoring/types";
import { AuthGuard } from "@nestjs/passport";
describe("ResponsesController (e2e)", () => {
let app: INestApplication;
let prisma: PrismaService;
const testModuleCode = "TEST_RESP_100";
const mockUserId = "test-user-id";
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideGuard(AuthGuard("jwt"))
.useValue({
canActivate: (context) => {
const req = context.switchToHttp().getRequest();
req.user = { id: mockUserId };
return true;
},
})
.compile();
app = moduleFixture.createNestApplication();
await app.init();
prisma = app.get<PrismaService>(PrismaService);
console.log("Setup: Ensuring user exists");
// Setup: Create mock user (needed for foreign key)
try {
await prisma.user.create({
data: {
id: mockUserId,
auth0Id: "auth0|test-resp-user",
email: "test-resp@example.com",
firstName: "Test",
lastName: "User",
},
});
} catch (e) {
// User might exist from previous run
console.log("User already exists");
}
// Setup: Create module and block
console.log("Setup: Creating Module and Block");
const module = await prisma.module.create({
data: {
code: testModuleCode,
title: "Test Response Module",
isPublished: true,
},
});
const section = await prisma.moduleSection.create({
data: {
moduleId: module.id,
title: "Section 1",
order: 1,
},
});
await prisma.contentBlock.create({
data: {
id: "test-block-id",
sectionId: section.id,
type: BlockType.QUIZ,
order: 1,
payload: { question: "Q1" },
},
});
});
afterAll(async () => {
// Cleanup
console.log("Cleanup: Deleting test data");
await prisma.userResponse.deleteMany({ where: { userId: mockUserId } });
await prisma.contentBlock.deleteMany({
where: { section: { module: { code: testModuleCode } } },
});
await prisma.moduleSection.deleteMany({
where: { module: { code: testModuleCode } },
});
await prisma.module.deleteMany({ where: { code: testModuleCode } });
await prisma.user.deleteMany({ where: { id: mockUserId } });
await app.close();
});
it("/responses (POST) - Should save response", async () => {
console.log("Test: Sending POST request");
await request(app.getHttpServer())
.post("/responses")
.send({
blockId: "test-block-id",
type: BlockType.QUIZ,
response: { choiceId: "a" },
})
.expect(201);
console.log("Test: Request successful, checking DB");
const savedResponse = await prisma.userResponse.findUnique({
where: {
userId_blockId: {
userId: mockUserId,
blockId: "test-block-id",
},
},
});
console.log("Test: DB lookup result:", savedResponse);
expect(savedResponse).toBeDefined();
// Cast to any to access Json properties
const responseJson = savedResponse?.response as any;
expect(responseJson.choiceId).toBe("a");
});
});