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
145 it('Should fail with missing fields', async function () {
146 await makePutBodyRequest({
149 token: server.accessToken,
150 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION },
151 statusCodeExpected: 400
155 it('Should fail with incorrect field values', async function () {
157 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
159 await makePutBodyRequest({
162 token: server.accessToken,
164 statusCodeExpected: 400
169 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
171 await makePutBodyRequest({
175 token: server.accessToken,
176 statusCodeExpected: 400
181 it('Should fail with a non authenticated user', async function () {
182 await makePutBodyRequest({
185 fields: correctFields,
186 statusCodeExpected: 401
190 it('Should succeed with the correct parameters', async function () {
191 await makePutBodyRequest({
194 token: server.accessToken,
195 fields: correctFields,
196 statusCodeExpected: 204
201 describe('When connecting to my notification socket', function () {
202 it('Should fail with no token', function (next) {
203 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
205 socket.on('error', () => {
206 socket.removeListener('error', this)
211 socket.on('connect', () => {
213 next(new Error('Connected with a missing token.'))
217 it('Should fail with an invalid token', function (next) {
218 const socket = io('http://localhost:9001/user-notifications', {
219 query: { accessToken: 'bad_access_token' },
223 socket.on('error', () => {
224 socket.removeListener('error', this)
229 socket.on('connect', () => {
231 next(new Error('Connected with an invalid token.'))
235 it('Should success with the correct token', function (next) {
236 const socket = io('http://localhost:9001/user-notifications', {
237 query: { accessToken: server.accessToken },
241 const errorListener = socket.on('error', err => {
242 next(new Error('Error in connection: ' + err))
245 socket.on('connect', async () => {
246 socket.removeListener('error', errorListener)
255 after(async function () {
256 killallServers([ server ])
258 // Keep the logs if the test failed