Created
September 6, 2019 10:27
-
-
Save akmalhazim/93080bb8b6237f9251e985b92d8525b9 to your computer and use it in GitHub Desktop.
ProjekIoT Unit Test Review
This file contains hidden or 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
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