]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/follows.ts
Improve check jobs parameters tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / follows.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as request from 'supertest'
5
6 import {
7 createUser, flushTests, killallServers, makeDeleteRequest, makePostBodyRequest, runServer, ServerInfo, setAccessTokensToServers,
8 userLogin
9 } from '../../utils'
10 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
11
12 describe('Test server follows API validators', function () {
13 let server: ServerInfo
14
15 // ---------------------------------------------------------------
16
17 before(async function () {
18 this.timeout(20000)
19
20 await flushTests()
21 server = await runServer(1)
22
23 await setAccessTokensToServers([ server ])
24 })
25
26 describe('When managing following', function () {
27 let userAccessToken = null
28
29 before(async function () {
30 const user = {
31 username: 'user1',
32 password: 'password'
33 }
34
35 await createUser(server.url, server.accessToken, user.username, user.password)
36 userAccessToken = await userLogin(server, user)
37 })
38
39 describe('When adding follows', function () {
40 const path = '/api/v1/server/following'
41
42 it('Should fail without hosts', async function () {
43 await makePostBodyRequest({
44 url: server.url,
45 path,
46 token: server.accessToken,
47 statusCodeExpected: 400
48 })
49 })
50
51 it('Should fail if hosts is not an array', async function () {
52 await makePostBodyRequest({
53 url: server.url,
54 path,
55 token: server.accessToken,
56 fields: { hosts: 'localhost:9002' },
57 statusCodeExpected: 400
58 })
59 })
60
61 it('Should fail if the array is not composed by hosts', async function () {
62 await makePostBodyRequest({
63 url: server.url,
64 path,
65 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
66 token: server.accessToken,
67 statusCodeExpected: 400
68 })
69 })
70
71 it('Should fail if the array is composed with http schemes', async function () {
72 await makePostBodyRequest({
73 url: server.url,
74 path,
75 fields: { hosts: [ 'localhost:9002', 'http://localhost:9003' ] },
76 token: server.accessToken,
77 statusCodeExpected: 400
78 })
79 })
80
81 it('Should fail if hosts are not unique', async function () {
82 await makePostBodyRequest({
83 url: server.url,
84 path,
85 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
86 token: server.accessToken,
87 statusCodeExpected: 400
88 })
89 })
90
91 it('Should fail with an invalid token', async function () {
92 await makePostBodyRequest({
93 url: server.url,
94 path,
95 fields: { hosts: [ 'localhost:9002' ] },
96 token: 'fake_token',
97 statusCodeExpected: 401
98 })
99 })
100
101 it('Should fail if the user is not an administrator', async function () {
102 await makePostBodyRequest({
103 url: server.url,
104 path,
105 fields: { hosts: [ 'localhost:9002' ] },
106 token: userAccessToken,
107 statusCodeExpected: 403
108 })
109 })
110 })
111
112 describe('When listing followings', function () {
113 const path = '/api/v1/server/following'
114
115 it('Should fail with a bad start pagination', async function () {
116 await checkBadStartPagination(server.url, path)
117 })
118
119 it('Should fail with a bad count pagination', async function () {
120 await checkBadCountPagination(server.url, path)
121 })
122
123 it('Should fail with an incorrect sort', async function () {
124 await checkBadSortPagination(server.url, path)
125 })
126 })
127
128 describe('When listing followers', function () {
129 const path = '/api/v1/server/followers'
130
131 it('Should fail with a bad start pagination', async function () {
132 await checkBadStartPagination(server.url, path)
133 })
134
135 it('Should fail with a bad count pagination', async function () {
136 await checkBadCountPagination(server.url, path)
137 })
138
139 it('Should fail with an incorrect sort', async function () {
140 await checkBadSortPagination(server.url, path)
141 })
142 })
143
144 describe('When removing following', function () {
145 const path = '/api/v1/server/following'
146
147 it('Should fail with an invalid token', async function () {
148 await makeDeleteRequest({
149 url: server.url,
150 path: path + '/localhost:9002',
151 token: 'fake_token',
152 statusCodeExpected: 401
153 })
154 })
155
156 it('Should fail if the user is not an administrator', async function () {
157 await makeDeleteRequest({
158 url: server.url,
159 path: path + '/localhost:9002',
160 token: userAccessToken,
161 statusCodeExpected: 403
162 })
163 })
164
165 it('Should fail if we do not follow this server', async function () {
166 await makeDeleteRequest({
167 url: server.url,
168 path: path + '/example.com',
169 token: server.accessToken,
170 statusCodeExpected: 404
171 })
172 })
173
174 it('Should succeed with the correct parameters', async function () {
175 await makeDeleteRequest({
176 url: server.url,
177 path: path + '/localhost:9002',
178 token: server.accessToken,
179 statusCodeExpected: 404
180 })
181 })
182 })
183 })
184
185 after(async function () {
186 killallServers([ server ])
187
188 // Keep the logs if the test failed
189 if (this['ok']) {
190 await flushTests()
191 }
192 })
193 })