diff options
Diffstat (limited to 'shared/extra-utils/users/user-notifications.ts')
-rw-r--r-- | shared/extra-utils/users/user-notifications.ts | 108 |
1 files changed, 100 insertions, 8 deletions
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 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | 2 | ||
3 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' | ||
4 | import { UserNotification, UserNotificationSetting, UserNotificationType } from '../../models/users' | ||
5 | import { ServerInfo } from '..' | ||
6 | import { expect } from 'chai' | 3 | import { expect } from 'chai' |
7 | import { inspect } from 'util' | 4 | import { inspect } from 'util' |
5 | import { UserNotification, UserNotificationSetting, UserNotificationSettingValue, UserNotificationType } from '../../models/users' | ||
6 | import { MockSmtpServer } from '../miscs/email' | ||
7 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest } from '../requests/requests' | ||
8 | import { doubleFollow } from '../server/follows' | ||
9 | import { flushAndRunMultipleServers, ServerInfo } from '../server/servers' | ||
10 | import { getUserNotificationSocket } from '../socket/socket-io' | ||
11 | import { setAccessTokensToServers, userLogin } from './login' | ||
12 | import { createUser, getMyUserInformation } from './users' | ||
8 | 13 | ||
9 | function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) { | 14 | function updateMyNotificationSettings (url: string, token: string, settings: UserNotificationSetting, statusCodeExpected = 204) { |
10 | const path = '/api/v1/users/me/notification-settings' | 15 | const path = '/api/v1/users/me/notification-settings' |
@@ -76,7 +81,7 @@ async function getLastNotification (serverUrl: string, accessToken: string) { | |||
76 | 81 | ||
77 | type CheckerBaseParams = { | 82 | type CheckerBaseParams = { |
78 | server: ServerInfo | 83 | server: ServerInfo |
79 | emails: object[] | 84 | emails: any[] |
80 | socketNotifications: UserNotification[] | 85 | socketNotifications: UserNotification[] |
81 | token: string | 86 | token: string |
82 | check?: { web: boolean, mail: boolean } | 87 | check?: { web: boolean, mail: boolean } |
@@ -110,10 +115,10 @@ async function checkNotification ( | |||
110 | 115 | ||
111 | if (checkType === 'presence') { | 116 | if (checkType === 'presence') { |
112 | const obj = inspect(base.socketNotifications, { depth: 5 }) | 117 | const obj = inspect(base.socketNotifications, { depth: 5 }) |
113 | expect(socketNotification, 'The socket notification is absent when is should be present. ' + obj).to.not.be.undefined | 118 | expect(socketNotification, 'The socket notification is absent when it should be present. ' + obj).to.not.be.undefined |
114 | } else { | 119 | } else { |
115 | const obj = inspect(socketNotification, { depth: 5 }) | 120 | const obj = inspect(socketNotification, { depth: 5 }) |
116 | expect(socketNotification, 'The socket notification is present when is should not be present. ' + obj).to.be.undefined | 121 | expect(socketNotification, 'The socket notification is present when it should not be present. ' + obj).to.be.undefined |
117 | } | 122 | } |
118 | } | 123 | } |
119 | 124 | ||
@@ -125,7 +130,8 @@ async function checkNotification ( | |||
125 | .find(e => emailNotificationFinder(e)) | 130 | .find(e => emailNotificationFinder(e)) |
126 | 131 | ||
127 | if (checkType === 'presence') { | 132 | if (checkType === 'presence') { |
128 | expect(email, 'The email is absent when is should be present. ' + inspect(base.emails)).to.not.be.undefined | 133 | const emails = base.emails.map(e => e.text) |
134 | expect(email, 'The email is absent when is should be present. ' + inspect(emails)).to.not.be.undefined | ||
129 | } else { | 135 | } else { |
130 | expect(email, 'The email is present when is should not be present. ' + inspect(email)).to.be.undefined | 136 | expect(email, 'The email is present when is should not be present. ' + inspect(email)).to.be.undefined |
131 | } | 137 | } |
@@ -506,11 +512,96 @@ async function checkNewBlacklistOnMyVideo ( | |||
506 | await checkNotification(base, notificationChecker, emailNotificationFinder, 'presence') | 512 | await checkNotification(base, notificationChecker, emailNotificationFinder, 'presence') |
507 | } | 513 | } |
508 | 514 | ||
515 | function getAllNotificationsSettings () { | ||
516 | return { | ||
517 | newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
518 | newCommentOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
519 | videoAbuseAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
520 | videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
521 | blacklistOnMyVideo: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
522 | myVideoImportFinished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
523 | myVideoPublished: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
524 | commentMention: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
525 | newFollow: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
526 | newUserRegistration: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
527 | newInstanceFollower: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL, | ||
528 | autoInstanceFollowing: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL | ||
529 | } as UserNotificationSetting | ||
530 | } | ||
531 | |||
532 | async function prepareNotificationsTest (serversCount = 3) { | ||
533 | const userNotifications: UserNotification[] = [] | ||
534 | const adminNotifications: UserNotification[] = [] | ||
535 | const adminNotificationsServer2: UserNotification[] = [] | ||
536 | const emails: object[] = [] | ||
537 | |||
538 | const port = await MockSmtpServer.Instance.collectEmails(emails) | ||
539 | |||
540 | const overrideConfig = { | ||
541 | smtp: { | ||
542 | hostname: 'localhost', | ||
543 | port | ||
544 | } | ||
545 | } | ||
546 | const servers = await flushAndRunMultipleServers(serversCount, overrideConfig) | ||
547 | |||
548 | await setAccessTokensToServers(servers) | ||
549 | await doubleFollow(servers[0], servers[1]) | ||
550 | |||
551 | const user = { | ||
552 | username: 'user_1', | ||
553 | password: 'super password' | ||
554 | } | ||
555 | await createUser({ | ||
556 | url: servers[0].url, | ||
557 | accessToken: servers[0].accessToken, | ||
558 | username: user.username, | ||
559 | password: user.password, | ||
560 | videoQuota: 10 * 1000 * 1000 | ||
561 | }) | ||
562 | const userAccessToken = await userLogin(servers[0], user) | ||
563 | |||
564 | await updateMyNotificationSettings(servers[0].url, userAccessToken, getAllNotificationsSettings()) | ||
565 | await updateMyNotificationSettings(servers[0].url, servers[0].accessToken, getAllNotificationsSettings()) | ||
566 | |||
567 | if (serversCount > 1) { | ||
568 | await updateMyNotificationSettings(servers[1].url, servers[1].accessToken, getAllNotificationsSettings()) | ||
569 | } | ||
570 | |||
571 | { | ||
572 | const socket = getUserNotificationSocket(servers[0].url, userAccessToken) | ||
573 | socket.on('new-notification', n => userNotifications.push(n)) | ||
574 | } | ||
575 | { | ||
576 | const socket = getUserNotificationSocket(servers[0].url, servers[0].accessToken) | ||
577 | socket.on('new-notification', n => adminNotifications.push(n)) | ||
578 | } | ||
579 | |||
580 | if (serversCount > 1) { | ||
581 | const socket = getUserNotificationSocket(servers[1].url, servers[1].accessToken) | ||
582 | socket.on('new-notification', n => adminNotificationsServer2.push(n)) | ||
583 | } | ||
584 | |||
585 | const resChannel = await getMyUserInformation(servers[0].url, servers[0].accessToken) | ||
586 | const channelId = resChannel.body.videoChannels[0].id | ||
587 | |||
588 | return { | ||
589 | userNotifications, | ||
590 | adminNotifications, | ||
591 | adminNotificationsServer2, | ||
592 | userAccessToken, | ||
593 | emails, | ||
594 | servers, | ||
595 | channelId | ||
596 | } | ||
597 | } | ||
598 | |||
509 | // --------------------------------------------------------------------------- | 599 | // --------------------------------------------------------------------------- |
510 | 600 | ||
511 | export { | 601 | export { |
512 | CheckerBaseParams, | 602 | CheckerBaseParams, |
513 | CheckerType, | 603 | CheckerType, |
604 | getAllNotificationsSettings, | ||
514 | checkNotification, | 605 | checkNotification, |
515 | markAsReadAllNotifications, | 606 | markAsReadAllNotifications, |
516 | checkMyVideoImportIsFinished, | 607 | checkMyVideoImportIsFinished, |
@@ -528,5 +619,6 @@ export { | |||
528 | getUserNotifications, | 619 | getUserNotifications, |
529 | markAsReadNotifications, | 620 | markAsReadNotifications, |
530 | getLastNotification, | 621 | getLastNotification, |
531 | checkNewInstanceFollower | 622 | checkNewInstanceFollower, |
623 | prepareNotificationsTest | ||
532 | } | 624 | } |