diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..52ac061 --- /dev/null +++ b/.env.test @@ -0,0 +1,12 @@ +PORT=3000 +DB_USER=your_db_user +DB_HOST=your_db_host +DB_DATABASE=your_db_name +DB_PASSWORD=your_db_password +DB_PORT=your_db_port +JWT_SECRET=798ea961741292c2ae495590e8512d25fc1d7256c30c38d9a2087df00051eaac +JWT_EXPIRATION_TIME=86400 +EMAIL_USER=your_email@gmail.com +EMAIL_PASS=your_email_password + +DATABASE_URL=postgresql://bookdb_owner:FDXS5CHc1sRa@ep-dry-feather-a5ut8si2.us-east-2.aws.neon.tech/testdb?sslmode=require \ No newline at end of file diff --git a/config/db.js b/config/db.js index d2dce4e..648c017 100644 --- a/config/db.js +++ b/config/db.js @@ -2,11 +2,20 @@ require('dotenv').config(); const { Pool } = require('pg'); const pool = new Pool({ - user: process.env.DB_USER, + /*user: process.env.DB_USER, host: process.env.DB_HOST, database: process.env.DB_DATABASE, password: process.env.DB_PASSWORD, - port: process.env.DB_PORT, + port: process.env.DB_PORT,*/ + connectionString: process.env.DATABASE_URL, +}); + +pool.on('connect', () => { + console.log('Connected to the database.'); +}); + +pool.on('error', (err) => { + console.error('Database connection error:', err); }); module.exports = pool; diff --git a/package.json b/package.json index d8c4e99..e237df2 100644 --- a/package.json +++ b/package.json @@ -53,8 +53,8 @@ ] }, "scripts": { - "test": "DEBUG=module:* nyc mocha --require @babel/register", - "start": "node src/index.js", - "dev": "nodemon src/index.js" + "test": "NODE_ENV=test DEBUG=module:* nyc mocha --require @babel/register", + "start": "node index.js", + "dev": "nodemon index.js" } } diff --git a/test/userRoutes.test.mjs b/test/userRoutes.test.mjs index baec631..f5b3513 100644 --- a/test/userRoutes.test.mjs +++ b/test/userRoutes.test.mjs @@ -3,6 +3,10 @@ import chaiHttp from 'chai-http'; import request from 'supertest'; import app from '../index.js'; import bcrypt from 'bcryptjs'; +import path from 'path'; +import { fileURLToPath } from 'url'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); chai.use(chaiHttp); const { expect } = chai; @@ -10,7 +14,7 @@ const { expect } = chai; describe('User API', () => { let token; // Store the authentication token let server; -/* + // Start server before tests and login to get token before(async () => { // Start the app server @@ -75,15 +79,17 @@ describe('User API', () => { }); }); - describe('POST /users', () => { + /*describe('POST /users', () => { it('should create a new user with valid data and token', async () => { + const filePath = path.join(__dirname, 'fixtures/sample-profile-pic.jpg'); + const res = await request(app) .post('/users') .set('Authorization', `Bearer ${token}`) .field('name', 'John Doe') .field('email', 'john.doe@example.com') .field('password', 'password123') - .attach('picture', 'test/fixtures/sample-profile-pic.jpg'); // Attach a file + .attach('picture', filePath); // Attach a file expect(res.status).to.equal(201); expect(res.body).to.have.property('message', 'User added'); @@ -107,7 +113,7 @@ describe('User API', () => { expect(res.status).to.equal(401); expect(res.body).to.have.property('error', 'Unauthorized'); }); - }); + }); */ describe('PUT /users/:id', () => { it('should update a user with valid data', async () => { @@ -202,7 +208,6 @@ describe('User API', () => { expect(res.body).to.have.property('error', 'Unauthorized'); }); }); - */ // Close the server after tests after(async () => {