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
110 it('Should fail with a non authenticated user', async function () {
111 await makePostBodyRequest({
117 statusCodeExpected: 401
121 it('Should succeed with the correct parameters', async function () {
122 await makePostBodyRequest({
128 token: server.accessToken,
129 statusCodeExpected: 204
134 describe('When updating my notification settings', function () {
135 const path = '/api/v1/users/me/notification-settings'
136 const correctFields: UserNotificationSetting = {
137 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION,
138 newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
139 videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION,
140 blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
141 myVideoImportFinished: UserNotificationSettingValue.WEB_NOTIFICATION,
142 myVideoPublished: UserNotificationSettingValue.WEB_NOTIFICATION,
143 commentMention: UserNotificationSettingValue.WEB_NOTIFICATION,
144 newFollow: UserNotificationSettingValue.WEB_NOTIFICATION,
145 newUserRegistration: UserNotificationSettingValue.WEB_NOTIFICATION
148 it('Should fail with missing fields', async function () {
149 await makePutBodyRequest({
152 token: server.accessToken,
153 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION },
154 statusCodeExpected: 400
158 it('Should fail with incorrect field values', async function () {
160 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
162 await makePutBodyRequest({
165 token: server.accessToken,
167 statusCodeExpected: 400
172 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
174 await makePutBodyRequest({
178 token: server.accessToken,
179 statusCodeExpected: 400
184 it('Should fail with a non authenticated user', async function () {
185 await makePutBodyRequest({
188 fields: correctFields,
189 statusCodeExpected: 401
193 it('Should succeed with the correct parameters', async function () {
194 await makePutBodyRequest({
197 token: server.accessToken,
198 fields: correctFields,
199 statusCodeExpected: 204
204 describe('When connecting to my notification socket', function () {
205 it('Should fail with no token', function (next) {
206 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
208 socket.on('error', () => {
209 socket.removeListener('error', this)
214 socket.on('connect', () => {
216 next(new Error('Connected with a missing token.'))
220 it('Should fail with an invalid token', function (next) {
221 const socket = io('http://localhost:9001/user-notifications', {
222 query: { accessToken: 'bad_access_token' },
226 socket.on('error', () => {
227 socket.removeListener('error', this)
232 socket.on('connect', () => {
234 next(new Error('Connected with an invalid token.'))
238 it('Should success with the correct token', function (next) {
239 const socket = io('http://localhost:9001/user-notifications', {
240 query: { accessToken: server.accessToken },
244 const errorListener = socket.on('error', err => {
245 next(new Error('Error in connection: ' + err))
248 socket.on('connect', async () => {
249 socket.removeListener('error', errorListener)
258 after(async function () {
259 killallServers([ server ])
261 // Keep the logs if the test failed