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