1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as 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'
24 describe('Test user notifications API validators', function () {
25 let server: ServerInfo
27 // ---------------------------------------------------------------
29 before(async function () {
32 server = await flushAndRunServer(1)
34 await setAccessTokensToServers([ server ])
37 describe('When listing my notifications', function () {
38 const path = '/api/v1/users/me/notifications'
40 it('Should fail with a bad start pagination', async function () {
41 await checkBadStartPagination(server.url, path, server.accessToken)
44 it('Should fail with a bad count pagination', async function () {
45 await checkBadCountPagination(server.url, path, server.accessToken)
48 it('Should fail with an incorrect sort', async function () {
49 await checkBadSortPagination(server.url, path, server.accessToken)
52 it('Should fail with an incorrect unread parameter', async function () {
53 await makeGetRequest({
59 token: server.accessToken,
60 statusCodeExpected: 200
64 it('Should fail with a non authenticated user', async function () {
65 await makeGetRequest({
68 statusCodeExpected: 401
72 it('Should succeed with the correct parameters', async function () {
73 await makeGetRequest({
76 token: server.accessToken,
77 statusCodeExpected: 200
82 describe('When marking as read my notifications', function () {
83 const path = '/api/v1/users/me/notifications/read'
85 it('Should fail with wrong ids parameters', async function () {
86 await makePostBodyRequest({
92 token: server.accessToken,
93 statusCodeExpected: 400
96 await makePostBodyRequest({
102 token: server.accessToken,
103 statusCodeExpected: 400
106 await makePostBodyRequest({
112 token: server.accessToken,
113 statusCodeExpected: 400
117 it('Should fail with a non authenticated user', async function () {
118 await makePostBodyRequest({
124 statusCodeExpected: 401
128 it('Should succeed with the correct parameters', async function () {
129 await makePostBodyRequest({
135 token: server.accessToken,
136 statusCodeExpected: 204
141 describe('When marking as read my notifications', function () {
142 const path = '/api/v1/users/me/notifications/read-all'
144 it('Should fail with a non authenticated user', async function () {
145 await makePostBodyRequest({
148 statusCodeExpected: 401
152 it('Should succeed with the correct parameters', async function () {
153 await makePostBodyRequest({
156 token: server.accessToken,
157 statusCodeExpected: 204
162 describe('When updating my notification settings', function () {
163 const path = '/api/v1/users/me/notification-settings'
164 const correctFields: UserNotificationSetting = {
165 newVideoFromSubscription: UserNotificationSettingValue.WEB,
166 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
167 abuseAsModerator: UserNotificationSettingValue.WEB,
168 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
169 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
170 myVideoImportFinished: UserNotificationSettingValue.WEB,
171 myVideoPublished: UserNotificationSettingValue.WEB,
172 commentMention: UserNotificationSettingValue.WEB,
173 newFollow: UserNotificationSettingValue.WEB,
174 newUserRegistration: UserNotificationSettingValue.WEB,
175 newInstanceFollower: UserNotificationSettingValue.WEB,
176 autoInstanceFollowing: UserNotificationSettingValue.WEB,
177 abuseNewMessage: UserNotificationSettingValue.WEB,
178 abuseStateChange: UserNotificationSettingValue.WEB
181 it('Should fail with missing fields', async function () {
182 await makePutBodyRequest({
185 token: server.accessToken,
186 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
187 statusCodeExpected: 400
191 it('Should fail with incorrect field values', async function () {
193 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
195 await makePutBodyRequest({
198 token: server.accessToken,
200 statusCodeExpected: 400
205 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
207 await makePutBodyRequest({
211 token: server.accessToken,
212 statusCodeExpected: 400
217 it('Should fail with a non authenticated user', async function () {
218 await makePutBodyRequest({
221 fields: correctFields,
222 statusCodeExpected: 401
226 it('Should succeed with the correct parameters', async function () {
227 await makePutBodyRequest({
230 token: server.accessToken,
231 fields: correctFields,
232 statusCodeExpected: 204
237 describe('When connecting to my notification socket', function () {
238 it('Should fail with no token', function (next) {
239 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
241 socket.on('error', function () {
242 socket.removeListener('error', this)
247 socket.on('connect', () => {
249 next(new Error('Connected with a missing token.'))
253 it('Should fail with an invalid token', function (next) {
254 const socket = io(`http://localhost:${server.port}/user-notifications`, {
255 query: { accessToken: 'bad_access_token' },
259 socket.on('error', function () {
260 socket.removeListener('error', this)
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('error', errorListener)
283 socket.on('connect', async () => {
284 socket.removeListener('error', errorListener)
293 after(async function () {
294 await cleanupTests([ server ])