follows.ts 8.7 KB

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