From: Chocobozzz Date: Thu, 28 Dec 2017 13:40:11 +0000 (+0100) Subject: Improve check jobs parameters tests X-Git-Tag: v0.0.1-alpha~76 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=93e4a311f37d7fc82ec81190553f8beae28fdef5;p=github%2FChocobozzz%2FPeerTube.git Improve check jobs parameters tests --- diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 12872c4a5..9976993e0 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -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() diff --git a/server/tests/api/check-params/jobs.ts b/server/tests/api/check-params/jobs.ts index 3795d1d64..46b05cb4e 100644 --- a/server/tests/api/check-params/jobs.ts +++ b/server/tests/api/check-params/jobs.ts @@ -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 + }) }) }) diff --git a/server/tests/utils/requests/check-api-params.ts b/server/tests/utils/requests/check-api-params.ts index fbd660629..7550eb3d8 100644 --- a/server/tests/utils/requests/check-api-params.ts +++ b/server/tests/utils/requests/check-api-params.ts @@ -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 })