X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fnotifications%2Fnotifications-api.ts;h=f194656c9f8b1eb684812e5f7b53489dd486b340;hb=aac7f4304dc7fa48869cb3f09c30383222add436;hp=b819954494a36461060ac85c280e37b3536a098a;hpb=8eb07b01306429abe5c538ff7aa0a16e44fff26f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/notifications/notifications-api.ts b/server/tests/api/notifications/notifications-api.ts index b81995449..f194656c9 100644 --- a/server/tests/api/notifications/notifications-api.ts +++ b/server/tests/api/notifications/notifications-api.ts @@ -1,29 +1,20 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import 'mocha' -import * as chai from 'chai' -import { addUserSubscription } from '@shared/extra-utils/users/user-subscriptions' -import { cleanupTests, getMyUserInformation, immutableAssign, uploadRandomVideo, waitJobs } from '../../../../shared/extra-utils' -import { ServerInfo } from '../../../../shared/extra-utils/index' -import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' +import { expect } from 'chai' import { CheckerBaseParams, checkNewVideoFromSubscription, getAllNotificationsSettings, - getUserNotifications, - markAsReadAllNotifications, - markAsReadNotifications, - prepareNotificationsTest, - updateMyNotificationSettings -} from '../../../../shared/extra-utils/users/user-notifications' -import { User, UserNotification, UserNotificationSettingValue } from '../../../../shared/models/users' - -const expect = chai.expect + MockSmtpServer, + prepareNotificationsTest +} from '@server/tests/shared' +import { UserNotification, UserNotificationSettingValue } from '@shared/models' +import { cleanupTests, PeerTubeServer, waitJobs } from '@shared/server-commands' describe('Test notifications API', function () { - let server: ServerInfo + let server: PeerTubeServer let userNotifications: UserNotification[] = [] - let userAccessToken: string + let userToken: string let emails: object[] = [] before(async function () { @@ -31,65 +22,72 @@ describe('Test notifications API', function () { const res = await prepareNotificationsTest(1) emails = res.emails - userAccessToken = res.userAccessToken + userToken = res.userAccessToken userNotifications = res.userNotifications server = res.servers[0] - await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port) + await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port }) for (let i = 0; i < 10; i++) { - await uploadRandomVideo(server, false) + await server.videos.randomUpload({ wait: false }) } 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) + }) + }) + describe('Mark as read', function () { it('Should mark as read some notifications', async function () { - const res = await getUserNotifications(server.url, userAccessToken, 2, 3) - const ids = res.body.data.map(n => n.id) + const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 }) + const ids = data.map(n => n.id) - await markAsReadNotifications(server.url, userAccessToken, ids) + await server.notifications.markAsRead({ token: userToken, ids }) }) it('Should have the notifications marked as read', async function () { - const res = await getUserNotifications(server.url, userAccessToken, 0, 10) - - const notifications = res.body.data as UserNotification[] - expect(notifications[0].read).to.be.false - expect(notifications[1].read).to.be.false - expect(notifications[2].read).to.be.true - expect(notifications[3].read).to.be.true - expect(notifications[4].read).to.be.true - expect(notifications[5].read).to.be.false + 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 res = await getUserNotifications(server.url, userAccessToken, 0, 10, false) + const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false }) - const notifications = res.body.data as UserNotification[] - for (const notification of notifications) { + for (const notification of data) { expect(notification.read).to.be.true } }) it('Should only list unread notifications', async function () { - const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) + const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true }) - const notifications = res.body.data as UserNotification[] - for (const notification of notifications) { + for (const notification of data) { expect(notification.read).to.be.false } }) it('Should mark as read all notifications', async function () { - await markAsReadAllNotifications(server.url, userAccessToken) + await server.notifications.markAsReadAll({ token: userToken }) - const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) + const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true }) - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) + expect(body.total).to.equal(0) + expect(body.data).to.have.lengthOf(0) }) }) @@ -98,102 +96,105 @@ describe('Test notifications API', function () { before(() => { baseParams = { - server: server, + server, emails, socketNotifications: userNotifications, - token: userAccessToken + token: userToken } }) it('Should not have notifications', async function () { this.timeout(20000) - await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { - newVideoFromSubscription: UserNotificationSettingValue.NONE - })) + await server.notifications.updateMySettings({ + token: userToken, + settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE } + }) { - const res = await getMyUserInformation(server.url, userAccessToken) - const info = res.body as User + const info = await server.users.getMyInfo({ token: userToken }) expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) } - const { name, uuid } = await uploadRandomVideo(server) + const { name, shortUUID } = await server.videos.randomUpload() const check = { web: true, mail: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' }) }) it('Should only have web notifications', async function () { this.timeout(20000) - await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { - newVideoFromSubscription: UserNotificationSettingValue.WEB - })) + await server.notifications.updateMySettings({ + token: userToken, + settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB } + }) { - const res = await getMyUserInformation(server.url, userAccessToken) - const info = res.body as User + const info = await server.users.getMyInfo({ token: userToken }) expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) } - const { name, uuid } = await uploadRandomVideo(server) + const { name, shortUUID } = await server.videos.randomUpload() { const check = { mail: true, web: false } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' }) } { const check = { mail: false, web: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') + await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' }) } }) it('Should only have mail notifications', async function () { this.timeout(20000) - await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { - newVideoFromSubscription: UserNotificationSettingValue.EMAIL - })) + await server.notifications.updateMySettings({ + token: userToken, + settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL } + }) { - const res = await getMyUserInformation(server.url, userAccessToken) - const info = res.body as User + const info = await server.users.getMyInfo({ token: userToken }) expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) } - const { name, uuid } = await uploadRandomVideo(server) + const { name, shortUUID } = await server.videos.randomUpload() { const check = { mail: false, web: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'absence' }) } { const check = { mail: true, web: false } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') + await checkNewVideoFromSubscription({ ...baseParams, check, videoName: name, shortUUID, checkType: 'presence' }) } }) it('Should have email and web notifications', async function () { this.timeout(20000) - await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { - newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - })) + await server.notifications.updateMySettings({ + token: userToken, + settings: { + ...getAllNotificationsSettings(), + newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + } + }) { - const res = await getMyUserInformation(server.url, userAccessToken) - const info = res.body as User + const info = await server.users.getMyInfo({ token: userToken }) expect(info.notificationSettings.newVideoFromSubscription).to.equal( UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL ) } - const { name, uuid } = await uploadRandomVideo(server) + const { name, shortUUID } = await server.videos.randomUpload() - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + await checkNewVideoFromSubscription({ ...baseParams, videoName: name, shortUUID, checkType: 'presence' }) }) })