PeerTube/server/tests/api/notifications/notifications-api.ts

202 lines
6.4 KiB
TypeScript
Raw Normal View History

2020-06-16 08:52:05 -05:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
import 'mocha'
import * as chai from 'chai'
import {
CheckerBaseParams,
checkNewVideoFromSubscription,
2021-07-07 09:40:49 -05:00
cleanupTests,
2020-06-16 08:52:05 -05:00
getAllNotificationsSettings,
2021-07-07 09:40:49 -05:00
MockSmtpServer,
2020-06-16 08:52:05 -05:00
prepareNotificationsTest,
2021-07-07 09:40:49 -05:00
ServerInfo,
waitJobs
} from '@shared/extra-utils'
2021-07-13 07:23:01 -05:00
import { UserNotification, UserNotificationSettingValue } from '@shared/models'
2020-06-16 08:52:05 -05:00
const expect = chai.expect
describe('Test notifications API', function () {
let server: ServerInfo
let userNotifications: UserNotification[] = []
2021-07-09 09:23:01 -05:00
let userToken: string
2020-06-16 08:52:05 -05:00
let emails: object[] = []
before(async function () {
this.timeout(120000)
const res = await prepareNotificationsTest(1)
emails = res.emails
2021-07-09 09:23:01 -05:00
userToken = res.userAccessToken
2020-06-16 08:52:05 -05:00
userNotifications = res.userNotifications
server = res.servers[0]
2021-07-09 09:23:01 -05:00
await server.subscriptionsCommand.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
2020-06-16 08:52:05 -05:00
for (let i = 0; i < 10; i++) {
2021-07-15 03:02:54 -05:00
await server.videosCommand.randomUpload({ wait: false })
2020-06-16 08:52:05 -05:00
}
await waitJobs([ server ])
})
describe('Mark as read', function () {
it('Should mark as read some notifications', async function () {
2021-07-09 09:23:01 -05:00
const { data } = await server.notificationsCommand.list({ token: userToken, start: 2, count: 3 })
const ids = data.map(n => n.id)
2020-06-16 08:52:05 -05:00
2021-07-09 09:23:01 -05:00
await server.notificationsCommand.markAsRead({ token: userToken, ids })
2020-06-16 08:52:05 -05:00
})
it('Should have the notifications marked as read', async function () {
2021-07-09 09:23:01 -05:00
const { data } = await server.notificationsCommand.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
2020-06-16 08:52:05 -05:00
})
it('Should only list read notifications', async function () {
2021-07-09 09:23:01 -05:00
const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: false })
2020-06-16 08:52:05 -05:00
2021-07-09 09:23:01 -05:00
for (const notification of data) {
2020-06-16 08:52:05 -05:00
expect(notification.read).to.be.true
}
})
it('Should only list unread notifications', async function () {
2021-07-09 09:23:01 -05:00
const { data } = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
2020-06-16 08:52:05 -05:00
2021-07-09 09:23:01 -05:00
for (const notification of data) {
2020-06-16 08:52:05 -05:00
expect(notification.read).to.be.false
}
})
it('Should mark as read all notifications', async function () {
2021-07-09 09:23:01 -05:00
await server.notificationsCommand.markAsReadAll({ token: userToken })
2020-06-16 08:52:05 -05:00
2021-07-09 09:23:01 -05:00
const body = await server.notificationsCommand.list({ token: userToken, start: 0, count: 10, unread: true })
2020-06-16 08:52:05 -05:00
2021-07-09 09:23:01 -05:00
expect(body.total).to.equal(0)
expect(body.data).to.have.lengthOf(0)
2020-06-16 08:52:05 -05:00
})
})
describe('Notification settings', function () {
let baseParams: CheckerBaseParams
before(() => {
baseParams = {
server: server,
emails,
socketNotifications: userNotifications,
2021-07-09 09:23:01 -05:00
token: userToken
2020-06-16 08:52:05 -05:00
}
})
it('Should not have notifications', async function () {
this.timeout(20000)
2021-07-09 09:23:01 -05:00
await server.notificationsCommand.updateMySettings({
token: userToken,
settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
})
2020-06-16 08:52:05 -05:00
{
2021-07-13 07:23:01 -05:00
const info = await server.usersCommand.getMyInfo({ token: userToken })
2020-06-16 08:52:05 -05:00
expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
}
2021-07-15 03:02:54 -05:00
const { name, uuid } = await server.videosCommand.randomUpload()
2020-06-16 08:52:05 -05:00
const check = { web: true, mail: true }
2021-07-13 07:23:01 -05:00
await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
2020-06-16 08:52:05 -05:00
})
it('Should only have web notifications', async function () {
this.timeout(20000)
2021-07-09 09:23:01 -05:00
await server.notificationsCommand.updateMySettings({
token: userToken,
settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
})
2020-06-16 08:52:05 -05:00
{
2021-07-13 07:23:01 -05:00
const info = await server.usersCommand.getMyInfo({ token: userToken })
2020-06-16 08:52:05 -05:00
expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
}
2021-07-15 03:02:54 -05:00
const { name, uuid } = await server.videosCommand.randomUpload()
2020-06-16 08:52:05 -05:00
{
const check = { mail: true, web: false }
2021-07-13 07:23:01 -05:00
await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
2020-06-16 08:52:05 -05:00
}
{
const check = { mail: false, web: true }
2021-07-13 07:23:01 -05:00
await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
2020-06-16 08:52:05 -05:00
}
})
it('Should only have mail notifications', async function () {
this.timeout(20000)
2021-07-09 09:23:01 -05:00
await server.notificationsCommand.updateMySettings({
token: userToken,
settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
})
2020-06-16 08:52:05 -05:00
{
2021-07-13 07:23:01 -05:00
const info = await server.usersCommand.getMyInfo({ token: userToken })
2020-06-16 08:52:05 -05:00
expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
}
2021-07-15 03:02:54 -05:00
const { name, uuid } = await server.videosCommand.randomUpload()
2020-06-16 08:52:05 -05:00
{
const check = { mail: false, web: true }
2021-07-13 07:23:01 -05:00
await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
2020-06-16 08:52:05 -05:00
}
{
const check = { mail: true, web: false }
2021-07-13 07:23:01 -05:00
await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
2020-06-16 08:52:05 -05:00
}
})
it('Should have email and web notifications', async function () {
this.timeout(20000)
2021-07-09 09:23:01 -05:00
await server.notificationsCommand.updateMySettings({
token: userToken,
settings: {
...getAllNotificationsSettings(),
newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
}
})
2020-06-16 08:52:05 -05:00
{
2021-07-13 07:23:01 -05:00
const info = await server.usersCommand.getMyInfo({ token: userToken })
2020-06-16 08:52:05 -05:00
expect(info.notificationSettings.newVideoFromSubscription).to.equal(
UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
)
}
2021-07-15 03:02:54 -05:00
const { name, uuid } = await server.videosCommand.randomUpload()
2020-06-16 08:52:05 -05:00
await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
})
})
after(async function () {
MockSmtpServer.Instance.kill()
await cleanupTests([ server ])
})
})