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