1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
6 import { cleanupTests, doubleFollow, flushAndRunMultipleServers, ServerInfo, setAccessTokensToServers } from '@shared/extra-utils'
7 import { PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
9 const expect = chai.expect
11 describe('Test follow constraints', function () {
12 let servers: ServerInfo[] = []
13 let video1UUID: string
14 let video2UUID: string
17 before(async function () {
20 servers = await flushAndRunMultipleServers(2)
22 // Get the access tokens
23 await setAccessTokensToServers(servers)
26 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } })
30 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } })
36 password: 'super_password'
38 await servers[0].users.create({ username: user.username, password: user.password })
39 userToken = await servers[0].login.getAccessToken(user)
41 await doubleFollow(servers[0], servers[1])
44 describe('With a followed instance', function () {
46 describe('With an unlogged user', function () {
48 it('Should get the local video', async function () {
49 await servers[0].videos.get({ id: video1UUID })
52 it('Should get the remote video', async function () {
53 await servers[0].videos.get({ id: video2UUID })
56 it('Should list local account videos', async function () {
57 const { total, data } = await servers[0].videos.listByAccount({ accountName: 'root@localhost:' + servers[0].port })
59 expect(total).to.equal(1)
60 expect(data).to.have.lengthOf(1)
63 it('Should list remote account videos', async function () {
64 const { total, data } = await servers[0].videos.listByAccount({ accountName: 'root@localhost:' + servers[1].port })
66 expect(total).to.equal(1)
67 expect(data).to.have.lengthOf(1)
70 it('Should list local channel videos', async function () {
71 const videoChannelName = 'root_channel@localhost:' + servers[0].port
72 const { total, data } = await servers[0].videos.listByChannel({ videoChannelName })
74 expect(total).to.equal(1)
75 expect(data).to.have.lengthOf(1)
78 it('Should list remote channel videos', async function () {
79 const videoChannelName = 'root_channel@localhost:' + servers[1].port
80 const { total, data } = await servers[0].videos.listByChannel({ videoChannelName })
82 expect(total).to.equal(1)
83 expect(data).to.have.lengthOf(1)
87 describe('With a logged user', function () {
88 it('Should get the local video', async function () {
89 await servers[0].videos.getWithToken({ token: userToken, id: video1UUID })
92 it('Should get the remote video', async function () {
93 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
96 it('Should list local account videos', async function () {
97 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, accountName: 'root@localhost:' + servers[0].port })
99 expect(total).to.equal(1)
100 expect(data).to.have.lengthOf(1)
103 it('Should list remote account videos', async function () {
104 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, accountName: 'root@localhost:' + servers[1].port })
106 expect(total).to.equal(1)
107 expect(data).to.have.lengthOf(1)
110 it('Should list local channel videos', async function () {
111 const videoChannelName = 'root_channel@localhost:' + servers[0].port
112 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, videoChannelName })
114 expect(total).to.equal(1)
115 expect(data).to.have.lengthOf(1)
118 it('Should list remote channel videos', async function () {
119 const videoChannelName = 'root_channel@localhost:' + servers[1].port
120 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, videoChannelName })
122 expect(total).to.equal(1)
123 expect(data).to.have.lengthOf(1)
128 describe('With a non followed instance', function () {
130 before(async function () {
133 await servers[0].follows.unfollow({ target: servers[1] })
136 describe('With an unlogged user', function () {
138 it('Should get the local video', async function () {
139 await servers[0].videos.get({ id: video1UUID })
142 it('Should not get the remote video', async function () {
143 const body = await servers[0].videos.get({ id: video2UUID, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
144 const error = body as unknown as PeerTubeProblemDocument
146 const doc = 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/does_not_respect_follow_constraints'
147 expect(error.type).to.equal(doc)
148 expect(error.code).to.equal(ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS)
150 expect(error.detail).to.equal('Cannot get this video regarding follow constraints')
151 expect(error.error).to.equal(error.detail)
153 expect(error.status).to.equal(HttpStatusCode.FORBIDDEN_403)
155 expect(error.originUrl).to.contains(servers[1].url)
158 it('Should list local account videos', async function () {
159 const { total, data } = await servers[0].videos.listByAccount({
161 accountName: 'root@localhost:' + servers[0].port
164 expect(total).to.equal(1)
165 expect(data).to.have.lengthOf(1)
168 it('Should not list remote account videos', async function () {
169 const { total, data } = await servers[0].videos.listByAccount({
171 accountName: 'root@localhost:' + servers[1].port
174 expect(total).to.equal(0)
175 expect(data).to.have.lengthOf(0)
178 it('Should list local channel videos', async function () {
179 const videoChannelName = 'root_channel@localhost:' + servers[0].port
180 const { total, data } = await servers[0].videos.listByChannel({ token: undefined, videoChannelName })
182 expect(total).to.equal(1)
183 expect(data).to.have.lengthOf(1)
186 it('Should not list remote channel videos', async function () {
187 const videoChannelName = 'root_channel@localhost:' + servers[1].port
188 const { total, data } = await servers[0].videos.listByChannel({ token: undefined, videoChannelName })
190 expect(total).to.equal(0)
191 expect(data).to.have.lengthOf(0)
195 describe('With a logged user', function () {
196 it('Should get the local video', async function () {
197 await servers[0].videos.getWithToken({ token: userToken, id: video1UUID })
200 it('Should get the remote video', async function () {
201 await servers[0].videos.getWithToken({ token: userToken, id: video2UUID })
204 it('Should list local account videos', async function () {
205 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, accountName: 'root@localhost:' + servers[0].port })
207 expect(total).to.equal(1)
208 expect(data).to.have.lengthOf(1)
211 it('Should list remote account videos', async function () {
212 const { total, data } = await servers[0].videos.listByAccount({ token: userToken, accountName: 'root@localhost:' + servers[1].port })
214 expect(total).to.equal(1)
215 expect(data).to.have.lengthOf(1)
218 it('Should list local channel videos', async function () {
219 const videoChannelName = 'root_channel@localhost:' + servers[0].port
220 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, videoChannelName })
222 expect(total).to.equal(1)
223 expect(data).to.have.lengthOf(1)
226 it('Should list remote channel videos', async function () {
227 const videoChannelName = 'root_channel@localhost:' + servers[1].port
228 const { total, data } = await servers[0].videos.listByChannel({ token: userToken, videoChannelName })
230 expect(total).to.equal(1)
231 expect(data).to.have.lengthOf(1)
236 after(async function () {
237 await cleanupTests(servers)