Last active
November 24, 2018 15:02
-
-
Save theghostyced/22f4d8d2ccf20f39b1b542d48956120a to your computer and use it in GitHub Desktop.
A simple login Authentication test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chai from 'chai-http'; | |
import requestAgent from 'supertest'; | |
import server from './server'; | |
// Destructuring our expect file from chai | |
const { expect } = chai; | |
// Our various custom messages that have been abstracted. | |
import { | |
LOGIN_ERROR_MSG, | |
LOGIN_SUCCESSFUL_MSG, | |
} from '../../helpers'; | |
// Creating a request agent server for testing. | |
const app = requestAgent(server); | |
// Our login app url | |
const LOGIN_URL = '/auth/v1/login'; | |
// Our mock data | |
const fakeUsers = [ | |
{ email: '', password: '' }, | |
{ email: '[email protected]', password: 'secret' } | |
]; | |
// The test | |
describe(`POST: Testing ${LOGIN_URL} route`, () => { | |
it('should throw error if no data is passed', (done) => { | |
app | |
.post(LOGIN_URL) | |
.send(fakeUsers[0]) | |
.end((err, response) => { | |
expect(404); | |
expect(response.body).to.have.property('err_msg'); | |
expect(response.body.message).to.equal(LOGIN_ERROR_MSG); | |
done(); | |
}); | |
it('should return successful message with token if valid data is passed', (done) => { | |
app | |
.post(LOGIN_URL) | |
.send(fakeUsers[1]) | |
.end((err, response) => { | |
expect(200); | |
expect(response.body).to.have.property('message'); | |
expect(response.body).to.have.property('token'); | |
expect(response.body.message).to.equal(LOGIN_SUCCESSFUL_MSG); | |
done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great!