1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { io } from 'socket.io-client'
6 checkBadCountPagination,
7 checkBadSortPagination,
8 checkBadStartPagination,
15 setAccessTokensToServers,
17 } from '@shared/extra-utils'
18 import { HttpStatusCode, UserNotificationSetting, UserNotificationSettingValue } from '@shared/models'
20 describe('Test user notifications API validators', function () {
21 let server: PeerTubeServer
23 // ---------------------------------------------------------------
25 before(async function () {
28 server = await createSingleServer(1)
30 await setAccessTokensToServers([ server ])
33 describe('When listing my notifications', function () {
34 const path = '/api/v1/users/me/notifications'
36 it('Should fail with a bad start pagination', async function () {
37 await checkBadStartPagination(server.url, path, server.accessToken)
40 it('Should fail with a bad count pagination', async function () {
41 await checkBadCountPagination(server.url, path, server.accessToken)
44 it('Should fail with an incorrect sort', async function () {
45 await checkBadSortPagination(server.url, path, server.accessToken)
48 it('Should fail with an incorrect unread parameter', async function () {
49 await makeGetRequest({
55 token: server.accessToken,
56 expectedStatus: HttpStatusCode.OK_200
60 it('Should fail with a non authenticated user', async function () {
61 await makeGetRequest({
64 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
68 it('Should succeed with the correct parameters', async function () {
69 await makeGetRequest({
72 token: server.accessToken,
73 expectedStatus: HttpStatusCode.OK_200
78 describe('When marking as read my notifications', function () {
79 const path = '/api/v1/users/me/notifications/read'
81 it('Should fail with wrong ids parameters', async function () {
82 await makePostBodyRequest({
88 token: server.accessToken,
89 expectedStatus: HttpStatusCode.BAD_REQUEST_400
92 await makePostBodyRequest({
98 token: server.accessToken,
99 expectedStatus: HttpStatusCode.BAD_REQUEST_400
102 await makePostBodyRequest({
108 token: server.accessToken,
109 expectedStatus: HttpStatusCode.BAD_REQUEST_400
113 it('Should fail with a non authenticated user', async function () {
114 await makePostBodyRequest({
120 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
124 it('Should succeed with the correct parameters', async function () {
125 await makePostBodyRequest({
131 token: server.accessToken,
132 expectedStatus: HttpStatusCode.NO_CONTENT_204
137 describe('When marking as read my notifications', function () {
138 const path = '/api/v1/users/me/notifications/read-all'
140 it('Should fail with a non authenticated user', async function () {
141 await makePostBodyRequest({
144 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
148 it('Should succeed with the correct parameters', async function () {
149 await makePostBodyRequest({
152 token: server.accessToken,
153 expectedStatus: HttpStatusCode.NO_CONTENT_204
158 describe('When updating my notification settings', function () {
159 const path = '/api/v1/users/me/notification-settings'
160 const correctFields: UserNotificationSetting = {
161 newVideoFromSubscription: UserNotificationSettingValue.WEB,
162 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
163 abuseAsModerator: UserNotificationSettingValue.WEB,
164 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
165 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
166 myVideoImportFinished: UserNotificationSettingValue.WEB,
167 myVideoPublished: UserNotificationSettingValue.WEB,
168 commentMention: UserNotificationSettingValue.WEB,
169 newFollow: UserNotificationSettingValue.WEB,
170 newUserRegistration: UserNotificationSettingValue.WEB,
171 newInstanceFollower: UserNotificationSettingValue.WEB,
172 autoInstanceFollowing: UserNotificationSettingValue.WEB,
173 abuseNewMessage: UserNotificationSettingValue.WEB,
174 abuseStateChange: UserNotificationSettingValue.WEB,
175 newPeerTubeVersion: UserNotificationSettingValue.WEB,
176 newPluginVersion: UserNotificationSettingValue.WEB
179 it('Should fail with missing fields', async function () {
180 await makePutBodyRequest({
183 token: server.accessToken,
184 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
185 expectedStatus: HttpStatusCode.BAD_REQUEST_400
189 it('Should fail with incorrect field values', async function () {
191 const fields = { ...correctFields, newCommentOnMyVideo: 15 }
193 await makePutBodyRequest({
196 token: server.accessToken,
198 expectedStatus: HttpStatusCode.BAD_REQUEST_400
203 const fields = { ...correctFields, newCommentOnMyVideo: 'toto' }
205 await makePutBodyRequest({
209 token: server.accessToken,
210 expectedStatus: HttpStatusCode.BAD_REQUEST_400
215 it('Should fail with a non authenticated user', async function () {
216 await makePutBodyRequest({
219 fields: correctFields,
220 expectedStatus: HttpStatusCode.UNAUTHORIZED_401
224 it('Should succeed with the correct parameters', async function () {
225 await makePutBodyRequest({
228 token: server.accessToken,
229 fields: correctFields,
230 expectedStatus: HttpStatusCode.NO_CONTENT_204
235 describe('When connecting to my notification socket', function () {
237 it('Should fail with no token', function (next) {
238 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
240 socket.once('connect_error', function () {
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.once('connect_error', function () {
262 socket.on('connect', () => {
264 next(new Error('Connected with an invalid token.'))
268 it('Should success with the correct token', function (next) {
269 const socket = io(`http://localhost:${server.port}/user-notifications`, {
270 query: { accessToken: server.accessToken },
274 function errorListener (err) {
275 next(new Error('Error in connection: ' + err))
278 socket.on('connect_error', errorListener)
280 socket.once('connect', async () => {
289 after(async function () {
290 await cleanupTests([ server ])