X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fserver%2Ffollow-constraints.ts;h=e1ec2b069baefd4da052a863a4c1d6a42edc9980;hb=77239b425a8e00822a53c9907415832a473c3eb6;hp=8bb073c413061b0d6826dd02d0eff3764652b896;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/server/follow-constraints.ts b/server/tests/api/server/follow-constraints.ts index 8bb073c41..e1ec2b069 100644 --- a/server/tests/api/server/follow-constraints.ts +++ b/server/tests/api/server/follow-constraints.ts @@ -1,54 +1,45 @@ -/* tslint:disable:no-unused-expression */ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' -import 'mocha' +import { expect } from 'chai' +import { HttpStatusCode, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models' import { + cleanupTests, + createMultipleServers, doubleFollow, - getAccountVideos, - getVideo, - getVideoChannelVideos, - getVideoWithToken, - flushAndRunMultipleServers, - killallServers, - ServerInfo, + PeerTubeServer, setAccessTokensToServers, - uploadVideo -} from '../../../../shared/utils' -import { unfollow } from '../../../../shared/utils/server/follows' -import { userLogin } from '../../../../shared/utils/users/login' -import { createUser } from '../../../../shared/utils/users/users' - -const expect = chai.expect + waitJobs +} from '@shared/server-commands' describe('Test follow constraints', function () { - let servers: ServerInfo[] = [] + let servers: PeerTubeServer[] = [] let video1UUID: string let video2UUID: string - let userAccessToken: string + let userToken: string before(async function () { - this.timeout(30000) + this.timeout(240000) - servers = await flushAndRunMultipleServers(2) + servers = await createMultipleServers(2) // Get the access tokens await setAccessTokensToServers(servers) { - const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video server 1' }) - video1UUID = res.body.video.uuid + const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video server 1' } }) + video1UUID = uuid } { - const res = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, { name: 'video server 2' }) - video2UUID = res.body.video.uuid + const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video server 2' } }) + video2UUID = uuid } const user = { username: 'user1', password: 'super_password' } - await createUser(servers[0].url, servers[0].accessToken, user.username, user.password) - userAccessToken = await userLogin(servers[0], user) + await servers[0].users.create({ username: user.username, password: user.password }) + userToken = await servers[0].login.getAccessToken(user) await doubleFollow(servers[0], servers[1]) }) @@ -58,77 +49,81 @@ describe('Test follow constraints', function () { describe('With an unlogged user', function () { it('Should get the local video', async function () { - await getVideo(servers[0].url, video1UUID, 200) + await servers[0].videos.get({ id: video1UUID }) }) it('Should get the remote video', async function () { - await getVideo(servers[0].url, video2UUID, 200) + await servers[0].videos.get({ id: video2UUID }) }) it('Should list local account videos', async function () { - const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9001', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[0].port }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list remote account videos', async function () { - const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9002', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ handle: 'root@localhost:' + servers[1].port }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list local channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9001', 0, 5) + const handle = 'root_channel@localhost:' + servers[0].port + const { total, data } = await servers[0].videos.listByChannel({ handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list remote channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9002', 0, 5) + const handle = 'root_channel@localhost:' + servers[1].port + const { total, data } = await servers[0].videos.listByChannel({ handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) }) describe('With a logged user', function () { it('Should get the local video', async function () { - await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, 200) + await servers[0].videos.getWithToken({ token: userToken, id: video1UUID }) }) it('Should get the remote video', async function () { - await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, 200) + await servers[0].videos.getWithToken({ token: userToken, id: video2UUID }) }) it('Should list local account videos', async function () { - const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9001', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[0].port }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list remote account videos', async function () { - const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9002', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[1].port }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list local channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9001', 0, 5) + const handle = 'root_channel@localhost:' + servers[0].port + const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list remote channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9002', 0, 5) + const handle = 'root_channel@localhost:' + servers[1].port + const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) }) }) @@ -138,88 +133,189 @@ describe('Test follow constraints', function () { before(async function () { this.timeout(30000) - await unfollow(servers[0].url, servers[0].accessToken, servers[1]) + await servers[0].follows.unfollow({ target: servers[1] }) }) describe('With an unlogged user', function () { it('Should get the local video', async function () { - await getVideo(servers[0].url, video1UUID, 200) + await servers[0].videos.get({ id: video1UUID }) }) it('Should not get the remote video', async function () { - await getVideo(servers[0].url, video2UUID, 403) + const body = await servers[0].videos.get({ id: video2UUID, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) + const error = body as unknown as PeerTubeProblemDocument + + const doc = 'https://docs.joinpeertube.org/api-rest-reference.html#section/Errors/does_not_respect_follow_constraints' + expect(error.type).to.equal(doc) + expect(error.code).to.equal(ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS) + + expect(error.detail).to.equal('Cannot get this video regarding follow constraints') + expect(error.error).to.equal(error.detail) + + expect(error.status).to.equal(HttpStatusCode.FORBIDDEN_403) + + expect(error.originUrl).to.contains(servers[1].url) }) it('Should list local account videos', async function () { - const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9001', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ + token: null, + handle: 'root@localhost:' + servers[0].port + }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should not list remote account videos', async function () { - const res = await getAccountVideos(servers[0].url, undefined, 'root@localhost:9002', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ + token: null, + handle: 'root@localhost:' + servers[1].port + }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) }) it('Should list local channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9001', 0, 5) + const handle = 'root_channel@localhost:' + servers[0].port + const { total, data } = await servers[0].videos.listByChannel({ token: null, handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should not list remote channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, undefined, 'root_channel@localhost:9002', 0, 5) + const handle = 'root_channel@localhost:' + servers[1].port + const { total, data } = await servers[0].videos.listByChannel({ token: null, handle }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) + expect(total).to.equal(0) + expect(data).to.have.lengthOf(0) }) }) describe('With a logged user', function () { + it('Should get the local video', async function () { - await getVideoWithToken(servers[0].url, userAccessToken, video1UUID, 200) + await servers[0].videos.getWithToken({ token: userToken, id: video1UUID }) }) it('Should get the remote video', async function () { - await getVideoWithToken(servers[0].url, userAccessToken, video2UUID, 200) + await servers[0].videos.getWithToken({ token: userToken, id: video2UUID }) }) it('Should list local account videos', async function () { - const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9001', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[0].port }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list remote account videos', async function () { - const res = await getAccountVideos(servers[0].url, userAccessToken, 'root@localhost:9002', 0, 5) + const { total, data } = await servers[0].videos.listByAccount({ token: userToken, handle: 'root@localhost:' + servers[1].port }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list local channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9001', 0, 5) + const handle = 'root_channel@localhost:' + servers[0].port + const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) it('Should list remote channel videos', async function () { - const res = await getVideoChannelVideos(servers[0].url, userAccessToken, 'root_channel@localhost:9002', 0, 5) + const handle = 'root_channel@localhost:' + servers[1].port + const { total, data } = await servers[0].videos.listByChannel({ token: userToken, handle }) - expect(res.body.total).to.equal(1) - expect(res.body.data).to.have.lengthOf(1) + expect(total).to.equal(1) + expect(data).to.have.lengthOf(1) }) }) }) + describe('When following a remote account', function () { + + before(async function () { + this.timeout(60000) + + await servers[0].follows.follow({ handles: [ 'root@' + servers[1].host ] }) + await waitJobs(servers) + }) + + it('Should get the remote video with an unlogged user', async function () { + await servers[0].videos.get({ id: video2UUID }) + }) + + it('Should get the remote video with a logged in user', async function () { + await servers[0].videos.getWithToken({ token: userToken, id: video2UUID }) + }) + }) + + describe('When unfollowing a remote account', function () { + + before(async function () { + this.timeout(60000) + + await servers[0].follows.unfollow({ target: 'root@' + servers[1].host }) + await waitJobs(servers) + }) + + it('Should not get the remote video with an unlogged user', async function () { + const body = await servers[0].videos.get({ id: video2UUID, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) + + const error = body as unknown as PeerTubeProblemDocument + expect(error.code).to.equal(ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS) + }) + + it('Should get the remote video with a logged in user', async function () { + await servers[0].videos.getWithToken({ token: userToken, id: video2UUID }) + }) + }) + + describe('When following a remote channel', function () { + + before(async function () { + this.timeout(60000) + + await servers[0].follows.follow({ handles: [ 'root_channel@' + servers[1].host ] }) + await waitJobs(servers) + }) + + it('Should get the remote video with an unlogged user', async function () { + await servers[0].videos.get({ id: video2UUID }) + }) + + it('Should get the remote video with a logged in user', async function () { + await servers[0].videos.getWithToken({ token: userToken, id: video2UUID }) + }) + }) + + describe('When unfollowing a remote channel', function () { + + before(async function () { + this.timeout(60000) + + await servers[0].follows.unfollow({ target: 'root_channel@' + servers[1].host }) + await waitJobs(servers) + }) + + it('Should not get the remote video with an unlogged user', async function () { + const body = await servers[0].videos.get({ id: video2UUID, expectedStatus: HttpStatusCode.FORBIDDEN_403 }) + + const error = body as unknown as PeerTubeProblemDocument + expect(error.code).to.equal(ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS) + }) + + it('Should get the remote video with a logged in user', async function () { + await servers[0].videos.getWithToken({ token: userToken, id: video2UUID }) + }) + }) + after(async function () { - killallServers(servers) + await cleanupTests(servers) }) })