user-subscriptions.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import {
  4. cleanupTests,
  5. createUser,
  6. flushAndRunServer,
  7. makeDeleteRequest,
  8. makeGetRequest,
  9. makePostBodyRequest,
  10. ServerInfo,
  11. setAccessTokensToServers,
  12. userLogin
  13. } from '../../../../shared/extra-utils'
  14. import {
  15. checkBadCountPagination,
  16. checkBadSortPagination,
  17. checkBadStartPagination
  18. } from '../../../../shared/extra-utils/requests/check-api-params'
  19. import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
  20. describe('Test user subscriptions API validators', function () {
  21. const path = '/api/v1/users/me/subscriptions'
  22. let server: ServerInfo
  23. let userAccessToken = ''
  24. // ---------------------------------------------------------------
  25. before(async function () {
  26. this.timeout(30000)
  27. server = await flushAndRunServer(1)
  28. await setAccessTokensToServers([ server ])
  29. const user = {
  30. username: 'user1',
  31. password: 'my super password'
  32. }
  33. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  34. userAccessToken = await userLogin(server, user)
  35. })
  36. describe('When listing my subscriptions', function () {
  37. it('Should fail with a bad start pagination', async function () {
  38. await checkBadStartPagination(server.url, path, server.accessToken)
  39. })
  40. it('Should fail with a bad count pagination', async function () {
  41. await checkBadCountPagination(server.url, path, server.accessToken)
  42. })
  43. it('Should fail with an incorrect sort', async function () {
  44. await checkBadSortPagination(server.url, path, server.accessToken)
  45. })
  46. it('Should fail with a non authenticated user', async function () {
  47. await makeGetRequest({
  48. url: server.url,
  49. path,
  50. statusCodeExpected: 401
  51. })
  52. })
  53. it('Should succeed with the correct parameters', async function () {
  54. await makeGetRequest({
  55. url: server.url,
  56. path,
  57. token: userAccessToken,
  58. statusCodeExpected: 200
  59. })
  60. })
  61. })
  62. describe('When listing my subscriptions videos', function () {
  63. const path = '/api/v1/users/me/subscriptions/videos'
  64. it('Should fail with a bad start pagination', async function () {
  65. await checkBadStartPagination(server.url, path, server.accessToken)
  66. })
  67. it('Should fail with a bad count pagination', async function () {
  68. await checkBadCountPagination(server.url, path, server.accessToken)
  69. })
  70. it('Should fail with an incorrect sort', async function () {
  71. await checkBadSortPagination(server.url, path, server.accessToken)
  72. })
  73. it('Should fail with a non authenticated user', async function () {
  74. await makeGetRequest({
  75. url: server.url,
  76. path,
  77. statusCodeExpected: 401
  78. })
  79. })
  80. it('Should succeed with the correct parameters', async function () {
  81. await makeGetRequest({
  82. url: server.url,
  83. path,
  84. token: userAccessToken,
  85. statusCodeExpected: 200
  86. })
  87. })
  88. })
  89. describe('When adding a subscription', function () {
  90. it('Should fail with a non authenticated user', async function () {
  91. await makePostBodyRequest({
  92. url: server.url,
  93. path,
  94. fields: { uri: 'user1_channel@localhost:' + server.port },
  95. statusCodeExpected: 401
  96. })
  97. })
  98. it('Should fail with bad URIs', async function () {
  99. await makePostBodyRequest({
  100. url: server.url,
  101. path,
  102. token: server.accessToken,
  103. fields: { uri: 'root' },
  104. statusCodeExpected: 400
  105. })
  106. await makePostBodyRequest({
  107. url: server.url,
  108. path,
  109. token: server.accessToken,
  110. fields: { uri: 'root@' },
  111. statusCodeExpected: 400
  112. })
  113. await makePostBodyRequest({
  114. url: server.url,
  115. path,
  116. token: server.accessToken,
  117. fields: { uri: 'root@hello@' },
  118. statusCodeExpected: 400
  119. })
  120. })
  121. it('Should succeed with the correct parameters', async function () {
  122. this.timeout(20000)
  123. await makePostBodyRequest({
  124. url: server.url,
  125. path,
  126. token: server.accessToken,
  127. fields: { uri: 'user1_channel@localhost:' + server.port },
  128. statusCodeExpected: 204
  129. })
  130. await waitJobs([ server ])
  131. })
  132. })
  133. describe('When getting a subscription', function () {
  134. it('Should fail with a non authenticated user', async function () {
  135. await makeGetRequest({
  136. url: server.url,
  137. path: path + '/user1_channel@localhost:' + server.port,
  138. statusCodeExpected: 401
  139. })
  140. })
  141. it('Should fail with bad URIs', async function () {
  142. await makeGetRequest({
  143. url: server.url,
  144. path: path + '/root',
  145. token: server.accessToken,
  146. statusCodeExpected: 400
  147. })
  148. await makeGetRequest({
  149. url: server.url,
  150. path: path + '/root@',
  151. token: server.accessToken,
  152. statusCodeExpected: 400
  153. })
  154. await makeGetRequest({
  155. url: server.url,
  156. path: path + '/root@hello@',
  157. token: server.accessToken,
  158. statusCodeExpected: 400
  159. })
  160. })
  161. it('Should fail with an unknown subscription', async function () {
  162. await makeGetRequest({
  163. url: server.url,
  164. path: path + '/root1@localhost:' + server.port,
  165. token: server.accessToken,
  166. statusCodeExpected: 404
  167. })
  168. })
  169. it('Should succeed with the correct parameters', async function () {
  170. await makeGetRequest({
  171. url: server.url,
  172. path: path + '/user1_channel@localhost:' + server.port,
  173. token: server.accessToken,
  174. statusCodeExpected: 200
  175. })
  176. })
  177. })
  178. describe('When checking if subscriptions exist', function () {
  179. const existPath = path + '/exist'
  180. it('Should fail with a non authenticated user', async function () {
  181. await makeGetRequest({
  182. url: server.url,
  183. path: existPath,
  184. statusCodeExpected: 401
  185. })
  186. })
  187. it('Should fail with bad URIs', async function () {
  188. await makeGetRequest({
  189. url: server.url,
  190. path: existPath,
  191. query: { uris: 'toto' },
  192. token: server.accessToken,
  193. statusCodeExpected: 400
  194. })
  195. await makeGetRequest({
  196. url: server.url,
  197. path: existPath,
  198. query: { 'uris[]': 1 },
  199. token: server.accessToken,
  200. statusCodeExpected: 400
  201. })
  202. })
  203. it('Should succeed with the correct parameters', async function () {
  204. await makeGetRequest({
  205. url: server.url,
  206. path: existPath,
  207. query: { 'uris[]': 'coucou@localhost:' + server.port },
  208. token: server.accessToken,
  209. statusCodeExpected: 200
  210. })
  211. })
  212. })
  213. describe('When removing a subscription', function () {
  214. it('Should fail with a non authenticated user', async function () {
  215. await makeDeleteRequest({
  216. url: server.url,
  217. path: path + '/user1_channel@localhost:' + server.port,
  218. statusCodeExpected: 401
  219. })
  220. })
  221. it('Should fail with bad URIs', async function () {
  222. await makeDeleteRequest({
  223. url: server.url,
  224. path: path + '/root',
  225. token: server.accessToken,
  226. statusCodeExpected: 400
  227. })
  228. await makeDeleteRequest({
  229. url: server.url,
  230. path: path + '/root@',
  231. token: server.accessToken,
  232. statusCodeExpected: 400
  233. })
  234. await makeDeleteRequest({
  235. url: server.url,
  236. path: path + '/root@hello@',
  237. token: server.accessToken,
  238. statusCodeExpected: 400
  239. })
  240. })
  241. it('Should fail with an unknown subscription', async function () {
  242. await makeDeleteRequest({
  243. url: server.url,
  244. path: path + '/root1@localhost:' + server.port,
  245. token: server.accessToken,
  246. statusCodeExpected: 404
  247. })
  248. })
  249. it('Should succeed with the correct parameters', async function () {
  250. await makeDeleteRequest({
  251. url: server.url,
  252. path: path + '/user1_channel@localhost:' + server.port,
  253. token: server.accessToken,
  254. statusCodeExpected: 204
  255. })
  256. })
  257. })
  258. after(async function () {
  259. await cleanupTests([ server ])
  260. })
  261. })