user-notifications.ts 7.8 KB

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