Begin moving video channel to actor
[oweals/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,
8   flushTests,
9   killallServers,
10   loginAndGetAccessToken,
11   runServer,
12   ServerInfo,
13   setAccessTokensToServers
14 } from '../../utils'
15
16 describe('Test server follows API validators', function () {
17   let server: ServerInfo
18
19   // ---------------------------------------------------------------
20
21   before(async function () {
22     this.timeout(45000)
23
24     await flushTests()
25     server = await runServer(1)
26
27     await setAccessTokensToServers([ server ])
28   })
29
30   describe('When managing following', function () {
31     let userAccessToken = null
32
33     before(async function () {
34       await createUser(server.url, server.accessToken, 'user1', 'password')
35       server.user = {
36         username: 'user1',
37         password: 'password'
38       }
39
40       userAccessToken = await loginAndGetAccessToken(server)
41     })
42
43     describe('When adding follows', function () {
44       const path = '/api/v1/server/following'
45       const body = {
46         hosts: [ 'localhost:9002' ]
47       }
48
49       it('Should fail without hosts', async function () {
50         await request(server.url)
51                 .post(path)
52                 .set('Authorization', 'Bearer ' + server.accessToken)
53                 .set('Accept', 'application/json')
54                 .expect(400)
55       })
56
57       it('Should fail if hosts is not an array', async function () {
58         await request(server.url)
59                 .post(path)
60                 .send({ hosts: 'localhost:9002' })
61                 .set('Authorization', 'Bearer ' + server.accessToken)
62                 .set('Accept', 'application/json')
63                 .expect(400)
64       })
65
66       it('Should fail if the array is not composed by hosts', async function () {
67         await request(server.url)
68                 .post(path)
69                 .send({ hosts: [ 'localhost:9002', 'localhost:coucou' ] })
70                 .set('Authorization', 'Bearer ' + server.accessToken)
71                 .set('Accept', 'application/json')
72                 .expect(400)
73       })
74
75       it('Should fail if the array is composed with http schemes', async function () {
76         await request(server.url)
77                 .post(path)
78                 .send({ hosts: [ 'localhost:9002', 'http://localhost:9003' ] })
79                 .set('Authorization', 'Bearer ' + server.accessToken)
80                 .set('Accept', 'application/json')
81                 .expect(400)
82       })
83
84       it('Should fail if hosts are not unique', async function () {
85         await request(server.url)
86                 .post(path)
87                 .send({ urls: [ 'localhost:9002', 'localhost:9002' ] })
88                 .set('Authorization', 'Bearer ' + server.accessToken)
89                 .set('Accept', 'application/json')
90                 .expect(400)
91       })
92
93       it('Should fail with an invalid token', async function () {
94         await request(server.url)
95                 .post(path)
96                 .send(body)
97                 .set('Authorization', 'Bearer fake_token')
98                 .set('Accept', 'application/json')
99                 .expect(401)
100       })
101
102       it('Should fail if the user is not an administrator', async function () {
103         await request(server.url)
104                 .post(path)
105                 .send(body)
106                 .set('Authorization', 'Bearer ' + userAccessToken)
107                 .set('Accept', 'application/json')
108                 .expect(403)
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 request(server.url)
117           .get(path)
118           .query({ start: 'hello' })
119           .set('Accept', 'application/json')
120           .expect(400)
121       })
122
123       it('Should fail with a bad count pagination', async function () {
124         await request(server.url)
125           .get(path)
126           .query({ count: 'hello' })
127           .set('Accept', 'application/json')
128           .expect(400)
129       })
130
131       it('Should fail with an incorrect sort', async function () {
132         await request(server.url)
133           .get(path)
134           .query({ sort: 'hello' })
135           .set('Accept', 'application/json')
136           .expect(400)
137       })
138     })
139
140     describe('When listing followers', function () {
141       const path = '/api/v1/server/followers'
142
143       it('Should fail with a bad start pagination', async function () {
144         await request(server.url)
145           .get(path)
146           .query({ start: 'hello' })
147           .set('Accept', 'application/json')
148           .expect(400)
149       })
150
151       it('Should fail with a bad count pagination', async function () {
152         await request(server.url)
153           .get(path)
154           .query({ count: 'hello' })
155           .set('Accept', 'application/json')
156           .expect(400)
157       })
158
159       it('Should fail with an incorrect sort', async function () {
160         await request(server.url)
161           .get(path)
162           .query({ sort: 'hello' })
163           .set('Accept', 'application/json')
164           .expect(400)
165       })
166     })
167
168     describe('When removing following', function () {
169       const path = '/api/v1/server/following'
170
171       it('Should fail with an invalid token', async function () {
172         await request(server.url)
173                 .delete(path + '/1')
174                 .set('Authorization', 'Bearer faketoken')
175                 .set('Accept', 'application/json')
176                 .expect(401)
177       })
178
179       it('Should fail if the user is not an administrator', async function () {
180         await request(server.url)
181                 .delete(path + '/1')
182                 .set('Authorization', 'Bearer ' + userAccessToken)
183                 .set('Accept', 'application/json')
184                 .expect(403)
185       })
186
187       it('Should fail we do not follow this server', async function () {
188               await request(server.url)
189                 .delete(path + '/example.com')
190                 .set('Authorization', 'Bearer ' + server.accessToken)
191                 .set('Accept', 'application/json')
192                 .expect(404)
193       })
194
195       it('Should succeed with the correct parameters')
196     })
197   })
198
199   after(async function () {
200     killallServers([ server ])
201
202     // Keep the logs if the test failed
203     if (this['ok']) {
204       await flushTests()
205     }
206   })
207 })