| 1 | import test from "ava";
|
| 2 | import { PrismaClient } from '@prisma/client';
|
| 3 | import { PrismaPg } from '@prisma/adapter-pg';
|
| 4 | import request from 'supertest';
|
| 5 | import jwt from 'jsonwebtoken';
|
| 6 | import 'dotenv/config';
|
| 7 | import { Express } from 'express';
|
| 8 | import { createApp } from '../../src/createApp';
|
| 9 |
|
| 10 | let prisma: PrismaClient;
|
| 11 | let app: Express;
|
| 12 |
|
| 13 | test.before(() => {
|
| 14 | process.env.NODE_ENV = 'test';
|
| 15 | process.env.JWT_SECRET = 'test-secret-key';
|
| 16 |
|
| 17 | const connectionString = process.env.DATABASE_URL || 'postgres://user:password@orbit_db:5432/orbit_test';
|
| 18 | const adapter = new PrismaPg({ connectionString });
|
| 19 | prisma = new PrismaClient({ adapter });
|
| 20 |
|
| 21 | // Create Express app with test Prisma client
|
| 22 | app = createApp(prisma);
|
| 23 | });
|
| 24 |
|
| 25 | test.beforeEach(async () => {
|
| 26 | // Clean database before each test
|
| 27 | try {
|
| 28 | await prisma.user.deleteMany({});
|
| 29 | } catch (err) {
|
| 30 | console.error('Error cleaning test DB:', err);
|
| 31 | }
|
| 32 | });
|
| 33 |
|
| 34 | test.after.always(async () => {
|
| 35 | await prisma?.$disconnect();
|
| 36 | });
|
| 37 |
|
| 38 | test("POST /users creates a new user and returns JWT token", async (t) => {
|
| 39 | const userData = {
|
| 40 | email: "falamansa@example.com",
|
| 41 | firstName: "Fala",
|
| 42 | lastName: "Mansa",
|
| 43 | password: "IscreviSeuNomeNaAreia"
|
| 44 | };
|
| 45 |
|
| 46 | const response = await request(app)
|
| 47 | .post('/users')
|
| 48 | .send(userData)
|
| 49 | .expect('Content-Type', /json/)
|
| 50 | .expect(201);
|
| 51 |
|
| 52 | // Should return a JWT token
|
| 53 | t.truthy(response.body.token);
|
| 54 |
|
| 55 | // Verify the token is valid
|
| 56 | const decoded = jwt.verify(response.body.token, process.env.JWT_SECRET!) as any;
|
| 57 | t.is(decoded.email, userData.email);
|
| 58 | t.truthy(decoded.userId);
|
| 59 |
|
| 60 | // Verify user was created in database
|
| 61 | const dbUser = await prisma.user.findUnique({
|
| 62 | where: { email: userData.email }
|
| 63 | });
|
| 64 | t.truthy(dbUser);
|
| 65 | t.is(dbUser!.email, userData.email);
|
| 66 | t.is(dbUser!.firstName, userData.firstName);
|
| 67 | t.is(dbUser!.lastName, userData.lastName);
|
| 68 | });
|
| 69 |
|
| 70 | test("POST /users returns 400 for missing required fields", async (t) => {
|
| 71 | const response = await request(app)
|
| 72 | .post('/users')
|
| 73 | .send({
|
| 74 | email: "invalid@example.com"
|
| 75 | // Missing firstName and password
|
| 76 | })
|
| 77 | .expect('Content-Type', /json/)
|
| 78 | .expect(400);
|
| 79 |
|
| 80 | t.truthy(response.body.error);
|
| 81 | t.is(response.body.error, "Campos faltantes"); // TODO: i18n?
|
| 82 | t.truthy(response.body.missing);
|
| 83 | t.true(response.body.missing.includes('password'));
|
| 84 | t.true(response.body.missing.includes('firstName'));
|
| 85 | });
|
| 86 |
|
| 87 | test("POST /users returns 409 for duplicate email", async (t) => {
|
| 88 | const email = "duplicate@example.com";
|
| 89 |
|
| 90 | // Create first user
|
| 91 | await prisma.user.create({
|
| 92 | data: {
|
| 93 | email,
|
| 94 | firstName: "First",
|
| 95 | lastName: "User",
|
| 96 | encryptedPassword: "$2a$10$test"
|
| 97 | }
|
| 98 | });
|
| 99 |
|
| 100 | // Try to create second user with same email
|
| 101 | const response = await request(app)
|
| 102 | .post('/users')
|
| 103 | .send({
|
| 104 | email,
|
| 105 | firstName: "Second",
|
| 106 | lastName: "User",
|
| 107 | password: "hunter2"
|
| 108 | })
|
| 109 | .expect('Content-Type', /json/)
|
| 110 | .expect(409);
|
| 111 |
|
| 112 | t.truthy(response.body.error);
|
| 113 | });
|
| 114 |
|
| 115 | test("createApp successfully integrates with Prisma and Express", async (t) => {
|
| 116 | // Test that the app factory works correctly
|
| 117 | const testApp = createApp(prisma);
|
| 118 |
|
| 119 | t.truthy(testApp);
|
| 120 | t.is(typeof testApp.use, 'function');
|
| 121 | t.is(typeof testApp.listen, 'function');
|
| 122 |
|
| 123 | // Verify the app has the prisma client in locals
|
| 124 | t.is(testApp.locals.prisma, prisma);
|
| 125 | });
|