diff --git a/back-end/package.json b/back-end/package.json index 2a23cc7..41a37b5 100644 --- a/back-end/package.json +++ b/back-end/package.json @@ -4,7 +4,8 @@ "description": "The back-end of your project will live in this directory.", "main": "Server.js", "scripts": { - "test": "mocha --timeout 10000", + "test": "mocha", + "coverage": "c8 mocha", "start": "nodemon Server" }, "author": "", diff --git a/test/login.test.js b/test/login.test.js new file mode 100644 index 0000000..6decbd0 --- /dev/null +++ b/test/login.test.js @@ -0,0 +1,32 @@ +const chai = require('chai'); +const chaiHttp = require('chai-http'); +const expect = chai.expect; +const app = require('../back-end/App.js'); + +chai.use(chaiHttp); + +describe('Login', () => { + // Test successful login for BobbyImpasto + it('should log in a user with the correct credentials', done => { + chai.request(app) + .post('/login') + .send({'username': 'john123', 'password': 'qwerasdf'}) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('message').eql('Login successful'); + done(); + }); + }); + + // Test failed login for wrong password + it('should not log in a user with incorrect credentials', done => { + chai.request(app) + .post('/login') + .send({'username': 'john123', 'password': 'incorrectPassword'}) + .end((err, res) => { + expect(res).to.have.status(401); + expect(res.body).to.have.property('message').eql('Invalid username or password'); + done(); + }); + }); +}); diff --git a/test/signup.test.js b/test/signup.test.js new file mode 100644 index 0000000..82313d8 --- /dev/null +++ b/test/signup.test.js @@ -0,0 +1,38 @@ +const chai = require('chai'); +const chaiHttp = require('chai-http'); +const expect = chai.expect; +const app = require('../back-end/App.js'); // Replace with the correct path to your Express app + +chai.use(chaiHttp); + +describe('Signup', () => { + // Test successful signup + it('should register a new user', done => { + chai.request(app) + .post('/signup') + .send({ + 'username': 'newUser123', + 'password': 'NewPass123!' + }) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('message').eql('Signup successful.'); + done(); + }); + }); + + // Test signup with existing username + it('should not register a user with an existing username', done => { + chai.request(app) + .post('/signup') + .send({ + 'username': 'john123', // Assuming john123 is already in the mockDatabase.json + 'password': 'SomePass123!' + }) + .end((err, res) => { + expect(res).to.have.status(400); + expect(res.body).to.have.property('message').eql('Username already exists.'); + done(); + }); + }); +});