aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/notifications/admin-notifications.ts
blob: 4824542c97e3f855a0e11f99cb6e62d668043f71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import {
  CheckerBaseParams,
  checkNewPeerTubeVersion,
  checkNewPluginVersion,
  MockJoinPeerTubeVersions,
  MockSmtpServer,
  prepareNotificationsTest,
  SQLCommand
} from '@server/tests/shared'
import { wait } from '@shared/core-utils'
import { PluginType, UserNotification, UserNotificationType } from '@shared/models'
import { cleanupTests, PeerTubeServer } from '@shared/server-commands'

describe('Test admin notifications', function () {
  let server: PeerTubeServer
  let sqlCommand: SQLCommand
  let userNotifications: UserNotification[] = []
  let adminNotifications: UserNotification[] = []
  let emails: object[] = []
  let baseParams: CheckerBaseParams
  let joinPeerTubeServer: MockJoinPeerTubeVersions

  before(async function () {
    this.timeout(120000)

    joinPeerTubeServer = new MockJoinPeerTubeVersions()
    const port = await joinPeerTubeServer.initialize()

    const config = {
      peertube: {
        check_latest_version: {
          enabled: true,
          url: `http://127.0.0.1:${port}/versions.json`
        }
      },
      plugins: {
        index: {
          enabled: true,
          check_latest_versions_interval: '3 seconds'
        }
      }
    }

    const res = await prepareNotificationsTest(1, config)
    emails = res.emails
    server = res.servers[0]

    userNotifications = res.userNotifications
    adminNotifications = res.adminNotifications

    baseParams = {
      server,
      emails,
      socketNotifications: adminNotifications,
      token: server.accessToken
    }

    await server.plugins.install({ npmName: 'peertube-plugin-hello-world' })
    await server.plugins.install({ npmName: 'peertube-theme-background-red' })

    sqlCommand = new SQLCommand(server)
  })

  describe('Latest PeerTube version notification', function () {

    it('Should not send a notification to admins if there is no new version', async function () {
      this.timeout(30000)

      joinPeerTubeServer.setLatestVersion('1.4.2')

      await wait(3000)
      await checkNewPeerTubeVersion({ ...baseParams, latestVersion: '1.4.2', checkType: 'absence' })
    })

    it('Should send a notification to admins on new version', async function () {
      this.timeout(30000)

      joinPeerTubeServer.setLatestVersion('15.4.2')

      await wait(3000)
      await checkNewPeerTubeVersion({ ...baseParams, latestVersion: '15.4.2', checkType: 'presence' })
    })

    it('Should not send the same notification to admins', async function () {
      this.timeout(30000)

      await wait(3000)
      expect(adminNotifications.filter(n => n.type === UserNotificationType.NEW_PEERTUBE_VERSION)).to.have.lengthOf(1)
    })

    it('Should not have sent a notification to users', async function () {
      this.timeout(30000)

      expect(userNotifications.filter(n => n.type === UserNotificationType.NEW_PEERTUBE_VERSION)).to.have.lengthOf(0)
    })

    it('Should send a new notification after a new release', async function () {
      this.timeout(30000)

      joinPeerTubeServer.setLatestVersion('15.4.3')

      await wait(3000)
      await checkNewPeerTubeVersion({ ...baseParams, latestVersion: '15.4.3', checkType: 'presence' })
      expect(adminNotifications.filter(n => n.type === UserNotificationType.NEW_PEERTUBE_VERSION)).to.have.lengthOf(2)
    })
  })

  describe('Latest plugin version notification', function () {

    it('Should not send a notification to admins if there is no new plugin version', async function () {
      this.timeout(30000)

      await wait(6000)
      await checkNewPluginVersion({ ...baseParams, pluginType: PluginType.PLUGIN, pluginName: 'hello-world', checkType: 'absence' })
    })

    it('Should send a notification to admins on new plugin version', async function () {
      this.timeout(30000)

      await sqlCommand.setPluginVersion('hello-world', '0.0.1')
      await sqlCommand.setPluginLatestVersion('hello-world', '0.0.1')
      await wait(6000)

      await checkNewPluginVersion({ ...baseParams, pluginType: PluginType.PLUGIN, pluginName: 'hello-world', checkType: 'presence' })
    })

    it('Should not send the same notification to admins', async function () {
      this.timeout(30000)

      await wait(6000)

      expect(adminNotifications.filter(n => n.type === UserNotificationType.NEW_PLUGIN_VERSION)).to.have.lengthOf(1)
    })

    it('Should not have sent a notification to users', async function () {
      expect(userNotifications.filter(n => n.type === UserNotificationType.NEW_PLUGIN_VERSION)).to.have.lengthOf(0)
    })

    it('Should send a new notification after a new plugin release', async function () {
      this.timeout(30000)

      await sqlCommand.setPluginVersion('hello-world', '0.0.1')
      await sqlCommand.setPluginLatestVersion('hello-world', '0.0.1')
      await wait(6000)

      expect(adminNotifications.filter(n => n.type === UserNotificationType.NEW_PEERTUBE_VERSION)).to.have.lengthOf(2)
    })
  })

  after(async function () {
    MockSmtpServer.Instance.kill()

    await sqlCommand.cleanup()
    await cleanupTests([ server ])
  })
})