aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/notifications/notifications-api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/notifications/notifications-api.ts')
-rw-r--r--server/tests/api/notifications/notifications-api.ts130
1 files changed, 63 insertions, 67 deletions
diff --git a/server/tests/api/notifications/notifications-api.ts b/server/tests/api/notifications/notifications-api.ts
index b81995449..fa4b53db6 100644
--- a/server/tests/api/notifications/notifications-api.ts
+++ b/server/tests/api/notifications/notifications-api.ts
@@ -2,28 +2,24 @@
2 2
3import 'mocha' 3import 'mocha'
4import * as chai from 'chai' 4import * as chai from 'chai'
5import { addUserSubscription } from '@shared/extra-utils/users/user-subscriptions'
6import { cleanupTests, getMyUserInformation, immutableAssign, uploadRandomVideo, waitJobs } from '../../../../shared/extra-utils'
7import { ServerInfo } from '../../../../shared/extra-utils/index'
8import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
9import { 5import {
10 CheckerBaseParams, 6 CheckerBaseParams,
11 checkNewVideoFromSubscription, 7 checkNewVideoFromSubscription,
8 cleanupTests,
12 getAllNotificationsSettings, 9 getAllNotificationsSettings,
13 getUserNotifications, 10 MockSmtpServer,
14 markAsReadAllNotifications, 11 PeerTubeServer,
15 markAsReadNotifications,
16 prepareNotificationsTest, 12 prepareNotificationsTest,
17 updateMyNotificationSettings 13 waitJobs
18} from '../../../../shared/extra-utils/users/user-notifications' 14} from '@shared/extra-utils'
19import { User, UserNotification, UserNotificationSettingValue } from '../../../../shared/models/users' 15import { UserNotification, UserNotificationSettingValue } from '@shared/models'
20 16
21const expect = chai.expect 17const expect = chai.expect
22 18
23describe('Test notifications API', function () { 19describe('Test notifications API', function () {
24 let server: ServerInfo 20 let server: PeerTubeServer
25 let userNotifications: UserNotification[] = [] 21 let userNotifications: UserNotification[] = []
26 let userAccessToken: string 22 let userToken: string
27 let emails: object[] = [] 23 let emails: object[] = []
28 24
29 before(async function () { 25 before(async function () {
@@ -31,14 +27,14 @@ describe('Test notifications API', function () {
31 27
32 const res = await prepareNotificationsTest(1) 28 const res = await prepareNotificationsTest(1)
33 emails = res.emails 29 emails = res.emails
34 userAccessToken = res.userAccessToken 30 userToken = res.userAccessToken
35 userNotifications = res.userNotifications 31 userNotifications = res.userNotifications
36 server = res.servers[0] 32 server = res.servers[0]
37 33
38 await addUserSubscription(server.url, userAccessToken, 'root_channel@localhost:' + server.port) 34 await server.subscriptions.add({ token: userToken, targetUri: 'root_channel@localhost:' + server.port })
39 35
40 for (let i = 0; i < 10; i++) { 36 for (let i = 0; i < 10; i++) {
41 await uploadRandomVideo(server, false) 37 await server.videos.randomUpload({ wait: false })
42 } 38 }
43 39
44 await waitJobs([ server ]) 40 await waitJobs([ server ])
@@ -47,49 +43,46 @@ describe('Test notifications API', function () {
47 describe('Mark as read', function () { 43 describe('Mark as read', function () {
48 44
49 it('Should mark as read some notifications', async function () { 45 it('Should mark as read some notifications', async function () {
50 const res = await getUserNotifications(server.url, userAccessToken, 2, 3) 46 const { data } = await server.notifications.list({ token: userToken, start: 2, count: 3 })
51 const ids = res.body.data.map(n => n.id) 47 const ids = data.map(n => n.id)
52 48
53 await markAsReadNotifications(server.url, userAccessToken, ids) 49 await server.notifications.markAsRead({ token: userToken, ids })
54 }) 50 })
55 51
56 it('Should have the notifications marked as read', async function () { 52 it('Should have the notifications marked as read', async function () {
57 const res = await getUserNotifications(server.url, userAccessToken, 0, 10) 53 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10 })
58 54
59 const notifications = res.body.data as UserNotification[] 55 expect(data[0].read).to.be.false
60 expect(notifications[0].read).to.be.false 56 expect(data[1].read).to.be.false
61 expect(notifications[1].read).to.be.false 57 expect(data[2].read).to.be.true
62 expect(notifications[2].read).to.be.true 58 expect(data[3].read).to.be.true
63 expect(notifications[3].read).to.be.true 59 expect(data[4].read).to.be.true
64 expect(notifications[4].read).to.be.true 60 expect(data[5].read).to.be.false
65 expect(notifications[5].read).to.be.false
66 }) 61 })
67 62
68 it('Should only list read notifications', async function () { 63 it('Should only list read notifications', async function () {
69 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, false) 64 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: false })
70 65
71 const notifications = res.body.data as UserNotification[] 66 for (const notification of data) {
72 for (const notification of notifications) {
73 expect(notification.read).to.be.true 67 expect(notification.read).to.be.true
74 } 68 }
75 }) 69 })
76 70
77 it('Should only list unread notifications', async function () { 71 it('Should only list unread notifications', async function () {
78 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) 72 const { data } = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
79 73
80 const notifications = res.body.data as UserNotification[] 74 for (const notification of data) {
81 for (const notification of notifications) {
82 expect(notification.read).to.be.false 75 expect(notification.read).to.be.false
83 } 76 }
84 }) 77 })
85 78
86 it('Should mark as read all notifications', async function () { 79 it('Should mark as read all notifications', async function () {
87 await markAsReadAllNotifications(server.url, userAccessToken) 80 await server.notifications.markAsReadAll({ token: userToken })
88 81
89 const res = await getUserNotifications(server.url, userAccessToken, 0, 10, true) 82 const body = await server.notifications.list({ token: userToken, start: 0, count: 10, unread: true })
90 83
91 expect(res.body.total).to.equal(0) 84 expect(body.total).to.equal(0)
92 expect(res.body.data).to.have.lengthOf(0) 85 expect(body.data).to.have.lengthOf(0)
93 }) 86 })
94 }) 87 })
95 88
@@ -101,97 +94,100 @@ describe('Test notifications API', function () {
101 server: server, 94 server: server,
102 emails, 95 emails,
103 socketNotifications: userNotifications, 96 socketNotifications: userNotifications,
104 token: userAccessToken 97 token: userToken
105 } 98 }
106 }) 99 })
107 100
108 it('Should not have notifications', async function () { 101 it('Should not have notifications', async function () {
109 this.timeout(20000) 102 this.timeout(20000)
110 103
111 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 104 await server.notifications.updateMySettings({
112 newVideoFromSubscription: UserNotificationSettingValue.NONE 105 token: userToken,
113 })) 106 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.NONE }
107 })
114 108
115 { 109 {
116 const res = await getMyUserInformation(server.url, userAccessToken) 110 const info = await server.users.getMyInfo({ token: userToken })
117 const info = res.body as User
118 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE) 111 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.NONE)
119 } 112 }
120 113
121 const { name, uuid } = await uploadRandomVideo(server) 114 const { name, uuid } = await server.videos.randomUpload()
122 115
123 const check = { web: true, mail: true } 116 const check = { web: true, mail: true }
124 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') 117 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
125 }) 118 })
126 119
127 it('Should only have web notifications', async function () { 120 it('Should only have web notifications', async function () {
128 this.timeout(20000) 121 this.timeout(20000)
129 122
130 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 123 await server.notifications.updateMySettings({
131 newVideoFromSubscription: UserNotificationSettingValue.WEB 124 token: userToken,
132 })) 125 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.WEB }
126 })
133 127
134 { 128 {
135 const res = await getMyUserInformation(server.url, userAccessToken) 129 const info = await server.users.getMyInfo({ token: userToken })
136 const info = res.body as User
137 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB) 130 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.WEB)
138 } 131 }
139 132
140 const { name, uuid } = await uploadRandomVideo(server) 133 const { name, uuid } = await server.videos.randomUpload()
141 134
142 { 135 {
143 const check = { mail: true, web: false } 136 const check = { mail: true, web: false }
144 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') 137 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
145 } 138 }
146 139
147 { 140 {
148 const check = { mail: false, web: true } 141 const check = { mail: false, web: true }
149 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') 142 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
150 } 143 }
151 }) 144 })
152 145
153 it('Should only have mail notifications', async function () { 146 it('Should only have mail notifications', async function () {
154 this.timeout(20000) 147 this.timeout(20000)
155 148
156 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 149 await server.notifications.updateMySettings({
157 newVideoFromSubscription: UserNotificationSettingValue.EMAIL 150 token: userToken,
158 })) 151 settings: { ...getAllNotificationsSettings(), newVideoFromSubscription: UserNotificationSettingValue.EMAIL }
152 })
159 153
160 { 154 {
161 const res = await getMyUserInformation(server.url, userAccessToken) 155 const info = await server.users.getMyInfo({ token: userToken })
162 const info = res.body as User
163 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL) 156 expect(info.notificationSettings.newVideoFromSubscription).to.equal(UserNotificationSettingValue.EMAIL)
164 } 157 }
165 158
166 const { name, uuid } = await uploadRandomVideo(server) 159 const { name, uuid } = await server.videos.randomUpload()
167 160
168 { 161 {
169 const check = { mail: false, web: true } 162 const check = { mail: false, web: true }
170 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'absence') 163 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'absence')
171 } 164 }
172 165
173 { 166 {
174 const check = { mail: true, web: false } 167 const check = { mail: true, web: false }
175 await checkNewVideoFromSubscription(immutableAssign(baseParams, { check }), name, uuid, 'presence') 168 await checkNewVideoFromSubscription({ ...baseParams, check }, name, uuid, 'presence')
176 } 169 }
177 }) 170 })
178 171
179 it('Should have email and web notifications', async function () { 172 it('Should have email and web notifications', async function () {
180 this.timeout(20000) 173 this.timeout(20000)
181 174
182 await updateMyNotificationSettings(server.url, userAccessToken, immutableAssign(getAllNotificationsSettings(), { 175 await server.notifications.updateMySettings({
183 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL 176 token: userToken,
184 })) 177 settings: {
178 ...getAllNotificationsSettings(),
179 newVideoFromSubscription: UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
180 }
181 })
185 182
186 { 183 {
187 const res = await getMyUserInformation(server.url, userAccessToken) 184 const info = await server.users.getMyInfo({ token: userToken })
188 const info = res.body as User
189 expect(info.notificationSettings.newVideoFromSubscription).to.equal( 185 expect(info.notificationSettings.newVideoFromSubscription).to.equal(
190 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL 186 UserNotificationSettingValue.WEB | UserNotificationSettingValue.EMAIL
191 ) 187 )
192 } 188 }
193 189
194 const { name, uuid } = await uploadRandomVideo(server) 190 const { name, uuid } = await server.videos.randomUpload()
195 191
196 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence') 192 await checkNewVideoFromSubscription(baseParams, name, uuid, 'presence')
197 }) 193 })