1 /* tslint:disable:no-unused-expression */
4 import * as io from 'socket.io-client'
15 setAccessTokensToServers,
17 } from '../../../../shared/utils'
19 checkBadCountPagination,
20 checkBadSortPagination,
21 checkBadStartPagination
22 } from '../../../../shared/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 () {
35 server = await runServer(1)
37 await setAccessTokensToServers([ server ])
40 describe('When listing my notifications', function () {
41 const path = '/api/v1/users/me/notifications'
43 it('Should fail with a bad start pagination', async function () {
44 await checkBadStartPagination(server.url, path, server.accessToken)
47 it('Should fail with a bad count pagination', async function () {
48 await checkBadCountPagination(server.url, path, server.accessToken)
51 it('Should fail with an incorrect sort', async function () {
52 await checkBadSortPagination(server.url, path, server.accessToken)
55 it('Should fail with an incorrect unread parameter', async function () {
56 await makeGetRequest({
62 token: server.accessToken,
63 statusCodeExpected: 200
67 it('Should fail with a non authenticated user', async function () {
68 await makeGetRequest({
71 statusCodeExpected: 401
75 it('Should succeed with the correct parameters', async function () {
76 await makeGetRequest({
79 token: server.accessToken,
80 statusCodeExpected: 200
85 describe('When marking as read my notifications', function () {
86 const path = '/api/v1/users/me/notifications/read'
88 it('Should fail with wrong ids parameters', async function () {
89 await makePostBodyRequest({
95 token: server.accessToken,
96 statusCodeExpected: 400
99 await makePostBodyRequest({
105 token: server.accessToken,
106 statusCodeExpected: 400
109 await makePostBodyRequest({
115 token: server.accessToken,
116 statusCodeExpected: 400
120 it('Should fail with a non authenticated user', async function () {
121 await makePostBodyRequest({
127 statusCodeExpected: 401
131 it('Should succeed with the correct parameters', async function () {
132 await makePostBodyRequest({
138 token: server.accessToken,
139 statusCodeExpected: 204
144 describe('When marking as read my notifications', function () {
145 const path = '/api/v1/users/me/notifications/read-all'
147 it('Should fail with a non authenticated user', async function () {
148 await makePostBodyRequest({
151 statusCodeExpected: 401
155 it('Should succeed with the correct parameters', async function () {
156 await makePostBodyRequest({
159 token: server.accessToken,
160 statusCodeExpected: 204
165 describe('When updating my notification settings', function () {
166 const path = '/api/v1/users/me/notification-settings'
167 const correctFields: UserNotificationSetting = {
168 newVideoFromSubscription: UserNotificationSettingValue.WEB,
169 newCommentOnMyVideo: UserNotificationSettingValue.WEB,
170 videoAbuseAsModerator: UserNotificationSettingValue.WEB,
171 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
172 myVideoImportFinished: UserNotificationSettingValue.WEB,
173 myVideoPublished: UserNotificationSettingValue.WEB,
174 commentMention: UserNotificationSettingValue.WEB,
175 newFollow: UserNotificationSettingValue.WEB,
176 newUserRegistration: 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)
289 after(async function () {
290 killallServers([ server ])
292 // Keep the logs if the test failed