Add simple test with Mocha

This commit is contained in:
Adolfo Reyna
2025-02-21 22:06:45 -05:00
parent 2e7c0a6915
commit ad62814d82
4 changed files with 3180 additions and 3 deletions

View File

@@ -67,7 +67,7 @@ DB.getDB.then((DB) => {
app.get('/', sessionChecker, async (req, res) => {
try {
const userInfo = req.userInfo;
if(!userInfo) {
if (!userInfo) {
// This should not happend, since the sessionChecker should redirect to login
return res.status(401).json({ status: "Unauthorized" });
}
@@ -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

File diff suppressed because it is too large Load Diff

View File

@@ -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
View 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');
//});
});