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