accounts.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import * as request from 'supertest'
  3. import { expect } from 'chai'
  4. import { existsSync, readdir } from 'fs-extra'
  5. import { join } from 'path'
  6. import { Account } from '../../models/actors'
  7. import { root } from '../miscs/miscs'
  8. import { makeGetRequest } from '../requests/requests'
  9. import { VideoRateType } from '../../models/videos'
  10. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  11. function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = HttpStatusCode.OK_200) {
  12. const path = '/api/v1/accounts'
  13. return makeGetRequest({
  14. url,
  15. query: { sort },
  16. path,
  17. statusCodeExpected
  18. })
  19. }
  20. function getAccount (url: string, accountName: string, statusCodeExpected = HttpStatusCode.OK_200) {
  21. const path = '/api/v1/accounts/' + accountName
  22. return makeGetRequest({
  23. url,
  24. path,
  25. statusCodeExpected
  26. })
  27. }
  28. async function expectAccountFollows (url: string, nameWithDomain: string, followersCount: number, followingCount: number) {
  29. const res = await getAccountsList(url)
  30. const account = res.body.data.find((a: Account) => a.name + '@' + a.host === nameWithDomain)
  31. const message = `${nameWithDomain} on ${url}`
  32. expect(account.followersCount).to.equal(followersCount, message)
  33. expect(account.followingCount).to.equal(followingCount, message)
  34. }
  35. async function checkActorFilesWereRemoved (filename: string, serverNumber: number) {
  36. const testDirectory = 'test' + serverNumber
  37. for (const directory of [ 'avatars' ]) {
  38. const directoryPath = join(root(), testDirectory, directory)
  39. const directoryExists = existsSync(directoryPath)
  40. expect(directoryExists).to.be.true
  41. const files = await readdir(directoryPath)
  42. for (const file of files) {
  43. expect(file).to.not.contain(filename)
  44. }
  45. }
  46. }
  47. function getAccountRatings (
  48. url: string,
  49. accountName: string,
  50. accessToken: string,
  51. rating?: VideoRateType,
  52. statusCodeExpected = HttpStatusCode.OK_200
  53. ) {
  54. const path = '/api/v1/accounts/' + accountName + '/ratings'
  55. const query = rating ? { rating } : {}
  56. return request(url)
  57. .get(path)
  58. .query(query)
  59. .set('Accept', 'application/json')
  60. .set('Authorization', 'Bearer ' + accessToken)
  61. .expect(statusCodeExpected)
  62. .expect('Content-Type', /json/)
  63. }
  64. // ---------------------------------------------------------------------------
  65. export {
  66. getAccount,
  67. expectAccountFollows,
  68. getAccountsList,
  69. checkActorFilesWereRemoved,
  70. getAccountRatings
  71. }