user-notifications.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import 'mocha'
  3. import { io } from 'socket.io-client'
  4. import {
  5. cleanupTests,
  6. flushAndRunServer,
  7. immutableAssign,
  8. makeGetRequest,
  9. makePostBodyRequest,
  10. makePutBodyRequest,
  11. ServerInfo,
  12. setAccessTokensToServers,
  13. wait
  14. } from '../../../../shared/extra-utils'
  15. import {
  16. checkBadCountPagination,
  17. checkBadSortPagination,
  18. checkBadStartPagination
  19. } from '../../../../shared/extra-utils/requests/check-api-params'
  20. import { UserNotificationSetting, UserNotificationSettingValue } from '../../../../shared/models/users'
  21. import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
  22. describe('Test user notifications API validators', function () {
  23. let server: ServerInfo
  24. // ---------------------------------------------------------------
  25. before(async function () {
  26. this.timeout(30000)
  27. server = await flushAndRunServer(1)
  28. await setAccessTokensToServers([ server ])
  29. })
  30. describe('When listing my notifications', function () {
  31. const path = '/api/v1/users/me/notifications'
  32. it('Should fail with a bad start pagination', async function () {
  33. await checkBadStartPagination(server.url, path, server.accessToken)
  34. })
  35. it('Should fail with a bad count pagination', async function () {
  36. await checkBadCountPagination(server.url, path, server.accessToken)
  37. })
  38. it('Should fail with an incorrect sort', async function () {
  39. await checkBadSortPagination(server.url, path, server.accessToken)
  40. })
  41. it('Should fail with an incorrect unread parameter', async function () {
  42. await makeGetRequest({
  43. url: server.url,
  44. path,
  45. query: {
  46. unread: 'toto'
  47. },
  48. token: server.accessToken,
  49. statusCodeExpected: HttpStatusCode.OK_200
  50. })
  51. })
  52. it('Should fail with a non authenticated user', async function () {
  53. await makeGetRequest({
  54. url: server.url,
  55. path,
  56. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  57. })
  58. })
  59. it('Should succeed with the correct parameters', async function () {
  60. await makeGetRequest({
  61. url: server.url,
  62. path,
  63. token: server.accessToken,
  64. statusCodeExpected: HttpStatusCode.OK_200
  65. })
  66. })
  67. })
  68. describe('When marking as read my notifications', function () {
  69. const path = '/api/v1/users/me/notifications/read'
  70. it('Should fail with wrong ids parameters', async function () {
  71. await makePostBodyRequest({
  72. url: server.url,
  73. path,
  74. fields: {
  75. ids: [ 'hello' ]
  76. },
  77. token: server.accessToken,
  78. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  79. })
  80. await makePostBodyRequest({
  81. url: server.url,
  82. path,
  83. fields: {
  84. ids: [ ]
  85. },
  86. token: server.accessToken,
  87. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  88. })
  89. await makePostBodyRequest({
  90. url: server.url,
  91. path,
  92. fields: {
  93. ids: 5
  94. },
  95. token: server.accessToken,
  96. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  97. })
  98. })
  99. it('Should fail with a non authenticated user', async function () {
  100. await makePostBodyRequest({
  101. url: server.url,
  102. path,
  103. fields: {
  104. ids: [ 5 ]
  105. },
  106. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  107. })
  108. })
  109. it('Should succeed with the correct parameters', async function () {
  110. await makePostBodyRequest({
  111. url: server.url,
  112. path,
  113. fields: {
  114. ids: [ 5 ]
  115. },
  116. token: server.accessToken,
  117. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  118. })
  119. })
  120. })
  121. describe('When marking as read my notifications', function () {
  122. const path = '/api/v1/users/me/notifications/read-all'
  123. it('Should fail with a non authenticated user', async function () {
  124. await makePostBodyRequest({
  125. url: server.url,
  126. path,
  127. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  128. })
  129. })
  130. it('Should succeed with the correct parameters', async function () {
  131. await makePostBodyRequest({
  132. url: server.url,
  133. path,
  134. token: server.accessToken,
  135. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  136. })
  137. })
  138. })
  139. describe('When updating my notification settings', function () {
  140. const path = '/api/v1/users/me/notification-settings'
  141. const correctFields: UserNotificationSetting = {
  142. newVideoFromSubscription: UserNotificationSettingValue.WEB,
  143. newCommentOnMyVideo: UserNotificationSettingValue.WEB,
  144. abuseAsModerator: UserNotificationSettingValue.WEB,
  145. videoAutoBlacklistAsModerator: UserNotificationSettingValue.WEB,
  146. blacklistOnMyVideo: UserNotificationSettingValue.WEB,
  147. myVideoImportFinished: UserNotificationSettingValue.WEB,
  148. myVideoPublished: UserNotificationSettingValue.WEB,
  149. commentMention: UserNotificationSettingValue.WEB,
  150. newFollow: UserNotificationSettingValue.WEB,
  151. newUserRegistration: UserNotificationSettingValue.WEB,
  152. newInstanceFollower: UserNotificationSettingValue.WEB,
  153. autoInstanceFollowing: UserNotificationSettingValue.WEB,
  154. abuseNewMessage: UserNotificationSettingValue.WEB,
  155. abuseStateChange: UserNotificationSettingValue.WEB
  156. }
  157. it('Should fail with missing fields', async function () {
  158. await makePutBodyRequest({
  159. url: server.url,
  160. path,
  161. token: server.accessToken,
  162. fields: { newVideoFromSubscription: UserNotificationSettingValue.WEB },
  163. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  164. })
  165. })
  166. it('Should fail with incorrect field values', async function () {
  167. {
  168. const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 15 })
  169. await makePutBodyRequest({
  170. url: server.url,
  171. path,
  172. token: server.accessToken,
  173. fields,
  174. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  175. })
  176. }
  177. {
  178. const fields = immutableAssign(correctFields, { newCommentOnMyVideo: 'toto' })
  179. await makePutBodyRequest({
  180. url: server.url,
  181. path,
  182. fields,
  183. token: server.accessToken,
  184. statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
  185. })
  186. }
  187. })
  188. it('Should fail with a non authenticated user', async function () {
  189. await makePutBodyRequest({
  190. url: server.url,
  191. path,
  192. fields: correctFields,
  193. statusCodeExpected: HttpStatusCode.UNAUTHORIZED_401
  194. })
  195. })
  196. it('Should succeed with the correct parameters', async function () {
  197. await makePutBodyRequest({
  198. url: server.url,
  199. path,
  200. token: server.accessToken,
  201. fields: correctFields,
  202. statusCodeExpected: HttpStatusCode.NO_CONTENT_204
  203. })
  204. })
  205. })
  206. describe('When connecting to my notification socket', function () {
  207. it('Should fail with no token', function (next) {
  208. const socket = io(`http://localhost:${server.port}/user-notifications`, { reconnection: false })
  209. socket.once('connect_error', function () {
  210. socket.disconnect()
  211. next()
  212. })
  213. socket.on('connect', () => {
  214. socket.disconnect()
  215. next(new Error('Connected with a missing token.'))
  216. })
  217. })
  218. it('Should fail with an invalid token', function (next) {
  219. const socket = io(`http://localhost:${server.port}/user-notifications`, {
  220. query: { accessToken: 'bad_access_token' },
  221. reconnection: false
  222. })
  223. socket.once('connect_error', function () {
  224. socket.disconnect()
  225. next()
  226. })
  227. socket.on('connect', () => {
  228. socket.disconnect()
  229. next(new Error('Connected with an invalid token.'))
  230. })
  231. })
  232. it('Should success with the correct token', function (next) {
  233. const socket = io(`http://localhost:${server.port}/user-notifications`, {
  234. query: { accessToken: server.accessToken },
  235. reconnection: false
  236. })
  237. function errorListener (err) {
  238. next(new Error('Error in connection: ' + err))
  239. }
  240. socket.on('connect_error', errorListener)
  241. socket.once('connect', async () => {
  242. socket.disconnect()
  243. await wait(500)
  244. next()
  245. })
  246. })
  247. })
  248. after(async function () {
  249. await cleanupTests([ server ])
  250. })
  251. })