Add tests for emails
[oweals/peertube.git] / server / tests / utils / users / accounts.ts
1 /* tslint:disable:no-unused-expression */
2
3 import { expect } from 'chai'
4 import { existsSync } from 'fs'
5 import { join } from 'path'
6 import { Account } from '../../../../shared/models/actors'
7 import { readdirPromise } from '../../../helpers/core-utils'
8 import { root } from '../index'
9 import { makeGetRequest } from '../requests/requests'
10
11 function getAccountsList (url: string, sort = '-createdAt', statusCodeExpected = 200) {
12   const path = '/api/v1/accounts'
13
14   return makeGetRequest({
15     url,
16     query: { sort },
17     path,
18     statusCodeExpected
19   })
20 }
21
22 function getAccount (url: string, accountId: number | string, statusCodeExpected = 200) {
23   const path = '/api/v1/accounts/' + accountId
24
25   return makeGetRequest({
26     url,
27     path,
28     statusCodeExpected
29   })
30 }
31
32 async function expectAccountFollows (url: string, nameWithDomain: string, followersCount: number, followingCount: number) {
33   const res = await getAccountsList(url)
34   const account = res.body.data.find((a: Account) => a.name + '@' + a.host === nameWithDomain)
35
36   const message = `${nameWithDomain} on ${url}`
37   expect(account.followersCount).to.equal(followersCount, message)
38   expect(account.followingCount).to.equal(followingCount, message)
39 }
40
41 async function checkActorFilesWereRemoved (actorUUID: string, serverNumber: number) {
42   const testDirectory = 'test' + serverNumber
43
44   for (const directory of [ 'avatars' ]) {
45     const directoryPath = join(root(), testDirectory, directory)
46
47     const directoryExists = existsSync(directoryPath)
48     expect(directoryExists).to.be.true
49
50     const files = await readdirPromise(directoryPath)
51     for (const file of files) {
52       expect(file).to.not.contain(actorUUID)
53     }
54   }
55 }
56
57 // ---------------------------------------------------------------------------
58
59 export {
60   getAccount,
61   expectAccountFollows,
62   getAccountsList,
63   checkActorFilesWereRemoved
64 }