1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { io } from 'socket.io-client'
5 import { HttpStatusCode } from '@shared/core-utils'
7 checkBadCountPagination,
8 checkBadSortPagination,
9 checkBadStartPagination,
16 setAccessTokensToServers,
18 } from '@shared/extra-utils'
19 import { UserNotificationSetting, UserNotificationSettingValue } from '@shared/models'
21 describe('Test user notifications API validators', function () {
22 let server: PeerTubeServer
24 // ---------------------------------------------------------------
26 before(async function () {
29 server = await createSingleServer(1)
31 await setAccessTokensToServers([ server ])
34 describe('When listing my notifications', function () {
35 const path = '/api/v1/users/me/notifications'
37 it('Should fail with a bad start pagination', async function () {
38 await checkBadStartPagination(server.url, path, server.accessToken)
41 it('Should fail with a bad count pagination', async function () {
42 await checkBadCountPagination(server.url, path, server.accessToken)
45 it('Should fail with an incorrect sort', async function () {
46 await checkBadSortPagination(server.url, path, server.accessToken)
49 it('Should fail with an incorrect unread parameter', async function () {
50 await makeGetRequest({
56 token: server.accessToken,
57 statusCodeExpected: HttpStatusCode.OK_200
61 it('Should fail with a non authenticated user', async function () {
62 await makeGetRequest({
65 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
69 it('Should succeed with the correct parameters', async function () {
70 await makeGetRequest({
73 token: server.accessToken,
74 statusCodeExpected: HttpStatusCode.OK_200
79 describe('When marking as read my notifications', function () {
80 const path = '/api/v1/users/me/notifications/read'
82 it('Should fail with wrong ids parameters', async function () {
83 await makePostBodyRequest({
89 token: server.accessToken,
90 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
93 await makePostBodyRequest({
99 token: server.accessToken,
100 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
103 await makePostBodyRequest({
109 token: server.accessToken,
110 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
114 it('Should fail with a non authenticated user', async function () {
115 await makePostBodyRequest({
121 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
125 it('Should succeed with the correct parameters', async function () {
126 await makePostBodyRequest({
132 token: server.accessToken,
133 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
138 describe('When marking as read my notifications', function () {
139 const path = '/api/v1/users/me/notifications/read-all'
141 it('Should fail with a non authenticated user', async function () {
142 await makePostBodyRequest({
145 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
149 it('Should succeed with the correct parameters', async function () {
150 await makePostBodyRequest({
153 token: server.accessToken,
154 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
159 describe('When updating my notification settings', function () {
160 const path = '/api/v1/users/me/notification-settings'
161 const correctFields: UserNotificationSetting = {
162 newVideoFromSubscription: UserNotificationSettingValue.WEB,
163 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
164 abuseAsModerator: UserNotificationSettingValue.WEB,
165 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
166 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
167 myVideoImportFinished: UserNotificationSettingValue.WEB,
168 myVideoPublished: UserNotificationSettingValue.WEB,
169 commentMention: UserNotificationSettingValue.WEB,
170 newFollow: UserNotificationSettingValue.WEB,
171 newUserRegistration: UserNotificationSettingValue.WEB,
172 newInstanceFollower: UserNotificationSettingValue.WEB,
173 autoInstanceFollowing: UserNotificationSettingValue.WEB,
174 abuseNewMessage: UserNotificationSettingValue.WEB,
175 abuseStateChange: UserNotificationSettingValue.WEB,
176 newPeerTubeVersion: UserNotificationSettingValue.WEB,
177 newPluginVersion: UserNotificationSettingValue.WEB
180 it('Should fail with missing fields', async function () {
181 await makePutBodyRequest({
184 token: server.accessToken,
185 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
186 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
190 it('Should fail with incorrect field values', async function () {
192 const fields = { ...correctFields, newCommentOnMyVideo: 15 }
194 await makePutBodyRequest({
197 token: server.accessToken,
199 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
204 const fields = { ...correctFields, newCommentOnMyVideo: 'toto' }
206 await makePutBodyRequest({
210 token: server.accessToken,
211 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
216 it('Should fail with a non authenticated user', async function () {
217 await makePutBodyRequest({
220 fields: correctFields,
221 statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
225 it('Should succeed with the correct parameters', async function () {
226 await makePutBodyRequest({
229 token: server.accessToken,
230 fields: correctFields,
231 statusCodeExpected: HttpStatusCode.NO_CONTENT_204
236 describe('When connecting to my notification socket', function () {
238 it('Should fail with no token', function (next) {
239 const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
241 socket.once('connect_error', function () {
246 socket.on('connect', () => {
248 next(new Error('Connected with a missing token.'))
252 it('Should fail with an invalid token', function (next) {
253 const socket = io(`http://localhost:${server.port}/user-notifications`, {
254 query: { accessToken: 'bad_access_token' },
258 socket.once('connect_error', function () {
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 function errorListener (err) {
276 next(new Error('Error in connection: ' + err))
279 socket.on('connect_error', errorListener)
281 socket.once('connect', async () => {
290 after(async function () {
291 await cleanupTests([ server ])