aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/users/accounts.ts
blob: f64a2dbad642d3be5e2ea3e20d7e430fbe70c072 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* tslint:disable:no-unused-expression */

import * as request from 'supertest'
import { expect } from 'chai'
import { existsSync, readdir } from 'fs-extra'
import { join } from 'path'
import { Account } from '../../models/actors'
import { root } from '../miscs/miscs'
import { makeGetRequest } from '../requests/requests'
import { VideoRateType } from '../../models/videos'

function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = 200) {
  const path = '/api/v1/accounts'

  return makeGetRequest({
    url,
    query: { sort },
    path,
    statusCodeExpected
  })
}

function getAccount (url: string, accountName: string, statusCodeExpected = 200) {
  const path = '/api/v1/accounts/' + accountName

  return makeGetRequest({
    url,
    path,
    statusCodeExpected
  })
}

async function expectAccountFollows (url: string, nameWithDomain: string, followersCount: number, followingCount: number) {
  const res = await getAccountsList(url)
  const account = res.body.data.find((a: Account) => a.name + '@' + a.host === nameWithDomain)

  const message = `${nameWithDomain} on ${url}`
  expect(account.followersCount).to.equal(followersCount, message)
  expect(account.followingCount).to.equal(followingCount, message)
}

async function checkActorFilesWereRemoved (actorUUID: string, serverNumber: number) {
  const testDirectory = 'test' + serverNumber

  for (const directory of [ 'avatars' ]) {
    const directoryPath = join(root(), testDirectory, directory)

    const directoryExists = existsSync(directoryPath)
    expect(directoryExists).to.be.true

    const files = await readdir(directoryPath)
    for (const file of files) {
      expect(file).to.not.contain(actorUUID)
    }
  }
}

function getAccountRatings (url: string, accountName: string, accessToken: string, rating?: VideoRateType, statusCodeExpected = 200) {
  const path = '/api/v1/accounts/' + accountName + '/ratings'

  const query = rating ? { rating } : {}

  return request(url)
          .get(path)
          .query(query)
          .set('Accept', 'application/json')
          .set('Authorization', 'Bearer ' + accessToken)
          .expect(statusCodeExpected)
          .expect('Content-Type', /json/)
}

// ---------------------------------------------------------------------------

export {
  getAccount,
  expectAccountFollows,
  getAccountsList,
  checkActorFilesWereRemoved,
  getAccountRatings
}