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 videoAbuseAsModerator: 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
179 it('Should fail with missing fields', async function () {
180 await makePutBodyRequest({
183 token: server.accessToken,
184 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
185 statusCodeExpected: 400
189 it('Should fail with incorrect field values', async function () {
191 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
193 await makePutBodyRequest({
196 token: server.accessToken,
198 statusCodeExpected: 400
203 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
205 await makePutBodyRequest({
209 token: server.accessToken,
210 statusCodeExpected: 400
215 it('Should fail with a non authenticated user', async function () {
216 await makePutBodyRequest({
219 fields: correctFields,
220 statusCodeExpected: 401
224 it('Should succeed with the correct parameters', async function () {
225 await makePutBodyRequest({
228 token: server.accessToken,
229 fields: correctFields,
230 statusCodeExpected: 204
235 describe('When connecting to my notification socket', function () {
236 it('Should fail with no token', function (next) {
237 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
239 socket.on('error', () => {
240 socket.removeListener('error', this)
245 socket.on('connect', () => {
247 next(new Error('Connected with a missing token.'))
251 it('Should fail with an invalid token', function (next) {
252 const socket = io(`http://localhost:${server.port}/user-notifications`, {
253 query: { accessToken: 'bad_access_token' },
257 socket.on('error', () => {
258 socket.removeListener('error', this)
263 socket.on('connect', () => {
265 next(new Error('Connected with an invalid token.'))
269 it('Should success with the correct token', function (next) {
270 const socket = io(`http://localhost:${server.port}/user-notifications`, {
271 query: { accessToken: server.accessToken },
275 const errorListener = socket.on('error', err => {
276 next(new Error('Error in connection: ' + err))
279 socket.on('connect', async () => {
280 socket.removeListener('error', errorListener)
289 after(async function () {
290 await cleanupTests([ server ])