accounts.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* tslint:disable:no-unused-expression */
  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. function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = 200) {
  11. const path = '/api/v1/accounts'
  12. return makeGetRequest({
  13. url,
  14. query: { sort },
  15. path,
  16. statusCodeExpected
  17. })
  18. }
  19. function getAccount (url: string, accountName: string, statusCodeExpected = 200) {
  20. const path = '/api/v1/accounts/' + accountName
  21. return makeGetRequest({
  22. url,
  23. path,
  24. statusCodeExpected
  25. })
  26. }
  27. async function expectAccountFollows (url: string, nameWithDomain: string, followersCount: number, followingCount: number) {
  28. const res = await getAccountsList(url)
  29. const account = res.body.data.find((a: Account) => a.name + '@' + a.host === nameWithDomain)
  30. const message = `${nameWithDomain} on ${url}`
  31. expect(account.followersCount).to.equal(followersCount, message)
  32. expect(account.followingCount).to.equal(followingCount, message)
  33. }
  34. async function checkActorFilesWereRemoved (filename: string, serverNumber: number) {
  35. const testDirectory = 'test' + serverNumber
  36. for (const directory of [ 'avatars' ]) {
  37. const directoryPath = join(root(), testDirectory, directory)
  38. const directoryExists = existsSync(directoryPath)
  39. expect(directoryExists).to.be.true
  40. const files = await readdir(directoryPath)
  41. for (const file of files) {
  42. expect(file).to.not.contain(filename)
  43. }
  44. }
  45. }
  46. function getAccountRatings (url: string, accountName: string, accessToken: string, rating?: VideoRateType, statusCodeExpected = 200) {
  47. const path = '/api/v1/accounts/' + accountName + '/ratings'
  48. const query = rating ? { rating } : {}
  49. return request(url)
  50. .get(path)
  51. .query(query)
  52. .set('Accept', 'application/json')
  53. .set('Authorization', 'Bearer ' + accessToken)
  54. .expect(statusCodeExpected)
  55. .expect('Content-Type', /json/)
  56. }
  57. // ---------------------------------------------------------------------------
  58. export {
  59. getAccount,
  60. expectAccountFollows,
  61. getAccountsList,
  62. checkActorFilesWereRemoved,
  63. getAccountRatings
  64. }