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