1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, createMultipleServers, doubleFollow, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
6 import { HttpStatusCode, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
8 const expect = chai.expect
10 describe('Test follow constraints', function () {
11 let servers: PeerTubeServer[] = []
12 let video1UUID: string
13 let video2UUID: string
16 before(async function () {
19 servers = await createMultipleServers(2)
21 // Get the access tokens
22 await setAccessTokensToServers(servers)
25 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
29 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
35 password: 'super_password'
37 await servers[0].users.create({ username: user.username, password: user.password })
38 userToken = await servers[0].login.getAccessToken(user)
40 await doubleFollow(servers[0], servers[1])
43 describe('With a followed instance', function () {
45 describe('With an unlogged user', function () {
47 it('Should get the local video', async function () {
48 await servers[0].videos.get({ id: video1UUID })
51 it('Should get the remote video', async function () {
52 await servers[0].videos.get({ id: video2UUID })
55 it('Should list local account videos', async function () {
56 const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[0].port })
58 expect(total).to.equal(1)
59 expect(data).to.have.lengthOf(1)
62 it('Should list remote account videos', async function () {
63 const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[1].port })
65 expect(total).to.equal(1)
66 expect(data).to.have.lengthOf(1)
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 })
73 expect(total).to.equal(1)
74 expect(data).to.have.lengthOf(1)
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 })
81 expect(total).to.equal(1)
82 expect(data).to.have.lengthOf(1)
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 })
91 it('Should get the remote video', async function () {
92 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
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 })
98 expect(total).to.equal(1)
99 expect(data).to.have.lengthOf(1)
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 })
105 expect(total).to.equal(1)
106 expect(data).to.have.lengthOf(1)
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 })
113 expect(total).to.equal(1)
114 expect(data).to.have.lengthOf(1)
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 })
121 expect(total).to.equal(1)
122 expect(data).to.have.lengthOf(1)
127 describe('With a non followed instance', function () {
129 before(async function () {
132 await servers[0].follows.unfollow({ target: servers[1] })
135 describe('With an unlogged user', function () {
137 it('Should get the local video', async function () {
138 await servers[0].videos.get({ id: video1UUID })
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
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)
149 expect(error.detail).to.equal('Cannot get this video regarding follow constraints')
150 expect(error.error).to.equal(error.detail)
152 expect(error.status).to.equal(HttpStatusCode.FORBIDDEN_403)
154 expect(error.originUrl).to.contains(servers[1].url)
157 it('Should list local account videos', async function () {
158 const { total, data } = await servers[0].videos.listByAccount({
160 handle: 'root@localhost:' + servers[0].port
163 expect(total).to.equal(1)
164 expect(data).to.have.lengthOf(1)
167 it('Should not list remote account videos', async function () {
168 const { total, data } = await servers[0].videos.listByAccount({
170 handle: 'root@localhost:' + servers[1].port
173 expect(total).to.equal(0)
174 expect(data).to.have.lengthOf(0)
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 })
181 expect(total).to.equal(1)
182 expect(data).to.have.lengthOf(1)
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 })
189 expect(total).to.equal(0)
190 expect(data).to.have.lengthOf(0)
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 })
199 it('Should get the remote video', async function () {
200 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
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 })
206 expect(total).to.equal(1)
207 expect(data).to.have.lengthOf(1)
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 })
213 expect(total).to.equal(1)
214 expect(data).to.have.lengthOf(1)
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 })
221 expect(total).to.equal(1)
222 expect(data).to.have.lengthOf(1)
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 })
229 expect(total).to.equal(1)
230 expect(data).to.have.lengthOf(1)
235 after(async function () {
236 await cleanupTests(servers)