follows.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import * as request from 'supertest'
  2. import { ServerInfo } from './servers'
  3. import { waitJobs } from './jobs'
  4. import { makePostBodyRequest } from '../requests/requests'
  5. function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
  6. const path = '/api/v1/server/followers'
  7. const query = {
  8. start,
  9. count,
  10. sort,
  11. search
  12. }
  13. return request(url)
  14. .get(path)
  15. .query(query)
  16. .set('Accept', 'application/json')
  17. .expect(200)
  18. .expect('Content-Type', /json/)
  19. }
  20. function acceptFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
  21. const path = '/api/v1/server/followers/' + follower + '/accept'
  22. return makePostBodyRequest({
  23. url,
  24. token,
  25. path,
  26. statusCodeExpected
  27. })
  28. }
  29. function rejectFollower (url: string, token: string, follower: string, statusCodeExpected = 204) {
  30. const path = '/api/v1/server/followers/' + follower + '/reject'
  31. return makePostBodyRequest({
  32. url,
  33. token,
  34. path,
  35. statusCodeExpected
  36. })
  37. }
  38. function getFollowingListPaginationAndSort (url: string, start: number, count: number, sort: string, search?: string) {
  39. const path = '/api/v1/server/following'
  40. const query = {
  41. start,
  42. count,
  43. sort,
  44. search
  45. }
  46. return request(url)
  47. .get(path)
  48. .query(query)
  49. .set('Accept', 'application/json')
  50. .expect(200)
  51. .expect('Content-Type', /json/)
  52. }
  53. function follow (follower: string, following: string[], accessToken: string, expectedStatus = 204) {
  54. const path = '/api/v1/server/following'
  55. const followingHosts = following.map(f => f.replace(/^http:\/\//, ''))
  56. return request(follower)
  57. .post(path)
  58. .set('Accept', 'application/json')
  59. .set('Authorization', 'Bearer ' + accessToken)
  60. .send({ 'hosts': followingHosts })
  61. .expect(expectedStatus)
  62. }
  63. async function unfollow (url: string, accessToken: string, target: ServerInfo, expectedStatus = 204) {
  64. const path = '/api/v1/server/following/' + target.host
  65. return request(url)
  66. .delete(path)
  67. .set('Accept', 'application/json')
  68. .set('Authorization', 'Bearer ' + accessToken)
  69. .expect(expectedStatus)
  70. }
  71. function removeFollower (url: string, accessToken: string, follower: ServerInfo, expectedStatus = 204) {
  72. const path = '/api/v1/server/followers/peertube@' + follower.host
  73. return request(url)
  74. .delete(path)
  75. .set('Accept', 'application/json')
  76. .set('Authorization', 'Bearer ' + accessToken)
  77. .expect(expectedStatus)
  78. }
  79. async function doubleFollow (server1: ServerInfo, server2: ServerInfo) {
  80. await Promise.all([
  81. follow(server1.url, [ server2.url ], server1.accessToken),
  82. follow(server2.url, [ server1.url ], server2.accessToken)
  83. ])
  84. // Wait request propagation
  85. await waitJobs([ server1, server2 ])
  86. return true
  87. }
  88. // ---------------------------------------------------------------------------
  89. export {
  90. getFollowersListPaginationAndSort,
  91. getFollowingListPaginationAndSort,
  92. unfollow,
  93. removeFollower,
  94. follow,
  95. doubleFollow,
  96. acceptFollower,
  97. rejectFollower
  98. }