Improve check jobs parameters tests

pull/175/head
Chocobozzz 2017-12-28 14:40:11 +01:00
parent eec63bbc0f
commit 93e4a311f3
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 30 additions and 40 deletions

View File

@ -1,10 +1,10 @@
import 'express-validator'
import * as express from 'express'
import * as OAuthServer from 'express-oauth-server'
import { logger } from '../helpers/logger'
import 'express-validator'
import { OAUTH_LIFETIME } from '../initializers'
const oAuthServer = new OAuthServer({
useErrorHandler: true,
accessTokenLifetime: OAUTH_LIFETIME.ACCESS_TOKEN,
refreshTokenLifetime: OAUTH_LIFETIME.REFRESH_TOKEN,
model: require('../lib/oauth-model')
@ -13,14 +13,12 @@ const oAuthServer = new OAuthServer({
function authenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
oAuthServer.authenticate()(req, res, err => {
if (err) {
logger.error('Cannot authenticate.', err)
return res.sendStatus(500)
}
if (res.statusCode === 401 || res.statusCode === 400 || res.statusCode === 503) {
return res.json({
error: 'Authentication failed.'
}).end()
return res.status(err.status)
.json({
error: 'Authentication failed.',
code: err.name
})
.end()
}
return next()

View File

@ -4,6 +4,8 @@ import 'mocha'
import * as request from 'supertest'
import { createUser, flushTests, userLogin, killallServers, runServer, ServerInfo, setAccessTokensToServers } from '../../utils'
import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
import { makeGetRequest } from '../../utils/requests/requests'
describe('Test jobs API validators', function () {
const path = '/api/v1/jobs/'
@ -31,45 +33,32 @@ describe('Test jobs API validators', function () {
describe('When listing jobs', function () {
it('Should fail with a bad start pagination', async function () {
await request(server.url)
.get(path)
.query({ start: 'hello' })
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + server.accessToken)
.expect(400)
await checkBadStartPagination(server.url, path, server.accessToken)
})
it('Should fail with a bad count pagination', async function () {
await request(server.url)
.get(path)
.query({ count: 'hello' })
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + server.accessToken)
.expect(400)
await checkBadCountPagination(server.url, path, server.accessToken)
})
it('Should fail with an incorrect sort', async function () {
await request(server.url)
.get(path)
.query({ sort: 'hello' })
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + server.accessToken)
.expect(400)
await checkBadSortPagination(server.url, path, server.accessToken)
})
it('Should fail with a non authenticated user', async function () {
await request(server.url)
.get(path)
.set('Accept', 'application/json')
.expect(401)
await makeGetRequest({
url: server.url,
path,
statusCodeExpected: 401
})
})
it('Should fail with a non admin user', async function () {
await request(server.url)
.get(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + userAccessToken)
.expect(403)
await makeGetRequest({
url: server.url,
path,
token: userAccessToken,
statusCodeExpected: 403
})
})
})

View File

@ -1,27 +1,30 @@
import { makeGetRequest } from './requests'
function checkBadStartPagination (url: string, path: string) {
function checkBadStartPagination (url: string, path: string, token?: string) {
return makeGetRequest({
url,
path,
token,
query: { start: 'hello' },
statusCodeExpected: 400
})
}
function checkBadCountPagination (url: string, path: string) {
function checkBadCountPagination (url: string, path: string, token?: string) {
return makeGetRequest({
url,
path,
token,
query: { count: 'hello' },
statusCodeExpected: 400
})
}
function checkBadSortPagination (url: string, path: string) {
function checkBadSortPagination (url: string, path: string, token?: string) {
return makeGetRequest({
url,
path,
token,
query: { sort: 'hello' },
statusCodeExpected: 400
})