aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils
diff options
context:
space:
mode:
Diffstat (limited to 'shared/utils')
-rw-r--r--shared/utils/index.ts5
-rw-r--r--shared/utils/miscs/miscs.ts8
-rw-r--r--shared/utils/miscs/sql.ts38
-rw-r--r--shared/utils/miscs/stubs.ts14
-rw-r--r--shared/utils/requests/activitypub.ts43
-rw-r--r--shared/utils/requests/requests.ts4
-rw-r--r--shared/utils/server/activitypub.ts5
-rw-r--r--shared/utils/server/servers.ts2
-rw-r--r--shared/utils/users/blocklist.ts3
-rw-r--r--shared/utils/users/users.ts2
-rw-r--r--shared/utils/videos/videos.ts2
11 files changed, 113 insertions, 13 deletions
diff --git a/shared/utils/index.ts b/shared/utils/index.ts
index 897389824..e08bbfd2a 100644
--- a/shared/utils/index.ts
+++ b/shared/utils/index.ts
@@ -2,10 +2,15 @@ export * from './server/activitypub'
2export * from './cli/cli' 2export * from './cli/cli'
3export * from './server/clients' 3export * from './server/clients'
4export * from './server/config' 4export * from './server/config'
5export * from './server/jobs'
5export * from './users/login' 6export * from './users/login'
6export * from './miscs/miscs' 7export * from './miscs/miscs'
8export * from './miscs/stubs'
9export * from './miscs/sql'
7export * from './server/follows' 10export * from './server/follows'
11export * from './requests/activitypub'
8export * from './requests/requests' 12export * from './requests/requests'
13export * from './requests/check-api-params'
9export * from './server/servers' 14export * from './server/servers'
10export * from './videos/services' 15export * from './videos/services'
11export * from './users/users' 16export * from './users/users'
diff --git a/shared/utils/miscs/miscs.ts b/shared/utils/miscs/miscs.ts
index 589daa420..91a93b631 100644
--- a/shared/utils/miscs/miscs.ts
+++ b/shared/utils/miscs/miscs.ts
@@ -33,8 +33,8 @@ function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
33} 33}
34 34
35function root () { 35function root () {
36 // We are in server/tests/utils/miscs 36 // We are in /shared/utils/miscs
37 return join(__dirname, '..', '..', '..', '..') 37 return join(__dirname, '..', '..', '..')
38} 38}
39 39
40async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { 40async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
@@ -44,7 +44,7 @@ async function testImage (url: string, imageName: string, imagePath: string, ext
44 44
45 const body = res.body 45 const body = res.body
46 46
47 const data = await readFile(join(__dirname, '..', '..', 'fixtures', imageName + extension)) 47 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
48 const minLength = body.length - ((20 * body.length) / 100) 48 const minLength = body.length - ((20 * body.length) / 100)
49 const maxLength = body.length + ((20 * body.length) / 100) 49 const maxLength = body.length + ((20 * body.length) / 100)
50 50
@@ -59,7 +59,7 @@ function buildAbsoluteFixturePath (path: string, customTravisPath = false) {
59 59
60 if (customTravisPath && process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path) 60 if (customTravisPath && process.env.TRAVIS) return join(process.env.HOME, 'fixtures', path)
61 61
62 return join(__dirname, '..', '..', 'fixtures', path) 62 return join(root(), 'server', 'tests', 'fixtures', path)
63} 63}
64 64
65async function generateHighBitrateVideo () { 65async function generateHighBitrateVideo () {
diff --git a/shared/utils/miscs/sql.ts b/shared/utils/miscs/sql.ts
new file mode 100644
index 000000000..027f78131
--- /dev/null
+++ b/shared/utils/miscs/sql.ts
@@ -0,0 +1,38 @@
1import * as Sequelize from 'sequelize'
2
3function getSequelize (serverNumber: number) {
4 const dbname = 'peertube_test' + serverNumber
5 const username = 'peertube'
6 const password = 'peertube'
7 const host = 'localhost'
8 const port = 5432
9
10 return new Sequelize(dbname, username, password, {
11 dialect: 'postgres',
12 host,
13 port,
14 operatorsAliases: false,
15 logging: false
16 })
17}
18
19function setActorField (serverNumber: number, to: string, field: string, value: string) {
20 const seq = getSequelize(serverNumber)
21
22 const options = { type: Sequelize.QueryTypes.UPDATE }
23
24 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
25}
26
27function setVideoField (serverNumber: number, uuid: string, field: string, value: string) {
28 const seq = getSequelize(serverNumber)
29
30 const options = { type: Sequelize.QueryTypes.UPDATE }
31
32 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
33}
34
35export {
36 setVideoField,
37 setActorField
38}
diff --git a/shared/utils/miscs/stubs.ts b/shared/utils/miscs/stubs.ts
new file mode 100644
index 000000000..d1eb0e3b2
--- /dev/null
+++ b/shared/utils/miscs/stubs.ts
@@ -0,0 +1,14 @@
1function buildRequestStub (): any {
2 return { }
3}
4
5function buildResponseStub (): any {
6 return {
7 locals: {}
8 }
9}
10
11export {
12 buildResponseStub,
13 buildRequestStub
14}
diff --git a/shared/utils/requests/activitypub.ts b/shared/utils/requests/activitypub.ts
new file mode 100644
index 000000000..e2348ace0
--- /dev/null
+++ b/shared/utils/requests/activitypub.ts
@@ -0,0 +1,43 @@
1import { doRequest } from '../../../server/helpers/requests'
2import { HTTP_SIGNATURE } from '../../../server/initializers'
3import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils'
4import { activityPubContextify } from '../../../server/helpers/activitypub'
5
6function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
7 const options = {
8 method: 'POST',
9 uri: url,
10 json: body,
11 httpSignature,
12 headers
13 }
14
15 return doRequest(options)
16}
17
18async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
19 const follow = {
20 type: 'Follow',
21 id: by.url + '/toto',
22 actor: by.url,
23 object: to.url
24 }
25
26 const body = activityPubContextify(follow)
27
28 const httpSignature = {
29 algorithm: HTTP_SIGNATURE.ALGORITHM,
30 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
31 keyId: by.url,
32 key: by.privateKey,
33 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
34 }
35 const headers = buildGlobalHeaders(body)
36
37 return makePOSTAPRequest(to.url, body, httpSignature, headers)
38}
39
40export {
41 makePOSTAPRequest,
42 makeFollowRequest
43}
diff --git a/shared/utils/requests/requests.ts b/shared/utils/requests/requests.ts
index 5796540f7..77e9f6164 100644
--- a/shared/utils/requests/requests.ts
+++ b/shared/utils/requests/requests.ts
@@ -1,5 +1,5 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { buildAbsoluteFixturePath } from '../miscs/miscs' 2import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
3import { isAbsolute, join } from 'path' 3import { isAbsolute, join } from 'path'
4 4
5function makeGetRequest (options: { 5function makeGetRequest (options: {
@@ -142,7 +142,7 @@ function updateAvatarRequest (options: {
142 if (isAbsolute(options.fixture)) { 142 if (isAbsolute(options.fixture)) {
143 filePath = options.fixture 143 filePath = options.fixture
144 } else { 144 } else {
145 filePath = join(__dirname, '..', '..', 'fixtures', options.fixture) 145 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
146 } 146 }
147 147
148 return makeUploadRequest({ 148 return makeUploadRequest({
diff --git a/shared/utils/server/activitypub.ts b/shared/utils/server/activitypub.ts
index cf3c1c3b3..eccb198ca 100644
--- a/shared/utils/server/activitypub.ts
+++ b/shared/utils/server/activitypub.ts
@@ -1,11 +1,10 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3function makeActivityPubGetRequest (url: string, path: string) { 3function makeActivityPubGetRequest (url: string, path: string, expectedStatus = 200) {
4 return request(url) 4 return request(url)
5 .get(path) 5 .get(path)
6 .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8') 6 .set('Accept', 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8')
7 .expect(200) 7 .expect(expectedStatus)
8 .expect('Content-Type', /json/)
9} 8}
10 9
11// --------------------------------------------------------------------------- 10// ---------------------------------------------------------------------------
diff --git a/shared/utils/server/servers.ts b/shared/utils/server/servers.ts
index f358a21f1..88d2b390c 100644
--- a/shared/utils/server/servers.ts
+++ b/shared/utils/server/servers.ts
@@ -115,7 +115,7 @@ function runServer (serverNumber: number, configOverride?: Object, args = []) {
115 } 115 }
116 116
117 return new Promise<ServerInfo>(res => { 117 return new Promise<ServerInfo>(res => {
118 server.app = fork(join(__dirname, '..', '..', '..', '..', 'dist', 'server.js'), args, options) 118 server.app = fork(join(root(), 'dist', 'server.js'), args, options)
119 server.app.stdout.on('data', function onStdout (data) { 119 server.app.stdout.on('data', function onStdout (data) {
120 let dontContinue = false 120 let dontContinue = false
121 121
diff --git a/shared/utils/users/blocklist.ts b/shared/utils/users/blocklist.ts
index 0ead5e5f6..5feb84179 100644
--- a/shared/utils/users/blocklist.ts
+++ b/shared/utils/users/blocklist.ts
@@ -1,7 +1,6 @@
1/* tslint:disable:no-unused-expression */ 1/* tslint:disable:no-unused-expression */
2 2
3import { makeDeleteRequest, makePostBodyRequest } from '../requests/requests' 3import { makeGetRequest, makeDeleteRequest, makePostBodyRequest } from '../requests/requests'
4import { makeGetRequest } from '../requests/requests'
5 4
6function getAccountBlocklistByAccount ( 5function getAccountBlocklistByAccount (
7 url: string, 6 url: string,
diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts
index d5d62a507..554e42c01 100644
--- a/shared/utils/users/users.ts
+++ b/shared/utils/users/users.ts
@@ -206,6 +206,7 @@ function updateUser (options: {
206 userId: number, 206 userId: number,
207 accessToken: string, 207 accessToken: string,
208 email?: string, 208 email?: string,
209 emailVerified?: boolean,
209 videoQuota?: number, 210 videoQuota?: number,
210 videoQuotaDaily?: number, 211 videoQuotaDaily?: number,
211 role?: UserRole 212 role?: UserRole
@@ -214,6 +215,7 @@ function updateUser (options: {
214 215
215 const toSend = {} 216 const toSend = {}
216 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email 217 if (options.email !== undefined && options.email !== null) toSend['email'] = options.email
218 if (options.emailVerified !== undefined && options.emailVerified !== null) toSend['emailVerified'] = options.emailVerified
217 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota 219 if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota
218 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily 220 if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily
219 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role 221 if (options.role !== undefined && options.role !== null) toSend['role'] = options.role
diff --git a/shared/utils/videos/videos.ts b/shared/utils/videos/videos.ts
index 1ab3e7c4b..f5fcc6a8a 100644
--- a/shared/utils/videos/videos.ts
+++ b/shared/utils/videos/videos.ts
@@ -417,7 +417,7 @@ function rateVideo (url: string, accessToken: string, id: number, rating: string
417function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) { 417function parseTorrentVideo (server: ServerInfo, videoUUID: string, resolution: number) {
418 return new Promise<any>((res, rej) => { 418 return new Promise<any>((res, rej) => {
419 const torrentName = videoUUID + '-' + resolution + '.torrent' 419 const torrentName = videoUUID + '-' + resolution + '.torrent'
420 const torrentPath = join(__dirname, '..', '..', '..', '..', 'test' + server.serverNumber, 'torrents', torrentName) 420 const torrentPath = join(root(), 'test' + server.serverNumber, 'torrents', torrentName)
421 readFile(torrentPath, (err, data) => { 421 readFile(torrentPath, (err, data) => {
422 if (err) return rej(err) 422 if (err) return rej(err)
423 423