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
182 it('Should fail with missing fields', async function () {
183 await makePutBodyRequest({
186 token: server.accessToken,
187 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
188 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
192 it('Should fail with incorrect field values', async function () {
194 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
196 await makePutBodyRequest({
199 token: server.accessToken,
201 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
206 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
208 await makePutBodyRequest({
212 token: server.accessToken,
213 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
218 it('Should fail with a non authenticated user', async function () {
219 await makePutBodyRequest({
222 fields: correctFields,
223 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
227 it('Should succeed with the correct parameters', async function () {
228 await makePutBodyRequest({
231 token: server.accessToken,
232 fields: correctFields,
233 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
238 describe('When connecting to my notification socket', function () {
240 it('Should fail with no token', function (next) {
241 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
243 socket.once('connect_error', function () {
248 socket.on('connect', () => {
250 next(new Error('Connected with a missing token.'))
254 it('Should fail with an invalid token', function (next) {
255 const socket = io(`http://localhost:${server.port}/user-notifications`, {
256 query: { accessToken: 'bad_access_token' },
260 socket.once('connect_error', function () {
265 socket.on('connect', () => {
267 next(new Error('Connected with an invalid token.'))
271 it('Should success with the correct token', function (next) {
272 const socket = io(`http://localhost:${server.port}/user-notifications`, {
273 query: { accessToken: server.accessToken },
277 function errorListener (err) {
278 next(new Error('Error in connection: ' + err))
281 socket.on('connect_error', errorListener)
283 socket.once('connect', async () => {
292 after(async function () {
293 await cleanupTests([ server ])