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 videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
172 blacklistOnMyVideo: UserNotificationSettingValue.WEB,
173 myVideoImportFinished: UserNotificationSettingValue.WEB,
174 myVideoPublished: UserNotificationSettingValue.WEB,
175 commentMention: UserNotificationSettingValue.WEB,
176 newFollow: UserNotificationSettingValue.WEB,
177 newUserRegistration: UserNotificationSettingValue.WEB,
178 newInstanceFollower: UserNotificationSettingValue.WEB
181 it('Should fail with missing fields', async function () {
182 await makePutBodyRequest({
185 token: server.accessToken,
186 fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
187 statusCodeExpected: 400
191 it('Should fail with incorrect field values', async function () {
193 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
195 await makePutBodyRequest({
198 token: server.accessToken,
200 statusCodeExpected: 400
205 const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
207 await makePutBodyRequest({
211 token: server.accessToken,
212 statusCodeExpected: 400
217 it('Should fail with a non authenticated user', async function () {
218 await makePutBodyRequest({
221 fields: correctFields,
222 statusCodeExpected: 401
226 it('Should succeed with the correct parameters', async function () {
227 await makePutBodyRequest({
230 token: server.accessToken,
231 fields: correctFields,
232 statusCodeExpected: 204
237 describe('When connecting to my notification socket', function () {
238 it('Should fail with no token', function (next) {
239 const socket = io('http://localhost:9001/user-notifications', { reconnection: false })
241 socket.on('error', () => {
242 socket.removeListener('error', this)
247 socket.on('connect', () => {
249 next(new Error('Connected with a missing token.'))
253 it('Should fail with an invalid token', function (next) {
254 const socket = io('http://localhost:9001/user-notifications', {
255 query: { accessToken: 'bad_access_token' },
259 socket.on('error', () => {
260 socket.removeListener('error', this)
265 socket.on('connect', () => {
267 next(new Error('Connected with an invalid token.'))
271 it('Should success with the correct token', function (next) {
272 const socket = io('http://localhost:9001/user-notifications', {
273 query: { accessToken: server.accessToken },
277 const errorListener = socket.on('error', err => {
278 next(new Error('Error in connection: ' + err))
281 socket.on('connect', async () => {
282 socket.removeListener('error', errorListener)
291 after(async function () {
292 killallServers([ server ])
294 // Keep the logs if the test failed