Skip to content

Instantly share code, notes, and snippets.

@theghostyced
Last active November 24, 2018 15:02
Show Gist options
  • Save theghostyced/22f4d8d2ccf20f39b1b542d48956120a to your computer and use it in GitHub Desktop.
Save theghostyced/22f4d8d2ccf20f39b1b542d48956120a to your computer and use it in GitHub Desktop.
A simple login Authentication test
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();
});
});
});
});
@Danielshow
Copy link

Nice one Daniel. The test is well structured, nice comments and variable names

@theghostyced
Copy link
Author

Thanks Daniel

@davidshare
Copy link

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment