Skip to content

Commit a0aef7a

Browse files
committed
passing tests now that using database
1 parent 7bd6926 commit a0aef7a

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

back-end/test/login.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const should = chai.should() // the same assertion library in the style using th
1111

1212
// import the server
1313
const server = require('../app')
14+
const User = require('../models/User')
1415

1516
// a group of tests related to the /protected route
1617
describe('Login', () => {
@@ -33,7 +34,26 @@ describe('Login', () => {
3334
})
3435

3536
describe('POST /auth/login with correct username/password', () => {
36-
const formData = { username: 'foo', password: 'bar' } // mock form data with correct credentials
37+
// establish a pseudo-random test username
38+
let mockUserCredentials = {
39+
username: `foo-${Math.random()}`, // randomish username
40+
password: 'bar',
41+
}
42+
let mockUser
43+
44+
// create test user before tests
45+
beforeEach(done => {
46+
mockUser = new User(mockUserCredentials).save().then(() => done())
47+
})
48+
49+
// delete test user after tests
50+
afterEach(done => {
51+
// delete the test user
52+
User.remove({ _id: mockUser._id }).then(() => done())
53+
})
54+
55+
const formData = mockUserCredentials // mock form data with correct credentials for test user
56+
3757
it('it should return a 200 HTTP response code', done => {
3858
chai
3959
.request(server)

back-end/test/protected.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,31 @@ describe('Protected', () => {
3939
// let's first create a valid JWT token to use in the requests where we want to be logged in
4040
const jwt = require('jsonwebtoken')
4141
const User = require('../models/User')
42-
const user = new User({ username: 'test', password: 'test' })
43-
const token = user.generateJWT()
42+
43+
// establish a pseudo-random test username
44+
let mockUserCredentials = {
45+
username: `foo-${Math.random()}`, // randomish username
46+
password: 'bar',
47+
}
48+
let mockUser // will hold a test user
49+
let token // will hold this user's JWT token
50+
51+
// create test user before tests
52+
before(done => {
53+
mockUser = new User(mockUserCredentials).save().then(user => {
54+
token = user.generateJWT() // generate a valid token for testing
55+
done()
56+
})
57+
})
58+
59+
// delete test user after tests
60+
after(done => {
61+
// delete the test user
62+
User.remove({ _id: mockUser._id }).then(() => done())
63+
})
4464

4565
it('it should return a 200 HTTP response code', done => {
66+
console.log(`DEBUG: running test`)
4667
chai
4768
.request(server)
4869
.get('/protected')
@@ -64,7 +85,7 @@ describe('Protected', () => {
6485
// res.body.should.have.property("user")
6586
// res.body.should.have.property("message")
6687
res.body.should.have.keys('success', 'user', 'message') // a way to test the presence of an exact set of keys in the response object
67-
expect(res.body).to.have.deep.property('user.id', 1) // check for exact value of a nested value
88+
expect(res.body).to.have.deep.property('user.id') // check for exact value of a nested value
6889
expect(res.body).to.have.deep.property('user.username') // check for existence of a nested value
6990

7091
done() // resolve the Promise that these tests create so mocha can move on

0 commit comments

Comments
 (0)