Add simple test with Mocha
This commit is contained in:
6
index.js
6
index.js
@@ -67,7 +67,7 @@ DB.getDB.then((DB) => {
|
|||||||
app.get('/', sessionChecker, async (req, res) => {
|
app.get('/', sessionChecker, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const userInfo = req.userInfo;
|
const userInfo = req.userInfo;
|
||||||
if(!userInfo) {
|
if (!userInfo) {
|
||||||
// This should not happend, since the sessionChecker should redirect to login
|
// This should not happend, since the sessionChecker should redirect to login
|
||||||
return res.status(401).json({ status: "Unauthorized" });
|
return res.status(401).json({ status: "Unauthorized" });
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,7 @@ DB.getDB.then((DB) => {
|
|||||||
app.post('/token/', sessionChecker, async (req, res) => {
|
app.post('/token/', sessionChecker, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const profileid = getProfileId(req);
|
const profileid = getProfileId(req);
|
||||||
const { token } = req.body;
|
const { token } = req.body;
|
||||||
// Validate token presence
|
// Validate token presence
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return res.status(400).json({ status: 'Token is required' });
|
return res.status(400).json({ status: 'Token is required' });
|
||||||
@@ -186,3 +186,5 @@ DB.getDB.then((DB) => {
|
|||||||
throw err;
|
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": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "npx mocha test/auth.test.js",
|
||||||
"start": "node index.js",
|
"start": "node index.js",
|
||||||
"docker": "docker compose up -d",
|
"docker": "docker compose up -d",
|
||||||
"docker_restore": "docker-compose exec mongo mongorestore --db EMI_SOCIAL /dump/EMI_SOCIAL/",
|
"docker_restore": "docker-compose exec mongo mongorestore --db EMI_SOCIAL /dump/EMI_SOCIAL/",
|
||||||
@@ -30,5 +30,10 @@
|
|||||||
"socket.io": "^4.6.1",
|
"socket.io": "^4.6.1",
|
||||||
"stripe": "^8.178.0",
|
"stripe": "^8.178.0",
|
||||||
"web-push": "^3.4.5"
|
"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