blocklist.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import {
  4. cleanupTests,
  5. createUser,
  6. doubleFollow,
  7. flushAndRunMultipleServers,
  8. makeDeleteRequest,
  9. makeGetRequest,
  10. makePostBodyRequest,
  11. ServerInfo,
  12. setAccessTokensToServers,
  13. userLogin
  14. } from '../../../../shared/extra-utils'
  15. import {
  16. checkBadCountPagination,
  17. checkBadSortPagination,
  18. checkBadStartPagination
  19. } from '../../../../shared/extra-utils/requests/check-api-params'
  20. describe('Test blocklist API validators', function () {
  21. let servers: ServerInfo[]
  22. let server: ServerInfo
  23. let userAccessToken: string
  24. before(async function () {
  25. this.timeout(60000)
  26. servers = await flushAndRunMultipleServers(2)
  27. await setAccessTokensToServers(servers)
  28. server = servers[0]
  29. const user = { username: 'user1', password: 'password' }
  30. await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
  31. userAccessToken = await userLogin(server, user)
  32. await doubleFollow(servers[0], servers[1])
  33. })
  34. // ---------------------------------------------------------------
  35. describe('When managing user blocklist', function () {
  36. describe('When managing user accounts blocklist', function () {
  37. const path = '/api/v1/users/me/blocklist/accounts'
  38. describe('When listing blocked accounts', function () {
  39. it('Should fail with an unauthenticated user', async function () {
  40. await makeGetRequest({
  41. url: server.url,
  42. path,
  43. statusCodeExpected: 401
  44. })
  45. })
  46. it('Should fail with a bad start pagination', async function () {
  47. await checkBadStartPagination(server.url, path, server.accessToken)
  48. })
  49. it('Should fail with a bad count pagination', async function () {
  50. await checkBadCountPagination(server.url, path, server.accessToken)
  51. })
  52. it('Should fail with an incorrect sort', async function () {
  53. await checkBadSortPagination(server.url, path, server.accessToken)
  54. })
  55. })
  56. describe('When blocking an account', function () {
  57. it('Should fail with an unauthenticated user', async function () {
  58. await makePostBodyRequest({
  59. url: server.url,
  60. path,
  61. fields: { accountName: 'user1' },
  62. statusCodeExpected: 401
  63. })
  64. })
  65. it('Should fail with an unknown account', async function () {
  66. await makePostBodyRequest({
  67. url: server.url,
  68. token: server.accessToken,
  69. path,
  70. fields: { accountName: 'user2' },
  71. statusCodeExpected: 404
  72. })
  73. })
  74. it('Should fail to block ourselves', async function () {
  75. await makePostBodyRequest({
  76. url: server.url,
  77. token: server.accessToken,
  78. path,
  79. fields: { accountName: 'root' },
  80. statusCodeExpected: 409
  81. })
  82. })
  83. it('Should succeed with the correct params', async function () {
  84. await makePostBodyRequest({
  85. url: server.url,
  86. token: server.accessToken,
  87. path,
  88. fields: { accountName: 'user1' },
  89. statusCodeExpected: 204
  90. })
  91. })
  92. })
  93. describe('When unblocking an account', function () {
  94. it('Should fail with an unauthenticated user', async function () {
  95. await makeDeleteRequest({
  96. url: server.url,
  97. path: path + '/user1',
  98. statusCodeExpected: 401
  99. })
  100. })
  101. it('Should fail with an unknown account block', async function () {
  102. await makeDeleteRequest({
  103. url: server.url,
  104. path: path + '/user2',
  105. token: server.accessToken,
  106. statusCodeExpected: 404
  107. })
  108. })
  109. it('Should succeed with the correct params', async function () {
  110. await makeDeleteRequest({
  111. url: server.url,
  112. path: path + '/user1',
  113. token: server.accessToken,
  114. statusCodeExpected: 204
  115. })
  116. })
  117. })
  118. })
  119. describe('When managing user servers blocklist', function () {
  120. const path = '/api/v1/users/me/blocklist/servers'
  121. describe('When listing blocked servers', function () {
  122. it('Should fail with an unauthenticated user', async function () {
  123. await makeGetRequest({
  124. url: server.url,
  125. path,
  126. statusCodeExpected: 401
  127. })
  128. })
  129. it('Should fail with a bad start pagination', async function () {
  130. await checkBadStartPagination(server.url, path, server.accessToken)
  131. })
  132. it('Should fail with a bad count pagination', async function () {
  133. await checkBadCountPagination(server.url, path, server.accessToken)
  134. })
  135. it('Should fail with an incorrect sort', async function () {
  136. await checkBadSortPagination(server.url, path, server.accessToken)
  137. })
  138. })
  139. describe('When blocking a server', function () {
  140. it('Should fail with an unauthenticated user', async function () {
  141. await makePostBodyRequest({
  142. url: server.url,
  143. path,
  144. fields: { host: 'localhost:9002' },
  145. statusCodeExpected: 401
  146. })
  147. })
  148. it('Should fail with an unknown server', async function () {
  149. await makePostBodyRequest({
  150. url: server.url,
  151. token: server.accessToken,
  152. path,
  153. fields: { host: 'localhost:9003' },
  154. statusCodeExpected: 404
  155. })
  156. })
  157. it('Should fail with our own server', async function () {
  158. await makePostBodyRequest({
  159. url: server.url,
  160. token: server.accessToken,
  161. path,
  162. fields: { host: 'localhost:' + server.port },
  163. statusCodeExpected: 409
  164. })
  165. })
  166. it('Should succeed with the correct params', async function () {
  167. await makePostBodyRequest({
  168. url: server.url,
  169. token: server.accessToken,
  170. path,
  171. fields: { host: 'localhost:' + servers[1].port },
  172. statusCodeExpected: 204
  173. })
  174. })
  175. })
  176. describe('When unblocking a server', function () {
  177. it('Should fail with an unauthenticated user', async function () {
  178. await makeDeleteRequest({
  179. url: server.url,
  180. path: path + '/localhost:' + servers[1].port,
  181. statusCodeExpected: 401
  182. })
  183. })
  184. it('Should fail with an unknown server block', async function () {
  185. await makeDeleteRequest({
  186. url: server.url,
  187. path: path + '/localhost:9003',
  188. token: server.accessToken,
  189. statusCodeExpected: 404
  190. })
  191. })
  192. it('Should succeed with the correct params', async function () {
  193. await makeDeleteRequest({
  194. url: server.url,
  195. path: path + '/localhost:' + servers[1].port,
  196. token: server.accessToken,
  197. statusCodeExpected: 204
  198. })
  199. })
  200. })
  201. })
  202. })
  203. describe('When managing server blocklist', function () {
  204. describe('When managing server accounts blocklist', function () {
  205. const path = '/api/v1/server/blocklist/accounts'
  206. describe('When listing blocked accounts', function () {
  207. it('Should fail with an unauthenticated user', async function () {
  208. await makeGetRequest({
  209. url: server.url,
  210. path,
  211. statusCodeExpected: 401
  212. })
  213. })
  214. it('Should fail with a user without the appropriate rights', async function () {
  215. await makeGetRequest({
  216. url: server.url,
  217. token: userAccessToken,
  218. path,
  219. statusCodeExpected: 403
  220. })
  221. })
  222. it('Should fail with a bad start pagination', async function () {
  223. await checkBadStartPagination(server.url, path, server.accessToken)
  224. })
  225. it('Should fail with a bad count pagination', async function () {
  226. await checkBadCountPagination(server.url, path, server.accessToken)
  227. })
  228. it('Should fail with an incorrect sort', async function () {
  229. await checkBadSortPagination(server.url, path, server.accessToken)
  230. })
  231. })
  232. describe('When blocking an account', function () {
  233. it('Should fail with an unauthenticated user', async function () {
  234. await makePostBodyRequest({
  235. url: server.url,
  236. path,
  237. fields: { accountName: 'user1' },
  238. statusCodeExpected: 401
  239. })
  240. })
  241. it('Should fail with a user without the appropriate rights', async function () {
  242. await makePostBodyRequest({
  243. url: server.url,
  244. token: userAccessToken,
  245. path,
  246. fields: { accountName: 'user1' },
  247. statusCodeExpected: 403
  248. })
  249. })
  250. it('Should fail with an unknown account', async function () {
  251. await makePostBodyRequest({
  252. url: server.url,
  253. token: server.accessToken,
  254. path,
  255. fields: { accountName: 'user2' },
  256. statusCodeExpected: 404
  257. })
  258. })
  259. it('Should fail to block ourselves', async function () {
  260. await makePostBodyRequest({
  261. url: server.url,
  262. token: server.accessToken,
  263. path,
  264. fields: { accountName: 'root' },
  265. statusCodeExpected: 409
  266. })
  267. })
  268. it('Should succeed with the correct params', async function () {
  269. await makePostBodyRequest({
  270. url: server.url,
  271. token: server.accessToken,
  272. path,
  273. fields: { accountName: 'user1' },
  274. statusCodeExpected: 204
  275. })
  276. })
  277. })
  278. describe('When unblocking an account', function () {
  279. it('Should fail with an unauthenticated user', async function () {
  280. await makeDeleteRequest({
  281. url: server.url,
  282. path: path + '/user1',
  283. statusCodeExpected: 401
  284. })
  285. })
  286. it('Should fail with a user without the appropriate rights', async function () {
  287. await makeDeleteRequest({
  288. url: server.url,
  289. path: path + '/user1',
  290. token: userAccessToken,
  291. statusCodeExpected: 403
  292. })
  293. })
  294. it('Should fail with an unknown account block', async function () {
  295. await makeDeleteRequest({
  296. url: server.url,
  297. path: path + '/user2',
  298. token: server.accessToken,
  299. statusCodeExpected: 404
  300. })
  301. })
  302. it('Should succeed with the correct params', async function () {
  303. await makeDeleteRequest({
  304. url: server.url,
  305. path: path + '/user1',
  306. token: server.accessToken,
  307. statusCodeExpected: 204
  308. })
  309. })
  310. })
  311. })
  312. describe('When managing server servers blocklist', function () {
  313. const path = '/api/v1/server/blocklist/servers'
  314. describe('When listing blocked servers', function () {
  315. it('Should fail with an unauthenticated user', async function () {
  316. await makeGetRequest({
  317. url: server.url,
  318. path,
  319. statusCodeExpected: 401
  320. })
  321. })
  322. it('Should fail with a user without the appropriate rights', async function () {
  323. await makeGetRequest({
  324. url: server.url,
  325. token: userAccessToken,
  326. path,
  327. statusCodeExpected: 403
  328. })
  329. })
  330. it('Should fail with a bad start pagination', async function () {
  331. await checkBadStartPagination(server.url, path, server.accessToken)
  332. })
  333. it('Should fail with a bad count pagination', async function () {
  334. await checkBadCountPagination(server.url, path, server.accessToken)
  335. })
  336. it('Should fail with an incorrect sort', async function () {
  337. await checkBadSortPagination(server.url, path, server.accessToken)
  338. })
  339. })
  340. describe('When blocking a server', function () {
  341. it('Should fail with an unauthenticated user', async function () {
  342. await makePostBodyRequest({
  343. url: server.url,
  344. path,
  345. fields: { host: 'localhost:' + servers[1].port },
  346. statusCodeExpected: 401
  347. })
  348. })
  349. it('Should fail with a user without the appropriate rights', async function () {
  350. await makePostBodyRequest({
  351. url: server.url,
  352. token: userAccessToken,
  353. path,
  354. fields: { host: 'localhost:' + servers[1].port },
  355. statusCodeExpected: 403
  356. })
  357. })
  358. it('Should fail with an unknown server', async function () {
  359. await makePostBodyRequest({
  360. url: server.url,
  361. token: server.accessToken,
  362. path,
  363. fields: { host: 'localhost:9003' },
  364. statusCodeExpected: 404
  365. })
  366. })
  367. it('Should fail with our own server', async function () {
  368. await makePostBodyRequest({
  369. url: server.url,
  370. token: server.accessToken,
  371. path,
  372. fields: { host: 'localhost:' + server.port },
  373. statusCodeExpected: 409
  374. })
  375. })
  376. it('Should succeed with the correct params', async function () {
  377. await makePostBodyRequest({
  378. url: server.url,
  379. token: server.accessToken,
  380. path,
  381. fields: { host: 'localhost:' + servers[1].port },
  382. statusCodeExpected: 204
  383. })
  384. })
  385. })
  386. describe('When unblocking a server', function () {
  387. it('Should fail with an unauthenticated user', async function () {
  388. await makeDeleteRequest({
  389. url: server.url,
  390. path: path + '/localhost:' + servers[1].port,
  391. statusCodeExpected: 401
  392. })
  393. })
  394. it('Should fail with a user without the appropriate rights', async function () {
  395. await makeDeleteRequest({
  396. url: server.url,
  397. path: path + '/localhost:' + servers[1].port,
  398. token: userAccessToken,
  399. statusCodeExpected: 403
  400. })
  401. })
  402. it('Should fail with an unknown server block', async function () {
  403. await makeDeleteRequest({
  404. url: server.url,
  405. path: path + '/localhost:9003',
  406. token: server.accessToken,
  407. statusCodeExpected: 404
  408. })
  409. })
  410. it('Should succeed with the correct params', async function () {
  411. await makeDeleteRequest({
  412. url: server.url,
  413. path: path + '/localhost:' + servers[1].port,
  414. token: server.accessToken,
  415. statusCodeExpected: 204
  416. })
  417. })
  418. })
  419. })
  420. })
  421. after(async function () {
  422. await cleanupTests(servers)
  423. })
  424. })