1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { io } from 'socket.io-client'
4 import { checkBadCountPagination, checkBadSortPagination, checkBadStartPagination } from '@server/tests/shared'
5 import { wait } from '@shared/core-utils'
6 import { HttpStatusCode, UserNotificationSetting, UserNotificationSettingValue } from '@shared/models'
14 setAccessTokensToServers
15 } from '@shared/server-commands'
17 describe('Test user notifications API validators', function () {
18 let server: PeerTubeServer
20 // ---------------------------------------------------------------
22 before(async function () {
25 server = await createSingleServer(1)
27 await setAccessTokensToServers([ server ])
30 describe('When listing my notifications', function () {
31 const path = '/api/v1/users/me/notifications'
33 it('Should fail with a bad start pagination', async function () {
34 await checkBadStartPagination(server.url, path, server.accessToken)
37 it('Should fail with a bad count pagination', async function () {
38 await checkBadCountPagination(server.url, path, server.accessToken)
41 it('Should fail with an incorrect sort', async function () {
42 await checkBadSortPagination(server.url, path, server.accessToken)
45 it('Should fail with an incorrect unread parameter', async function () {
46 await makeGetRequest({
52 token: server.accessToken,
53 expectedStatus: HttpStatusCode.OK_200
57 it('Should fail with a non authenticated user', async function () {
58 await makeGetRequest({
61 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
65 it('Should succeed with the correct parameters', async function () {
66 await makeGetRequest({
69 token: server.accessToken,
70 expectedStatus: HttpStatusCode.OK_200
75 describe('When marking as read my notifications', function () {
76 const path = '/api/v1/users/me/notifications/read'
78 it('Should fail with wrong ids parameters', async function () {
79 await makePostBodyRequest({
85 token: server.accessToken,
86 expectedStatus: HttpStatusCode.BAD_REQUEST_400
89 await makePostBodyRequest({
95 token: server.accessToken,
96 expectedStatus: HttpStatusCode.BAD_REQUEST_400
99 await makePostBodyRequest({
105 token: server.accessToken,
106 expectedStatus: HttpStatusCode.BAD_REQUEST_400
110 it('Should fail with a non authenticated user', async function () {
111 await makePostBodyRequest({
117 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
121 it('Should succeed with the correct parameters', async function () {
122 await makePostBodyRequest({
128 token: server.accessToken,
129 expectedStatus: HttpStatusCode.NO_CONTENT_204
134 describe('When marking as read my notifications', function () {
135 const path = '/api/v1/users/me/notifications/read-all'
137 it('Should fail with a non authenticated user', async function () {
138 await makePostBodyRequest({
141 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
145 it('Should succeed with the correct parameters', async function () {
146 await makePostBodyRequest({
149 token: server.accessToken,
150 expectedStatus: HttpStatusCode.NO_CONTENT_204
155 describe('When updating my notification settings', function () {
156 const path = '/api/v1/users/me/notification-settings'
157 const correctFields: UserNotificationSetting = {
158 newVideoFromSubscription: UserNotificationSettingValue.WEB,
159 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
160 abuseAsModerator: UserNotificationSettingValue.WEB,
161 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
162 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
163 myVideoImportFinished: UserNotificationSettingValue.WEB,
164 myVideoPublished: UserNotificationSettingValue.WEB,
165 commentMention: UserNotificationSettingValue.WEB,
166 newFollow: UserNotificationSettingValue.WEB,
167 newUserRegistration: UserNotificationSettingValue.WEB,
168 newInstanceFollower: UserNotificationSettingValue.WEB,
169 autoInstanceFollowing: UserNotificationSettingValue.WEB,
170 abuseNewMessage: UserNotificationSettingValue.WEB,
171 abuseStateChange: UserNotificationSettingValue.WEB,
172 newPeerTubeVersion: UserNotificationSettingValue.WEB,
173 myVideoStudioEditionFinished: 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(`${server.url}/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(`${server.url}/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(`${server.url}/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 ])