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 myVideoEditionFinished: UserNotificationSettingValue.WEB,
175 newPluginVersion: UserNotificationSettingValue.WEB
178 it('Should fail with missing fields', async function () {
179 await makePutBodyRequest({
182 token: server.accessToken,
183 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
184 expectedStatus: HttpStatusCode.BAD_REQUEST_400
188 it('Should fail with incorrect field values', async function () {
190 const fields = { ...correctFields, newCommentOnMyVideo: 15 }
192 await makePutBodyRequest({
195 token: server.accessToken,
197 expectedStatus: HttpStatusCode.BAD_REQUEST_400
202 const fields = { ...correctFields, newCommentOnMyVideo: 'toto' }
204 await makePutBodyRequest({
208 token: server.accessToken,
209 expectedStatus: HttpStatusCode.BAD_REQUEST_400
214 it('Should fail with a non authenticated user', async function () {
215 await makePutBodyRequest({
218 fields: correctFields,
219 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
223 it('Should succeed with the correct parameters', async function () {
224 await makePutBodyRequest({
227 token: server.accessToken,
228 fields: correctFields,
229 expectedStatus: HttpStatusCode.NO_CONTENT_204
234 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.once('connect_error', function () {
244 socket.on('connect', () => {
246 next(new Error('Connected with a missing token.'))
250 it('Should fail with an invalid token', function (next) {
251 const socket = io(`http://localhost:${server.port}/user-notifications`, {
252 query: { accessToken: 'bad_access_token' },
256 socket.once('connect_error', function () {
261 socket.on('connect', () => {
263 next(new Error('Connected with an invalid token.'))
267 it('Should success with the correct token', function (next) {
268 const socket = io(`http://localhost:${server.port}/user-notifications`, {
269 query: { accessToken: server.accessToken },
273 function errorListener (err) {
274 next(new Error('Error in connection: ' + err))
277 socket.on('connect_error', errorListener)
279 socket.once('connect', async () => {
288 after(async function () {
289 await cleanupTests([ server ])