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

View File

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

View File

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