1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { io } from 'socket.io-client'
14 setAccessTokensToServers,
16 } from '../../../../shared/extra-utils'
18 checkBadCountPagination,
19 checkBadSortPagination,
20 checkBadStartPagination
21 } from '../../../../shared/extra-utils/requests/check-api-params'
22 import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
23 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
25 describe('Test user notifications API validators', function () {
26 let server: ServerInfo
28 // ---------------------------------------------------------------
30 before(async function () {
33 server = await flushAndRunServer(1)
35 await setAccessTokensToServers([ server ])
38 describe('When listing my notifications', function () {
39 const path = '/api/v1/users/me/notifications'
41 it('Should fail with a bad start pagination', async function () {
42 await checkBadStartPagination(server.url, path, server.accessToken)
45 it('Should fail with a bad count pagination', async function () {
46 await checkBadCountPagination(server.url, path, server.accessToken)
49 it('Should fail with an incorrect sort', async function () {
50 await checkBadSortPagination(server.url, path, server.accessToken)
53 it('Should fail with an incorrect unread parameter', async function () {
54 await makeGetRequest({
60 token: server.accessToken,
61 statusCodeExpected: HttpStatusCode.OK_200
65 it('Should fail with a non authenticated user', async function () {
66 await makeGetRequest({
69 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
73 it('Should succeed with the correct parameters', async function () {
74 await makeGetRequest({
77 token: server.accessToken,
78 statusCodeExpected: HttpStatusCode.OK_200
83 describe('When marking as read my notifications', function () {
84 const path = '/api/v1/users/me/notifications/read'
86 it('Should fail with wrong ids parameters', async function () {
87 await makePostBodyRequest({
93 token: server.accessToken,
94 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
97 await makePostBodyRequest({
103 token: server.accessToken,
104 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
107 await makePostBodyRequest({
113 token: server.accessToken,
114 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
118 it('Should fail with a non authenticated user', async function () {
119 await makePostBodyRequest({
125 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
129 it('Should succeed with the correct parameters', async function () {
130 await makePostBodyRequest({
136 token: server.accessToken,
137 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
142 describe('When marking as read my notifications', function () {
143 const path = '/api/v1/users/me/notifications/read-all'
145 it('Should fail with a non authenticated user', async function () {
146 await makePostBodyRequest({
149 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
153 it('Should succeed with the correct parameters', async function () {
154 await makePostBodyRequest({
157 token: server.accessToken,
158 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
163 describe('When updating my notification settings', function () {
164 const path = '/api/v1/users/me/notification-settings'
165 const correctFields: UserNotificationSetting = {
166 newVideoFromSubscription: UserNotificationSettingValue.WEB,
167 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
168 abuseAsModerator: UserNotificationSettingValue.WEB,
169 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
170 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
171 myVideoImportFinished: UserNotificationSettingValue.WEB,
172 myVideoPublished: UserNotificationSettingValue.WEB,
173 commentMention: UserNotificationSettingValue.WEB,
174 newFollow: UserNotificationSettingValue.WEB,
175 newUserRegistration: UserNotificationSettingValue.WEB,
176 newInstanceFollower: UserNotificationSettingValue.WEB,
177 autoInstanceFollowing: UserNotificationSettingValue.WEB,
178 abuseNewMessage: UserNotificationSettingValue.WEB,
179 abuseStateChange: UserNotificationSettingValue.WEB,
180 newPeerTubeVersion: UserNotificationSettingValue.WEB,
181 newPluginVersion: UserNotificationSettingValue.WEB
184 it('Should fail with missing fields', async function () {
185 await makePutBodyRequest({
188 token: server.accessToken,
189 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
190 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
194 it('Should fail with incorrect field values', async function () {
196 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
198 await makePutBodyRequest({
201 token: server.accessToken,
203 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
208 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
210 await makePutBodyRequest({
214 token: server.accessToken,
215 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
220 it('Should fail with a non authenticated user', async function () {
221 await makePutBodyRequest({
224 fields: correctFields,
225 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
229 it('Should succeed with the correct parameters', async function () {
230 await makePutBodyRequest({
233 token: server.accessToken,
234 fields: correctFields,
235 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
240 describe('When connecting to my notification socket', function () {
242 it('Should fail with no token', function (next) {
243 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
245 socket.once('connect_error', function () {
250 socket.on('connect', () => {
252 next(new Error('Connected with a missing token.'))
256 it('Should fail with an invalid token', function (next) {
257 const socket = io(`http://localhost:${server.port}/user-notifications`, {
258 query: { accessToken: 'bad_access_token' },
262 socket.once('connect_error', function () {
267 socket.on('connect', () => {
269 next(new Error('Connected with an invalid token.'))
273 it('Should success with the correct token', function (next) {
274 const socket = io(`http://localhost:${server.port}/user-notifications`, {
275 query: { accessToken: server.accessToken },
279 function errorListener (err) {
280 next(new Error('Error in connection: ' + err))
283 socket.on('connect_error', errorListener)
285 socket.once('connect', async () => {
294 after(async function () {
295 await cleanupTests([ server ])