PeerTube/server/tests/api/videos/video-privacy.ts

292 lines
9.4 KiB
TypeScript
Raw Normal View History

2020-01-31 16:56:52 +01:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2022-08-17 15:44:32 +02:00
import { expect } from 'chai'
import { wait } from '@shared/core-utils'
2021-07-16 14:27:30 +02:00
import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models'
import { cleanupTests, createSingleServer, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands'
2017-11-17 12:05:59 +01:00
describe('Test video privacy', function () {
2021-07-16 09:47:51 +02:00
const servers: PeerTubeServer[] = []
2019-12-12 15:47:47 +01:00
let anotherUserToken: string
2018-04-04 10:21:36 +02:00
let privateVideoId: number
let privateVideoUUID: string
2019-12-12 15:47:47 +01:00
let internalVideoId: number
let internalVideoUUID: string
let unlistedVideo: VideoCreateResult
let nonFederatedUnlistedVideoUUID: string
2019-12-12 15:47:47 +01:00
2018-04-04 10:21:36 +02:00
let now: number
const dontFederateUnlistedConfig = {
federation: {
videos: {
federate_unlisted: false
}
}
}
before(async function () {
this.timeout(50000)
// Run servers
2021-07-16 09:47:51 +02:00
servers.push(await createSingleServer(1, dontFederateUnlistedConfig))
servers.push(await createSingleServer(2))
// Get the access tokens
await setAccessTokensToServers(servers)
2017-11-17 12:05:59 +01:00
// Server 1 and server 2 follow each other
await doubleFollow(servers[0], servers[1])
})
describe('Private and internal videos', function () {
it('Should upload a private and internal videos on server 1', async function () {
2022-09-28 08:24:18 +02:00
this.timeout(50000)
for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) {
const attributes = { privacy }
2021-07-16 09:04:35 +02:00
await servers[0].videos.upload({ attributes })
}
await waitJobs(servers)
})
it('Should not have these private and internal videos on server 2', async function () {
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[1].videos.list()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(0)
expect(data).to.have.lengthOf(0)
})
2019-12-12 15:47:47 +01:00
it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () {
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[0].videos.list()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(0)
expect(data).to.have.lengthOf(0)
})
2019-12-12 15:47:47 +01:00
it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () {
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[0].videos.listWithToken()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(1)
expect(data).to.have.lengthOf(1)
2021-07-15 10:02:54 +02:00
expect(data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL)
})
2019-12-12 15:47:47 +01:00
it('Should list my (private and internal) videos', async function () {
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[0].videos.listMyVideos()
2019-12-12 15:47:47 +01:00
2021-07-15 10:02:54 +02:00
expect(total).to.equal(2)
expect(data).to.have.lengthOf(2)
2019-12-12 15:47:47 +01:00
2021-07-15 10:02:54 +02:00
const privateVideo = data.find(v => v.privacy.id === VideoPrivacy.PRIVATE)
privateVideoId = privateVideo.id
privateVideoUUID = privateVideo.uuid
2019-12-12 15:47:47 +01:00
2021-07-15 10:02:54 +02:00
const internalVideo = data.find(v => v.privacy.id === VideoPrivacy.INTERNAL)
internalVideoId = internalVideo.id
internalVideoUUID = internalVideo.uuid
})
it('Should not be able to watch the private/internal video with non authenticated user', async function () {
2021-07-16 09:04:35 +02:00
await servers[0].videos.get({ id: privateVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
await servers[0].videos.get({ id: internalVideoUUID, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
})
it('Should not be able to watch the private video with another user', async function () {
this.timeout(10000)
2018-05-28 17:28:53 +02:00
const user = {
username: 'hello',
password: 'super password'
}
2021-07-16 09:04:35 +02:00
await servers[0].users.create({ username: user.username, password: user.password })
2021-07-16 09:04:35 +02:00
anotherUserToken = await servers[0].login.getAccessToken(user)
2021-07-15 10:02:54 +02:00
2021-07-16 09:04:35 +02:00
await servers[0].videos.getWithToken({
2021-07-15 10:02:54 +02:00
token: anotherUserToken,
id: privateVideoUUID,
expectedStatus: HttpStatusCode.FORBIDDEN_403
})
})
it('Should be able to watch the internal video with another user', async function () {
2021-07-16 09:04:35 +02:00
await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID })
})
2019-12-12 15:47:47 +01:00
it('Should be able to watch the private video with the correct user', async function () {
2021-07-16 09:04:35 +02:00
await servers[0].videos.getWithToken({ id: privateVideoUUID })
})
})
describe('Unlisted videos', function () {
it('Should upload an unlisted video on server 2', async function () {
2022-10-24 14:54:21 +02:00
this.timeout(120000)
const attributes = {
name: 'unlisted video',
privacy: VideoPrivacy.UNLISTED
}
2021-07-16 09:04:35 +02:00
await servers[1].videos.upload({ attributes })
// Server 2 has transcoding enabled
await waitJobs(servers)
})
it('Should not have this unlisted video listed on server 1 and 2', async function () {
for (const server of servers) {
2021-07-16 09:04:35 +02:00
const { total, data } = await server.videos.list()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(0)
expect(data).to.have.lengthOf(0)
}
})
it('Should list my (unlisted) videos', async function () {
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[1].videos.listMyVideos()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(1)
expect(data).to.have.lengthOf(1)
2021-07-15 10:02:54 +02:00
unlistedVideo = data[0]
})
it('Should not be able to get this unlisted video using its id', async function () {
2022-06-22 14:03:50 +02:00
await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
})
it('Should be able to get this unlisted video using its uuid/shortUUID', async function () {
for (const server of servers) {
for (const id of [ unlistedVideo.uuid, unlistedVideo.shortUUID ]) {
2021-07-16 09:04:35 +02:00
const video = await server.videos.get({ id })
2021-07-15 10:02:54 +02:00
expect(video.name).to.equal('unlisted video')
}
}
})
it('Should upload a non-federating unlisted video to server 1', async function () {
this.timeout(30000)
const attributes = {
name: 'unlisted video',
privacy: VideoPrivacy.UNLISTED
}
2021-07-16 09:04:35 +02:00
await servers[0].videos.upload({ attributes })
await waitJobs(servers)
})
it('Should list my new unlisted video', async function () {
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[0].videos.listMyVideos()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(3)
expect(data).to.have.lengthOf(3)
2021-07-15 10:02:54 +02:00
nonFederatedUnlistedVideoUUID = data[0].uuid
})
it('Should be able to get non-federated unlisted video from origin', async function () {
2021-07-16 09:04:35 +02:00
const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID })
2021-07-15 10:02:54 +02:00
expect(video.name).to.equal('unlisted video')
})
it('Should not be able to get non-federated unlisted video from federated server', async function () {
2021-07-16 09:04:35 +02:00
await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
})
})
describe('Privacy update', function () {
it('Should update the private and internal videos to public on server 1', async function () {
2021-12-09 15:44:54 +01:00
this.timeout(100000)
2019-12-12 15:47:47 +01:00
now = Date.now()
2019-12-12 15:47:47 +01:00
{
2021-07-15 10:02:54 +02:00
const attributes = {
name: 'private video becomes public',
privacy: VideoPrivacy.PUBLIC
}
2021-07-16 09:04:35 +02:00
await servers[0].videos.update({ id: privateVideoId, attributes })
2019-12-12 15:47:47 +01:00
}
{
2021-07-15 10:02:54 +02:00
const attributes = {
name: 'internal video becomes public',
privacy: VideoPrivacy.PUBLIC
}
2021-07-16 09:04:35 +02:00
await servers[0].videos.update({ id: internalVideoId, attributes })
}
2021-12-09 15:44:54 +01:00
await wait(10000)
await waitJobs(servers)
})
2019-12-12 15:47:47 +01:00
it('Should have this new public video listed on server 1 and 2', async function () {
for (const server of servers) {
2021-07-16 09:04:35 +02:00
const { total, data } = await server.videos.list()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(2)
expect(data).to.have.lengthOf(2)
2019-12-12 15:47:47 +01:00
2021-07-15 10:02:54 +02:00
const privateVideo = data.find(v => v.name === 'private video becomes public')
const internalVideo = data.find(v => v.name === 'internal video becomes public')
2019-12-12 15:47:47 +01:00
expect(privateVideo).to.not.be.undefined
expect(internalVideo).to.not.be.undefined
expect(new Date(privateVideo.publishedAt).getTime()).to.be.at.least(now)
// We don't change the publish date of internal videos
expect(new Date(internalVideo.publishedAt).getTime()).to.be.below(now)
expect(privateVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PUBLIC)
}
})
it('Should set these videos as private and internal', async function () {
this.timeout(10000)
2021-07-16 09:04:35 +02:00
await servers[0].videos.update({ id: internalVideoId, attributes: { privacy: VideoPrivacy.PRIVATE } })
await servers[0].videos.update({ id: privateVideoId, attributes: { privacy: VideoPrivacy.INTERNAL } })
await waitJobs(servers)
for (const server of servers) {
2021-07-16 09:04:35 +02:00
const { total, data } = await server.videos.list()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(0)
expect(data).to.have.lengthOf(0)
}
{
2021-07-16 09:04:35 +02:00
const { total, data } = await servers[0].videos.listMyVideos()
2021-07-15 10:02:54 +02:00
expect(total).to.equal(3)
expect(data).to.have.lengthOf(3)
2019-12-12 15:47:47 +01:00
2021-07-15 10:02:54 +02:00
const privateVideo = data.find(v => v.name === 'private video becomes public')
const internalVideo = data.find(v => v.name === 'internal video becomes public')
2019-12-12 15:47:47 +01:00
expect(privateVideo).to.not.be.undefined
expect(internalVideo).to.not.be.undefined
expect(privateVideo.privacy.id).to.equal(VideoPrivacy.INTERNAL)
expect(internalVideo.privacy.id).to.equal(VideoPrivacy.PRIVATE)
}
})
})
2019-04-24 15:10:37 +02:00
after(async function () {
await cleanupTests(servers)
})
})