-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')
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()
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/'
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
+ })
})
})
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
})