]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/follow-constraints.ts
Introduce user command
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / follow-constraints.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
6 import { PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
7 import {
8 cleanupTests,
9 doubleFollow,
10 flushAndRunMultipleServers,
11 getAccountVideos,
12 getVideo,
13 getVideoChannelVideos,
14 getVideoWithToken,
15 ServerInfo,
16 setAccessTokensToServers,
17 uploadVideo
18 } from '../../../../shared/extra-utils'
19
20 const expect = chai.expect
21
22 describe('Test follow constraints', function () {
23 let servers: ServerInfo[] = []
24 let video1UUID: string
25 let video2UUID: string
26 let userAccessToken: string
27
28 before(async function () {
29 this.timeout(90000)
30
31 servers = await flushAndRunMultipleServers(2)
32
33 // Get the access tokens
34 await setAccessTokensToServers(servers)
35
36 {
37 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video server 1' })
38 video1UUID = res.body.video.uuid
39 }
40 {
41 const res = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video server 2' })
42 video2UUID = res.body.video.uuid
43 }
44
45 const user = {
46 username: 'user1',
47 password: 'super_password'
48 }
49 await servers[0].usersCommand.create({ username: user.username, password: user.password })
50 userAccessToken = await servers[0].loginCommand.getAccessToken(user)
51
52 await doubleFollow(servers[0], servers[1])
53 })
54
55 describe('With a followed instance', function () {
56
57 describe('With an unlogged user', function () {
58
59 it('Should get the local video', async function () {
60 await getVideo(servers[0].url, video1UUID, HttpStatusCode.OK_200)
61 })
62
63 it('Should get the remote video', async function () {
64 await getVideo(servers[0].url, video2UUID, HttpStatusCode.OK_200)
65 })
66
67 it('Should list local account videos', async function () {
68 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[0].port, 0, 5)
69
70 expect(res.body.total).to.equal(1)
71 expect(res.body.data).to.have.lengthOf(1)
72 })
73
74 it('Should list remote account videos', async function () {
75 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[1].port, 0, 5)
76
77 expect(res.body.total).to.equal(1)
78 expect(res.body.data).to.have.lengthOf(1)
79 })
80
81 it('Should list local channel videos', async function () {
82 const videoChannelName = 'root_channel@localhost:' + servers[0].port
83 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5)
84
85 expect(res.body.total).to.equal(1)
86 expect(res.body.data).to.have.lengthOf(1)
87 })
88
89 it('Should list remote channel videos', async function () {
90 const videoChannelName = 'root_channel@localhost:' + servers[1].port
91 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5)
92
93 expect(res.body.total).to.equal(1)
94 expect(res.body.data).to.have.lengthOf(1)
95 })
96 })
97
98 describe('With a logged user', function () {
99 it('Should get the local video', async function () {
100 await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, HttpStatusCode.OK_200)
101 })
102
103 it('Should get the remote video', async function () {
104 await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, HttpStatusCode.OK_200)
105 })
106
107 it('Should list local account videos', async function () {
108 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[0].port, 0, 5)
109
110 expect(res.body.total).to.equal(1)
111 expect(res.body.data).to.have.lengthOf(1)
112 })
113
114 it('Should list remote account videos', async function () {
115 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[1].port, 0, 5)
116
117 expect(res.body.total).to.equal(1)
118 expect(res.body.data).to.have.lengthOf(1)
119 })
120
121 it('Should list local channel videos', async function () {
122 const videoChannelName = 'root_channel@localhost:' + servers[0].port
123 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5)
124
125 expect(res.body.total).to.equal(1)
126 expect(res.body.data).to.have.lengthOf(1)
127 })
128
129 it('Should list remote channel videos', async function () {
130 const videoChannelName = 'root_channel@localhost:' + servers[1].port
131 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5)
132
133 expect(res.body.total).to.equal(1)
134 expect(res.body.data).to.have.lengthOf(1)
135 })
136 })
137 })
138
139 describe('With a non followed instance', function () {
140
141 before(async function () {
142 this.timeout(30000)
143
144 await servers[0].followsCommand.unfollow({ target: servers[1] })
145 })
146
147 describe('With an unlogged user', function () {
148
149 it('Should get the local video', async function () {
150 await getVideo(servers[0].url, video1UUID, HttpStatusCode.OK_200)
151 })
152
153 it('Should not get the remote video', async function () {
154 const res = await getVideo(servers[0].url, video2UUID, HttpStatusCode.FORBIDDEN_403)
155
156 const error = res.body as PeerTubeProblemDocument
157
158 const doc = 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/does_not_respect_follow_constraints'
159 expect(error.type).to.equal(doc)
160 expect(error.code).to.equal(ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS)
161
162 expect(error.detail).to.equal('Cannot get this video regarding follow constraints')
163 expect(error.error).to.equal(error.detail)
164
165 expect(error.status).to.equal(HttpStatusCode.FORBIDDEN_403)
166
167 expect(error.originUrl).to.contains(servers[1].url)
168 })
169
170 it('Should list local account videos', async function () {
171 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[0].port, 0, 5)
172
173 expect(res.body.total).to.equal(1)
174 expect(res.body.data).to.have.lengthOf(1)
175 })
176
177 it('Should not list remote account videos', async function () {
178 const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:' + servers[1].port, 0, 5)
179
180 expect(res.body.total).to.equal(0)
181 expect(res.body.data).to.have.lengthOf(0)
182 })
183
184 it('Should list local channel videos', async function () {
185 const videoChannelName = 'root_channel@localhost:' + servers[0].port
186 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5)
187
188 expect(res.body.total).to.equal(1)
189 expect(res.body.data).to.have.lengthOf(1)
190 })
191
192 it('Should not list remote channel videos', async function () {
193 const videoChannelName = 'root_channel@localhost:' + servers[1].port
194 const res = await getVideoChannelVideos(servers[0].url, undefined, videoChannelName, 0, 5)
195
196 expect(res.body.total).to.equal(0)
197 expect(res.body.data).to.have.lengthOf(0)
198 })
199 })
200
201 describe('With a logged user', function () {
202 it('Should get the local video', async function () {
203 await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, HttpStatusCode.OK_200)
204 })
205
206 it('Should get the remote video', async function () {
207 await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, HttpStatusCode.OK_200)
208 })
209
210 it('Should list local account videos', async function () {
211 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[0].port, 0, 5)
212
213 expect(res.body.total).to.equal(1)
214 expect(res.body.data).to.have.lengthOf(1)
215 })
216
217 it('Should list remote account videos', async function () {
218 const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:' + servers[1].port, 0, 5)
219
220 expect(res.body.total).to.equal(1)
221 expect(res.body.data).to.have.lengthOf(1)
222 })
223
224 it('Should list local channel videos', async function () {
225 const videoChannelName = 'root_channel@localhost:' + servers[0].port
226 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5)
227
228 expect(res.body.total).to.equal(1)
229 expect(res.body.data).to.have.lengthOf(1)
230 })
231
232 it('Should list remote channel videos', async function () {
233 const videoChannelName = 'root_channel@localhost:' + servers[1].port
234 const res = await getVideoChannelVideos(servers[0].url, userAccessToken, videoChannelName, 0, 5)
235
236 expect(res.body.total).to.equal(1)
237 expect(res.body.data).to.have.lengthOf(1)
238 })
239 })
240 })
241
242 after(async function () {
243 await cleanupTests(servers)
244 })
245 })