aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/notifications/notifications-api.ts
blob: ac08449f898cf58ed703f9404be93dec5a4c1cc7 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import {
  CheckerBaseParams,
  checkNewVideoFromSubscription,
  getAllNotificationsSettings,
  MockSmtpServer,
  prepareNotificationsTest
} from '@server/tests/shared'
import { UserNotification, UserNotificationSettingValue } from '@shared/models'
import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands'

const expect = chai.expect

describe('Test notifications API', function () {
  let server: PeerTubeServer
  let userNotifications: UserNotification[] = []
  let userToken: string
  let emails: object[] = []

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

    const res = await prepareNotificationsTest(1)
    emails = res.emails
    userToken = res.userAccessToken
    userNotifications = res.userNotifications
    server = res.servers[0]

    await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })

    for (let i = 0; i < 10; i++) {
      await server.videos.randomUpload({ wait: false })
    }

    await waitJobs([ server ])
  })

  describe('Mark as read', function () {

    it('Should mark as read some notifications', async function () {
      const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
      const ids = data.map(n => n.id)

      await server.notifications.markAsRead({ token: userToken, ids })
    })

    it('Should have the notifications marked as read', async function () {
      const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })

      expect(data[0].read).to.be.false
      expect(data[1].read).to.be.false
      expect(data[2].read).to.be.true
      expect(data[3].read).to.be.true
      expect(data[4].read).to.be.true
      expect(data[5].read).to.be.false
    })

    it('Should only list read notifications', async function () {
      const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })

      for (const notification of data) {
        expect(notification.read).to.be.true
      }
    })

    it('Should only list unread notifications', async function () {
      const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })

      for (const notification of data) {
        expect(notification.read).to.be.false
      }
    })

    it('Should mark as read all notifications', async function () {
      await server.notifications.markAsReadAll({ token: userToken })

      const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })

      expect(body.total).to.equal(0)
      expect(body.data).to.have.lengthOf(0)
    })
  })

  describe('Notification settings', function () {
    let baseParams: CheckerBaseParams

    before(() => {
      baseParams = {
        server: server,
        emails,
        socketNotifications: userNotifications,
        token: userToken
      }
    })

    it('Should not have notifications', async function () {
      this.timeout(20000)

      await server.notifications.updateMySettings({
        token: userToken,
        settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
      })

      {
        const info = await server.users.getMyInfo({ token: userToken })
        expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
      }

      const { name, shortUUID } = await server.videos.randomUpload()

      const check = { web: true, mail: true }
      await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
    })

    it('Should only have web notifications', async function () {
      this.timeout(20000)

      await server.notifications.updateMySettings({
        token: userToken,
        settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
      })

      {
        const info = await server.users.getMyInfo({ token: userToken })
        expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
      }

      const { name, shortUUID } = await server.videos.randomUpload()

      {
        const check = { mail: true, web: false }
        await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
      }

      {
        const check = { mail: false, web: true }
        await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
      }
    })

    it('Should only have mail notifications', async function () {
      this.timeout(20000)

      await server.notifications.updateMySettings({
        token: userToken,
        settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
      })

      {
        const info = await server.users.getMyInfo({ token: userToken })
        expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
      }

      const { name, shortUUID } = await server.videos.randomUpload()

      {
        const check = { mail: false, web: true }
        await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' })
      }

      {
        const check = { mail: true, web: false }
        await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' })
      }
    })

    it('Should have email and web notifications', async function () {
      this.timeout(20000)

      await server.notifications.updateMySettings({
        token: userToken,
        settings: {
          ...getAllNotificationsSettings(),
          newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
        }
      })

      {
        const info = await server.users.getMyInfo({ token: userToken })
        expect(info.notificationSettings.newVideoFromSubscription).to.equal(
          UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
        )
      }

      const { name, shortUUID } = await server.videos.randomUpload()

      await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' })
    })
  })

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

    await cleanupTests([ server ])
  })
})