blocklist.ts 16 KB

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