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