|
@@ -7,19 +7,27 @@ let should = chai.should()
|
|
|
let Models = require('../models')
|
|
|
let User = Models.User
|
|
|
let AdminToken = Models.AdminToken
|
|
|
+
|
|
|
const Errors = require('../lib/errors.js')
|
|
|
|
|
|
chai.use(require('chai-http'))
|
|
|
chai.use(require('chai-things'))
|
|
|
|
|
|
-describe('User', () => {
|
|
|
+describe('AdminToken', () => {
|
|
|
+ //Wait for app to start before commencing
|
|
|
+ before((done) => {
|
|
|
+ server.on('appStarted', () => {
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
//Delete all rows in table after
|
|
|
//tests completed
|
|
|
after((done) => {
|
|
|
- Promise.all[
|
|
|
+ Promise.all([
|
|
|
User.sync({ force: true }),
|
|
|
AdminToken.sync({ force: true })
|
|
|
- ]
|
|
|
+ ])
|
|
|
.then(() => {
|
|
|
done(null);
|
|
|
})
|
|
@@ -28,12 +36,12 @@ describe('User', () => {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- describe('POST /admin_token', async (done) => {
|
|
|
- try {
|
|
|
- let token
|
|
|
- let agent = chai.request.agent(server)
|
|
|
+ describe('POST /admin_token', async () => {
|
|
|
+ let token
|
|
|
+ let agent = chai.request.agent(server)
|
|
|
|
|
|
- await agent
|
|
|
+ before((done) => {
|
|
|
+ agent
|
|
|
.post('/api/v1/user')
|
|
|
.set('content-type', 'application/json')
|
|
|
.send({
|
|
@@ -41,38 +49,41 @@ describe('User', () => {
|
|
|
password: 'password',
|
|
|
admin: true
|
|
|
})
|
|
|
+ .then(() => {
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ .catch(done)
|
|
|
+ })
|
|
|
+
|
|
|
|
|
|
- it('should generate a token if logged in', (done) => {
|
|
|
- let res = await agent.post('/api/v1/admin_token')
|
|
|
-
|
|
|
- res.should.have.status(200)
|
|
|
- res.body.should.have.property('token')
|
|
|
-
|
|
|
- token = res.body.token
|
|
|
+ it('should generate a token if logged in', async () => {
|
|
|
+ let res = await agent.post('/api/v1/admin_token')
|
|
|
|
|
|
- done()
|
|
|
- })
|
|
|
+ res.should.have.status(200)
|
|
|
+ res.body.should.have.property('token')
|
|
|
|
|
|
- it('should generate a different token if logged in', (done) => {
|
|
|
- let res = await agent.post('/api/v1/admin_token')
|
|
|
+ token = res.body.token
|
|
|
+ })
|
|
|
|
|
|
- res.should.have.status(200)
|
|
|
- res.body.should.have.property('token')
|
|
|
- res.body.token.should.not.equal(token)
|
|
|
+ it('should generate a different token if logged in', async () => {
|
|
|
+ let res = await agent.post('/api/v1/admin_token')
|
|
|
|
|
|
- done()
|
|
|
- })
|
|
|
+ res.should.have.status(200)
|
|
|
+ res.body.should.have.property('token')
|
|
|
+ res.body.token.should.not.equal(token)
|
|
|
+ })
|
|
|
|
|
|
- it('should give an error if not logged in', (done) => {
|
|
|
+ it('should give an error if not logged in', async () => {
|
|
|
+ try {
|
|
|
let res = await chai.request(server).post('/api/v1/admin_token')
|
|
|
|
|
|
res.should.have.status(403)
|
|
|
res.body.errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
|
|
|
+ } catch(res) {
|
|
|
+ res.should.have.status(403)
|
|
|
+ JSON.parse(res.response.text).errors.should.contain.something.that.deep.equals(Errors.requestNotAuthorized)
|
|
|
+ }
|
|
|
|
|
|
- done()
|
|
|
- })
|
|
|
- } catch (err) {
|
|
|
- done(err)
|
|
|
- }
|
|
|
+ })
|
|
|
})
|
|
|
})
|