Skip to content

Instantly share code, notes, and snippets.

@akmalhazim
Created September 7, 2019 04:12
Show Gist options
  • Save akmalhazim/9a811cb9312669525986877f1d78e552 to your computer and use it in GitHub Desktop.
Save akmalhazim/9a811cb9312669525986877f1d78e552 to your computer and use it in GitHub Desktop.
ProjekIoT Unit Test Review Second Revision
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const { should, expect } = chai;
chai.should();
chai.use(chaiAsPromised);
const { User } = require('../models/index');
const AuthController = require('../controllers/auth');
let user;
describe('AuthController class', () => {
before(async () => {
user = await User.create({
name: 'Akmal Hazim',
email: '[email protected]',
password: 'password'
});
});
after(async () => {
await user.destroy();
});
context('user can login with correct credentials', () => {
let authClass;
it('user can login with username and password', async () => {
authClass = await AuthController.loginWithCredentials({
email: '[email protected]',
password: 'password'
}).should.be.fulfilled;
});
it('user can view profile', async () => {
await authClass.me().should.be.fulfilled;
});
describe('user can authenticate with jwt access token', () => {
let accessToken;
it('user can retrieve jwt access token', async () => {
accessToken = await authClass.getAccessToken().should.be.fulfilled;
});
it('user can view profile using jwt access token', async() => {
await AuthController.login(accessToken).should.be.fulfilled;
})
});
});
context('user cannot login with wrong credentials', () => {
it('user cannot login with wrong password', async () => {
await AuthController.loginWithCredentials({
email: '[email protected]',
password: 'wrong-password'
}).should.be.rejectedWith(Error, 'Wrong email and/or password');
});
it('user cannot login with wrong email', async () => {
await AuthController.loginWithCredentials({
email: '[email protected]',
password: 'password'
}).should.be.rejectedWith(Error, 'Wrong email and/or password');
});
it('user cannot login with wrong email and wrong password', async () => {
await AuthController.loginWithCredentials({
email: '[email protected]',
password: 'not-valid-password'
}).should.be.rejectedWith(Error, 'Wrong email and/or password');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment