From f2eb23cd87cf32b8fe545178143b5f49e06a58da Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Tue, 8 Dec 2020 21:16:10 +0100 Subject: emit more specific status codes on video upload (#3423) - reduce http status codes list to potentially useful codes - convert more codes to typed ones - factorize html generator for error responses --- server/tests/api/check-params/accounts.ts | 3 +- server/tests/api/check-params/contact-form.ts | 59 ++++++++++++++++++++++----- server/tests/api/check-params/users.ts | 12 +++--- server/tests/api/check-params/videos.ts | 18 +++++++- 4 files changed, 72 insertions(+), 20 deletions(-) (limited to 'server/tests/api/check-params') diff --git a/server/tests/api/check-params/accounts.ts b/server/tests/api/check-params/accounts.ts index c29af7cd7..d1712cff6 100644 --- a/server/tests/api/check-params/accounts.ts +++ b/server/tests/api/check-params/accounts.ts @@ -9,6 +9,7 @@ import { checkBadStartPagination } from '../../../../shared/extra-utils/requests/check-api-params' import { getAccount } from '../../../../shared/extra-utils/users/accounts' +import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' describe('Test accounts API validators', function () { const path = '/api/v1/accounts/' @@ -38,7 +39,7 @@ describe('Test accounts API validators', function () { describe('When getting an account', function () { it('Should return 404 with a non existing name', async function () { - await getAccount(server.url, 'arfaze', 404) + await getAccount(server.url, 'arfaze', HttpStatusCode.NOT_FOUND_404) }) }) diff --git a/server/tests/api/check-params/contact-form.ts b/server/tests/api/check-params/contact-form.ts index b2126b9b0..c7f9c1b47 100644 --- a/server/tests/api/check-params/contact-form.ts +++ b/server/tests/api/check-params/contact-form.ts @@ -5,6 +5,7 @@ import 'mocha' import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils' import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' +import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' describe('Test contact form API validators', function () { let server: ServerInfo @@ -29,7 +30,7 @@ describe('Test contact form API validators', function () { }) it('Should not accept a contact form if emails are disabled', async function () { - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 })) + await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) }) it('Should not accept a contact form if it is disabled in the configuration', async function () { @@ -39,7 +40,7 @@ describe('Test contact form API validators', function () { // Contact form is disabled await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 })) + await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) }) it('Should not accept a contact form if from email is invalid', async function () { @@ -50,21 +51,57 @@ describe('Test contact form API validators', function () { // Email & contact form enabled await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } }) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail' })) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail@' })) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: undefined })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + fromEmail: 'badEmail' + })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + fromEmail: 'badEmail@' + })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + fromEmail: undefined + })) }) it('Should not accept a contact form if from name is invalid', async function () { - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: 'name'.repeat(100) })) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: '' })) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: undefined })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + fromName: 'name'.repeat(100) + })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + fromName: '' + })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + fromName: undefined + })) }) it('Should not accept a contact form if body is invalid', async function () { - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'body'.repeat(5000) })) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'a' })) - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: undefined })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + body: 'body'.repeat(5000) + })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + body: 'a' + })) + await sendContactForm(immutableAssign(defaultBody, { + url: server.url, + expectedStatus: HttpStatusCode.BAD_REQUEST_400, + body: undefined + })) }) it('Should accept a contact form with the correct parameters', async function () { diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index 21ace36aa..0a13f5b67 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -1102,7 +1102,7 @@ describe('Test users API validators', function () { videoQuota: 42 }) - await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.FORBIDDEN_403) + await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413) }) it('Should fail with a registered user having too many videos', async function () { @@ -1120,7 +1120,7 @@ describe('Test users API validators', function () { await uploadVideo(server.url, userAccessToken, videoAttributes) await uploadVideo(server.url, userAccessToken, videoAttributes) await uploadVideo(server.url, userAccessToken, videoAttributes) - await uploadVideo(server.url, userAccessToken, videoAttributes, HttpStatusCode.FORBIDDEN_403) + await uploadVideo(server.url, userAccessToken, videoAttributes, HttpStatusCode.PAYLOAD_TOO_LARGE_413) }) it('Should fail to import with HTTP/Torrent/magnet', async function () { @@ -1151,7 +1151,7 @@ describe('Test users API validators', function () { }) describe('When having a daily video quota', function () { - it('Should fail with a user having too many videos', async function () { + it('Should fail with a user having too many videos daily', async function () { await updateUser({ url: server.url, userId: rootId, @@ -1159,7 +1159,7 @@ describe('Test users API validators', function () { videoQuotaDaily: 42 }) - await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.FORBIDDEN_403) + await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413) }) }) @@ -1173,7 +1173,7 @@ describe('Test users API validators', function () { videoQuotaDaily: 1024 * 1024 * 1024 }) - await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.FORBIDDEN_403) + await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413) }) it('Should fail if exceeding daily quota', async function () { @@ -1185,7 +1185,7 @@ describe('Test users API validators', function () { videoQuotaDaily: 42 }) - await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.FORBIDDEN_403) + await uploadVideo(server.url, server.accessToken, {}, HttpStatusCode.PAYLOAD_TOO_LARGE_413) }) }) diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts index d60546917..5faba82c4 100644 --- a/server/tests/api/check-params/videos.ts +++ b/server/tests/api/check-params/videos.ts @@ -348,12 +348,26 @@ describe('Test videos API validator', function () { let attaches = { videofile: join(root(), 'server', 'tests', 'fixtures', 'video_short_fake.webm') } - await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches }) + await makeUploadRequest({ + url: server.url, + path: path + '/upload', + token: server.accessToken, + fields, + attaches, + statusCodeExpected: HttpStatusCode.UNPROCESSABLE_ENTITY_422 + }) attaches = { videofile: join(root(), 'server', 'tests', 'fixtures', 'video_short.mkv') } - await makeUploadRequest({ url: server.url, path: path + '/upload', token: server.accessToken, fields, attaches }) + await makeUploadRequest({ + url: server.url, + path: path + '/upload', + token: server.accessToken, + fields, + attaches, + statusCodeExpected: HttpStatusCode.UNSUPPORTED_MEDIA_TYPE_415 + }) }) it('Should fail with an incorrect thumbnail file', async function () { -- cgit v1.2.3