Add simple test with Mocha
This commit is contained in:
2
index.js
2
index.js
@@ -186,3 +186,5 @@ DB.getDB.then((DB) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Export the app for testing purposes
|
||||
module.exports = { app, mongoDB: DB };
|
||||
3107
package-lock.json
generated
3107
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"test": "npx mocha test/auth.test.js",
|
||||
"start": "node index.js",
|
||||
"docker": "docker compose up -d",
|
||||
"docker_restore": "docker-compose exec mongo mongorestore --db EMI_SOCIAL /dump/EMI_SOCIAL/",
|
||||
@@ -30,5 +30,10 @@
|
||||
"socket.io": "^4.6.1",
|
||||
"stripe": "^8.178.0",
|
||||
"web-push": "^3.4.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^5.2.0",
|
||||
"mocha": "^11.1.0",
|
||||
"supertest": "^7.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
63
test/auth.test.js
Normal file
63
test/auth.test.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const request = require('supertest');
|
||||
const { expect } = require('chai');
|
||||
const { app, mongoDB } = require('../index'); // Adjust the path to your app
|
||||
|
||||
describe('Auth API', function () {
|
||||
this.timeout(10000); // Set timeout to 10000ms (10 seconds)
|
||||
let server;
|
||||
let userToken;
|
||||
|
||||
// Start the server before running the tests
|
||||
before(async function () {
|
||||
this.timeout(10000); // Set timeout to 10000ms (10 seconds)
|
||||
await mongoDB.getDB; // Ensure the database is connected
|
||||
server = app.listen(3005); // Start the server on a different port to avoid conflicts
|
||||
});
|
||||
|
||||
// Close the server after running the tests
|
||||
after(async function () {
|
||||
this.timeout(10000); // Set timeout to 10000ms (10 seconds)
|
||||
server.close();
|
||||
});
|
||||
|
||||
it('should create a new user', async function () {
|
||||
const res = await request(app)
|
||||
.post('/signup')
|
||||
.send({
|
||||
username: 'test@example.com',
|
||||
email: 'test@example.com',
|
||||
password: 'password123',
|
||||
profile:
|
||||
{
|
||||
firstName: 'Test',
|
||||
lastName: 'User',
|
||||
description: 'This is a test user from the backend test',
|
||||
}
|
||||
});
|
||||
expect(res.status).to.equal(200);
|
||||
expect(res.body).to.have.property('status');
|
||||
expect(res.body.status).to.equal('This user is already registered');
|
||||
//userToken = res.body.token;
|
||||
});
|
||||
|
||||
it('should login the user', async function () {
|
||||
const res = await request(app)
|
||||
.post('/login')
|
||||
.send({
|
||||
username: 'test@example.com',
|
||||
password: 'password123'
|
||||
});
|
||||
expect(res.status).to.equal(200);
|
||||
expect(res.body).to.have.property('user_sid');
|
||||
expect(res.body).to.have.property('session_id');
|
||||
userToken = res.body.user_sid;
|
||||
});
|
||||
|
||||
//it('should logout the user', async function () {
|
||||
// const res = await request(app)
|
||||
// .post('/logout')
|
||||
// .set('Authorization', `Bearer ${userToken}`);
|
||||
// expect(res.status).to.equal(200);
|
||||
// expect(res.body).to.have.property('status', 'ok');
|
||||
//});
|
||||
});
|
||||
Reference in New Issue
Block a user