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