]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/activitypub/client.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / activitypub / client.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { processViewersStats } from '@server/tests/shared'
5 import { HttpStatusCode, VideoPlaylistPrivacy, WatchActionObject } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 makeActivityPubGetRequest,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultVideoChannel
14 } from '@shared/server-commands'
15
16 const expect = chai.expect
17
18 describe('Test activitypub', function () {
19 let servers: PeerTubeServer[] = []
20 let video: { id: number, uuid: string, shortUUID: string }
21 let playlist: { id: number, uuid: string, shortUUID: string }
22
23 async function testAccount (path: string) {
24 const res = await makeActivityPubGetRequest(servers[0].url, path)
25 const object = res.body
26
27 expect(object.type).to.equal('Person')
28 expect(object.id).to.equal('http://localhost:' + servers[0].port + '/accounts/root')
29 expect(object.name).to.equal('root')
30 expect(object.preferredUsername).to.equal('root')
31 }
32
33 async function testChannel (path: string) {
34 const res = await makeActivityPubGetRequest(servers[0].url, path)
35 const object = res.body
36
37 expect(object.type).to.equal('Group')
38 expect(object.id).to.equal('http://localhost:' + servers[0].port + '/video-channels/root_channel')
39 expect(object.name).to.equal('Main root channel')
40 expect(object.preferredUsername).to.equal('root_channel')
41 }
42
43 async function testVideo (path: string) {
44 const res = await makeActivityPubGetRequest(servers[0].url, path)
45 const object = res.body
46
47 expect(object.type).to.equal('Video')
48 expect(object.id).to.equal('http://localhost:' + servers[0].port + '/videos/watch/' + video.uuid)
49 expect(object.name).to.equal('video')
50 }
51
52 async function testPlaylist (path: string) {
53 const res = await makeActivityPubGetRequest(servers[0].url, path)
54 const object = res.body
55
56 expect(object.type).to.equal('Playlist')
57 expect(object.id).to.equal('http://localhost:' + servers[0].port + '/video-playlists/' + playlist.uuid)
58 expect(object.name).to.equal('playlist')
59 }
60
61 before(async function () {
62 this.timeout(30000)
63
64 servers = await createMultipleServers(2)
65
66 await setAccessTokensToServers(servers)
67 await setDefaultVideoChannel(servers)
68
69 {
70 video = await servers[0].videos.quickUpload({ name: 'video' })
71 }
72
73 {
74 const attributes = { displayName: 'playlist', privacy: VideoPlaylistPrivacy.PUBLIC, videoChannelId: servers[0].store.channel.id }
75 playlist = await servers[0].playlists.create({ attributes })
76 }
77
78 await doubleFollow(servers[0], servers[1])
79 })
80
81 it('Should return the account object', async function () {
82 await testAccount('/accounts/root')
83 await testAccount('/a/root')
84 })
85
86 it('Should return the channel object', async function () {
87 await testChannel('/video-channels/root_channel')
88 await testChannel('/c/root_channel')
89 })
90
91 it('Should return the video object', async function () {
92 await testVideo('/videos/watch/' + video.id)
93 await testVideo('/videos/watch/' + video.uuid)
94 await testVideo('/videos/watch/' + video.shortUUID)
95 await testVideo('/w/' + video.id)
96 await testVideo('/w/' + video.uuid)
97 await testVideo('/w/' + video.shortUUID)
98 })
99
100 it('Should return the playlist object', async function () {
101 await testPlaylist('/video-playlists/' + playlist.id)
102 await testPlaylist('/video-playlists/' + playlist.uuid)
103 await testPlaylist('/video-playlists/' + playlist.shortUUID)
104 await testPlaylist('/w/p/' + playlist.id)
105 await testPlaylist('/w/p/' + playlist.uuid)
106 await testPlaylist('/w/p/' + playlist.shortUUID)
107 await testPlaylist('/videos/watch/playlist/' + playlist.id)
108 await testPlaylist('/videos/watch/playlist/' + playlist.uuid)
109 await testPlaylist('/videos/watch/playlist/' + playlist.shortUUID)
110 })
111
112 it('Should redirect to the origin video object', async function () {
113 const res = await makeActivityPubGetRequest(servers[1].url, '/videos/watch/' + video.uuid, HttpStatusCode.FOUND_302)
114
115 expect(res.header.location).to.equal('http://localhost:' + servers[0].port + '/videos/watch/' + video.uuid)
116 })
117
118 it('Should return the watch action', async function () {
119 this.timeout(50000)
120
121 await servers[0].views.simulateViewer({ id: video.uuid, currentTimes: [ 0, 2 ] })
122 await processViewersStats(servers)
123
124 const res = await makeActivityPubGetRequest(servers[0].url, '/videos/local-viewer/1', HttpStatusCode.OK_200)
125
126 const object: WatchActionObject = res.body
127 expect(object.type).to.equal('WatchAction')
128 expect(object.duration).to.equal('PT2S')
129 expect(object.actionStatus).to.equal('CompletedActionStatus')
130 expect(object.watchSections).to.have.lengthOf(1)
131 expect(object.watchSections[0].startTimestamp).to.equal(0)
132 expect(object.watchSections[0].endTimestamp).to.equal(2)
133 })
134
135 after(async function () {
136 await cleanupTests(servers)
137 })
138 })