From: Chocobozzz Date: Tue, 16 Jun 2020 13:52:05 +0000 (+0200) Subject: Split notification tests X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8eb07b01306429abe5c538ff7aa0a16e44fff26f;p=oweals%2Fpeertube.git Split notification tests --- diff --git a/server/tests/api/notifications/comments-notifications.ts b/server/tests/api/notifications/comments-notifications.ts new file mode 100644 index 000000000..cd3ed2f70 --- /dev/null +++ b/server/tests/api/notifications/comments-notifications.ts @@ -0,0 +1,309 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import * as chai from 'chai' +import { cleanupTests, getVideoCommentThreads, getVideoThreadComments, updateMyUser, wait } from '../../../../shared/extra-utils' +import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index' +import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' +import { waitJobs } from '../../../../shared/extra-utils/server/jobs' +import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/extra-utils/users/blocklist' +import { + checkCommentMention, + CheckerBaseParams, + checkNewCommentOnMyVideo, + prepareNotificationsTest +} from '../../../../shared/extra-utils/users/user-notifications' +import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments' +import { UserNotification } from '../../../../shared/models/users' +import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' + +const expect = chai.expect + +describe('Test comments notifications', function () { + let servers: ServerInfo[] = [] + let userAccessToken: string + let userNotifications: UserNotification[] = [] + let emails: object[] = [] + + before(async function () { + this.timeout(120000) + + const res = await prepareNotificationsTest(2) + emails = res.emails + userAccessToken = res.userAccessToken + servers = res.servers + userNotifications = res.userNotifications + }) + + describe('Comment on my video notifications', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should not send a new comment notification after a comment on another video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') + }) + + it('Should not send a new comment notification if I comment my own video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') + }) + + it('Should not send a new comment notification if the account is muted', async function () { + this.timeout(10000) + + await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root') + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') + + await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root') + }) + + it('Should send a new comment notification after a local comment on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') + }) + + it('Should send a new comment notification after a remote comment on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + + await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') + + await waitJobs(servers) + + const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) + expect(resComment.body.data).to.have.lengthOf(1) + const commentId = resComment.body.data[0].id + + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') + }) + + it('Should send a new comment notification after a local reply on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') + const threadId = resThread.body.comment.id + + const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply') + const commentId = resComment.body.comment.id + + await wait(500) + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') + }) + + it('Should send a new comment notification after a remote reply on my video', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + await waitJobs(servers) + + { + const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') + const threadId = resThread.body.comment.id + await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply') + } + + await waitJobs(servers) + + const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) + expect(resThread.body.data).to.have.lengthOf(1) + const threadId = resThread.body.data[0].id + + const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId) + const tree = resComments.body as VideoCommentThreadTree + + expect(tree.children).to.have.lengthOf(1) + const commentId = tree.children[0].comment.id + + await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') + }) + }) + + describe('Mention notifications', function () { + let baseParams: CheckerBaseParams + + before(async () => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + + await updateMyUser({ + url: servers[0].url, + accessToken: servers[0].accessToken, + displayName: 'super root name' + }) + + await updateMyUser({ + url: servers[1].url, + accessToken: servers[1].accessToken, + displayName: 'super root 2 name' + }) + }) + + it('Should not send a new mention comment notification if I mention the video owner', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') + }) + + it('Should not send a new mention comment notification if I mention myself', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') + }) + + it('Should not send a new mention notification if the account is muted', async function () { + this.timeout(10000) + + await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root') + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') + + await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root') + }) + + it('Should not send a new mention notification if the remote account mention a local account', async function () { + this.timeout(20000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello') + const threadId = resThread.body.comment.id + + await waitJobs(servers) + await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence') + }) + + it('Should send a new mention notification after local comments', async function () { + this.timeout(10000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') + const threadId = resThread.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence') + + const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1') + const commentId = resComment.body.comment.id + + await wait(500) + await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence') + }) + + it('Should send a new mention notification after remote comments', async function () { + this.timeout(20000) + + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + + const text1 = `hello @user_1@localhost:${servers[0].port} 1` + const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, text1) + const server2ThreadId = resThread.body.comment.id + + await waitJobs(servers) + + const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) + expect(resThread2.body.data).to.have.lengthOf(1) + const server1ThreadId = resThread2.body.data[0].id + await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence') + + const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}` + await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text2) + + await waitJobs(servers) + + const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId) + const tree = resComments.body as VideoCommentThreadTree + + expect(tree.children).to.have.lengthOf(1) + const commentId = tree.children[0].comment.id + + await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence') + }) + }) + + after(async function () { + MockSmtpServer.Instance.kill() + + await cleanupTests(servers) + }) +}) diff --git a/server/tests/api/notifications/index.ts b/server/tests/api/notifications/index.ts index b573f850e..bd07a339e 100644 --- a/server/tests/api/notifications/index.ts +++ b/server/tests/api/notifications/index.ts @@ -1 +1,4 @@ +import './comments-notifications' +import './moderation-notifications' +import './notifications-api' import './user-notifications' diff --git a/server/tests/api/notifications/moderation-notifications.ts b/server/tests/api/notifications/moderation-notifications.ts new file mode 100644 index 000000000..b90732a7a --- /dev/null +++ b/server/tests/api/notifications/moderation-notifications.ts @@ -0,0 +1,439 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import { v4 as uuidv4 } from 'uuid' +import { + addVideoToBlacklist, + cleanupTests, + follow, + getCustomConfig, + immutableAssign, + MockInstancesIndex, + registerUser, + removeVideoFromBlacklist, + reportVideoAbuse, + unfollow, + updateCustomConfig, + updateCustomSubConfig, + wait +} from '../../../../shared/extra-utils' +import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index' +import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' +import { waitJobs } from '../../../../shared/extra-utils/server/jobs' +import { + checkAutoInstanceFollowing, + CheckerBaseParams, + checkNewBlacklistOnMyVideo, + checkNewInstanceFollower, + checkNewVideoAbuseForModerators, + checkNewVideoFromSubscription, + checkUserRegistered, + checkVideoAutoBlacklistForModerators, + checkVideoIsPublished, + prepareNotificationsTest +} from '../../../../shared/extra-utils/users/user-notifications' +import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions' +import { CustomConfig } from '../../../../shared/models/server' +import { UserNotification } from '../../../../shared/models/users' +import { VideoPrivacy } from '../../../../shared/models/videos' + +describe('Test moderation notifications', function () { + let servers: ServerInfo[] = [] + let userAccessToken: string + let userNotifications: UserNotification[] = [] + let adminNotifications: UserNotification[] = [] + let adminNotificationsServer2: UserNotification[] = [] + let emails: object[] = [] + + before(async function () { + this.timeout(120000) + + const res = await prepareNotificationsTest(3) + emails = res.emails + userAccessToken = res.userAccessToken + servers = res.servers + userNotifications = res.userNotifications + adminNotifications = res.adminNotifications + adminNotificationsServer2 = res.adminNotificationsServer2 + }) + + describe('Video abuse for moderators notification', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + }) + + it('Should send a notification to moderators on local video abuse', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason') + + await waitJobs(servers) + await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') + }) + + it('Should send a notification to moderators on remote video abuse', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + + await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason') + + await waitJobs(servers) + await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') + }) + }) + + describe('Video blacklist on my video', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should send a notification to video owner on blacklist', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) + + await waitJobs(servers) + await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist') + }) + + it('Should send a notification to video owner on unblacklist', async function () { + this.timeout(10000) + + const name = 'video for abuse ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) + const uuid = resVideo.body.video.uuid + + await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) + + await waitJobs(servers) + await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) + await waitJobs(servers) + + await wait(500) + await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist') + }) + }) + + describe('New registration', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + }) + + it('Should send a notification only to moderators when a user registers on the instance', async function () { + this.timeout(10000) + + await registerUser(servers[0].url, 'user_45', 'password') + + await waitJobs(servers) + + await checkUserRegistered(baseParams, 'user_45', 'presence') + + const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } + await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence') + }) + }) + + describe('New instance follows', function () { + const instanceIndexServer = new MockInstancesIndex() + const config = { + followings: { + instance: { + autoFollowIndex: { + indexUrl: 'http://localhost:42101/api/v1/instances/hosts', + enabled: true + } + } + } + } + let baseParams: CheckerBaseParams + + before(async () => { + baseParams = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + + await instanceIndexServer.initialize() + instanceIndexServer.addInstance(servers[1].host) + }) + + it('Should send a notification only to admin when there is a new instance follower', async function () { + this.timeout(20000) + + await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) + + await waitJobs(servers) + + await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence') + + const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } + await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence') + }) + + it('Should send a notification on auto follow back', async function () { + this.timeout(40000) + + await unfollow(servers[2].url, servers[2].accessToken, servers[0]) + await waitJobs(servers) + + const config = { + followings: { + instance: { + autoFollowBack: { enabled: true } + } + } + } + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + + await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) + + await waitJobs(servers) + + const followerHost = servers[0].host + const followingHost = servers[2].host + await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') + + const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } + await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence') + + config.followings.instance.autoFollowBack.enabled = false + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + await unfollow(servers[0].url, servers[0].accessToken, servers[2]) + await unfollow(servers[2].url, servers[2].accessToken, servers[0]) + }) + + it('Should send a notification on auto instances index follow', async function () { + this.timeout(30000) + await unfollow(servers[0].url, servers[0].accessToken, servers[1]) + + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + + await wait(5000) + await waitJobs(servers) + + const followerHost = servers[0].host + const followingHost = servers[1].host + await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') + + config.followings.instance.autoFollowIndex.enabled = false + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + await unfollow(servers[0].url, servers[0].accessToken, servers[1]) + }) + }) + + describe('Video-related notifications when video auto-blacklist is enabled', function () { + let userBaseParams: CheckerBaseParams + let adminBaseParamsServer1: CheckerBaseParams + let adminBaseParamsServer2: CheckerBaseParams + let videoUUID: string + let videoName: string + let currentCustomConfig: CustomConfig + + before(async () => { + + adminBaseParamsServer1 = { + server: servers[0], + emails, + socketNotifications: adminNotifications, + token: servers[0].accessToken + } + + adminBaseParamsServer2 = { + server: servers[1], + emails, + socketNotifications: adminNotificationsServer2, + token: servers[1].accessToken + } + + userBaseParams = { + server: servers[0], + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + + const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken) + currentCustomConfig = resCustomConfig.body + const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, { + autoBlacklist: { + videos: { + ofUsers: { + enabled: true + } + } + } + }) + // enable transcoding otherwise own publish notification after transcoding not expected + autoBlacklistTestsCustomConfig.transcoding.enabled = true + await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig) + + await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) + await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) + + }) + + it('Should send notification to moderators on new video with auto-blacklist', async function () { + this.timeout(20000) + + videoName = 'video with auto-blacklist ' + uuidv4() + const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName }) + videoUUID = resVideo.body.video.uuid + + await waitJobs(servers) + await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence') + }) + + it('Should not send video publish notification if auto-blacklisted', async function () { + await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence') + }) + + it('Should not send a local user subscription notification if auto-blacklisted', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence') + }) + + it('Should not send a remote user subscription notification if auto-blacklisted', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence') + }) + + it('Should send video published and unblacklist after video unblacklisted', async function () { + this.timeout(20000) + + await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID) + + await waitJobs(servers) + + // FIXME: Can't test as two notifications sent to same user and util only checks last one + // One notification might be better anyways + // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist') + // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence') + }) + + it('Should send a local user subscription notification after removed from blacklist', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence') + }) + + it('Should send a remote user subscription notification after removed from blacklist', async function () { + await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence') + }) + + it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () { + this.timeout(20000) + + const updateAt = new Date(new Date().getTime() + 1000000) + + const name = 'video with auto-blacklist and future schedule ' + uuidv4() + + const data = { + name, + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) + const uuid = resVideo.body.video.uuid + + await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) + + await waitJobs(servers) + await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist') + + // FIXME: Can't test absence as two notifications sent to same user and util only checks last one + // One notification might be better anyways + // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') + + await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') + await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') + }) + + it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () { + this.timeout(20000) + + // In 2 seconds + const updateAt = new Date(new Date().getTime() + 2000) + + const name = 'video with schedule done and still auto-blacklisted ' + uuidv4() + + const data = { + name, + privacy: VideoPrivacy.PRIVATE, + scheduleUpdate: { + updateAt: updateAt.toISOString(), + privacy: VideoPrivacy.PUBLIC + } + } + + const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) + const uuid = resVideo.body.video.uuid + + await wait(6000) + await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') + await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') + await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') + }) + + it('Should not send a notification to moderators on new video without auto-blacklist', async function () { + this.timeout(20000) + + const name = 'video without auto-blacklist ' + uuidv4() + + // admin with blacklist right will not be auto-blacklisted + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name }) + const uuid = resVideo.body.video.uuid + + await waitJobs(servers) + await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence') + }) + + after(async () => { + await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig) + + await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) + await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) + }) + }) + + after(async function () { + MockSmtpServer.Instance.kill() + + await cleanupTests(servers) + }) +}) diff --git a/server/tests/api/notifications/notifications-api.ts b/server/tests/api/notifications/notifications-api.ts new file mode 100644 index 000000000..b81995449 --- /dev/null +++ b/server/tests/api/notifications/notifications-api.ts @@ -0,0 +1,205 @@ +/* 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 { + 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 + +describe('Test notifications API', function () { + let server: ServerInfo + let userNotifications: UserNotification[] = [] + let userAccessToken: string + let emails: object[] = [] + + before(async function () { + this.timeout(120000) + + const res = await prepareNotificationsTest(1) + emails = res.emails + userAccessToken = res.userAccessToken + userNotifications = res.userNotifications + server = res.servers[0] + + await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port) + + for (let i = 0; i < 10; i++) { + await uploadRandomVideo(server, false) + } + + await waitJobs([ server ]) + }) + + 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) + + await markAsReadNotifications(server.url, userAccessToken, 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 + }) + + it('Should only list read notifications', async function () { + const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false) + + const notifications = res.body.data as UserNotification[] + for (const notification of notifications) { + 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 notifications = res.body.data as UserNotification[] + for (const notification of notifications) { + expect(notification.read).to.be.false + } + }) + + it('Should mark as read all notifications', async function () { + await markAsReadAllNotifications(server.url, userAccessToken) + + const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) + + expect(res.body.total).to.equal(0) + expect(res.body.data).to.have.lengthOf(0) + }) + }) + + describe('Notification settings', function () { + let baseParams: CheckerBaseParams + + before(() => { + baseParams = { + server: server, + emails, + socketNotifications: userNotifications, + token: userAccessToken + } + }) + + it('Should not have notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { + newVideoFromSubscription: UserNotificationSettingValue.NONE + })) + + { + const res = await getMyUserInformation(server.url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) + } + + const { name, uuid } = await uploadRandomVideo(server) + + const check = { web: true, mail: true } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + }) + + it('Should only have web notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { + newVideoFromSubscription: UserNotificationSettingValue.WEB + })) + + { + const res = await getMyUserInformation(server.url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) + } + + const { name, uuid } = await uploadRandomVideo(server) + + { + const check = { mail: true, web: false } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + } + + { + const check = { mail: false, web: true } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') + } + }) + + it('Should only have mail notifications', async function () { + this.timeout(20000) + + await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { + newVideoFromSubscription: UserNotificationSettingValue.EMAIL + })) + + { + const res = await getMyUserInformation(server.url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) + } + + const { name, uuid } = await uploadRandomVideo(server) + + { + const check = { mail: false, web: true } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') + } + + { + const check = { mail: true, web: false } + await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, '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 + })) + + { + const res = await getMyUserInformation(server.url, userAccessToken) + const info = res.body as User + expect(info.notificationSettings.newVideoFromSubscription).to.equal( + UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + ) + } + + const { name, uuid } = await uploadRandomVideo(server) + + await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') + }) + }) + + after(async function () { + MockSmtpServer.Instance.kill() + + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/api/notifications/user-notifications.ts b/server/tests/api/notifications/user-notifications.ts index 04abc6738..e5f6aa864 100644 --- a/server/tests/api/notifications/user-notifications.ts +++ b/server/tests/api/notifications/user-notifications.ts @@ -1,176 +1,55 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' +import * as chai from 'chai' +import { v4 as uuidv4 } from 'uuid' import { - addVideoToBlacklist, cleanupTests, - createUser, - doubleFollow, - flushAndRunMultipleServers, - follow, - getCustomConfig, - getMyUserInformation, - getVideoCommentThreads, - getVideoThreadComments, - immutableAssign, - MockInstancesIndex, - registerUser, - removeVideoFromBlacklist, - reportVideoAbuse, - unfollow, - updateCustomConfig, - updateCustomSubConfig, updateMyUser, updateVideo, updateVideoChannel, - userLogin, + uploadRandomVideoOnServers, wait } from '../../../../shared/extra-utils' -import { ServerInfo, uploadVideo } from '../../../../shared/extra-utils/index' -import { setAccessTokensToServers } from '../../../../shared/extra-utils/users/login' +import { ServerInfo } from '../../../../shared/extra-utils/index' +import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' import { waitJobs } from '../../../../shared/extra-utils/server/jobs' -import { getUserNotificationSocket } from '../../../../shared/extra-utils/socket/socket-io' import { - checkAutoInstanceFollowing, - checkCommentMention, CheckerBaseParams, checkMyVideoImportIsFinished, checkNewActorFollow, - checkNewBlacklistOnMyVideo, - checkNewCommentOnMyVideo, - checkNewInstanceFollower, - checkNewVideoAbuseForModerators, checkNewVideoFromSubscription, - checkUserRegistered, - checkVideoAutoBlacklistForModerators, checkVideoIsPublished, getLastNotification, - getUserNotifications, - markAsReadAllNotifications, - markAsReadNotifications, - updateMyNotificationSettings + prepareNotificationsTest } from '../../../../shared/extra-utils/users/user-notifications' -import { - User, - UserNotification, - UserNotificationSetting, - UserNotificationSettingValue, - UserNotificationType -} from '../../../../shared/models/users' -import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' import { addUserSubscription, removeUserSubscription } from '../../../../shared/extra-utils/users/user-subscriptions' -import { VideoPrivacy } from '../../../../shared/models/videos' import { getBadVideoUrl, getYoutubeVideoUrl, importVideo } from '../../../../shared/extra-utils/videos/video-imports' -import { addVideoCommentReply, addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments' -import { v4 as uuidv4 } from 'uuid' -import { addAccountToAccountBlocklist, removeAccountFromAccountBlocklist } from '../../../../shared/extra-utils/users/blocklist' -import { CustomConfig } from '../../../../shared/models/server' -import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' +import { UserNotification, UserNotificationType } from '../../../../shared/models/users' +import { VideoPrivacy } from '../../../../shared/models/videos' const expect = chai.expect -async function uploadVideoByRemoteAccount (servers: ServerInfo[], additionalParams: any = {}) { - const name = 'remote video ' + uuidv4() - - const data = Object.assign({ name }, additionalParams) - const res = await uploadVideo(servers[1].url, servers[1].accessToken, data) - - await waitJobs(servers) - - return { uuid: res.body.video.uuid, name } -} - -async function uploadVideoByLocalAccount (servers: ServerInfo[], additionalParams: any = {}) { - const name = 'local video ' + uuidv4() - - const data = Object.assign({ name }, additionalParams) - const res = await uploadVideo(servers[0].url, servers[0].accessToken, data) - - await waitJobs(servers) - - return { uuid: res.body.video.uuid, name } -} - -describe('Test users notifications', function () { +describe('Test user notifications', function () { let servers: ServerInfo[] = [] let userAccessToken: string - const userNotifications: UserNotification[] = [] - const adminNotifications: UserNotification[] = [] - const adminNotificationsServer2: UserNotification[] = [] - const emails: object[] = [] + let userNotifications: UserNotification[] = [] + let adminNotifications: UserNotification[] = [] + let adminNotificationsServer2: UserNotification[] = [] + let emails: object[] = [] let channelId: number - const allNotificationSettings: UserNotificationSetting = { - newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, - autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - } - before(async function () { this.timeout(120000) - const port = await MockSmtpServer.Instance.collectEmails(emails) - - const overrideConfig = { - smtp: { - hostname: 'localhost', - port - } - } - servers = await flushAndRunMultipleServers(3, overrideConfig) - - // Get the access tokens - await setAccessTokensToServers(servers) - - // Server 1 and server 2 follow each other - await doubleFollow(servers[0], servers[1]) - - await waitJobs(servers) - - const user = { - username: 'user_1', - password: 'super password' - } - await createUser({ - url: servers[0].url, - accessToken: servers[0].accessToken, - username: user.username, - password: user.password, - videoQuota: 10 * 1000 * 1000 - }) - userAccessToken = await userLogin(servers[0], user) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, allNotificationSettings) - await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, allNotificationSettings) - await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, allNotificationSettings) - - { - const socket = getUserNotificationSocket(servers[0].url, userAccessToken) - socket.on('new-notification', n => userNotifications.push(n)) - } - { - const socket = getUserNotificationSocket(servers[0].url, servers[0].accessToken) - socket.on('new-notification', n => adminNotifications.push(n)) - } - { - const socket = getUserNotificationSocket(servers[1].url, servers[1].accessToken) - socket.on('new-notification', n => adminNotificationsServer2.push(n)) - } - - { - const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken) - channelId = resChannel.body.videoChannels[0].id - } + const res = await prepareNotificationsTest(3) + emails = res.emails + userAccessToken = res.userAccessToken + servers = res.servers + userNotifications = res.userNotifications + adminNotifications = res.adminNotifications + adminNotificationsServer2 = res.adminNotificationsServer2 + channelId = res.channelId }) describe('New video from my subscription notification', function () { @@ -188,7 +67,7 @@ describe('Test users notifications', function () { it('Should not send notifications if the user does not follow the video publisher', async function () { this.timeout(10000) - await uploadVideoByLocalAccount(servers) + await uploadRandomVideoOnServers(servers, 1) const notification = await getLastNotification(servers[0].url, userAccessToken) expect(notification).to.be.undefined @@ -203,7 +82,7 @@ describe('Test users notifications', function () { await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[0].port) await waitJobs(servers) - const { name, uuid } = await uploadVideoByLocalAccount(servers) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 1) await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') }) @@ -213,7 +92,7 @@ describe('Test users notifications', function () { await addUserSubscription(servers[0].url, userAccessToken, 'root_channel@localhost:' + servers[1].port) await waitJobs(servers) - const { name, uuid } = await uploadVideoByRemoteAccount(servers) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2) await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') }) @@ -230,7 +109,7 @@ describe('Test users notifications', function () { privacy: VideoPrivacy.PUBLIC } } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) await wait(6000) await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') @@ -249,7 +128,7 @@ describe('Test users notifications', function () { privacy: VideoPrivacy.PUBLIC } } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) await waitJobs(servers) await wait(6000) @@ -268,7 +147,7 @@ describe('Test users notifications', function () { privacy: VideoPrivacy.PUBLIC } } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) await wait(6000) await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') @@ -278,7 +157,7 @@ describe('Test users notifications', function () { this.timeout(10000) const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') @@ -292,7 +171,7 @@ describe('Test users notifications', function () { this.timeout(20000) const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) await checkNewVideoFromSubscription(baseParams, name, uuid, 'absence') @@ -306,7 +185,7 @@ describe('Test users notifications', function () { this.timeout(20000) const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByLocalAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 1, data) await updateVideo(servers[0].url, servers[0].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) @@ -317,7 +196,7 @@ describe('Test users notifications', function () { this.timeout(20000) const data = { privacy: VideoPrivacy.PRIVATE } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) await updateVideo(servers[1].url, servers[1].accessToken, uuid, { privacy: VideoPrivacy.UNLISTED }) @@ -345,356 +224,6 @@ describe('Test users notifications', function () { }) }) - describe('Comment on my video notifications', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should not send a new comment notification after a comment on another video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - }) - - it('Should not send a new comment notification if I comment my own video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - }) - - it('Should not send a new comment notification if the account is muted', async function () { - this.timeout(10000) - - await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root') - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'absence') - - await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root') - }) - - it('Should send a new comment notification after a local comment on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') - }) - - it('Should send a new comment notification after a remote comment on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - - await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') - - await waitJobs(servers) - - const resComment = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resComment.body.data).to.have.lengthOf(1) - const commentId = resComment.body.data[0].id - - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, commentId, 'presence') - }) - - it('Should send a new comment notification after a local reply on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, 'comment') - const threadId = resThread.body.comment.id - - const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'reply') - const commentId = resComment.body.comment.id - - await wait(500) - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') - }) - - it('Should send a new comment notification after a remote reply on my video', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - await waitJobs(servers) - - { - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, 'comment') - const threadId = resThread.body.comment.id - await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, threadId, 'reply') - } - - await waitJobs(servers) - - const resThread = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resThread.body.data).to.have.lengthOf(1) - const threadId = resThread.body.data[0].id - - const resComments = await getVideoThreadComments(servers[0].url, uuid, threadId) - const tree = resComments.body as VideoCommentThreadTree - - expect(tree.children).to.have.lengthOf(1) - const commentId = tree.children[0].comment.id - - await checkNewCommentOnMyVideo(baseParams, uuid, commentId, threadId, 'presence') - }) - }) - - describe('Mention notifications', function () { - let baseParams: CheckerBaseParams - - before(async () => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - - await updateMyUser({ - url: servers[0].url, - accessToken: servers[0].accessToken, - displayName: 'super root name' - }) - - await updateMyUser({ - url: servers[1].url, - accessToken: servers[1].accessToken, - displayName: 'super root 2 name' - }) - }) - - it('Should not send a new mention comment notification if I mention the video owner', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - }) - - it('Should not send a new mention comment notification if I mention myself', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, userAccessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - }) - - it('Should not send a new mention notification if the account is muted', async function () { - this.timeout(10000) - - await addAccountToAccountBlocklist(servers[0].url, userAccessToken, 'root') - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, commentId, 'super root name', 'absence') - - await removeAccountFromAccountBlocklist(servers[0].url, userAccessToken, 'root') - }) - - it('Should not send a new mention notification if the remote account mention a local account', async function () { - this.timeout(20000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, '@user_1 hello') - const threadId = resThread.body.comment.id - - await waitJobs(servers) - await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root 2 name', 'absence') - }) - - it('Should send a new mention notification after local comments', async function () { - this.timeout(10000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - const resThread = await addVideoCommentThread(servers[0].url, servers[0].accessToken, uuid, '@user_1 hello 1') - const threadId = resThread.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, threadId, threadId, 'super root name', 'presence') - - const resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, uuid, threadId, 'hello 2 @user_1') - const commentId = resComment.body.comment.id - - await wait(500) - await checkCommentMention(baseParams, uuid, commentId, threadId, 'super root name', 'presence') - }) - - it('Should send a new mention notification after remote comments', async function () { - this.timeout(20000) - - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'super video' }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - - const text1 = `hello @user_1@localhost:${servers[0].port} 1` - const resThread = await addVideoCommentThread(servers[1].url, servers[1].accessToken, uuid, text1) - const server2ThreadId = resThread.body.comment.id - - await waitJobs(servers) - - const resThread2 = await getVideoCommentThreads(servers[0].url, uuid, 0, 5) - expect(resThread2.body.data).to.have.lengthOf(1) - const server1ThreadId = resThread2.body.data[0].id - await checkCommentMention(baseParams, uuid, server1ThreadId, server1ThreadId, 'super root 2 name', 'presence') - - const text2 = `@user_1@localhost:${servers[0].port} hello 2 @root@localhost:${servers[0].port}` - await addVideoCommentReply(servers[1].url, servers[1].accessToken, uuid, server2ThreadId, text2) - - await waitJobs(servers) - - const resComments = await getVideoThreadComments(servers[0].url, uuid, server1ThreadId) - const tree = resComments.body as VideoCommentThreadTree - - expect(tree.children).to.have.lengthOf(1) - const commentId = tree.children[0].comment.id - - await checkCommentMention(baseParams, uuid, commentId, server1ThreadId, 'super root 2 name', 'presence') - }) - }) - - describe('Video abuse for moderators notification', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - }) - - it('Should send a notification to moderators on local video abuse', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await reportVideoAbuse(servers[0].url, servers[0].accessToken, uuid, 'super reason') - - await waitJobs(servers) - await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') - }) - - it('Should send a notification to moderators on remote video abuse', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - - await reportVideoAbuse(servers[1].url, servers[1].accessToken, uuid, 'super reason') - - await waitJobs(servers) - await checkNewVideoAbuseForModerators(baseParams, uuid, name, 'presence') - }) - }) - - describe('Video blacklist on my video', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should send a notification to video owner on blacklist', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) - - await waitJobs(servers) - await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'blacklist') - }) - - it('Should send a notification to video owner on unblacklist', async function () { - this.timeout(10000) - - const name = 'video for abuse ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name }) - const uuid = resVideo.body.video.uuid - - await addVideoToBlacklist(servers[0].url, servers[0].accessToken, uuid) - - await waitJobs(servers) - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) - await waitJobs(servers) - - await wait(500) - await checkNewBlacklistOnMyVideo(baseParams, uuid, name, 'unblacklist') - }) - }) - describe('My video is published', function () { let baseParams: CheckerBaseParams @@ -710,7 +239,7 @@ describe('Test users notifications', function () { it('Should not send a notification if transcoding is not enabled', async function () { this.timeout(10000) - const { name, uuid } = await uploadVideoByLocalAccount(servers) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 1) await waitJobs(servers) await checkVideoIsPublished(baseParams, name, uuid, 'absence') @@ -719,7 +248,7 @@ describe('Test users notifications', function () { it('Should not send a notification if the wait transcoding is false', async function () { this.timeout(50000) - await uploadVideoByRemoteAccount(servers, { waitTranscoding: false }) + await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: false }) await waitJobs(servers) const notification = await getLastNotification(servers[0].url, userAccessToken) @@ -731,7 +260,7 @@ describe('Test users notifications', function () { it('Should send a notification even if the video is not transcoded in other resolutions', async function () { this.timeout(50000) - const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true, fixture: 'video_short_240p.mp4' }) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true, fixture: 'video_short_240p.mp4' }) await waitJobs(servers) await checkVideoIsPublished(baseParams, name, uuid, 'presence') @@ -740,7 +269,7 @@ describe('Test users notifications', function () { it('Should send a notification with a transcoded video', async function () { this.timeout(50000) - const { name, uuid } = await uploadVideoByRemoteAccount(servers, { waitTranscoding: true }) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, { waitTranscoding: true }) await waitJobs(servers) await checkVideoIsPublished(baseParams, name, uuid, 'presence') @@ -778,14 +307,14 @@ describe('Test users notifications', function () { privacy: VideoPrivacy.PUBLIC } } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) await wait(6000) await checkVideoIsPublished(baseParams, name, uuid, 'presence') }) it('Should not send a notification before the video is published', async function () { - this.timeout(20000) + this.timeout(40000) const updateAt = new Date(new Date().getTime() + 1000000) @@ -796,7 +325,7 @@ describe('Test users notifications', function () { privacy: VideoPrivacy.PUBLIC } } - const { name, uuid } = await uploadVideoByRemoteAccount(servers, data) + const { name, uuid } = await uploadRandomVideoOnServers(servers, 2, data) await wait(6000) await checkVideoIsPublished(baseParams, name, uuid, 'absence') @@ -852,122 +381,6 @@ describe('Test users notifications', function () { }) }) - describe('New registration', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - }) - - it('Should send a notification only to moderators when a user registers on the instance', async function () { - this.timeout(10000) - - await registerUser(servers[0].url, 'user_45', 'password') - - await waitJobs(servers) - - await checkUserRegistered(baseParams, 'user_45', 'presence') - - const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } - await checkUserRegistered(immutableAssign(baseParams, userOverride), 'user_45', 'absence') - }) - }) - - describe('New instance follows', function () { - const instanceIndexServer = new MockInstancesIndex() - const config = { - followings: { - instance: { - autoFollowIndex: { - indexUrl: 'http://localhost:42101/api/v1/instances/hosts', - enabled: true - } - } - } - } - let baseParams: CheckerBaseParams - - before(async () => { - baseParams = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - - await instanceIndexServer.initialize() - instanceIndexServer.addInstance(servers[1].host) - }) - - it('Should send a notification only to admin when there is a new instance follower', async function () { - this.timeout(20000) - - await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) - - await waitJobs(servers) - - await checkNewInstanceFollower(baseParams, 'localhost:' + servers[2].port, 'presence') - - const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } - await checkNewInstanceFollower(immutableAssign(baseParams, userOverride), 'localhost:' + servers[2].port, 'absence') - }) - - it('Should send a notification on auto follow back', async function () { - this.timeout(40000) - - await unfollow(servers[2].url, servers[2].accessToken, servers[0]) - await waitJobs(servers) - - const config = { - followings: { - instance: { - autoFollowBack: { enabled: true } - } - } - } - await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) - - await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken) - - await waitJobs(servers) - - const followerHost = servers[0].host - const followingHost = servers[2].host - await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') - - const userOverride = { socketNotifications: userNotifications, token: userAccessToken, check: { web: true, mail: false } } - await checkAutoInstanceFollowing(immutableAssign(baseParams, userOverride), followerHost, followingHost, 'absence') - - config.followings.instance.autoFollowBack.enabled = false - await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) - await unfollow(servers[0].url, servers[0].accessToken, servers[2]) - await unfollow(servers[2].url, servers[2].accessToken, servers[0]) - }) - - it('Should send a notification on auto instances index follow', async function () { - this.timeout(30000) - await unfollow(servers[0].url, servers[0].accessToken, servers[1]) - - await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) - - await wait(5000) - await waitJobs(servers) - - const followerHost = servers[0].host - const followingHost = servers[1].host - await checkAutoInstanceFollowing(baseParams, followerHost, followingHost, 'presence') - - config.followings.instance.autoFollowIndex.enabled = false - await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) - await unfollow(servers[0].url, servers[0].accessToken, servers[1]) - }) - }) - describe('New actor follow', function () { let baseParams: CheckerBaseParams const myChannelName = 'super channel name' @@ -1046,332 +459,6 @@ describe('Test users notifications', function () { // }) }) - describe('Video-related notifications when video auto-blacklist is enabled', function () { - let userBaseParams: CheckerBaseParams - let adminBaseParamsServer1: CheckerBaseParams - let adminBaseParamsServer2: CheckerBaseParams - let videoUUID: string - let videoName: string - let currentCustomConfig: CustomConfig - - before(async () => { - - adminBaseParamsServer1 = { - server: servers[0], - emails, - socketNotifications: adminNotifications, - token: servers[0].accessToken - } - - adminBaseParamsServer2 = { - server: servers[1], - emails, - socketNotifications: adminNotificationsServer2, - token: servers[1].accessToken - } - - userBaseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - - const resCustomConfig = await getCustomConfig(servers[0].url, servers[0].accessToken) - currentCustomConfig = resCustomConfig.body - const autoBlacklistTestsCustomConfig = immutableAssign(currentCustomConfig, { - autoBlacklist: { - videos: { - ofUsers: { - enabled: true - } - } - } - }) - // enable transcoding otherwise own publish notification after transcoding not expected - autoBlacklistTestsCustomConfig.transcoding.enabled = true - await updateCustomConfig(servers[0].url, servers[0].accessToken, autoBlacklistTestsCustomConfig) - - await addUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) - await addUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) - - }) - - it('Should send notification to moderators on new video with auto-blacklist', async function () { - this.timeout(20000) - - videoName = 'video with auto-blacklist ' + uuidv4() - const resVideo = await uploadVideo(servers[0].url, userAccessToken, { name: videoName }) - videoUUID = resVideo.body.video.uuid - - await waitJobs(servers) - await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, videoUUID, videoName, 'presence') - }) - - it('Should not send video publish notification if auto-blacklisted', async function () { - await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'absence') - }) - - it('Should not send a local user subscription notification if auto-blacklisted', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'absence') - }) - - it('Should not send a remote user subscription notification if auto-blacklisted', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'absence') - }) - - it('Should send video published and unblacklist after video unblacklisted', async function () { - this.timeout(20000) - - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, videoUUID) - - await waitJobs(servers) - - // FIXME: Can't test as two notifications sent to same user and util only checks last one - // One notification might be better anyways - // await checkNewBlacklistOnMyVideo(userBaseParams, videoUUID, videoName, 'unblacklist') - // await checkVideoIsPublished(userBaseParams, videoName, videoUUID, 'presence') - }) - - it('Should send a local user subscription notification after removed from blacklist', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer1, videoName, videoUUID, 'presence') - }) - - it('Should send a remote user subscription notification after removed from blacklist', async function () { - await checkNewVideoFromSubscription(adminBaseParamsServer2, videoName, videoUUID, 'presence') - }) - - it('Should send unblacklist but not published/subscription notes after unblacklisted if scheduled update pending', async function () { - this.timeout(20000) - - const updateAt = new Date(new Date().getTime() + 1000000) - - const name = 'video with auto-blacklist and future schedule ' + uuidv4() - - const data = { - name, - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) - const uuid = resVideo.body.video.uuid - - await removeVideoFromBlacklist(servers[0].url, servers[0].accessToken, uuid) - - await waitJobs(servers) - await checkNewBlacklistOnMyVideo(userBaseParams, uuid, name, 'unblacklist') - - // FIXME: Can't test absence as two notifications sent to same user and util only checks last one - // One notification might be better anyways - // await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') - - await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') - await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') - }) - - it('Should not send publish/subscription notifications after scheduled update if video still auto-blacklisted', async function () { - this.timeout(20000) - - // In 2 seconds - const updateAt = new Date(new Date().getTime() + 2000) - - const name = 'video with schedule done and still auto-blacklisted ' + uuidv4() - - const data = { - name, - privacy: VideoPrivacy.PRIVATE, - scheduleUpdate: { - updateAt: updateAt.toISOString(), - privacy: VideoPrivacy.PUBLIC - } - } - - const resVideo = await uploadVideo(servers[0].url, userAccessToken, data) - const uuid = resVideo.body.video.uuid - - await wait(6000) - await checkVideoIsPublished(userBaseParams, name, uuid, 'absence') - await checkNewVideoFromSubscription(adminBaseParamsServer1, name, uuid, 'absence') - await checkNewVideoFromSubscription(adminBaseParamsServer2, name, uuid, 'absence') - }) - - it('Should not send a notification to moderators on new video without auto-blacklist', async function () { - this.timeout(20000) - - const name = 'video without auto-blacklist ' + uuidv4() - - // admin with blacklist right will not be auto-blacklisted - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { name }) - const uuid = resVideo.body.video.uuid - - await waitJobs(servers) - await checkVideoAutoBlacklistForModerators(adminBaseParamsServer1, uuid, name, 'absence') - }) - - after(async () => { - await updateCustomConfig(servers[0].url, servers[0].accessToken, currentCustomConfig) - - await removeUserSubscription(servers[0].url, servers[0].accessToken, 'user_1_channel@localhost:' + servers[0].port) - await removeUserSubscription(servers[1].url, servers[1].accessToken, 'user_1_channel@localhost:' + servers[0].port) - }) - }) - - describe('Mark as read', function () { - it('Should mark as read some notifications', async function () { - const res = await getUserNotifications(servers[0].url, userAccessToken, 2, 3) - const ids = res.body.data.map(n => n.id) - - await markAsReadNotifications(servers[0].url, userAccessToken, ids) - }) - - it('Should have the notifications marked as read', async function () { - const res = await getUserNotifications(servers[0].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 - }) - - it('Should only list read notifications', async function () { - const res = await getUserNotifications(servers[0].url, userAccessToken, 0, 10, false) - - const notifications = res.body.data as UserNotification[] - for (const notification of notifications) { - expect(notification.read).to.be.true - } - }) - - it('Should only list unread notifications', async function () { - const res = await getUserNotifications(servers[0].url, userAccessToken, 0, 10, true) - - const notifications = res.body.data as UserNotification[] - for (const notification of notifications) { - expect(notification.read).to.be.false - } - }) - - it('Should mark as read all notifications', async function () { - await markAsReadAllNotifications(servers[0].url, userAccessToken) - - const res = await getUserNotifications(servers[0].url, userAccessToken, 0, 10, true) - - expect(res.body.total).to.equal(0) - expect(res.body.data).to.have.lengthOf(0) - }) - }) - - describe('Notification settings', function () { - let baseParams: CheckerBaseParams - - before(() => { - baseParams = { - server: servers[0], - emails, - socketNotifications: userNotifications, - token: userAccessToken - } - }) - - it('Should not have notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.NONE - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - const check = { web: true, mail: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') - }) - - it('Should only have web notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.WEB - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - { - const check = { mail: true, web: false } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') - } - - { - const check = { mail: false, web: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') - } - }) - - it('Should only have mail notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.EMAIL - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - { - const check = { mail: false, web: true } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') - } - - { - const check = { mail: true, web: false } - await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') - } - }) - - it('Should have email and web notifications', async function () { - this.timeout(20000) - - await updateMyNotificationSettings(servers[0].url, userAccessToken, immutableAssign(allNotificationSettings, { - newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - })) - - { - const res = await getMyUserInformation(servers[0].url, userAccessToken) - const info = res.body as User - expect(info.notificationSettings.newVideoFromSubscription).to.equal( - UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL - ) - } - - const { name, uuid } = await uploadVideoByLocalAccount(servers) - - await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') - }) - }) - after(async function () { MockSmtpServer.Instance.kill() diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts index e7d9238dd..3493a723d 100644 --- a/server/tests/api/videos/video-channels.ts +++ b/server/tests/api/videos/video-channels.ts @@ -1,8 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import * as chai from 'chai' import 'mocha' -import { User, Video, VideoChannel, ViewsPerDate, VideoDetails } from '../../../../shared/index' +import * as chai from 'chai' import { cleanupTests, createUser, @@ -30,6 +29,7 @@ import { viewVideo } from '../../../../shared/extra-utils/index' import { waitJobs } from '../../../../shared/extra-utils/server/jobs' +import { User, Video, VideoChannel, VideoDetails } from '../../../../shared/index' const expect = chai.expect diff --git a/shared/extra-utils/users/user-notifications.ts b/shared/extra-utils/users/user-notifications.ts index bd00894c4..30c36a388 100644 --- a/shared/extra-utils/users/user-notifications.ts +++ b/shared/extra-utils/users/user-notifications.ts @@ -1,10 +1,15 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ -import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' -import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users' -import { ServerInfo } from '..' import { expect } from 'chai' import { inspect } from 'util' +import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users' +import { MockSmtpServer } from '../miscs/email' +import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' +import { doubleFollow } from '../server/follows' +import { flushAndRunMultipleServers, ServerInfo } from '../server/servers' +import { getUserNotificationSocket } from '../socket/socket-io' +import { setAccessTokensToServers, userLogin } from './login' +import { createUser, getMyUserInformation } from './users' function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) { const path = '/api/v1/users/me/notification-settings' @@ -76,7 +81,7 @@ async function getLastNotification (serverUrl: string, accessToken: string) { type CheckerBaseParams = { server: ServerInfo - emails: object[] + emails: any[] socketNotifications: UserNotification[] token: string check?: { web: boolean, mail: boolean } @@ -110,10 +115,10 @@ async function checkNotification ( if (checkType === 'presence') { const obj = inspect(base.socketNotifications, { depth: 5 }) - expect(socketNotification, 'The socket notification is absent when is should be present. ' + obj).to.not.be.undefined + expect(socketNotification, 'The socket notification is absent when it should be present. ' + obj).to.not.be.undefined } else { const obj = inspect(socketNotification, { depth: 5 }) - expect(socketNotification, 'The socket notification is present when is should not be present. ' + obj).to.be.undefined + expect(socketNotification, 'The socket notification is present when it should not be present. ' + obj).to.be.undefined } } @@ -125,7 +130,8 @@ async function checkNotification ( .find(e => emailNotificationFinder(e)) if (checkType === 'presence') { - expect(email, 'The email is absent when is should be present. ' + inspect(base.emails)).to.not.be.undefined + const emails = base.emails.map(e => e.text) + expect(email, 'The email is absent when is should be present. ' + inspect(emails)).to.not.be.undefined } else { expect(email, 'The email is present when is should not be present. ' + inspect(email)).to.be.undefined } @@ -506,11 +512,96 @@ async function checkNewBlacklistOnMyVideo ( await checkNotification(base, notificationChecker, emailNotificationFinder, 'presence') } +function getAllNotificationsSettings () { + return { + newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, + autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL + } as UserNotificationSetting +} + +async function prepareNotificationsTest (serversCount = 3) { + const userNotifications: UserNotification[] = [] + const adminNotifications: UserNotification[] = [] + const adminNotificationsServer2: UserNotification[] = [] + const emails: object[] = [] + + const port = await MockSmtpServer.Instance.collectEmails(emails) + + const overrideConfig = { + smtp: { + hostname: 'localhost', + port + } + } + const servers = await flushAndRunMultipleServers(serversCount, overrideConfig) + + await setAccessTokensToServers(servers) + await doubleFollow(servers[0], servers[1]) + + const user = { + username: 'user_1', + password: 'super password' + } + await createUser({ + url: servers[0].url, + accessToken: servers[0].accessToken, + username: user.username, + password: user.password, + videoQuota: 10 * 1000 * 1000 + }) + const userAccessToken = await userLogin(servers[0], user) + + await updateMyNotificationSettings(servers[0].url, userAccessToken, getAllNotificationsSettings()) + await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, getAllNotificationsSettings()) + + if (serversCount > 1) { + await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, getAllNotificationsSettings()) + } + + { + const socket = getUserNotificationSocket(servers[0].url, userAccessToken) + socket.on('new-notification', n => userNotifications.push(n)) + } + { + const socket = getUserNotificationSocket(servers[0].url, servers[0].accessToken) + socket.on('new-notification', n => adminNotifications.push(n)) + } + + if (serversCount > 1) { + const socket = getUserNotificationSocket(servers[1].url, servers[1].accessToken) + socket.on('new-notification', n => adminNotificationsServer2.push(n)) + } + + const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken) + const channelId = resChannel.body.videoChannels[0].id + + return { + userNotifications, + adminNotifications, + adminNotificationsServer2, + userAccessToken, + emails, + servers, + channelId + } +} + // --------------------------------------------------------------------------- export { CheckerBaseParams, CheckerType, + getAllNotificationsSettings, checkNotification, markAsReadAllNotifications, checkMyVideoImportIsFinished, @@ -528,5 +619,6 @@ export { getUserNotifications, markAsReadNotifications, getLastNotification, - checkNewInstanceFollower + checkNewInstanceFollower, + prepareNotificationsTest } diff --git a/shared/extra-utils/videos/videos.ts b/shared/extra-utils/videos/videos.ts index 99e591cb2..2f7f2182c 100644 --- a/shared/extra-utils/videos/videos.ts +++ b/shared/extra-utils/videos/videos.ts @@ -5,21 +5,23 @@ import { pathExists, readdir, readFile } from 'fs-extra' import * as parseTorrent from 'parse-torrent' import { extname, join } from 'path' import * as request from 'supertest' +import { v4 as uuidv4 } from 'uuid' +import validator from 'validator' +import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' +import { VideoDetails, VideoPrivacy } from '../../models/videos' import { buildAbsoluteFixturePath, - getMyUserInformation, + buildServerDirectory, + dateIsValid, immutableAssign, - makeGetRequest, - makePutBodyRequest, - makeUploadRequest, root, - ServerInfo, - testImage -} from '../' -import validator from 'validator' -import { VideoDetails, VideoPrivacy } from '../../models/videos' -import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, loadLanguages, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' -import { dateIsValid, webtorrentAdd, buildServerDirectory } from '../miscs/miscs' + testImage, + webtorrentAdd +} from '../miscs/miscs' +import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests' +import { waitJobs } from '../server/jobs' +import { ServerInfo } from '../server/servers' +import { getMyUserInformation } from '../users/users' loadLanguages() @@ -644,11 +646,34 @@ async function getLocalIdByUUID (url: string, uuid: string) { return res.body.id } +// serverNumber starts from 1 +async function uploadRandomVideoOnServers (servers: ServerInfo[], serverNumber: number, additionalParams: any = {}) { + const server = servers.find(s => s.serverNumber === serverNumber) + const res = await uploadRandomVideo(server, false, additionalParams) + + await waitJobs(servers) + + return res +} + +async function uploadRandomVideo (server: ServerInfo, wait = true, additionalParams: any = {}) { + const prefixName = additionalParams.prefixName || '' + const name = prefixName + uuidv4() + + const data = Object.assign({ name }, additionalParams) + const res = await uploadVideo(server.url, server.accessToken, data) + + if (wait) await waitJobs([ server ]) + + return { uuid: res.body.video.uuid, name } +} + // --------------------------------------------------------------------------- export { getVideoDescription, getVideoCategories, + uploadRandomVideo, getVideoLicences, videoUUIDToId, getVideoPrivacies, @@ -666,6 +691,7 @@ export { getVideosListWithToken, uploadVideo, getVideosWithFilters, + uploadRandomVideoOnServers, updateVideo, rateVideo, viewVideo,