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