]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/follows.ts
Improve check jobs parameters tests
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / follows.ts
CommitLineData
9a27cdc2
C
1/* tslint:disable:no-unused-expression */
2
9a27cdc2 3import 'mocha'
79d5caf9 4import * as request from 'supertest'
9a27cdc2
C
5
6import {
eec63bbc
C
7 createUser, flushTests, killallServers, makeDeleteRequest, makePostBodyRequest, runServer, ServerInfo, setAccessTokensToServers,
8 userLogin
9a27cdc2 9} from '../../utils'
eec63bbc 10import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params'
9a27cdc2
C
11
12describe('Test server follows API validators', function () {
13 let server: ServerInfo
14
15 // ---------------------------------------------------------------
16
17 before(async function () {
eec63bbc 18 this.timeout(20000)
9a27cdc2
C
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 () {
eec63bbc 30 const user = {
9a27cdc2
C
31 username: 'user1',
32 password: 'password'
33 }
34
eec63bbc
C
35 await createUser(server.url, server.accessToken, user.username, user.password)
36 userAccessToken = await userLogin(server, user)
9a27cdc2
C
37 })
38
39 describe('When adding follows', function () {
40 const path = '/api/v1/server/following'
9a27cdc2
C
41
42 it('Should fail without hosts', async function () {
eec63bbc
C
43 await makePostBodyRequest({
44 url: server.url,
45 path,
46 token: server.accessToken,
47 statusCodeExpected: 400
48 })
9a27cdc2
C
49 })
50
51 it('Should fail if hosts is not an array', async function () {
eec63bbc
C
52 await makePostBodyRequest({
53 url: server.url,
54 path,
55 token: server.accessToken,
56 fields: { hosts: 'localhost:9002' },
57 statusCodeExpected: 400
58 })
9a27cdc2
C
59 })
60
61 it('Should fail if the array is not composed by hosts', async function () {
eec63bbc
C
62 await makePostBodyRequest({
63 url: server.url,
64 path,
65 fields: { hosts: [ 'localhost:9002', 'localhost:coucou' ] },
66 token: server.accessToken,
67 statusCodeExpected: 400
68 })
9a27cdc2
C
69 })
70
71 it('Should fail if the array is composed with http schemes', async function () {
eec63bbc
C
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 })
9a27cdc2
C
79 })
80
81 it('Should fail if hosts are not unique', async function () {
eec63bbc
C
82 await makePostBodyRequest({
83 url: server.url,
84 path,
85 fields: { urls: [ 'localhost:9002', 'localhost:9002' ] },
86 token: server.accessToken,
87 statusCodeExpected: 400
88 })
9a27cdc2
C
89 })
90
91 it('Should fail with an invalid token', async function () {
eec63bbc
C
92 await makePostBodyRequest({
93 url: server.url,
94 path,
95 fields: { hosts: [ 'localhost:9002' ] },
96 token: 'fake_token',
97 statusCodeExpected: 401
98 })
9a27cdc2
C
99 })
100
101 it('Should fail if the user is not an administrator', async function () {
eec63bbc
C
102 await makePostBodyRequest({
103 url: server.url,
104 path,
105 fields: { hosts: [ 'localhost:9002' ] },
106 token: userAccessToken,
107 statusCodeExpected: 403
108 })
9a27cdc2
C
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 () {
eec63bbc 116 await checkBadStartPagination(server.url, path)
9a27cdc2
C
117 })
118
119 it('Should fail with a bad count pagination', async function () {
eec63bbc 120 await checkBadCountPagination(server.url, path)
9a27cdc2
C
121 })
122
123 it('Should fail with an incorrect sort', async function () {
eec63bbc 124 await checkBadSortPagination(server.url, path)
9a27cdc2
C
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 () {
eec63bbc 132 await checkBadStartPagination(server.url, path)
9a27cdc2
C
133 })
134
135 it('Should fail with a bad count pagination', async function () {
eec63bbc 136 await checkBadCountPagination(server.url, path)
9a27cdc2
C
137 })
138
139 it('Should fail with an incorrect sort', async function () {
eec63bbc 140 await checkBadSortPagination(server.url, path)
9a27cdc2
C
141 })
142 })
143
144 describe('When removing following', function () {
0f91ae62
C
145 const path = '/api/v1/server/following'
146
147 it('Should fail with an invalid token', async function () {
eec63bbc
C
148 await makeDeleteRequest({
149 url: server.url,
150 path: path + '/localhost:9002',
151 token: 'fake_token',
152 statusCodeExpected: 401
153 })
0f91ae62
C
154 })
155
156 it('Should fail if the user is not an administrator', async function () {
eec63bbc
C
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 })
0f91ae62 181 })
9a27cdc2
C
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})