From 06a05d5f4784a7cbb27aa1188385b5679845dad8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Aug 2018 15:25:20 +0200 Subject: Add subscriptions endpoints to REST API --- server/tests/api/check-params/index.ts | 1 + .../tests/api/check-params/user-subscriptions.ts | 220 +++++++++++++++ server/tests/api/index-slow.ts | 1 + server/tests/api/users/user-subscriptions.ts | 312 +++++++++++++++++++++ 4 files changed, 534 insertions(+) create mode 100644 server/tests/api/check-params/user-subscriptions.ts create mode 100644 server/tests/api/users/user-subscriptions.ts (limited to 'server/tests/api') diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 03fdd5c4e..777acbb0f 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -12,3 +12,4 @@ import './video-comments' import './videos' import './video-imports' import './search' +import './user-subscriptions' diff --git a/server/tests/api/check-params/user-subscriptions.ts b/server/tests/api/check-params/user-subscriptions.ts new file mode 100644 index 000000000..9f7d15b27 --- /dev/null +++ b/server/tests/api/check-params/user-subscriptions.ts @@ -0,0 +1,220 @@ +/* tslint:disable:no-unused-expression */ + +import 'mocha' + +import { + createUser, + flushTests, + getMyUserInformation, + killallServers, + makeDeleteRequest, + makeGetRequest, + makePostBodyRequest, + runServer, + ServerInfo, + setAccessTokensToServers, + userLogin +} from '../../utils' +import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '../../utils/requests/check-api-params' + +describe('Test user subscriptions API validators', function () { + const path = '/api/v1/users/me/subscriptions' + let server: ServerInfo + let userAccessToken = '' + let userChannelUUID: string + + // --------------------------------------------------------------- + + before(async function () { + this.timeout(30000) + + await flushTests() + + server = await runServer(1) + + await setAccessTokensToServers([ server ]) + + const user = { + username: 'user1', + password: 'my super password' + } + await createUser(server.url, server.accessToken, user.username, user.password) + userAccessToken = await userLogin(server, user) + + { + const res = await getMyUserInformation(server.url, server.accessToken) + userChannelUUID = res.body.videoChannels[ 0 ].uuid + } + }) + + describe('When listing my subscriptions', function () { + it('Should fail with a bad start pagination', async function () { + await checkBadStartPagination(server.url, path, server.accessToken) + }) + + it('Should fail with a bad count pagination', async function () { + await checkBadCountPagination(server.url, path, server.accessToken) + }) + + it('Should fail with an incorrect sort', async function () { + await checkBadSortPagination(server.url, path, server.accessToken) + }) + + it('Should fail with a non authenticated user', async function () { + await makeGetRequest({ + url: server.url, + path, + statusCodeExpected: 401 + }) + }) + + it('Should success with the correct parameters', async function () { + await await makeGetRequest({ + url: server.url, + path, + token: userAccessToken, + statusCodeExpected: 200 + }) + }) + }) + + describe('When listing my subscriptions videos', function () { + const path = '/api/v1/users/me/subscriptions/videos' + + it('Should fail with a bad start pagination', async function () { + await checkBadStartPagination(server.url, path, server.accessToken) + }) + + it('Should fail with a bad count pagination', async function () { + await checkBadCountPagination(server.url, path, server.accessToken) + }) + + it('Should fail with an incorrect sort', async function () { + await checkBadSortPagination(server.url, path, server.accessToken) + }) + + it('Should fail with a non authenticated user', async function () { + await makeGetRequest({ + url: server.url, + path, + statusCodeExpected: 401 + }) + }) + + it('Should success with the correct parameters', async function () { + await await makeGetRequest({ + url: server.url, + path, + token: userAccessToken, + statusCodeExpected: 200 + }) + }) + }) + + describe('When adding a subscription', function () { + it('Should fail with a non authenticated user', async function () { + await makePostBodyRequest({ + url: server.url, + path, + fields: { uri: userChannelUUID + '@localhost:9001' }, + statusCodeExpected: 401 + }) + }) + + it('Should fail with bad URIs', async function () { + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + fields: { uri: 'root' }, + statusCodeExpected: 400 + }) + + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + fields: { uri: 'root@' }, + statusCodeExpected: 400 + }) + + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + fields: { uri: 'root@hello@' }, + statusCodeExpected: 400 + }) + }) + + it('Should success with the correct parameters', async function () { + await makePostBodyRequest({ + url: server.url, + path, + token: server.accessToken, + fields: { uri: userChannelUUID + '@localhost:9001' }, + statusCodeExpected: 204 + }) + }) + }) + + describe('When removing a subscription', function () { + it('Should fail with a non authenticated user', async function () { + await makeDeleteRequest({ + url: server.url, + path: path + '/' + userChannelUUID + '@localhost:9001', + statusCodeExpected: 401 + }) + }) + + it('Should fail with bad URIs', async function () { + await makeDeleteRequest({ + url: server.url, + path: path + '/root', + token: server.accessToken, + statusCodeExpected: 400 + }) + + await makeDeleteRequest({ + url: server.url, + path: path + '/root@', + token: server.accessToken, + statusCodeExpected: 400 + }) + + await makeDeleteRequest({ + url: server.url, + path: path + '/root@hello@', + token: server.accessToken, + statusCodeExpected: 400 + }) + }) + + it('Should fail with an unknown subscription', async function () { + await makeDeleteRequest({ + url: server.url, + path: path + '/root1@localhost:9001', + token: server.accessToken, + statusCodeExpected: 404 + }) + }) + + it('Should success with the correct parameters', async function () { + await makeDeleteRequest({ + url: server.url, + path: path + '/' + userChannelUUID + '@localhost:9001', + token: server.accessToken, + statusCodeExpected: 204 + }) + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/api/index-slow.ts b/server/tests/api/index-slow.ts index 243c941cb..e24a7b664 100644 --- a/server/tests/api/index-slow.ts +++ b/server/tests/api/index-slow.ts @@ -6,6 +6,7 @@ import './server/follows' import './server/jobs' import './videos/video-comments' import './users/users-multiple-servers' +import './users/user-subscriptions' import './server/handle-down' import './videos/video-schedule-update' import './videos/video-imports' diff --git a/server/tests/api/users/user-subscriptions.ts b/server/tests/api/users/user-subscriptions.ts new file mode 100644 index 000000000..2ba6cdfaf --- /dev/null +++ b/server/tests/api/users/user-subscriptions.ts @@ -0,0 +1,312 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { createUser, doubleFollow, flushAndRunMultipleServers, follow, getVideosList, unfollow, userLogin } from '../../utils' +import { getMyUserInformation, killallServers, ServerInfo, uploadVideo } from '../../utils/index' +import { setAccessTokensToServers } from '../../utils/users/login' +import { Video, VideoChannel } from '../../../../shared/models/videos' +import { waitJobs } from '../../utils/server/jobs' +import { + addUserSubscription, + listUserSubscriptions, + listUserSubscriptionVideos, + removeUserSubscription +} from '../../utils/users/user-subscriptions' + +const expect = chai.expect + +describe('Test users subscriptions', function () { + let servers: ServerInfo[] = [] + const users: { accessToken: string, videoChannelName: string }[] = [] + let rootChannelNameServer1: string + + before(async function () { + this.timeout(120000) + + servers = await flushAndRunMultipleServers(3) + + // Get the access tokens + await setAccessTokensToServers(servers) + + // Server 1 and server 2 follow each other + await doubleFollow(servers[0], servers[1]) + + const res = await getMyUserInformation(servers[0].url, servers[0].accessToken) + rootChannelNameServer1 = res.body.videoChannels[0].name + + { + for (const server of servers) { + const user = { username: 'user' + server.serverNumber, password: 'password' } + await createUser(server.url, server.accessToken, user.username, user.password) + + const accessToken = await userLogin(server, user) + const res = await getMyUserInformation(server.url, accessToken) + const videoChannels: VideoChannel[] = res.body.videoChannels + + users.push({ accessToken, videoChannelName: videoChannels[0].name }) + + const videoName1 = 'video 1-' + server.serverNumber + await uploadVideo(server.url, accessToken, { name: videoName1 }) + + const videoName2 = 'video 2-' + server.serverNumber + await uploadVideo(server.url, accessToken, { name: videoName2 }) + } + } + + await waitJobs(servers) + }) + + it('Should display videos of server 2 on server 1', async function () { + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(4) + }) + + it('User of server 1 should follow user of server 3 and root of server 1', async function () { + this.timeout(30000) + + await addUserSubscription(servers[0].url, users[0].accessToken, users[2].videoChannelName + '@localhost:9003') + await addUserSubscription(servers[0].url, users[0].accessToken, rootChannelNameServer1 + '@localhost:9001') + + await waitJobs(servers) + + await uploadVideo(servers[2].url, users[2].accessToken, { name: 'video server 3 added after follow' }) + + await waitJobs(servers) + }) + + it('Should not display videos of server 3 on server 1', async function () { + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(4) + for (const video of res.body.data) { + expect(video.name).to.not.contain('1-3') + expect(video.name).to.not.contain('2-3') + expect(video.name).to.not.contain('video server 3 added after follow') + } + }) + + it('Should list subscriptions', async function () { + { + const res = await listUserSubscriptions(servers[0].url, servers[0].accessToken) + expect(res.body.total).to.equal(0) + expect(res.body.data).to.be.an('array') + expect(res.body.data).to.have.lengthOf(0) + } + + { + const res = await listUserSubscriptions(servers[0].url, users[0].accessToken) + expect(res.body.total).to.equal(2) + + const subscriptions: VideoChannel[] = res.body.data + expect(subscriptions).to.be.an('array') + expect(subscriptions).to.have.lengthOf(2) + + expect(subscriptions[0].name).to.equal(users[2].videoChannelName) + expect(subscriptions[1].name).to.equal(rootChannelNameServer1) + } + }) + + it('Should list subscription videos', async function () { + { + const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken) + expect(res.body.total).to.equal(0) + expect(res.body.data).to.be.an('array') + expect(res.body.data).to.have.lengthOf(0) + } + + { + const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') + expect(res.body.total).to.equal(3) + + const videos: Video[] = res.body.data + expect(videos).to.be.an('array') + expect(videos).to.have.lengthOf(3) + + expect(videos[0].name).to.equal('video 1-3') + expect(videos[1].name).to.equal('video 2-3') + expect(videos[2].name).to.equal('video server 3 added after follow') + } + }) + + it('Should upload a video by root on server 1 and see it in the subscription videos', async function () { + this.timeout(30000) + + const videoName = 'video server 1 added after follow' + await uploadVideo(servers[0].url, servers[0].accessToken, { name: videoName }) + + await waitJobs(servers) + + { + const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken) + expect(res.body.total).to.equal(0) + expect(res.body.data).to.be.an('array') + expect(res.body.data).to.have.lengthOf(0) + } + + { + const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') + expect(res.body.total).to.equal(4) + + const videos: Video[] = res.body.data + expect(videos).to.be.an('array') + expect(videos).to.have.lengthOf(4) + + expect(videos[0].name).to.equal('video 1-3') + expect(videos[1].name).to.equal('video 2-3') + expect(videos[2].name).to.equal('video server 3 added after follow') + expect(videos[3].name).to.equal('video server 1 added after follow') + } + + { + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(5) + for (const video of res.body.data) { + expect(video.name).to.not.contain('1-3') + expect(video.name).to.not.contain('2-3') + expect(video.name).to.not.contain('video server 3 added after follow') + } + } + }) + + it('Should have server 1 follow server 3 and display server 3 videos', async function () { + this.timeout(30000) + + await follow(servers[0].url, [ servers[2].url ], servers[0].accessToken) + + await waitJobs(servers) + + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(8) + + const names = [ '1-3', '2-3', 'video server 3 added after follow' ] + for (const name of names) { + const video = res.body.data.find(v => v.name.indexOf(name) === -1) + expect(video).to.not.be.undefined + } + }) + + it('Should remove follow server 1 -> server 3 and hide server 3 videos', async function () { + this.timeout(30000) + + await unfollow(servers[0].url, servers[0].accessToken, servers[2]) + + await waitJobs(servers) + + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(5) + for (const video of res.body.data) { + expect(video.name).to.not.contain('1-3') + expect(video.name).to.not.contain('2-3') + expect(video.name).to.not.contain('video server 3 added after follow') + } + }) + + it('Should still list subscription videos', async function () { + { + const res = await listUserSubscriptionVideos(servers[0].url, servers[0].accessToken) + expect(res.body.total).to.equal(0) + expect(res.body.data).to.be.an('array') + expect(res.body.data).to.have.lengthOf(0) + } + + { + const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') + expect(res.body.total).to.equal(4) + + const videos: Video[] = res.body.data + expect(videos).to.be.an('array') + expect(videos).to.have.lengthOf(4) + + expect(videos[0].name).to.equal('video 1-3') + expect(videos[1].name).to.equal('video 2-3') + expect(videos[2].name).to.equal('video server 3 added after follow') + expect(videos[3].name).to.equal('video server 1 added after follow') + } + }) + + it('Should remove user of server 3 subscription', async function () { + await removeUserSubscription(servers[0].url, users[0].accessToken, users[2].videoChannelName + '@localhost:9003') + + await waitJobs(servers) + }) + + it('Should not display its videos anymore', async function () { + { + const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') + expect(res.body.total).to.equal(1) + + const videos: Video[] = res.body.data + expect(videos).to.be.an('array') + expect(videos).to.have.lengthOf(1) + + expect(videos[0].name).to.equal('video server 1 added after follow') + } + }) + + it('Should remove the root subscription and not display the videos anymore', async function () { + await removeUserSubscription(servers[0].url, users[0].accessToken, rootChannelNameServer1 + '@localhost:9001') + + await waitJobs(servers) + + { + const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') + expect(res.body.total).to.equal(0) + + const videos: Video[] = res.body.data + expect(videos).to.be.an('array') + expect(videos).to.have.lengthOf(0) + } + }) + + it('Should correctly display public videos on server 1', async function () { + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(5) + for (const video of res.body.data) { + expect(video.name).to.not.contain('1-3') + expect(video.name).to.not.contain('2-3') + expect(video.name).to.not.contain('video server 3 added after follow') + } + }) + + it('Should follow user of server 3 again', async function () { + this.timeout(30000) + + await addUserSubscription(servers[0].url, users[0].accessToken, users[2].videoChannelName + '@localhost:9003') + + await waitJobs(servers) + + { + const res = await listUserSubscriptionVideos(servers[0].url, users[0].accessToken, 'createdAt') + expect(res.body.total).to.equal(3) + + const videos: Video[] = res.body.data + expect(videos).to.be.an('array') + expect(videos).to.have.lengthOf(3) + + expect(videos[0].name).to.equal('video 1-3') + expect(videos[1].name).to.equal('video 2-3') + expect(videos[2].name).to.equal('video server 3 added after follow') + } + + { + const res = await getVideosList(servers[0].url) + + expect(res.body.total).to.equal(5) + for (const video of res.body.data) { + expect(video.name).to.not.contain('1-3') + expect(video.name).to.not.contain('2-3') + expect(video.name).to.not.contain('video server 3 added after follow') + } + } + }) + + after(async function () { + killallServers(servers) + }) +}) -- cgit v1.2.3