1
0
Fork 0
peertube/server/tests/api/notifications/notifications-api.ts

207 lines
6.7 KiB
TypeScript
Raw Normal View History

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