From eec63bbc0f4fdb39e56f37127b35c449f90a135f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 28 Dec 2017 14:29:57 +0100 Subject: Improve check follow params tests --- server/tests/api/check-params/follows.ts | 196 +++++++++++------------ server/tests/api/check-params/jobs.ts | 4 +- server/tests/api/check-params/users.ts | 10 +- server/tests/api/check-params/video-abuses.ts | 4 +- server/tests/api/check-params/video-blacklist.ts | 4 +- server/tests/api/check-params/video-channels.ts | 4 +- server/tests/api/check-params/videos.ts | 4 +- 7 files changed, 106 insertions(+), 120 deletions(-) (limited to 'server/tests/api/check-params') diff --git a/server/tests/api/check-params/follows.ts b/server/tests/api/check-params/follows.ts index cdd2783df..d5fcb7477 100644 --- a/server/tests/api/check-params/follows.ts +++ b/server/tests/api/check-params/follows.ts @@ -4,14 +4,10 @@ import 'mocha' import * as request from 'supertest' import { - createUser, - flushTests, - killallServers, - loginAndGetAccessToken, - runServer, - ServerInfo, - setAccessTokensToServers + createUser, flushTests, killallServers, makeDeleteRequest, makePostBodyRequest, runServer, ServerInfo, setAccessTokensToServers, + userLogin } from '../../utils' +import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' describe('Test server follows API validators', function () { let server: ServerInfo @@ -19,7 +15,7 @@ describe('Test server follows API validators', function () { // --------------------------------------------------------------- before(async function () { - this.timeout(45000) + this.timeout(20000) await flushTests() server = await runServer(1) @@ -31,81 +27,85 @@ describe('Test server follows API validators', function () { let userAccessToken = null before(async function () { - await createUser(server.url, server.accessToken, 'user1', 'password') - server.user = { + const user = { username: 'user1', password: 'password' } - userAccessToken = await loginAndGetAccessToken(server) + await createUser(server.url, server.accessToken, user.username, user.password) + userAccessToken = await userLogin(server, user) }) describe('When adding follows', function () { const path = '/api/v1/server/following' - const body = { - hosts: [ 'localhost:9002' ] - } it('Should fail without hosts', async function () { - await request(server.url) - .post(path) - .set('Authorization', 'Bearer ' + server.accessToken) - .set('Accept', 'application/json') - .expect(400) + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + statusCodeExpected: 400 + }) }) it('Should fail if hosts is not an array', async function () { - await request(server.url) - .post(path) - .send({ hosts: 'localhost:9002' }) - .set('Authorization', 'Bearer ' + server.accessToken) - .set('Accept', 'application/json') - .expect(400) + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + fields: { hosts: 'localhost:9002' }, + statusCodeExpected: 400 + }) }) it('Should fail if the array is not composed by hosts', async function () { - await request(server.url) - .post(path) - .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] }) - .set('Authorization', 'Bearer ' + server.accessToken) - .set('Accept', 'application/json') - .expect(400) + await makePostBodyRequest({ + url: server.url, + path, + fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] }, + token: server.accessToken, + statusCodeExpected: 400 + }) }) it('Should fail if the array is composed with http schemes', async function () { - await request(server.url) - .post(path) - .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] }) - .set('Authorization', 'Bearer ' + server.accessToken) - .set('Accept', 'application/json') - .expect(400) + await makePostBodyRequest({ + url: server.url, + path, + fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] }, + token: server.accessToken, + statusCodeExpected: 400 + }) }) it('Should fail if hosts are not unique', async function () { - await request(server.url) - .post(path) - .send({ urls: [ 'localhost:9002', 'localhost:9002' ] }) - .set('Authorization', 'Bearer ' + server.accessToken) - .set('Accept', 'application/json') - .expect(400) + await makePostBodyRequest({ + url: server.url, + path, + fields: { urls: [ 'localhost:9002', 'localhost:9002' ] }, + token: server.accessToken, + statusCodeExpected: 400 + }) }) it('Should fail with an invalid token', async function () { - await request(server.url) - .post(path) - .send(body) - .set('Authorization', 'Bearer fake_token') - .set('Accept', 'application/json') - .expect(401) + await makePostBodyRequest({ + url: server.url, + path, + fields: { hosts: [ 'localhost:9002' ] }, + token: 'fake_token', + statusCodeExpected: 401 + }) }) it('Should fail if the user is not an administrator', async function () { - await request(server.url) - .post(path) - .send(body) - .set('Authorization', 'Bearer ' + userAccessToken) - .set('Accept', 'application/json') - .expect(403) + await makePostBodyRequest({ + url: server.url, + path, + fields: { hosts: [ 'localhost:9002' ] }, + token: userAccessToken, + statusCodeExpected: 403 + }) }) }) @@ -113,27 +113,15 @@ describe('Test server follows API validators', function () { const path = '/api/v1/server/following' it('Should fail with a bad start pagination', async function () { - await request(server.url) - .get(path) - .query({ start: 'hello' }) - .set('Accept', 'application/json') - .expect(400) + await checkBadStartPagination(server.url, path) }) it('Should fail with a bad count pagination', async function () { - await request(server.url) - .get(path) - .query({ count: 'hello' }) - .set('Accept', 'application/json') - .expect(400) + await checkBadCountPagination(server.url, path) }) it('Should fail with an incorrect sort', async function () { - await request(server.url) - .get(path) - .query({ sort: 'hello' }) - .set('Accept', 'application/json') - .expect(400) + await checkBadSortPagination(server.url, path) }) }) @@ -141,27 +129,15 @@ describe('Test server follows API validators', function () { const path = '/api/v1/server/followers' it('Should fail with a bad start pagination', async function () { - await request(server.url) - .get(path) - .query({ start: 'hello' }) - .set('Accept', 'application/json') - .expect(400) + await checkBadStartPagination(server.url, path) }) it('Should fail with a bad count pagination', async function () { - await request(server.url) - .get(path) - .query({ count: 'hello' }) - .set('Accept', 'application/json') - .expect(400) + await checkBadCountPagination(server.url, path) }) it('Should fail with an incorrect sort', async function () { - await request(server.url) - .get(path) - .query({ sort: 'hello' }) - .set('Accept', 'application/json') - .expect(400) + await checkBadSortPagination(server.url, path) }) }) @@ -169,30 +145,40 @@ describe('Test server follows API validators', function () { const path = '/api/v1/server/following' it('Should fail with an invalid token', async function () { - await request(server.url) - .delete(path + '/1') - .set('Authorization', 'Bearer faketoken') - .set('Accept', 'application/json') - .expect(401) + await makeDeleteRequest({ + url: server.url, + path: path + '/localhost:9002', + token: 'fake_token', + statusCodeExpected: 401 + }) }) it('Should fail if the user is not an administrator', async function () { - await request(server.url) - .delete(path + '/1') - .set('Authorization', 'Bearer ' + userAccessToken) - .set('Accept', 'application/json') - .expect(403) - }) - - it('Should fail we do not follow this server', async function () { - await request(server.url) - .delete(path + '/example.com') - .set('Authorization', 'Bearer ' + server.accessToken) - .set('Accept', 'application/json') - .expect(404) + await makeDeleteRequest({ + url: server.url, + path: path + '/localhost:9002', + token: userAccessToken, + statusCodeExpected: 403 + }) + }) + + it('Should fail if we do not follow this server', async function () { + await makeDeleteRequest({ + url: server.url, + path: path + '/example.com', + token: server.accessToken, + statusCodeExpected: 404 + }) + }) + + it('Should succeed with the correct parameters', async function () { + await makeDeleteRequest({ + url: server.url, + path: path + '/localhost:9002', + token: server.accessToken, + statusCodeExpected: 404 + }) }) - - it('Should succeed with the correct parameters') }) }) diff --git a/server/tests/api/check-params/jobs.ts b/server/tests/api/check-params/jobs.ts index 7a0dd6e8c..3795d1d64 100644 --- a/server/tests/api/check-params/jobs.ts +++ b/server/tests/api/check-params/jobs.ts @@ -3,7 +3,7 @@ import 'mocha' import * as request from 'supertest' -import { createUser, flushTests, getUserAccessToken, killallServers, runServer, ServerInfo, setAccessTokensToServers } from '../../utils' +import { createUser, flushTests, userLogin, killallServers, runServer, ServerInfo, setAccessTokensToServers } from '../../utils' describe('Test jobs API validators', function () { const path = '/api/v1/jobs/' @@ -26,7 +26,7 @@ describe('Test jobs API validators', function () { password: 'my super password' } await createUser(server.url, server.accessToken, user.username, user.password) - userAccessToken = await getUserAccessToken(server, user) + userAccessToken = await userLogin(server, user) }) describe('When listing jobs', function () { diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index 72488e5c4..b566a2f1e 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -11,13 +11,13 @@ import { getVideosList, makePutBodyRequest, createUser, - loginAndGetAccessToken, + serverLogin, getUsersList, registerUser, setAccessTokensToServers, killallServers, makePostBodyRequest, - getUserAccessToken + userLogin } from '../../utils' import { UserRole } from '../../../../shared' @@ -58,7 +58,7 @@ describe('Test users API validators', function () { username: 'user1', password: 'my super password' } - userAccessToken = await getUserAccessToken(server, user) + userAccessToken = await userLogin(server, user) }) describe('When listing users', function () { @@ -304,7 +304,7 @@ describe('Test users API validators', function () { password: 'my super password' } - userAccessToken = await loginAndGetAccessToken(server) + userAccessToken = await serverLogin(server) const fields = { username: 'user3', email: 'test@example.com', @@ -675,7 +675,7 @@ describe('Test users API validators', function () { email: 'test3@example.com', password: 'my super password' } - userAccessToken = await loginAndGetAccessToken(server) + userAccessToken = await serverLogin(server) const videoAttributes = { fixture: 'video_short2.webm' } await uploadVideo(server.url, userAccessToken, videoAttributes) diff --git a/server/tests/api/check-params/video-abuses.ts b/server/tests/api/check-params/video-abuses.ts index eac12b6f0..e994ccd49 100644 --- a/server/tests/api/check-params/video-abuses.ts +++ b/server/tests/api/check-params/video-abuses.ts @@ -13,7 +13,7 @@ import { setAccessTokensToServers, killallServers, makePostBodyRequest, - getUserAccessToken + userLogin } from '../../utils' describe('Test video abuses API validators', function () { @@ -35,7 +35,7 @@ describe('Test video abuses API validators', function () { const password = 'my super password' await createUser(server.url, server.accessToken, username, password) - userAccessToken = await getUserAccessToken(server, { username, password }) + userAccessToken = await userLogin(server, { username, password }) // Upload a video const videoAttributes = {} diff --git a/server/tests/api/check-params/video-blacklist.ts b/server/tests/api/check-params/video-blacklist.ts index eb16b3af0..c8b457182 100644 --- a/server/tests/api/check-params/video-blacklist.ts +++ b/server/tests/api/check-params/video-blacklist.ts @@ -13,7 +13,7 @@ import { setAccessTokensToServers, killallServers, makePostBodyRequest, - getUserAccessToken + userLogin } from '../../utils' describe('Test video blacklist API validators', function () { @@ -34,7 +34,7 @@ describe('Test video blacklist API validators', function () { const username = 'user1' const password = 'my super password' await createUser(server.url, server.accessToken, username, password) - userAccessToken = await getUserAccessToken(server, { username, password }) + userAccessToken = await userLogin(server, { username, password }) // Upload a video const videoAttributes = {} diff --git a/server/tests/api/check-params/video-channels.ts b/server/tests/api/check-params/video-channels.ts index 7103aec25..bf464152b 100644 --- a/server/tests/api/check-params/video-channels.ts +++ b/server/tests/api/check-params/video-channels.ts @@ -15,7 +15,7 @@ import { makePostBodyRequest, getVideoChannelsList, createUser, - getUserAccessToken + userLogin } from '../../utils' describe('Test videos API validator', function () { @@ -40,7 +40,7 @@ describe('Test videos API validator', function () { } await createUser(server.url, server.accessToken, user.username, user.password) - accessTokenUser = await getUserAccessToken(server, user) + accessTokenUser = await userLogin(server, user) }) describe('When listing a video channels', function () { diff --git a/server/tests/api/check-params/videos.ts b/server/tests/api/check-params/videos.ts index 0aaa6e7c9..b0c850f0c 100644 --- a/server/tests/api/check-params/videos.ts +++ b/server/tests/api/check-params/videos.ts @@ -17,7 +17,7 @@ import { makePostUploadRequest, getMyUserInformation, createUser, - getUserAccessToken + userLogin } from '../../utils' import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' @@ -260,7 +260,7 @@ describe('Test videos API validator', function () { } await createUser(server.url, server.accessToken, user.username, user.password) - const accessTokenUser = await getUserAccessToken(server, user) + const accessTokenUser = await userLogin(server, user) const res = await getMyUserInformation(server.url, accessTokenUser) const customChannelId = res.body.videoChannels[0].id -- cgit v1.2.3