Added e2e validation tests

This commit is contained in:
Chris Troutner
2021-07-06 15:00:00 -07:00
parent ada35eb092
commit 1dcb7f84c2
2 changed files with 48 additions and 58 deletions
@@ -47,6 +47,7 @@ class Validators {
}
// return next()
return true
} catch (error) {
ctx.throw(401)
}
@@ -85,6 +86,7 @@ class Validators {
}
// return next()
return true
} catch (error) {
ctx.throw(401, error.message)
}
@@ -142,6 +144,7 @@ class Validators {
}
// return next()
return true
} catch (error) {
ctx.throw(401, error.message)
}
@@ -1,7 +1,7 @@
const assert = require('chai').assert
const testUtils = require('../../utils/test-utils')
const Validators = require('../../../src/middleware/validators')
const Validators = require('../../../src/controllers/rest-api/middleware/validators')
const sinon = require('sinon')
const mockContext = require('../../unit/mocks/ctx-mock').context
@@ -71,6 +71,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if token not found', async () => {
try {
// Mock the context object.
@@ -84,6 +85,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if token is invalid', async () => {
try {
// Mock the context object.
@@ -101,8 +103,8 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should trigger the "next" function if user is admin', async () => {
try {
it('should return true if user is admin', async () => {
// Mock the context object.
const ctx = mockContext()
ctx.params = { id: context.id }
@@ -112,17 +114,10 @@ describe('Validators', () => {
authorization: `Bearer ${context.adminJWT}`
}
}
// Function that execute if the validations
// are successful
const next = () => { return 'next function' }
const result = await uut.ensureUser(ctx, next)
const result = await uut.ensureUser(ctx)
assert.isString(result)
assert.equal(result, 'next function')
} catch (err) {
assert(false, 'Unexpected result')
}
assert.equal(result, true)
})
})
@@ -140,6 +135,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if token is invalid', async () => {
try {
// Mock the context object.
@@ -157,6 +153,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if user cant be found', async () => {
try {
// Force an error
@@ -177,6 +174,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if user is not admin type', async () => {
try {
// Mock the context object.
@@ -194,8 +192,8 @@ describe('Validators', () => {
assert.include(err.message, 'not admin')
}
})
it('should trigger the "next" function if user is admin', async () => {
try {
it('should return true if user is admin', async () => {
// Mock the context object.
const ctx = mockContext()
ctx.request = {
@@ -203,17 +201,10 @@ describe('Validators', () => {
authorization: `Bearer ${context.adminJWT}`
}
}
// Function that execute if the validations
// are successful
const next = () => { return 'next function' }
const result = await uut.ensureAdmin(ctx, next)
const result = await uut.ensureAdmin(ctx)
assert.isString(result)
assert.equal(result, 'next function')
} catch (err) {
assert(false, 'Unexpected result')
}
assert.equal(result, true)
})
})
@@ -231,6 +222,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if token is invalid', async () => {
try {
// Mock the context object.
@@ -250,6 +242,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if user cant be found', async () => {
try {
// Force an error
@@ -272,6 +265,7 @@ describe('Validators', () => {
assert.include(err.message, 'Unauthorized')
}
})
it('should throw 401 if user is not admin type', async () => {
try {
// Mock the context object.
@@ -291,8 +285,8 @@ describe('Validators', () => {
assert.include(err.message, 'not admin')
}
})
it('should trigger the "next" function if user is admin', async () => {
try {
it('should return true if user is admin', async () => {
// Mock the context object.
const ctx = mockContext()
ctx.params = { id: context.id }
@@ -302,17 +296,10 @@ describe('Validators', () => {
authorization: `Bearer ${context.adminJWT}`
}
}
// Function that execute if the validations
// are successful
const next = () => { return 'next function' }
const result = await uut.ensureTargetUserOrAdmin(ctx, next)
const result = await uut.ensureTargetUserOrAdmin(ctx)
assert.isString(result)
assert.equal(result, 'next function')
} catch (err) {
assert(false, 'Unexpected result')
}
assert.equal(result, true)
})
})
})