1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
7 const expect = chai.expect
9 describe('Test follow constraints', function () {
10 let servers: PeerTubeServer[] = []
11 let video1UUID: string
12 let video2UUID: string
15 before(async function () {
18 servers = await createMultipleServers(2)
20 // Get the access tokens
21 await setAccessTokensToServers(servers)
24 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
28 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
34 password: 'super_password'
36 await servers[0].users.create({ username: user.username, password: user.password })
37 userToken = await servers[0].login.getAccessToken(user)
39 await doubleFollow(servers[0], servers[1])
42 describe('With a followed instance', function () {
44 describe('With an unlogged user', function () {
46 it('Should get the local video', async function () {
47 await servers[0].videos.get({ id: video1UUID })
50 it('Should get the remote video', async function () {
51 await servers[0].videos.get({ id: video2UUID })
54 it('Should list local account videos', async function () {
55 const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[0].port })
57 expect(total).to.equal(1)
58 expect(data).to.have.lengthOf(1)
61 it('Should list remote account videos', async function () {
62 const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[1].port })
64 expect(total).to.equal(1)
65 expect(data).to.have.lengthOf(1)
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 })
72 expect(total).to.equal(1)
73 expect(data).to.have.lengthOf(1)
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 })
80 expect(total).to.equal(1)
81 expect(data).to.have.lengthOf(1)
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 })
90 it('Should get the remote video', async function () {
91 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
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 })
97 expect(total).to.equal(1)
98 expect(data).to.have.lengthOf(1)
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 })
104 expect(total).to.equal(1)
105 expect(data).to.have.lengthOf(1)
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 })
112 expect(total).to.equal(1)
113 expect(data).to.have.lengthOf(1)
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 })
120 expect(total).to.equal(1)
121 expect(data).to.have.lengthOf(1)
126 describe('With a non followed instance', function () {
128 before(async function () {
131 await servers[0].follows.unfollow({ target: servers[1] })
134 describe('With an unlogged user', function () {
136 it('Should get the local video', async function () {
137 await servers[0].videos.get({ id: video1UUID })
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
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)
148 expect(error.detail).to.equal('Cannot get this video regarding follow constraints')
149 expect(error.error).to.equal(error.detail)
151 expect(error.status).to.equal(HttpStatusCode.FORBIDDEN_403)
153 expect(error.originUrl).to.contains(servers[1].url)
156 it('Should list local account videos', async function () {
157 const { total, data } = await servers[0].videos.listByAccount({
159 handle: 'root@localhost:' + servers[0].port
162 expect(total).to.equal(1)
163 expect(data).to.have.lengthOf(1)
166 it('Should not list remote account videos', async function () {
167 const { total, data } = await servers[0].videos.listByAccount({
169 handle: 'root@localhost:' + servers[1].port
172 expect(total).to.equal(0)
173 expect(data).to.have.lengthOf(0)
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 })
180 expect(total).to.equal(1)
181 expect(data).to.have.lengthOf(1)
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 })
188 expect(total).to.equal(0)
189 expect(data).to.have.lengthOf(0)
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 })
198 it('Should get the remote video', async function () {
199 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
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 })
205 expect(total).to.equal(1)
206 expect(data).to.have.lengthOf(1)
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 })
212 expect(total).to.equal(1)
213 expect(data).to.have.lengthOf(1)
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 })
220 expect(total).to.equal(1)
221 expect(data).to.have.lengthOf(1)
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 })
228 expect(total).to.equal(1)
229 expect(data).to.have.lengthOf(1)
234 after(async function () {
235 await cleanupTests(servers)