]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/user-notifications.ts
Add user notification base code
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / user-notifications.ts
CommitLineData
cef534ed
C
1/* tslint:disable:no-unused-expression */
2
3import 'mocha'
4import * as io from 'socket.io-client'
5
6import {
7 flushTests,
8 immutableAssign,
9 killallServers,
10 makeGetRequest,
11 makePostBodyRequest,
12 makePutBodyRequest,
13 runServer,
14 ServerInfo,
15 setAccessTokensToServers,
16 wait
17} from '../../../../shared/utils'
18import {
19 checkBadCountPagination,
20 checkBadSortPagination,
21 checkBadStartPagination
22} from '../../../../shared/utils/requests/check-api-params'
23import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
24
25describe('Test user notifications API validators', function () {
26 let server: ServerInfo
27
28 // ---------------------------------------------------------------
29
30 before(async function () {
31 this.timeout(30000)
32
33 await flushTests()
34
35 server = await runServer(1)
36
37 await setAccessTokensToServers([ server ])
38 })
39
40 describe('When listing my notifications', function () {
41 const path = '/api/v1/users/me/notifications'
42
43 it('Should fail with a bad start pagination', async function () {
44 await checkBadStartPagination(server.url, path, server.accessToken)
45 })
46
47 it('Should fail with a bad count pagination', async function () {
48 await checkBadCountPagination(server.url, path, server.accessToken)
49 })
50
51 it('Should fail with an incorrect sort', async function () {
52 await checkBadSortPagination(server.url, path, server.accessToken)
53 })
54
55 it('Should fail with a non authenticated user', async function () {
56 await makeGetRequest({
57 url: server.url,
58 path,
59 statusCodeExpected: 401
60 })
61 })
62
63 it('Should succeed with the correct parameters', async function () {
64 await makeGetRequest({
65 url: server.url,
66 path,
67 token: server.accessToken,
68 statusCodeExpected: 200
69 })
70 })
71 })
72
73 describe('When marking as read my notifications', function () {
74 const path = '/api/v1/users/me/notifications/read'
75
76 it('Should fail with wrong ids parameters', async function () {
77 await makePostBodyRequest({
78 url: server.url,
79 path,
80 fields: {
81 ids: [ 'hello' ]
82 },
83 token: server.accessToken,
84 statusCodeExpected: 400
85 })
86
87 await makePostBodyRequest({
88 url: server.url,
89 path,
90 fields: {
91 ids: 5
92 },
93 token: server.accessToken,
94 statusCodeExpected: 400
95 })
96 })
97
98 it('Should fail with a non authenticated user', async function () {
99 await makePostBodyRequest({
100 url: server.url,
101 path,
102 fields: {
103 ids: [ 5 ]
104 },
105 statusCodeExpected: 401
106 })
107 })
108
109 it('Should succeed with the correct parameters', async function () {
110 await makePostBodyRequest({
111 url: server.url,
112 path,
113 fields: {
114 ids: [ 5 ]
115 },
116 token: server.accessToken,
117 statusCodeExpected: 204
118 })
119 })
120 })
121
122 describe('When updating my notification settings', function () {
123 const path = '/api/v1/users/me/notification-settings'
124 const correctFields: UserNotificationSetting = {
125 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION,
126 newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
127 videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION,
128 blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION
129 }
130
131 it('Should fail with missing fields', async function () {
132 await makePutBodyRequest({
133 url: server.url,
134 path,
135 token: server.accessToken,
136 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION },
137 statusCodeExpected: 400
138 })
139 })
140
141 it('Should fail with incorrect field values', async function () {
142 {
143 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
144
145 await makePutBodyRequest({
146 url: server.url,
147 path,
148 token: server.accessToken,
149 fields,
150 statusCodeExpected: 400
151 })
152 }
153
154 {
155 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
156
157 await makePutBodyRequest({
158 url: server.url,
159 path,
160 fields,
161 token: server.accessToken,
162 statusCodeExpected: 400
163 })
164 }
165 })
166
167 it('Should fail with a non authenticated user', async function () {
168 await makePutBodyRequest({
169 url: server.url,
170 path,
171 fields: correctFields,
172 statusCodeExpected: 401
173 })
174 })
175
176 it('Should succeed with the correct parameters', async function () {
177 await makePutBodyRequest({
178 url: server.url,
179 path,
180 token: server.accessToken,
181 fields: correctFields,
182 statusCodeExpected: 204
183 })
184 })
185 })
186
187 describe('When connecting to my notification socket', function () {
188 it('Should fail with no token', function (next) {
189 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
190
191 socket.on('error', () => {
192 socket.removeListener('error', this)
193 socket.disconnect()
194 next()
195 })
196
197 socket.on('connect', () => {
198 socket.disconnect()
199 next(new Error('Connected with a missing token.'))
200 })
201 })
202
203 it('Should fail with an invalid token', function (next) {
204 const socket = io('http://localhost:9001/user-notifications', {
205 query: { accessToken: 'bad_access_token' },
206 reconnection: false
207 })
208
209 socket.on('error', () => {
210 socket.removeListener('error', this)
211 socket.disconnect()
212 next()
213 })
214
215 socket.on('connect', () => {
216 socket.disconnect()
217 next(new Error('Connected with an invalid token.'))
218 })
219 })
220
221 it('Should success with the correct token', function (next) {
222 const socket = io('http://localhost:9001/user-notifications', {
223 query: { accessToken: server.accessToken },
224 reconnection: false
225 })
226
227 const errorListener = socket.on('error', err => {
228 next(new Error('Error in connection: ' + err))
229 })
230
231 socket.on('connect', async () => {
232 socket.removeListener('error', errorListener)
233 socket.disconnect()
234
235 await wait(500)
236 next()
237 })
238 })
239 })
240
241 after(async function () {
242 killallServers([ server ])
243
244 // Keep the logs if the test failed
245 if (this['ok']) {
246 await flushTests()
247 }
248 })
249})