aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-11 17:23:24 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commit8d2be0ed7bb87283a1ec98609df6b82d83db706a (patch)
tree31a36b252df32be83ceb77658a53b57f9d15e8ac /shared
parentdba85a1e9e9f603ba52e1ea42deaf3fdd799b1d8 (diff)
downloadPeerTube-8d2be0ed7bb87283a1ec98609df6b82d83db706a.tar.gz
PeerTube-8d2be0ed7bb87283a1ec98609df6b82d83db706a.tar.zst
PeerTube-8d2be0ed7bb87283a1ec98609df6b82d83db706a.zip
WIP plugins: move plugin CLI in peertube script
Install/uninstall/list plugins remotely
Diffstat (limited to 'shared')
-rw-r--r--shared/extra-utils/index.ts1
-rw-r--r--shared/extra-utils/miscs/miscs.ts5
-rw-r--r--shared/extra-utils/server/plugins.ts125
-rw-r--r--shared/extra-utils/server/servers.ts14
-rw-r--r--shared/extra-utils/users/login.ts19
-rw-r--r--shared/extra-utils/users/users.ts8
-rw-r--r--shared/extra-utils/videos/video-channels.ts8
-rw-r--r--shared/models/plugins/install-plugin.model.ts3
8 files changed, 168 insertions, 15 deletions
diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts
index 9d0bbaa38..53ddaa681 100644
--- a/shared/extra-utils/index.ts
+++ b/shared/extra-utils/index.ts
@@ -11,6 +11,7 @@ export * from './server/follows'
11export * from './requests/requests' 11export * from './requests/requests'
12export * from './requests/check-api-params' 12export * from './requests/check-api-params'
13export * from './server/servers' 13export * from './server/servers'
14export * from './server/plugins'
14export * from './videos/services' 15export * from './videos/services'
15export * from './videos/video-playlists' 16export * from './videos/video-playlists'
16export * from './users/users' 17export * from './users/users'
diff --git a/shared/extra-utils/miscs/miscs.ts b/shared/extra-utils/miscs/miscs.ts
index fb6430e4f..42250886c 100644
--- a/shared/extra-utils/miscs/miscs.ts
+++ b/shared/extra-utils/miscs/miscs.ts
@@ -8,7 +8,7 @@ import { pathExists, readFile } from 'fs-extra'
8import * as ffmpeg from 'fluent-ffmpeg' 8import * as ffmpeg from 'fluent-ffmpeg'
9 9
10const expect = chai.expect 10const expect = chai.expect
11let webtorrent = new WebTorrent() 11let webtorrent: WebTorrent.Instance
12 12
13function immutableAssign <T, U> (target: T, source: U) { 13function immutableAssign <T, U> (target: T, source: U) {
14 return Object.assign<{}, T, U>({}, target, source) 14 return Object.assign<{}, T, U>({}, target, source)
@@ -27,6 +27,9 @@ function wait (milliseconds: number) {
27} 27}
28 28
29function webtorrentAdd (torrent: string, refreshWebTorrent = false) { 29function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
30 const WebTorrent = require('webtorrent')
31
32 if (!webtorrent) webtorrent = new WebTorrent()
30 if (refreshWebTorrent === true) webtorrent = new WebTorrent() 33 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
31 34
32 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res)) 35 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts
new file mode 100644
index 000000000..6cd7cd17a
--- /dev/null
+++ b/shared/extra-utils/server/plugins.ts
@@ -0,0 +1,125 @@
1import { makeGetRequest, makePostBodyRequest } from '../requests/requests'
2import { PluginType } from '../../models/plugins/plugin.type'
3
4function listPlugins (parameters: {
5 url: string,
6 accessToken: string,
7 start?: number,
8 count?: number,
9 sort?: string,
10 type?: PluginType,
11 expectedStatus?: number
12}) {
13 const { url, accessToken, start, count, sort, type, expectedStatus = 200 } = parameters
14 const path = '/api/v1/plugins'
15
16 return makeGetRequest({
17 url,
18 path,
19 token: accessToken,
20 query: {
21 start,
22 count,
23 sort,
24 type
25 },
26 statusCodeExpected: expectedStatus
27 })
28}
29
30function getPlugin (parameters: {
31 url: string,
32 accessToken: string,
33 npmName: string,
34 expectedStatus?: number
35}) {
36 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
37 const path = '/api/v1/plugins/' + npmName
38
39 return makeGetRequest({
40 url,
41 path,
42 token: accessToken,
43 statusCodeExpected: expectedStatus
44 })
45}
46
47function getPluginSettings (parameters: {
48 url: string,
49 accessToken: string,
50 npmName: string,
51 expectedStatus?: number
52}) {
53 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
54 const path = '/api/v1/plugins/' + npmName + '/settings'
55
56 return makeGetRequest({
57 url,
58 path,
59 token: accessToken,
60 statusCodeExpected: expectedStatus
61 })
62}
63
64function getPluginRegisteredSettings (parameters: {
65 url: string,
66 accessToken: string,
67 npmName: string,
68 expectedStatus?: number
69}) {
70 const { url, accessToken, npmName, expectedStatus = 200 } = parameters
71 const path = '/api/v1/plugins/' + npmName + '/registered-settings'
72
73 return makeGetRequest({
74 url,
75 path,
76 token: accessToken,
77 statusCodeExpected: expectedStatus
78 })
79}
80
81function installPlugin (parameters: {
82 url: string,
83 accessToken: string,
84 path?: string,
85 npmName?: string
86 expectedStatus?: number
87}) {
88 const { url, accessToken, npmName, path, expectedStatus = 204 } = parameters
89 const apiPath = '/api/v1/plugins/install'
90
91 return makePostBodyRequest({
92 url,
93 path: apiPath,
94 token: accessToken,
95 fields: { npmName, path },
96 statusCodeExpected: expectedStatus
97 })
98}
99
100function uninstallPlugin (parameters: {
101 url: string,
102 accessToken: string,
103 npmName: string
104 expectedStatus?: number
105}) {
106 const { url, accessToken, npmName, expectedStatus = 204 } = parameters
107 const apiPath = '/api/v1/plugins/uninstall'
108
109 return makePostBodyRequest({
110 url,
111 path: apiPath,
112 token: accessToken,
113 fields: { npmName },
114 statusCodeExpected: expectedStatus
115 })
116}
117
118export {
119 listPlugins,
120 installPlugin,
121 getPlugin,
122 uninstallPlugin,
123 getPluginSettings,
124 getPluginRegisteredSettings
125}
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 4c7d6862a..9167ebe5b 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -3,7 +3,7 @@
3import { ChildProcess, exec, fork } from 'child_process' 3import { ChildProcess, exec, fork } from 'child_process'
4import { join } from 'path' 4import { join } from 'path'
5import { root, wait } from '../miscs/miscs' 5import { root, wait } from '../miscs/miscs'
6import { copy, readdir, readFile, remove } from 'fs-extra' 6import { copy, pathExists, readdir, readFile, remove } from 'fs-extra'
7import { existsSync } from 'fs' 7import { existsSync } from 'fs'
8import { expect } from 'chai' 8import { expect } from 'chai'
9import { VideoChannel } from '../../models/videos' 9import { VideoChannel } from '../../models/videos'
@@ -241,20 +241,22 @@ async function reRunServer (server: ServerInfo, configOverride?: any) {
241 return server 241 return server
242} 242}
243 243
244async function checkTmpIsEmpty (server: ServerInfo) { 244function checkTmpIsEmpty (server: ServerInfo) {
245 return checkDirectoryIsEmpty(server, 'tmp') 245 return checkDirectoryIsEmpty(server, 'tmp', [ 'plugins-global.css' ])
246} 246}
247 247
248async function checkDirectoryIsEmpty (server: ServerInfo, directory: string) { 248async function checkDirectoryIsEmpty (server: ServerInfo, directory: string, exceptions: string[] = []) {
249 const testDirectory = 'test' + server.internalServerNumber 249 const testDirectory = 'test' + server.internalServerNumber
250 250
251 const directoryPath = join(root(), testDirectory, directory) 251 const directoryPath = join(root(), testDirectory, directory)
252 252
253 const directoryExists = existsSync(directoryPath) 253 const directoryExists = await pathExists(directoryPath)
254 expect(directoryExists).to.be.true 254 expect(directoryExists).to.be.true
255 255
256 const files = await readdir(directoryPath) 256 const files = await readdir(directoryPath)
257 expect(files).to.have.lengthOf(0) 257 const filtered = files.filter(f => exceptions.includes(f) === false)
258
259 expect(filtered).to.have.lengthOf(0)
258} 260}
259 261
260function killallServers (servers: ServerInfo[]) { 262function killallServers (servers: ServerInfo[]) {
diff --git a/shared/extra-utils/users/login.ts b/shared/extra-utils/users/login.ts
index ddeb9df2a..f9bfb3cb3 100644
--- a/shared/extra-utils/users/login.ts
+++ b/shared/extra-utils/users/login.ts
@@ -1,6 +1,7 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3import { ServerInfo } from '../server/servers' 3import { ServerInfo } from '../server/servers'
4import { getClient } from '../server/clients'
4 5
5type Client = { id: string, secret: string } 6type Client = { id: string, secret: string }
6type User = { username: string, password: string } 7type User = { username: string, password: string }
@@ -38,6 +39,23 @@ async function userLogin (server: Server, user: User, expectedStatus = 200) {
38 return res.body.access_token as string 39 return res.body.access_token as string
39} 40}
40 41
42async function getAccessToken (url: string, username: string, password: string) {
43 const resClient = await getClient(url)
44 const client = {
45 id: resClient.body.client_id,
46 secret: resClient.body.client_secret
47 }
48
49 const user = { username, password }
50
51 try {
52 const res = await login(url, client, user)
53 return res.body.access_token
54 } catch (err) {
55 throw new Error('Cannot authenticate. Please check your username/password.')
56 }
57}
58
41function setAccessTokensToServers (servers: ServerInfo[]) { 59function setAccessTokensToServers (servers: ServerInfo[]) {
42 const tasks: Promise<any>[] = [] 60 const tasks: Promise<any>[] = []
43 61
@@ -55,6 +73,7 @@ export {
55 login, 73 login,
56 serverLogin, 74 serverLogin,
57 userLogin, 75 userLogin,
76 getAccessToken,
58 setAccessTokensToServers, 77 setAccessTokensToServers,
59 Server, 78 Server,
60 Client, 79 Client,
diff --git a/shared/extra-utils/users/users.ts b/shared/extra-utils/users/users.ts
index 1c39881d6..5fa8cde0c 100644
--- a/shared/extra-utils/users/users.ts
+++ b/shared/extra-utils/users/users.ts
@@ -1,11 +1,11 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests' 2import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../requests/requests'
3
4import { UserCreate, UserRole } from '../../index'
5import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' 3import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type'
6import { ServerInfo, userLogin } from '..'
7import { UserAdminFlag } from '../../models/users/user-flag.model' 4import { UserAdminFlag } from '../../models/users/user-flag.model'
8import { UserRegister } from '../../models/users/user-register.model' 5import { UserRegister } from '../../models/users/user-register.model'
6import { UserRole } from '../../models/users/user-role'
7import { ServerInfo } from '../server/servers'
8import { userLogin } from './login'
9 9
10type CreateUserArgs = { url: string, 10type CreateUserArgs = { url: string,
11 accessToken: string, 11 accessToken: string,
diff --git a/shared/extra-utils/videos/video-channels.ts b/shared/extra-utils/videos/video-channels.ts
index 3e79cf15a..053842331 100644
--- a/shared/extra-utils/videos/video-channels.ts
+++ b/shared/extra-utils/videos/video-channels.ts
@@ -1,8 +1,10 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { VideoChannelCreate, VideoChannelUpdate } from '../../models/videos' 2import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
3import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
3import { makeGetRequest, updateAvatarRequest } from '../requests/requests' 4import { makeGetRequest, updateAvatarRequest } from '../requests/requests'
4import { getMyUserInformation, ServerInfo } from '..' 5import { ServerInfo } from '../server/servers'
5import { User } from '../..' 6import { User } from '../../models/users/user.model'
7import { getMyUserInformation } from '../users/users'
6 8
7function getVideoChannelsList (url: string, start: number, count: number, sort?: string) { 9function getVideoChannelsList (url: string, start: number, count: number, sort?: string) {
8 const path = '/api/v1/video-channels' 10 const path = '/api/v1/video-channels'
diff --git a/shared/models/plugins/install-plugin.model.ts b/shared/models/plugins/install-plugin.model.ts
index 03d87fe57..b1b46fa08 100644
--- a/shared/models/plugins/install-plugin.model.ts
+++ b/shared/models/plugins/install-plugin.model.ts
@@ -1,3 +1,4 @@
1export interface InstallPlugin { 1export interface InstallPlugin {
2 npmName: string 2 npmName?: string
3 path?: string
3} 4}