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 a non authenticated user', async function () {
56 await makeGetRequest({
59 statusCodeExpected: 401
63 it('Should succeed with the correct parameters', async function () {
64 await makeGetRequest({
67 token: server.accessToken,
68 statusCodeExpected: 200
73 describe('When marking as read my notifications', function () {
74 const path = '/api/v1/users/me/notifications/read'
76 it('Should fail with wrong ids parameters', async function () {
77 await makePostBodyRequest({
83 token: server.accessToken,
84 statusCodeExpected: 400
87 await makePostBodyRequest({
93 token: server.accessToken,
94 statusCodeExpected: 400
98 it('Should fail with a non authenticated user', async function () {
99 await makePostBodyRequest({
105 statusCodeExpected: 401
109 it('Should succeed with the correct parameters', async function () {
110 await makePostBodyRequest({
116 token: server.accessToken,
117 statusCodeExpected: 204
122 describe('When updating my notification settings', function () {
123 const path = '/api/v1/users/me/notification-settings'
124 const correctFields: UserNotificationSetting = {
125 newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION,
126 newCommentOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION,
127 videoAbuseAsModerator: UserNotificationSettingValue.WEB_NOTIFICATION,
128 blacklistOnMyVideo: UserNotificationSettingValue.WEB_NOTIFICATION
131 it('Should fail with missing fields', async function () {
132 await makePutBodyRequest({
135 token: server.accessToken,
136 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB_NOTIFICATION },
137 statusCodeExpected: 400
141 it('Should fail with incorrect field values', async function () {
143 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
145 await makePutBodyRequest({
148 token: server.accessToken,
150 statusCodeExpected: 400
155 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
157 await makePutBodyRequest({
161 token: server.accessToken,
162 statusCodeExpected: 400
167 it('Should fail with a non authenticated user', async function () {
168 await makePutBodyRequest({
171 fields: correctFields,
172 statusCodeExpected: 401
176 it('Should succeed with the correct parameters', async function () {
177 await makePutBodyRequest({
180 token: server.accessToken,
181 fields: correctFields,
182 statusCodeExpected: 204
187 describe('When connecting to my notification socket', function () {
188 it('Should fail with no token', function (next) {
189 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
191 socket.on('error', () => {
192 socket.removeListener('error', this)
197 socket.on('connect', () => {
199 next(new Error('Connected with a missing token.'))
203 it('Should fail with an invalid token', function (next) {
204 const socket = io('http://localhost:9001/user-notifications', {
205 query: { accessToken: 'bad_access_token' },
209 socket.on('error', () => {
210 socket.removeListener('error', this)
215 socket.on('connect', () => {
217 next(new Error('Connected with an invalid token.'))
221 it('Should success with the correct token', function (next) {
222 const socket = io('http://localhost:9001/user-notifications', {
223 query: { accessToken: server.accessToken },
227 const errorListener = socket.on('error', err => {
228 next(new Error('Error in connection: ' + err))
231 socket.on('connect', async () => {
232 socket.removeListener('error', errorListener)
241 after(async function () {
242 killallServers([ server ])
244 // Keep the logs if the test failed