From 32a18cbf33a7cdbbe3d4885d32e4b67e19cdc1cf Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 11 Mar 2021 16:54:52 +0100 Subject: Add new plugin/peertube version notifs --- shared/extra-utils/index.ts | 2 +- .../instances-index/mock-instances-index.ts | 38 ------------ shared/extra-utils/miscs/sql.ts | 13 +++- .../mock-servers/joinpeertube-versions.ts | 31 ++++++++++ .../mock-servers/mock-instances-index.ts | 38 ++++++++++++ shared/extra-utils/users/user-notifications.ts | 72 +++++++++++++++++++--- 6 files changed, 145 insertions(+), 49 deletions(-) delete mode 100644 shared/extra-utils/instances-index/mock-instances-index.ts create mode 100644 shared/extra-utils/mock-servers/joinpeertube-versions.ts create mode 100644 shared/extra-utils/mock-servers/mock-instances-index.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts index 5c95a1b3e..898a92d43 100644 --- a/shared/extra-utils/index.ts +++ b/shared/extra-utils/index.ts @@ -1,7 +1,7 @@ export * from './bulk/bulk' export * from './cli/cli' export * from './feeds/feeds' -export * from './instances-index/mock-instances-index' +export * from './mock-servers/mock-instances-index' export * from './miscs/miscs' export * from './miscs/sql' export * from './miscs/stubs' diff --git a/shared/extra-utils/instances-index/mock-instances-index.ts b/shared/extra-utils/instances-index/mock-instances-index.ts deleted file mode 100644 index 2604eda03..000000000 --- a/shared/extra-utils/instances-index/mock-instances-index.ts +++ /dev/null @@ -1,38 +0,0 @@ -import * as express from 'express' - -export class MockInstancesIndex { - private readonly indexInstances: { host: string, createdAt: string }[] = [] - - initialize () { - return new Promise(res => { - const app = express() - - app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { - if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) - - return next() - }) - - app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { - const since = req.query.since - - const filtered = this.indexInstances.filter(i => { - if (!since) return true - - return i.createdAt > since - }) - - return res.json({ - total: filtered.length, - data: filtered - }) - }) - - app.listen(42101, () => res()) - }) - } - - addInstance (host: string) { - this.indexInstances.push({ host, createdAt: new Date().toISOString() }) - } -} diff --git a/shared/extra-utils/miscs/sql.ts b/shared/extra-utils/miscs/sql.ts index 740f0c2d6..345e5bc16 100644 --- a/shared/extra-utils/miscs/sql.ts +++ b/shared/extra-utils/miscs/sql.ts @@ -106,12 +106,20 @@ async function closeAllSequelize (servers: ServerInfo[]) { } } -function setPluginVersion (internalServerNumber: number, pluginName: string, newVersion: string) { +function setPluginField (internalServerNumber: number, pluginName: string, field: string, value: string) { const seq = getSequelize(internalServerNumber) const options = { type: QueryTypes.UPDATE } - return seq.query(`UPDATE "plugin" SET "version" = '${newVersion}' WHERE "name" = '${pluginName}'`, options) + return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options) +} + +function setPluginVersion (internalServerNumber: number, pluginName: string, newVersion: string) { + return setPluginField(internalServerNumber, pluginName, 'version', newVersion) +} + +function setPluginLatestVersion (internalServerNumber: number, pluginName: string, newVersion: string) { + return setPluginField(internalServerNumber, pluginName, 'latestVersion', newVersion) } function setActorFollowScores (internalServerNumber: number, newScore: number) { @@ -128,6 +136,7 @@ export { setActorField, countVideoViewsOf, setPluginVersion, + setPluginLatestVersion, selectQuery, deleteAll, updateQuery, diff --git a/shared/extra-utils/mock-servers/joinpeertube-versions.ts b/shared/extra-utils/mock-servers/joinpeertube-versions.ts new file mode 100644 index 000000000..d7d5b2c49 --- /dev/null +++ b/shared/extra-utils/mock-servers/joinpeertube-versions.ts @@ -0,0 +1,31 @@ +import * as express from 'express' + +export class MockJoinPeerTubeVersions { + private latestVersion: string + + initialize () { + return new Promise(res => { + const app = express() + + app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) + + return next() + }) + + app.get('/versions.json', (req: express.Request, res: express.Response) => { + return res.json({ + peertube: { + latestVersion: this.latestVersion + } + }) + }) + + app.listen(42102, () => res()) + }) + } + + setLatestVersion (latestVersion: string) { + this.latestVersion = latestVersion + } +} diff --git a/shared/extra-utils/mock-servers/mock-instances-index.ts b/shared/extra-utils/mock-servers/mock-instances-index.ts new file mode 100644 index 000000000..2604eda03 --- /dev/null +++ b/shared/extra-utils/mock-servers/mock-instances-index.ts @@ -0,0 +1,38 @@ +import * as express from 'express' + +export class MockInstancesIndex { + private readonly indexInstances: { host: string, createdAt: string }[] = [] + + initialize () { + return new Promise(res => { + const app = express() + + app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => { + if (process.env.DEBUG) console.log('Receiving request on mocked server %s.', req.url) + + return next() + }) + + app.get('/api/v1/instances/hosts', (req: express.Request, res: express.Response) => { + const since = req.query.since + + const filtered = this.indexInstances.filter(i => { + if (!since) return true + + return i.createdAt > since + }) + + return res.json({ + total: filtered.length, + data: filtered + }) + }) + + app.listen(42101, () => res()) + }) + } + + addInstance (host: string) { + this.indexInstances.push({ host, createdAt: new Date().toISOString() }) + } +} diff --git a/shared/extra-utils/users/user-notifications.ts b/shared/extra-utils/users/user-notifications.ts index 467a3d959..249e82925 100644 --- a/shared/extra-utils/users/user-notifications.ts +++ b/shared/extra-utils/users/user-notifications.ts @@ -2,7 +2,8 @@ import { expect } from 'chai' import { inspect } from 'util' -import { AbuseState } from '@shared/models' +import { AbuseState, PluginType } from '@shared/models' +import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users' import { MockSmtpServer } from '../miscs/email' import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' @@ -11,7 +12,6 @@ import { flushAndRunMultipleServers, ServerInfo } from '../server/servers' import { getUserNotificationSocket } from '../socket/socket-io' import { setAccessTokensToServers, userLogin } from './login' import { createUser, getMyUserInformation } from './users' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function updateMyNotificationSettings ( url: string, @@ -629,7 +629,59 @@ async function checkNewBlacklistOnMyVideo ( await checkNotification(base, notificationChecker, emailNotificationFinder, 'presence') } -function getAllNotificationsSettings () { +async function checkNewPeerTubeVersion (base: CheckerBaseParams, latestVersion: string, type: CheckerType) { + const notificationType = UserNotificationType.NEW_PEERTUBE_VERSION + + function notificationChecker (notification: UserNotification, type: CheckerType) { + if (type === 'presence') { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + + expect(notification.peertube).to.exist + expect(notification.peertube.latestVersion).to.equal(latestVersion) + } else { + expect(notification).to.satisfy((n: UserNotification) => { + return n === undefined || n.peertube === undefined || n.peertube.latestVersion !== latestVersion + }) + } + } + + function emailNotificationFinder (email: object) { + const text = email['text'] + + return text.includes(latestVersion) + } + + await checkNotification(base, notificationChecker, emailNotificationFinder, type) +} + +async function checkNewPluginVersion (base: CheckerBaseParams, pluginType: PluginType, pluginName: string, type: CheckerType) { + const notificationType = UserNotificationType.NEW_PLUGIN_VERSION + + function notificationChecker (notification: UserNotification, type: CheckerType) { + if (type === 'presence') { + expect(notification).to.not.be.undefined + expect(notification.type).to.equal(notificationType) + + expect(notification.plugin.name).to.equal(pluginName) + expect(notification.plugin.type).to.equal(pluginType) + } else { + expect(notification).to.satisfy((n: UserNotification) => { + return n === undefined || n.plugin === undefined || n.plugin.name !== pluginName + }) + } + } + + function emailNotificationFinder (email: object) { + const text = email['text'] + + return text.includes(pluginName) + } + + await checkNotification(base, notificationChecker, emailNotificationFinder, type) +} + +function getAllNotificationsSettings (): UserNotificationSetting { return { newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, @@ -644,11 +696,13 @@ function getAllNotificationsSettings () { newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, abuseNewMessage: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, abuseStateChange: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - } as UserNotificationSetting + autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newPeerTubeVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newPluginVersion: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + } } -async function prepareNotificationsTest (serversCount = 3) { +async function prepareNotificationsTest (serversCount = 3, overrideConfigArg: any = {}) { const userNotifications: UserNotification[] = [] const adminNotifications: UserNotification[] = [] const adminNotificationsServer2: UserNotification[] = [] @@ -665,7 +719,7 @@ async function prepareNotificationsTest (serversCount = 3) { limit: 20 } } - const servers = await flushAndRunMultipleServers(serversCount, overrideConfig) + const servers = await flushAndRunMultipleServers(serversCount, Object.assign(overrideConfig, overrideConfigArg)) await setAccessTokensToServers(servers) @@ -749,5 +803,7 @@ export { checkNewInstanceFollower, prepareNotificationsTest, checkNewCommentAbuseForModerators, - checkNewAccountAbuseForModerators + checkNewAccountAbuseForModerators, + checkNewPeerTubeVersion, + checkNewPluginVersion } -- cgit v1.2.3