aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests
diff options
context:
space:
mode:
authorlutangar <johan.dufour@gmail.com>2021-11-02 19:11:20 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-12-16 10:08:43 +0100
commit06aad80165d09a8863ab8103149a8ff518b10641 (patch)
treea97fa31f3ade29ff807ca1b77704eb47085ab99d /server/tests
parent854f533c12bd2b88c70f9d5aeab770059e9a6861 (diff)
downloadPeerTube-06aad80165d09a8863ab8103149a8ff518b10641.tar.gz
PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.tar.zst
PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.zip
chore(refactor): remove shared folder dependencies to the server
Many files from the `shared` folder were importing files from the `server` folder. When attempting to use Typescript project references to describe dependencies, it highlighted a circular dependency beetween `shared` <-> `server`. The Typescript project forbid such usages. Using project references greatly improve performance by rebuilding only the updated project and not all source files. > see https://www.typescriptlang.org/docs/handbook/project-references.html
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/api/activitypub/security.ts2
-rw-r--r--server/tests/api/notifications/moderation-notifications.ts2
-rw-r--r--server/tests/api/notifications/user-notifications.ts2
-rw-r--r--server/tests/api/server/follows.ts2
-rw-r--r--server/tests/api/server/handle-down.ts2
-rw-r--r--server/tests/api/videos/multiple-servers.ts2
-rw-r--r--server/tests/api/videos/single-server.ts2
-rw-r--r--server/tests/cli/prune-storage.ts2
-rw-r--r--server/tests/shared/index.ts2
-rw-r--r--server/tests/shared/requests.ts37
-rw-r--r--server/tests/shared/video.ts148
11 files changed, 195 insertions, 8 deletions
diff --git a/server/tests/api/activitypub/security.ts b/server/tests/api/activitypub/security.ts
index 94d946563..2700cff13 100644
--- a/server/tests/api/activitypub/security.ts
+++ b/server/tests/api/activitypub/security.ts
@@ -7,7 +7,7 @@ import { buildDigest } from '@server/helpers/peertube-crypto'
7import { HTTP_SIGNATURE } from '@server/initializers/constants' 7import { HTTP_SIGNATURE } from '@server/initializers/constants'
8import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils' 8import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils'
9import { buildAbsoluteFixturePath, cleanupTests, createMultipleServers, killallServers, PeerTubeServer, wait } from '@shared/extra-utils' 9import { buildAbsoluteFixturePath, cleanupTests, createMultipleServers, killallServers, PeerTubeServer, wait } from '@shared/extra-utils'
10import { makeFollowRequest, makePOSTAPRequest } from '@shared/extra-utils/requests/activitypub' 10import { makeFollowRequest, makePOSTAPRequest } from '@server/tests/shared'
11import { HttpStatusCode } from '@shared/models' 11import { HttpStatusCode } from '@shared/models'
12 12
13const expect = chai.expect 13const expect = chai.expect
diff --git a/server/tests/api/notifications/moderation-notifications.ts b/server/tests/api/notifications/moderation-notifications.ts
index 81ce8061b..6d8e5359b 100644
--- a/server/tests/api/notifications/moderation-notifications.ts
+++ b/server/tests/api/notifications/moderation-notifications.ts
@@ -1,7 +1,7 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2 2
3import 'mocha' 3import 'mocha'
4import { buildUUID } from '@server/helpers/uuid' 4import { buildUUID } from '@shared/core-utils/uuid'
5import { 5import {
6 checkAbuseStateChange, 6 checkAbuseStateChange,
7 checkAutoInstanceFollowing, 7 checkAutoInstanceFollowing,
diff --git a/server/tests/api/notifications/user-notifications.ts b/server/tests/api/notifications/user-notifications.ts
index 9af20843e..6db0347cc 100644
--- a/server/tests/api/notifications/user-notifications.ts
+++ b/server/tests/api/notifications/user-notifications.ts
@@ -2,7 +2,7 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { buildUUID } from '@server/helpers/uuid' 5import { buildUUID } from '@shared/core-utils/uuid'
6import { 6import {
7 CheckerBaseParams, 7 CheckerBaseParams,
8 checkMyVideoImportIsFinished, 8 checkMyVideoImportIsFinished,
diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts
index 832ba561a..748f4cd35 100644
--- a/server/tests/api/server/follows.ts
+++ b/server/tests/api/server/follows.ts
@@ -4,7 +4,6 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 completeVideoCheck,
8 createMultipleServers, 7 createMultipleServers,
9 dateIsValid, 8 dateIsValid,
10 expectAccountFollows, 9 expectAccountFollows,
@@ -15,6 +14,7 @@ import {
15 waitJobs 14 waitJobs
16} from '@shared/extra-utils' 15} from '@shared/extra-utils'
17import { VideoCreateResult, VideoPrivacy } from '@shared/models' 16import { VideoCreateResult, VideoPrivacy } from '@shared/models'
17import { completeVideoCheck } from '@server/tests/shared/video'
18 18
19const expect = chai.expect 19const expect = chai.expect
20 20
diff --git a/server/tests/api/server/handle-down.ts b/server/tests/api/server/handle-down.ts
index fa1da8fe0..2d059c0ed 100644
--- a/server/tests/api/server/handle-down.ts
+++ b/server/tests/api/server/handle-down.ts
@@ -5,7 +5,6 @@ import * as chai from 'chai'
5import { 5import {
6 cleanupTests, 6 cleanupTests,
7 CommentsCommand, 7 CommentsCommand,
8 completeVideoCheck,
9 createMultipleServers, 8 createMultipleServers,
10 killallServers, 9 killallServers,
11 PeerTubeServer, 10 PeerTubeServer,
@@ -14,6 +13,7 @@ import {
14 waitJobs 13 waitJobs
15} from '@shared/extra-utils' 14} from '@shared/extra-utils'
16import { HttpStatusCode, JobState, VideoCreateResult, VideoPrivacy } from '@shared/models' 15import { HttpStatusCode, JobState, VideoCreateResult, VideoPrivacy } from '@shared/models'
16import { completeVideoCheck } from '@server/tests/shared/video'
17 17
18const expect = chai.expect 18const expect = chai.expect
19 19
diff --git a/server/tests/api/videos/multiple-servers.ts b/server/tests/api/videos/multiple-servers.ts
index c6c279064..9e7b39cfa 100644
--- a/server/tests/api/videos/multiple-servers.ts
+++ b/server/tests/api/videos/multiple-servers.ts
@@ -8,7 +8,6 @@ import {
8 checkTmpIsEmpty, 8 checkTmpIsEmpty,
9 checkVideoFilesWereRemoved, 9 checkVideoFilesWereRemoved,
10 cleanupTests, 10 cleanupTests,
11 completeVideoCheck,
12 createMultipleServers, 11 createMultipleServers,
13 dateIsValid, 12 dateIsValid,
14 doubleFollow, 13 doubleFollow,
@@ -21,6 +20,7 @@ import {
21 webtorrentAdd 20 webtorrentAdd
22} from '@shared/extra-utils' 21} from '@shared/extra-utils'
23import { HttpStatusCode, VideoCommentThreadTree, VideoPrivacy } from '@shared/models' 22import { HttpStatusCode, VideoCommentThreadTree, VideoPrivacy } from '@shared/models'
23import { completeVideoCheck } from '@server/tests/shared/video'
24 24
25const expect = chai.expect 25const expect = chai.expect
26 26
diff --git a/server/tests/api/videos/single-server.ts b/server/tests/api/videos/single-server.ts
index a0e4a156c..100067f18 100644
--- a/server/tests/api/videos/single-server.ts
+++ b/server/tests/api/videos/single-server.ts
@@ -5,7 +5,6 @@ import * as chai from 'chai'
5import { 5import {
6 checkVideoFilesWereRemoved, 6 checkVideoFilesWereRemoved,
7 cleanupTests, 7 cleanupTests,
8 completeVideoCheck,
9 createSingleServer, 8 createSingleServer,
10 PeerTubeServer, 9 PeerTubeServer,
11 setAccessTokensToServers, 10 setAccessTokensToServers,
@@ -13,6 +12,7 @@ import {
13 wait 12 wait
14} from '@shared/extra-utils' 13} from '@shared/extra-utils'
15import { Video, VideoPrivacy } from '@shared/models' 14import { Video, VideoPrivacy } from '@shared/models'
15import { completeVideoCheck } from '@server/tests/shared/video'
16 16
17const expect = chai.expect 17const expect = chai.expect
18 18
diff --git a/server/tests/cli/prune-storage.ts b/server/tests/cli/prune-storage.ts
index 1c0282da9..4b4f75527 100644
--- a/server/tests/cli/prune-storage.ts
+++ b/server/tests/cli/prune-storage.ts
@@ -4,7 +4,7 @@ import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { createFile, readdir } from 'fs-extra' 5import { createFile, readdir } from 'fs-extra'
6import { join } from 'path' 6import { join } from 'path'
7import { buildUUID } from '@server/helpers/uuid' 7import { buildUUID } from '@shared/core-utils/uuid'
8import { 8import {
9 cleanupTests, 9 cleanupTests,
10 CLICommand, 10 CLICommand,
diff --git a/server/tests/shared/index.ts b/server/tests/shared/index.ts
new file mode 100644
index 000000000..938817268
--- /dev/null
+++ b/server/tests/shared/index.ts
@@ -0,0 +1,2 @@
1export * from './requests'
2export * from './video'
diff --git a/server/tests/shared/requests.ts b/server/tests/shared/requests.ts
new file mode 100644
index 000000000..9eb596029
--- /dev/null
+++ b/server/tests/shared/requests.ts
@@ -0,0 +1,37 @@
1import { doRequest } from '@server/helpers/requests'
2import { activityPubContextify } from '@server/helpers/activitypub'
3import { HTTP_SIGNATURE } from '@server/initializers/constants'
4import { buildGlobalHeaders } from '@server/lib/job-queue/handlers/utils/activitypub-http-utils'
5
6export function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
7 const options = {
8 method: 'POST' as 'POST',
9 json: body,
10 httpSignature,
11 headers
12 }
13
14 return doRequest(url, options)
15}
16
17export async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
18 const follow = {
19 type: 'Follow',
20 id: by.url + '/' + new Date().getTime(),
21 actor: by.url,
22 object: to.url
23 }
24
25 const body = activityPubContextify(follow)
26
27 const httpSignature = {
28 algorithm: HTTP_SIGNATURE.ALGORITHM,
29 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
30 keyId: by.url,
31 key: by.privateKey,
32 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
33 }
34 const headers = buildGlobalHeaders(body)
35
36 return makePOSTAPRequest(to.url + '/inbox', body, httpSignature, headers)
37}
diff --git a/server/tests/shared/video.ts b/server/tests/shared/video.ts
new file mode 100644
index 000000000..0e6a00f5c
--- /dev/null
+++ b/server/tests/shared/video.ts
@@ -0,0 +1,148 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions */
2import { dateIsValid, makeRawRequest, PeerTubeServer, testImage, webtorrentAdd } from '@shared/extra-utils'
3import { expect } from 'chai'
4import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '@server/initializers/constants'
5import { getLowercaseExtension, uuidRegex } from '@shared/core-utils'
6
7export async function completeVideoCheck (
8 server: PeerTubeServer,
9 video: any,
10 attributes: {
11 name: string
12 category: number
13 licence: number
14 language: string
15 nsfw: boolean
16 commentsEnabled: boolean
17 downloadEnabled: boolean
18 description: string
19 publishedAt?: string
20 support: string
21 originallyPublishedAt?: string
22 account: {
23 name: string
24 host: string
25 }
26 isLocal: boolean
27 tags: string[]
28 privacy: number
29 likes?: number
30 dislikes?: number
31 duration: number
32 channel: {
33 displayName: string
34 name: string
35 description: string
36 isLocal: boolean
37 }
38 fixture: string
39 files: {
40 resolution: number
41 size: number
42 }[]
43 thumbnailfile?: string
44 previewfile?: string
45 }
46) {
47 if (!attributes.likes) attributes.likes = 0
48 if (!attributes.dislikes) attributes.dislikes = 0
49
50 const host = new URL(server.url).host
51 const originHost = attributes.account.host
52
53 expect(video.name).to.equal(attributes.name)
54 expect(video.category.id).to.equal(attributes.category)
55 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
56 expect(video.licence.id).to.equal(attributes.licence)
57 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
58 expect(video.language.id).to.equal(attributes.language)
59 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
60 expect(video.privacy.id).to.deep.equal(attributes.privacy)
61 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
62 expect(video.nsfw).to.equal(attributes.nsfw)
63 expect(video.description).to.equal(attributes.description)
64 expect(video.account.id).to.be.a('number')
65 expect(video.account.host).to.equal(attributes.account.host)
66 expect(video.account.name).to.equal(attributes.account.name)
67 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
68 expect(video.channel.name).to.equal(attributes.channel.name)
69 expect(video.likes).to.equal(attributes.likes)
70 expect(video.dislikes).to.equal(attributes.dislikes)
71 expect(video.isLocal).to.equal(attributes.isLocal)
72 expect(video.duration).to.equal(attributes.duration)
73 expect(video.url).to.contain(originHost)
74 expect(dateIsValid(video.createdAt)).to.be.true
75 expect(dateIsValid(video.publishedAt)).to.be.true
76 expect(dateIsValid(video.updatedAt)).to.be.true
77
78 if (attributes.publishedAt) {
79 expect(video.publishedAt).to.equal(attributes.publishedAt)
80 }
81
82 if (attributes.originallyPublishedAt) {
83 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
84 } else {
85 expect(video.originallyPublishedAt).to.be.null
86 }
87
88 const videoDetails = await server.videos.get({ id: video.uuid })
89
90 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
91 expect(videoDetails.tags).to.deep.equal(attributes.tags)
92 expect(videoDetails.account.name).to.equal(attributes.account.name)
93 expect(videoDetails.account.host).to.equal(attributes.account.host)
94 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
95 expect(video.channel.name).to.equal(attributes.channel.name)
96 expect(videoDetails.channel.host).to.equal(attributes.account.host)
97 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
98 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
99 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
100 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
101 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
102
103 for (const attributeFile of attributes.files) {
104 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
105 expect(file).not.to.be.undefined
106
107 let extension = getLowercaseExtension(attributes.fixture)
108 // Transcoding enabled: extension will always be .mp4
109 if (attributes.files.length > 1) extension = '.mp4'
110
111 expect(file.magnetUri).to.have.lengthOf.above(2)
112
113 expect(file.torrentDownloadUrl).to.match(new RegExp(`http://${host}/download/torrents/${uuidRegex}-${file.resolution.id}.torrent`))
114 expect(file.torrentUrl).to.match(new RegExp(`http://${host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}.torrent`))
115
116 expect(file.fileUrl).to.match(new RegExp(`http://${originHost}/static/webseed/${uuidRegex}-${file.resolution.id}${extension}`))
117 expect(file.fileDownloadUrl).to.match(new RegExp(`http://${originHost}/download/videos/${uuidRegex}-${file.resolution.id}${extension}`))
118
119 await Promise.all([
120 makeRawRequest(file.torrentUrl, 200),
121 makeRawRequest(file.torrentDownloadUrl, 200),
122 makeRawRequest(file.metadataUrl, 200)
123 ])
124
125 expect(file.resolution.id).to.equal(attributeFile.resolution)
126 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
127
128 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
129 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
130 expect(
131 file.size,
132 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
133 ).to.be.above(minSize).and.below(maxSize)
134
135 const torrent = await webtorrentAdd(file.magnetUri, true)
136 expect(torrent.files).to.be.an('array')
137 expect(torrent.files.length).to.equal(1)
138 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
139 }
140
141 expect(videoDetails.thumbnailPath).to.exist
142 await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
143
144 if (attributes.previewfile) {
145 expect(videoDetails.previewPath).to.exist
146 await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
147 }
148}