Skip to content

Commit

Permalink
Merge pull request #89 from agiledev-students-spring2024/loginSignUpT…
Browse files Browse the repository at this point in the history
…ests

create test folder & tests for signup + login
  • Loading branch information
hongsimmon authored Apr 1, 2024
2 parents 609fce2 + 95e3081 commit 3aba832
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion back-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
32 changes: 32 additions & 0 deletions test/login.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
38 changes: 38 additions & 0 deletions test/signup.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 3aba832

Please sign in to comment.