| 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 bcrypt from 'bcryptjs';
|
| 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 |
|
| 16 | const connectionString = process.env.DATABASE_URL || 'postgres://user:password@orbit_db:5432/orbit_test';
|
| 17 | const adapter = new PrismaPg({ connectionString });
|
| 18 | prisma = new PrismaClient({ adapter });
|
| 19 |
|
| 20 | // Create Express app with test Prisma client
|
| 21 | app = createApp(prisma);
|
| 22 | });
|
| 23 |
|
| 24 | test.beforeEach(async () => {
|
| 25 | // Clean database before each test
|
| 26 | try {
|
| 27 | await prisma.user.deleteMany({});
|
| 28 | } catch (err) {
|
| 29 | console.error('Error cleaning test DB:', err);
|
| 30 | }
|
| 31 | });
|
| 32 |
|
| 33 | test.after.always(async () => {
|
| 34 | await prisma?.$disconnect();
|
| 35 | });
|
| 36 |
|
| 37 | //test("GET /users/:id retrieves a user", async (t) => {
|
| 38 | // t.pass()
|
| 39 | // // Create a user first
|
| 40 | // const user = await prisma.user.create({
|
| 41 | // data: {
|
| 42 | // email: "get@example.com",
|
| 43 | // firstName: "Get",
|
| 44 | // lastName: "User",
|
| 45 | // encryptedPassword: bcrypt.hashSync("password", 10)
|
| 46 | // }
|
| 47 | // });
|
| 48 | //
|
| 49 | // const response = await request(app)
|
| 50 | // .get(`/users/${user.id}`)
|
| 51 | // .expect('Content-Type', /json/)
|
| 52 | // .expect(200);
|
| 53 | //
|
| 54 | // t.is(response.body.id, user.id);
|
| 55 | // t.is(response.body.email, user.email);
|
| 56 | // t.is(response.body.firstName, user.firstName);
|
| 57 | // t.is(response.body.lastName, user.lastName);
|
| 58 | // t.falsy(response.body.encryptedPassword); // Should not expose password
|
| 59 | //});
|
| 60 | //
|
| 61 | //test("GET /users/:id returns 404 for non-existent user", async (t) => {
|
| 62 | // const response = await request(app)
|
| 63 | // .get('/users/999999')
|
| 64 | // .expect('Content-Type', /json/)
|
| 65 | // .expect(404);
|
| 66 | //
|
| 67 | // t.truthy(response.body.error);
|
| 68 | //});
|