1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { io } from 'socket.io-client'
5 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
6 import { wait } from '@shared/core-utils'
7 import { HttpStatusCode, UserNotificationSetting, UserNotificationSettingValue } from '@shared/models'
15 setAccessTokensToServers
16 } from '@shared/server-commands'
18 describe('Test user notifications API validators', function () {
19 let server: PeerTubeServer
21 // ---------------------------------------------------------------
23 before(async function () {
26 server = await createSingleServer(1)
28 await setAccessTokensToServers([ server ])
31 describe('When listing my notifications', function () {
32 const path = '/api/v1/users/me/notifications'
34 it('Should fail with a bad start pagination', async function () {
35 await checkBadStartPagination(server.url, path, server.accessToken)
38 it('Should fail with a bad count pagination', async function () {
39 await checkBadCountPagination(server.url, path, server.accessToken)
42 it('Should fail with an incorrect sort', async function () {
43 await checkBadSortPagination(server.url, path, server.accessToken)
46 it('Should fail with an incorrect unread parameter', async function () {
47 await makeGetRequest({
53 token: server.accessToken,
54 expectedStatus: HttpStatusCode.OK_200
58 it('Should fail with a non authenticated user', async function () {
59 await makeGetRequest({
62 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
66 it('Should succeed with the correct parameters', async function () {
67 await makeGetRequest({
70 token: server.accessToken,
71 expectedStatus: HttpStatusCode.OK_200
76 describe('When marking as read my notifications', function () {
77 const path = '/api/v1/users/me/notifications/read'
79 it('Should fail with wrong ids parameters', async function () {
80 await makePostBodyRequest({
86 token: server.accessToken,
87 expectedStatus: HttpStatusCode.BAD_REQUEST_400
90 await makePostBodyRequest({
96 token: server.accessToken,
97 expectedStatus: HttpStatusCode.BAD_REQUEST_400
100 await makePostBodyRequest({
106 token: server.accessToken,
107 expectedStatus: HttpStatusCode.BAD_REQUEST_400
111 it('Should fail with a non authenticated user', async function () {
112 await makePostBodyRequest({
118 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
122 it('Should succeed with the correct parameters', async function () {
123 await makePostBodyRequest({
129 token: server.accessToken,
130 expectedStatus: HttpStatusCode.NO_CONTENT_204
135 describe('When marking as read my notifications', function () {
136 const path = '/api/v1/users/me/notifications/read-all'
138 it('Should fail with a non authenticated user', async function () {
139 await makePostBodyRequest({
142 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
146 it('Should succeed with the correct parameters', async function () {
147 await makePostBodyRequest({
150 token: server.accessToken,
151 expectedStatus: HttpStatusCode.NO_CONTENT_204
156 describe('When updating my notification settings', function () {
157 const path = '/api/v1/users/me/notification-settings'
158 const correctFields: UserNotificationSetting = {
159 newVideoFromSubscription: UserNotificationSettingValue.WEB,
160 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
161 abuseAsModerator: UserNotificationSettingValue.WEB,
162 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
163 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
164 myVideoImportFinished: UserNotificationSettingValue.WEB,
165 myVideoPublished: UserNotificationSettingValue.WEB,
166 commentMention: UserNotificationSettingValue.WEB,
167 newFollow: UserNotificationSettingValue.WEB,
168 newUserRegistration: UserNotificationSettingValue.WEB,
169 newInstanceFollower: UserNotificationSettingValue.WEB,
170 autoInstanceFollowing: UserNotificationSettingValue.WEB,
171 abuseNewMessage: UserNotificationSettingValue.WEB,
172 abuseStateChange: UserNotificationSettingValue.WEB,
173 newPeerTubeVersion: UserNotificationSettingValue.WEB,
174 newPluginVersion: UserNotificationSettingValue.WEB
177 it('Should fail with missing fields', async function () {
178 await makePutBodyRequest({
181 token: server.accessToken,
182 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
183 expectedStatus: HttpStatusCode.BAD_REQUEST_400
187 it('Should fail with incorrect field values', async function () {
189 const fields = { ...correctFields, newCommentOnMyVideo: 15 }
191 await makePutBodyRequest({
194 token: server.accessToken,
196 expectedStatus: HttpStatusCode.BAD_REQUEST_400
201 const fields = { ...correctFields, newCommentOnMyVideo: 'toto' }
203 await makePutBodyRequest({
207 token: server.accessToken,
208 expectedStatus: HttpStatusCode.BAD_REQUEST_400
213 it('Should fail with a non authenticated user', async function () {
214 await makePutBodyRequest({
217 fields: correctFields,
218 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
222 it('Should succeed with the correct parameters', async function () {
223 await makePutBodyRequest({
226 token: server.accessToken,
227 fields: correctFields,
228 expectedStatus: HttpStatusCode.NO_CONTENT_204
233 describe('When connecting to my notification socket', function () {
235 it('Should fail with no token', function (next) {
236 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
238 socket.once('connect_error', function () {
243 socket.on('connect', () => {
245 next(new Error('Connected with a missing token.'))
249 it('Should fail with an invalid token', function (next) {
250 const socket = io(`http://localhost:${server.port}/user-notifications`, {
251 query: { accessToken: 'bad_access_token' },
255 socket.once('connect_error', function () {
260 socket.on('connect', () => {
262 next(new Error('Connected with an invalid token.'))
266 it('Should success with the correct token', function (next) {
267 const socket = io(`http://localhost:${server.port}/user-notifications`, {
268 query: { accessToken: server.accessToken },
272 function errorListener (err) {
273 next(new Error('Error in connection: ' + err))
276 socket.on('connect_error', errorListener)
278 socket.once('connect', async () => {
287 after(async function () {
288 await cleanupTests([ server ])