1 /* tslint:disable:no-unused-expression */
4 import * as io from 'socket.io-client'
15 setAccessTokensToServers,
17 } from '../../../../shared/extra-utils'
19 checkBadCountPagination,
20 checkBadSortPagination,
21 checkBadStartPagination
22 } from '../../../../shared/extra-utils/requests/check-api-params'
23 import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
25 describe('Test user notifications API validators', function () {
26 let server: ServerInfo
28 // ---------------------------------------------------------------
30 before(async function () {
33 server = await flushAndRunServer(1)
35 await setAccessTokensToServers([ server ])
38 describe('When listing my notifications', function () {
39 const path = '/api/v1/users/me/notifications'
41 it('Should fail with a bad start pagination', async function () {
42 await checkBadStartPagination(server.url, path, server.accessToken)
45 it('Should fail with a bad count pagination', async function () {
46 await checkBadCountPagination(server.url, path, server.accessToken)
49 it('Should fail with an incorrect sort', async function () {
50 await checkBadSortPagination(server.url, path, server.accessToken)
53 it('Should fail with an incorrect unread parameter', async function () {
54 await makeGetRequest({
60 token: server.accessToken,
61 statusCodeExpected: 200
65 it('Should fail with a non authenticated user', async function () {
66 await makeGetRequest({
69 statusCodeExpected: 401
73 it('Should succeed with the correct parameters', async function () {
74 await makeGetRequest({
77 token: server.accessToken,
78 statusCodeExpected: 200
83 describe('When marking as read my notifications', function () {
84 const path = '/api/v1/users/me/notifications/read'
86 it('Should fail with wrong ids parameters', async function () {
87 await makePostBodyRequest({
93 token: server.accessToken,
94 statusCodeExpected: 400
97 await makePostBodyRequest({
103 token: server.accessToken,
104 statusCodeExpected: 400
107 await makePostBodyRequest({
113 token: server.accessToken,
114 statusCodeExpected: 400
118 it('Should fail with a non authenticated user', async function () {
119 await makePostBodyRequest({
125 statusCodeExpected: 401
129 it('Should succeed with the correct parameters', async function () {
130 await makePostBodyRequest({
136 token: server.accessToken,
137 statusCodeExpected: 204
142 describe('When marking as read my notifications', function () {
143 const path = '/api/v1/users/me/notifications/read-all'
145 it('Should fail with a non authenticated user', async function () {
146 await makePostBodyRequest({
149 statusCodeExpected: 401
153 it('Should succeed with the correct parameters', async function () {
154 await makePostBodyRequest({
157 token: server.accessToken,
158 statusCodeExpected: 204
163 describe('When updating my notification settings', function () {
164 const path = '/api/v1/users/me/notification-settings'
165 const correctFields: UserNotificationSetting = {
166 newVideoFromSubscription: UserNotificationSettingValue.WEB,
167 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
168 videoAbuseAsModerator: UserNotificationSettingValue.WEB,
169 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
170 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
171 myVideoImportFinished: UserNotificationSettingValue.WEB,
172 myVideoPublished: UserNotificationSettingValue.WEB,
173 commentMention: UserNotificationSettingValue.WEB,
174 newFollow: UserNotificationSettingValue.WEB,
175 newUserRegistration: UserNotificationSettingValue.WEB,
176 newInstanceFollower: UserNotificationSettingValue.WEB
179 it('Should fail with missing fields', async function () {
180 await makePutBodyRequest({
183 token: server.accessToken,
184 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
185 statusCodeExpected: 400
189 it('Should fail with incorrect field values', async function () {
191 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
193 await makePutBodyRequest({
196 token: server.accessToken,
198 statusCodeExpected: 400
203 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
205 await makePutBodyRequest({
209 token: server.accessToken,
210 statusCodeExpected: 400
215 it('Should fail with a non authenticated user', async function () {
216 await makePutBodyRequest({
219 fields: correctFields,
220 statusCodeExpected: 401
224 it('Should succeed with the correct parameters', async function () {
225 await makePutBodyRequest({
228 token: server.accessToken,
229 fields: correctFields,
230 statusCodeExpected: 204
235 describe('When connecting to my notification socket', function () {
236 it('Should fail with no token', function (next) {
237 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
239 socket.on('error', () => {
240 socket.removeListener('error', this)
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:9001/user-notifications', {
253 query: { accessToken: 'bad_access_token' },
257 socket.on('error', () => {
258 socket.removeListener('error', this)
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:9001/user-notifications', {
271 query: { accessToken: server.accessToken },
275 const errorListener = socket.on('error', err => {
276 next(new Error('Error in connection: ' + err))
279 socket.on('connect', async () => {
280 socket.removeListener('error', errorListener)
290 killallServers([ server ])