X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fvideos%2Fvideo-privacy.ts;h=de96bcfcc6609ddd7e5cfbb35394cb2782975a5e;hb=33b91e53d21fba295ecf516b717fb36e91990771;hp=0b4e66369f66b18fd10b81243c93eb9865109952;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/videos/video-privacy.ts b/server/tests/api/videos/video-privacy.ts index 0b4e66369..de96bcfcc 100644 --- a/server/tests/api/videos/video-privacy.ts +++ b/server/tests/api/videos/video-privacy.ts @@ -1,36 +1,39 @@ -/* tslint:disable:no-unused-expression */ - -import * as chai from 'chai' -import 'mocha' -import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum' -import { - flushAndRunMultipleServers, - getVideosList, - killallServers, - ServerInfo, - setAccessTokensToServers, - uploadVideo -} from '../../../../shared/utils/index' -import { doubleFollow } from '../../../../shared/utils/server/follows' -import { userLogin } from '../../../../shared/utils/users/login' -import { createUser } from '../../../../shared/utils/users/users' -import { getMyVideos, getVideo, getVideoWithToken, updateVideo } from '../../../../shared/utils/videos/videos' -import { waitJobs } from '../../../../shared/utils/server/jobs' - -const expect = chai.expect +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import { expect } from 'chai' +import { wait } from '@shared/core-utils' +import { HttpStatusCode, VideoCreateResult, VideoPrivacy } from '@shared/models' +import { cleanupTests, createSingleServer, doubleFollow, PeerTubeServer, setAccessTokensToServers, waitJobs } from '@shared/server-commands' describe('Test video privacy', function () { - let servers: ServerInfo[] = [] + const servers: PeerTubeServer[] = [] + let anotherUserToken: string + let privateVideoId: number let privateVideoUUID: string - let unlistedVideoUUID: string + + let internalVideoId: number + let internalVideoUUID: string + + let unlistedVideo: VideoCreateResult + let nonFederatedUnlistedVideoUUID: string + let now: number + const dontFederateUnlistedConfig = { + federation: { + videos: { + federate_unlisted: false + } + } + } + before(async function () { this.timeout(50000) // Run servers - servers = await flushAndRunMultipleServers(2) + servers.push(await createSingleServer(1, dontFederateUnlistedConfig)) + servers.push(await createSingleServer(2)) // Get the access tokens await setAccessTokensToServers(servers) @@ -39,120 +42,246 @@ describe('Test video privacy', function () { await doubleFollow(servers[0], servers[1]) }) - it('Should upload a private video on server 1', async function () { - this.timeout(10000) + describe('Private and internal videos', function () { - const attributes = { - privacy: VideoPrivacy.PRIVATE - } - await uploadVideo(servers[0].url, servers[0].accessToken, attributes) + it('Should upload a private and internal videos on server 1', async function () { + this.timeout(50000) - await waitJobs(servers) - }) + for (const privacy of [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]) { + const attributes = { privacy } + await servers[0].videos.upload({ attributes }) + } - it('Should not have this private video on server 2', async function () { - const res = await getVideosList(servers[1].url) + await waitJobs(servers) + }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) - }) + it('Should not have these private and internal videos on server 2', async function () { + const { total, data } = await servers[1].videos.list() - it('Should list my (private) videos', async function () { - const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 1) + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) + }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + it('Should not list the private and internal videos for an unauthenticated user on server 1', async function () { + const { total, data } = await servers[0].videos.list() - privateVideoId = res.body.data[0].id - privateVideoUUID = res.body.data[0].uuid - }) + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) + }) - it('Should not be able to watch this video with non authenticated user', async function () { - await getVideo(servers[0].url, privateVideoUUID, 401) - }) + it('Should not list the private video and list the internal video for an authenticated user on server 1', async function () { + const { total, data } = await servers[0].videos.listWithToken() - it('Should not be able to watch this private video with another user', async function () { - this.timeout(10000) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) - const user = { - username: 'hello', - password: 'super password' - } - await createUser(servers[0].url, servers[0].accessToken, user.username, user.password) + expect(data[0].privacy.id).to.equal(VideoPrivacy.INTERNAL) + }) - const token = await userLogin(servers[0], user) - await getVideoWithToken(servers[0].url, token, privateVideoUUID, 403) - }) + it('Should list my (private and internal) videos', async function () { + const { total, data } = await servers[0].videos.listMyVideos() - it('Should be able to watch this video with the correct user', async function () { - await getVideoWithToken(servers[0].url, servers[0].accessToken, privateVideoUUID) - }) + expect(total).to.equal(2) + expect(data).to.have.lengthOf(2) - it('Should upload an unlisted video on server 2', async function () { - this.timeout(30000) + const privateVideo = data.find(v => v.privacy.id === VideoPrivacy.PRIVATE) + privateVideoId = privateVideo.id + privateVideoUUID = privateVideo.uuid - const attributes = { - name: 'unlisted video', - privacy: VideoPrivacy.UNLISTED - } - await uploadVideo(servers[1].url, servers[1].accessToken, attributes) + const internalVideo = data.find(v => v.privacy.id === VideoPrivacy.INTERNAL) + internalVideoId = internalVideo.id + internalVideoUUID = internalVideo.uuid + }) - // Server 2 has transcoding enabled - await waitJobs(servers) - }) + it('Should not be able to watch the private/internal video with non authenticated user', async function () { + 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 have this unlisted video listed on server 1 and 2', async function () { - for (const server of servers) { - const res = await getVideosList(server.url) + it('Should not be able to watch the private video with another user', async function () { + const user = { + username: 'hello', + password: 'super password' + } + await servers[0].users.create({ username: user.username, password: user.password }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) - } - }) + anotherUserToken = await servers[0].login.getAccessToken(user) - it('Should list my (unlisted) videos', async function () { - const res = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 1) + await servers[0].videos.getWithToken({ + token: anotherUserToken, + id: privateVideoUUID, + expectedStatus: HttpStatusCode.FORBIDDEN_403 + }) + }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + it('Should be able to watch the internal video with another user', async function () { + await servers[0].videos.getWithToken({ token: anotherUserToken, id: internalVideoUUID }) + }) - unlistedVideoUUID = res.body.data[0].uuid + it('Should be able to watch the private video with the correct user', async function () { + await servers[0].videos.getWithToken({ id: privateVideoUUID }) + }) }) - it('Should be able to get this unlisted video', async function () { - for (const server of servers) { - const res = await getVideo(server.url, unlistedVideoUUID) + describe('Unlisted videos', function () { - expect(res.body.name).to.equal('unlisted video') - } - }) + it('Should upload an unlisted video on server 2', async function () { + this.timeout(120000) - it('Should update the private video to public on server 1', async function () { - this.timeout(10000) + const attributes = { + name: 'unlisted video', + privacy: VideoPrivacy.UNLISTED + } + await servers[1].videos.upload({ attributes }) - const attribute = { - name: 'super video public', - privacy: VideoPrivacy.PUBLIC - } + // 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) { + const { total, data } = await server.videos.list() + + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) + } + }) + + it('Should list my (unlisted) videos', async function () { + const { total, data } = await servers[1].videos.listMyVideos() + + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) + + unlistedVideo = data[0] + }) + + it('Should not be able to get this unlisted video using its id', async function () { + await servers[1].videos.get({ id: unlistedVideo.id, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 }) + }) - now = Date.now() - await updateVideo(servers[0].url, servers[0].accessToken, privateVideoId, attribute) + 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 ]) { + const video = await server.videos.get({ id }) - await waitJobs(servers) + 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 + } + await servers[0].videos.upload({ attributes }) + + await waitJobs(servers) + }) + + it('Should list my new unlisted video', async function () { + const { total, data } = await servers[0].videos.listMyVideos() + + expect(total).to.equal(3) + expect(data).to.have.lengthOf(3) + + nonFederatedUnlistedVideoUUID = data[0].uuid + }) + + it('Should be able to get non-federated unlisted video from origin', async function () { + const video = await servers[0].videos.get({ id: nonFederatedUnlistedVideoUUID }) + + expect(video.name).to.equal('unlisted video') + }) + + it('Should not be able to get non-federated unlisted video from federated server', async function () { + await servers[1].videos.get({ id: nonFederatedUnlistedVideoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) + }) }) - it('Should have this new public video listed on server 1 and 2', async function () { - for (const server of servers) { - const res = await getVideosList(server.url) + describe('Privacy update', function () { - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) - expect(res.body.data[0].name).to.equal('super video public') - expect(new Date(res.body.data[0].publishedAt).getTime()).to.be.at.least(now) - } + it('Should update the private and internal videos to public on server 1', async function () { + this.timeout(100000) + + now = Date.now() + + { + const attributes = { + name: 'private video becomes public', + privacy: VideoPrivacy.PUBLIC + } + + await servers[0].videos.update({ id: privateVideoId, attributes }) + } + + { + const attributes = { + name: 'internal video becomes public', + privacy: VideoPrivacy.PUBLIC + } + await servers[0].videos.update({ id: internalVideoId, attributes }) + } + + await wait(10000) + await waitJobs(servers) + }) + + it('Should have this new public video listed on server 1 and 2', async function () { + for (const server of servers) { + const { total, data } = await server.videos.list() + expect(total).to.equal(2) + expect(data).to.have.lengthOf(2) + + const privateVideo = data.find(v => v.name === 'private video becomes public') + const internalVideo = data.find(v => v.name === 'internal video becomes public') + + 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 () { + 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) { + const { total, data } = await server.videos.list() + + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) + } + + { + const { total, data } = await servers[0].videos.listMyVideos() + expect(total).to.equal(3) + expect(data).to.have.lengthOf(3) + + const privateVideo = data.find(v => v.name === 'private video becomes public') + const internalVideo = data.find(v => v.name === 'internal video becomes public') + + 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) + } + }) }) after(async function () { - killallServers(servers) + await cleanupTests(servers) }) })