aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/utils')
-rw-r--r--server/tests/utils/requests/check-api-params.ts36
-rw-r--r--server/tests/utils/requests/requests.ts46
-rw-r--r--server/tests/utils/server/servers.ts2
-rw-r--r--server/tests/utils/users/login.ts10
-rw-r--r--server/tests/utils/videos/videos.ts20
5 files changed, 98 insertions, 16 deletions
diff --git a/server/tests/utils/requests/check-api-params.ts b/server/tests/utils/requests/check-api-params.ts
new file mode 100644
index 000000000..fbd660629
--- /dev/null
+++ b/server/tests/utils/requests/check-api-params.ts
@@ -0,0 +1,36 @@
1import { makeGetRequest } from './requests'
2
3function checkBadStartPagination (url: string, path: string) {
4 return makeGetRequest({
5 url,
6 path,
7 query: { start: 'hello' },
8 statusCodeExpected: 400
9 })
10}
11
12function checkBadCountPagination (url: string, path: string) {
13 return makeGetRequest({
14 url,
15 path,
16 query: { count: 'hello' },
17 statusCodeExpected: 400
18 })
19}
20
21function checkBadSortPagination (url: string, path: string) {
22 return makeGetRequest({
23 url,
24 path,
25 query: { sort: 'hello' },
26 statusCodeExpected: 400
27 })
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 checkBadStartPagination,
34 checkBadCountPagination,
35 checkBadSortPagination
36}
diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts
index 52b7a4c29..eb02cf9e6 100644
--- a/server/tests/utils/requests/requests.ts
+++ b/server/tests/utils/requests/requests.ts
@@ -1,11 +1,43 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3function makeGetRequest (url: string, path: string) { 3function makeGetRequest (options: {
4 return request(url) 4 url: string,
5 .get(path) 5 path: string,
6 query?: any,
7 token?: string,
8 statusCodeExpected?: number
9}) {
10 if (!options.statusCodeExpected) options.statusCodeExpected = 400
11
12 const req = request(options.url)
13 .get(options.path)
6 .set('Accept', 'application/json') 14 .set('Accept', 'application/json')
7 .expect(200) 15
16 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
17 if (options.query) req.query(options.query)
18
19 return req
8 .expect('Content-Type', /json/) 20 .expect('Content-Type', /json/)
21 .expect(options.statusCodeExpected)
22}
23
24function makeDeleteRequest (options: {
25 url: string,
26 path: string,
27 token?: string,
28 statusCodeExpected?: number
29}) {
30 if (!options.statusCodeExpected) options.statusCodeExpected = 400
31
32 const req = request(options.url)
33 .delete(options.path)
34 .set('Accept', 'application/json')
35
36 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
37
38 return req
39 .expect('Content-Type', /json/)
40 .expect(options.statusCodeExpected)
9} 41}
10 42
11function makePostUploadRequest (options: { 43function makePostUploadRequest (options: {
@@ -48,9 +80,10 @@ function makePostBodyRequest (options: {
48 url: string, 80 url: string,
49 path: string, 81 path: string,
50 token?: string, 82 token?: string,
51 fields: { [ fieldName: string ]: any }, 83 fields?: { [ fieldName: string ]: any },
52 statusCodeExpected?: number 84 statusCodeExpected?: number
53}) { 85}) {
86 if (!options.fields) options.fields = {}
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400 87 if (!options.statusCodeExpected) options.statusCodeExpected = 400
55 88
56 const req = request(options.url) 89 const req = request(options.url)
@@ -88,5 +121,6 @@ export {
88 makeGetRequest, 121 makeGetRequest,
89 makePostUploadRequest, 122 makePostUploadRequest,
90 makePostBodyRequest, 123 makePostBodyRequest,
91 makePutBodyRequest 124 makePutBodyRequest,
125 makeDeleteRequest
92} 126}
diff --git a/server/tests/utils/server/servers.ts b/server/tests/utils/server/servers.ts
index 8340fbc18..4add2f69a 100644
--- a/server/tests/utils/server/servers.ts
+++ b/server/tests/utils/server/servers.ts
@@ -114,7 +114,7 @@ function runServer (serverNumber: number, configOverride?: Object) {
114 } 114 }
115 115
116 return new Promise<ServerInfo>(res => { 116 return new Promise<ServerInfo>(res => {
117 server.app = fork(join(__dirname, '..', '..', '..', 'dist', 'server.js'), [], options) 117 server.app = fork(join(__dirname, '..', '..', '..', '..', 'dist', 'server.js'), [], options)
118 server.app.stdout.on('data', function onStdout (data) { 118 server.app.stdout.on('data', function onStdout (data) {
119 let dontContinue = false 119 let dontContinue = false
120 120
diff --git a/server/tests/utils/users/login.ts b/server/tests/utils/users/login.ts
index 855c4828d..04444e2f1 100644
--- a/server/tests/utils/users/login.ts
+++ b/server/tests/utils/users/login.ts
@@ -26,13 +26,13 @@ function login (url: string, client: Client, user: User, expectedStatus = 200) {
26 .expect(expectedStatus) 26 .expect(expectedStatus)
27} 27}
28 28
29async function loginAndGetAccessToken (server: Server) { 29async function serverLogin (server: Server) {
30 const res = await login(server.url, server.client, server.user, 200) 30 const res = await login(server.url, server.client, server.user, 200)
31 31
32 return res.body.access_token as string 32 return res.body.access_token as string
33} 33}
34 34
35async function getUserAccessToken (server: Server, user: User) { 35async function userLogin (server: Server, user: User) {
36 const res = await login(server.url, server.client, user, 200) 36 const res = await login(server.url, server.client, user, 200)
37 37
38 return res.body.access_token as string 38 return res.body.access_token as string
@@ -42,7 +42,7 @@ function setAccessTokensToServers (servers: ServerInfo[]) {
42 const tasks: Promise<any>[] = [] 42 const tasks: Promise<any>[] = []
43 43
44 for (const server of servers) { 44 for (const server of servers) {
45 const p = loginAndGetAccessToken(server).then(t => server.accessToken = t) 45 const p = serverLogin(server).then(t => server.accessToken = t)
46 tasks.push(p) 46 tasks.push(p)
47 } 47 }
48 48
@@ -53,7 +53,7 @@ function setAccessTokensToServers (servers: ServerInfo[]) {
53 53
54export { 54export {
55 login, 55 login,
56 loginAndGetAccessToken, 56 serverLogin,
57 getUserAccessToken, 57 userLogin,
58 setAccessTokensToServers 58 setAccessTokensToServers
59} 59}
diff --git a/server/tests/utils/videos/videos.ts b/server/tests/utils/videos/videos.ts
index 6de1b8c92..f64ebd2b0 100644
--- a/server/tests/utils/videos/videos.ts
+++ b/server/tests/utils/videos/videos.ts
@@ -21,25 +21,37 @@ type VideoAttributes = {
21function getVideoCategories (url: string) { 21function getVideoCategories (url: string) {
22 const path = '/api/v1/videos/categories' 22 const path = '/api/v1/videos/categories'
23 23
24 return makeGetRequest(url, path) 24 return makeGetRequest({
25 url,
26 path
27 })
25} 28}
26 29
27function getVideoLicences (url: string) { 30function getVideoLicences (url: string) {
28 const path = '/api/v1/videos/licences' 31 const path = '/api/v1/videos/licences'
29 32
30 return makeGetRequest(url, path) 33 return makeGetRequest({
34 url,
35 path
36 })
31} 37}
32 38
33function getVideoLanguages (url: string) { 39function getVideoLanguages (url: string) {
34 const path = '/api/v1/videos/languages' 40 const path = '/api/v1/videos/languages'
35 41
36 return makeGetRequest(url, path) 42 return makeGetRequest({
43 url,
44 path
45 })
37} 46}
38 47
39function getVideoPrivacies (url: string) { 48function getVideoPrivacies (url: string) {
40 const path = '/api/v1/videos/privacies' 49 const path = '/api/v1/videos/privacies'
41 50
42 return makeGetRequest(url, path) 51 return makeGetRequest({
52 url,
53 path
54 })
43} 55}
44 56
45function getVideo (url: string, id: number | string, expectedStatus = 200) { 57function getVideo (url: string, id: number | string, expectedStatus = 200) {