From 9639bd175726b73f8fe664b5ced12a72407b1f0b Mon Sep 17 00:00:00 2001 From: buoyantair Date: Mon, 29 Oct 2018 22:18:31 +0530 Subject: Move utils to /shared Move utils used by /server/tools/* & /server/tests/**/* into /shared folder. Issue: #1336 --- shared/utils/users/accounts.ts | 63 +++++++ shared/utils/users/blocklist.ts | 198 +++++++++++++++++++++ shared/utils/users/login.ts | 62 +++++++ shared/utils/users/user-subscriptions.ts | 82 +++++++++ shared/utils/users/users.ts | 296 +++++++++++++++++++++++++++++++ 5 files changed, 701 insertions(+) create mode 100644 shared/utils/users/accounts.ts create mode 100644 shared/utils/users/blocklist.ts create mode 100644 shared/utils/users/login.ts create mode 100644 shared/utils/users/user-subscriptions.ts create mode 100644 shared/utils/users/users.ts (limited to 'shared/utils/users') diff --git a/shared/utils/users/accounts.ts b/shared/utils/users/accounts.ts new file mode 100644 index 000000000..f82b8d906 --- /dev/null +++ b/shared/utils/users/accounts.ts @@ -0,0 +1,63 @@ +/* tslint:disable:no-unused-expression */ + +import { expect } from 'chai' +import { existsSync, readdir } from 'fs-extra' +import { join } from 'path' +import { Account } from '../../../../shared/models/actors' +import { root } from '../index' +import { makeGetRequest } from '../requests/requests' + +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) + } + } +} + +// --------------------------------------------------------------------------- + +export { + getAccount, + expectAccountFollows, + getAccountsList, + checkActorFilesWereRemoved +} diff --git a/shared/utils/users/blocklist.ts b/shared/utils/users/blocklist.ts new file mode 100644 index 000000000..35b537571 --- /dev/null +++ b/shared/utils/users/blocklist.ts @@ -0,0 +1,198 @@ +/* tslint:disable:no-unused-expression */ + +import { makeDeleteRequest, makePostBodyRequest } from '../index' +import { makeGetRequest } from '../requests/requests' + +function getAccountBlocklistByAccount ( + url: string, + token: string, + start: number, + count: number, + sort = '-createdAt', + statusCodeExpected = 200 +) { + const path = '/api/v1/users/me/blocklist/accounts' + + return makeGetRequest({ + url, + token, + query: { start, count, sort }, + path, + statusCodeExpected + }) +} + +function addAccountToAccountBlocklist (url: string, token: string, accountToBlock: string, statusCodeExpected = 204) { + const path = '/api/v1/users/me/blocklist/accounts' + + return makePostBodyRequest({ + url, + path, + token, + fields: { + accountName: accountToBlock + }, + statusCodeExpected + }) +} + +function removeAccountFromAccountBlocklist (url: string, token: string, accountToUnblock: string, statusCodeExpected = 204) { + const path = '/api/v1/users/me/blocklist/accounts/' + accountToUnblock + + return makeDeleteRequest({ + url, + path, + token, + statusCodeExpected + }) +} + +function getServerBlocklistByAccount ( + url: string, + token: string, + start: number, + count: number, + sort = '-createdAt', + statusCodeExpected = 200 +) { + const path = '/api/v1/users/me/blocklist/servers' + + return makeGetRequest({ + url, + token, + query: { start, count, sort }, + path, + statusCodeExpected + }) +} + +function addServerToAccountBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { + const path = '/api/v1/users/me/blocklist/servers' + + return makePostBodyRequest({ + url, + path, + token, + fields: { + host: serverToBlock + }, + statusCodeExpected + }) +} + +function removeServerFromAccountBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { + const path = '/api/v1/users/me/blocklist/servers/' + serverToBlock + + return makeDeleteRequest({ + url, + path, + token, + statusCodeExpected + }) +} + +function getAccountBlocklistByServer ( + url: string, + token: string, + start: number, + count: number, + sort = '-createdAt', + statusCodeExpected = 200 +) { + const path = '/api/v1/server/blocklist/accounts' + + return makeGetRequest({ + url, + token, + query: { start, count, sort }, + path, + statusCodeExpected + }) +} + +function addAccountToServerBlocklist (url: string, token: string, accountToBlock: string, statusCodeExpected = 204) { + const path = '/api/v1/server/blocklist/accounts' + + return makePostBodyRequest({ + url, + path, + token, + fields: { + accountName: accountToBlock + }, + statusCodeExpected + }) +} + +function removeAccountFromServerBlocklist (url: string, token: string, accountToUnblock: string, statusCodeExpected = 204) { + const path = '/api/v1/server/blocklist/accounts/' + accountToUnblock + + return makeDeleteRequest({ + url, + path, + token, + statusCodeExpected + }) +} + +function getServerBlocklistByServer ( + url: string, + token: string, + start: number, + count: number, + sort = '-createdAt', + statusCodeExpected = 200 +) { + const path = '/api/v1/server/blocklist/servers' + + return makeGetRequest({ + url, + token, + query: { start, count, sort }, + path, + statusCodeExpected + }) +} + +function addServerToServerBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { + const path = '/api/v1/server/blocklist/servers' + + return makePostBodyRequest({ + url, + path, + token, + fields: { + host: serverToBlock + }, + statusCodeExpected + }) +} + +function removeServerFromServerBlocklist (url: string, token: string, serverToBlock: string, statusCodeExpected = 204) { + const path = '/api/v1/server/blocklist/servers/' + serverToBlock + + return makeDeleteRequest({ + url, + path, + token, + statusCodeExpected + }) +} + +// --------------------------------------------------------------------------- + +export { + getAccountBlocklistByAccount, + addAccountToAccountBlocklist, + removeAccountFromAccountBlocklist, + getServerBlocklistByAccount, + addServerToAccountBlocklist, + removeServerFromAccountBlocklist, + + getAccountBlocklistByServer, + addAccountToServerBlocklist, + removeAccountFromServerBlocklist, + getServerBlocklistByServer, + addServerToServerBlocklist, + removeServerFromServerBlocklist +} diff --git a/shared/utils/users/login.ts b/shared/utils/users/login.ts new file mode 100644 index 000000000..ddeb9df2a --- /dev/null +++ b/shared/utils/users/login.ts @@ -0,0 +1,62 @@ +import * as request from 'supertest' + +import { ServerInfo } from '../server/servers' + +type Client = { id: string, secret: string } +type User = { username: string, password: string } +type Server = { url: string, client: Client, user: User } + +function login (url: string, client: Client, user: User, expectedStatus = 200) { + const path = '/api/v1/users/token' + + const body = { + client_id: client.id, + client_secret: client.secret, + username: user.username, + password: user.password, + response_type: 'code', + grant_type: 'password', + scope: 'upload' + } + + return request(url) + .post(path) + .type('form') + .send(body) + .expect(expectedStatus) +} + +async function serverLogin (server: Server) { + const res = await login(server.url, server.client, server.user, 200) + + return res.body.access_token as string +} + +async function userLogin (server: Server, user: User, expectedStatus = 200) { + const res = await login(server.url, server.client, user, expectedStatus) + + return res.body.access_token as string +} + +function setAccessTokensToServers (servers: ServerInfo[]) { + const tasks: Promise[] = [] + + for (const server of servers) { + const p = serverLogin(server).then(t => server.accessToken = t) + tasks.push(p) + } + + return Promise.all(tasks) +} + +// --------------------------------------------------------------------------- + +export { + login, + serverLogin, + userLogin, + setAccessTokensToServers, + Server, + Client, + User +} diff --git a/shared/utils/users/user-subscriptions.ts b/shared/utils/users/user-subscriptions.ts new file mode 100644 index 000000000..b0e7da7cc --- /dev/null +++ b/shared/utils/users/user-subscriptions.ts @@ -0,0 +1,82 @@ +import { makeDeleteRequest, makeGetRequest, makePostBodyRequest } from '../' + +function addUserSubscription (url: string, token: string, targetUri: string, statusCodeExpected = 204) { + const path = '/api/v1/users/me/subscriptions' + + return makePostBodyRequest({ + url, + path, + token, + statusCodeExpected, + fields: { uri: targetUri } + }) +} + +function listUserSubscriptions (url: string, token: string, sort = '-createdAt', statusCodeExpected = 200) { + const path = '/api/v1/users/me/subscriptions' + + return makeGetRequest({ + url, + path, + token, + statusCodeExpected, + query: { sort } + }) +} + +function listUserSubscriptionVideos (url: string, token: string, sort = '-createdAt', statusCodeExpected = 200) { + const path = '/api/v1/users/me/subscriptions/videos' + + return makeGetRequest({ + url, + path, + token, + statusCodeExpected, + query: { sort } + }) +} + +function getUserSubscription (url: string, token: string, uri: string, statusCodeExpected = 200) { + const path = '/api/v1/users/me/subscriptions/' + uri + + return makeGetRequest({ + url, + path, + token, + statusCodeExpected + }) +} + +function removeUserSubscription (url: string, token: string, uri: string, statusCodeExpected = 204) { + const path = '/api/v1/users/me/subscriptions/' + uri + + return makeDeleteRequest({ + url, + path, + token, + statusCodeExpected + }) +} + +function areSubscriptionsExist (url: string, token: string, uris: string[], statusCodeExpected = 200) { + const path = '/api/v1/users/me/subscriptions/exist' + + return makeGetRequest({ + url, + path, + query: { 'uris[]': uris }, + token, + statusCodeExpected + }) +} + +// --------------------------------------------------------------------------- + +export { + areSubscriptionsExist, + addUserSubscription, + listUserSubscriptions, + getUserSubscription, + listUserSubscriptionVideos, + removeUserSubscription +} diff --git a/shared/utils/users/users.ts b/shared/utils/users/users.ts new file mode 100644 index 000000000..1b385aaf7 --- /dev/null +++ b/shared/utils/users/users.ts @@ -0,0 +1,296 @@ +import * as request from 'supertest' +import { makePostBodyRequest, makePutBodyRequest, updateAvatarRequest } from '../' + +import { UserRole } from '../../index' +import { NSFWPolicyType } from '../../models/videos/nsfw-policy.type' + +function createUser ( + url: string, + accessToken: string, + username: string, + password: string, + videoQuota = 1000000, + videoQuotaDaily = -1, + role: UserRole = UserRole.USER, + specialStatus = 200 +) { + const path = '/api/v1/users' + const body = { + username, + password, + role, + email: username + '@example.com', + videoQuota, + videoQuotaDaily + } + + return request(url) + .post(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .send(body) + .expect(specialStatus) +} + +function registerUser (url: string, username: string, password: string, specialStatus = 204) { + const path = '/api/v1/users/register' + const body = { + username, + password, + email: username + '@example.com' + } + + return request(url) + .post(path) + .set('Accept', 'application/json') + .send(body) + .expect(specialStatus) +} + +function getMyUserInformation (url: string, accessToken: string, specialStatus = 200) { + const path = '/api/v1/users/me' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + +function deleteMe (url: string, accessToken: string, specialStatus = 204) { + const path = '/api/v1/users/me' + + return request(url) + .delete(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) +} + +function getMyUserVideoQuotaUsed (url: string, accessToken: string, specialStatus = 200) { + const path = '/api/v1/users/me/video-quota-used' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + +function getUserInformation (url: string, accessToken: string, userId: number) { + const path = '/api/v1/users/' + userId + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function getMyUserVideoRating (url: string, accessToken: string, videoId: number | string, specialStatus = 200) { + const path = '/api/v1/users/me/videos/' + videoId + '/rating' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(specialStatus) + .expect('Content-Type', /json/) +} + +function getUsersList (url: string, accessToken: string) { + const path = '/api/v1/users' + + return request(url) + .get(path) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function getUsersListPaginationAndSort (url: string, accessToken: string, start: number, count: number, sort: string, search?: string) { + const path = '/api/v1/users' + + return request(url) + .get(path) + .query({ start }) + .query({ count }) + .query({ sort }) + .query({ search }) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(200) + .expect('Content-Type', /json/) +} + +function removeUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { + const path = '/api/v1/users' + + return request(url) + .delete(path + '/' + userId) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function blockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204, reason?: string) { + const path = '/api/v1/users' + let body: any + if (reason) body = { reason } + + return request(url) + .post(path + '/' + userId + '/block') + .send(body) + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function unblockUser (url: string, userId: number | string, accessToken: string, expectedStatus = 204) { + const path = '/api/v1/users' + + return request(url) + .post(path + '/' + userId + '/unblock') + .set('Accept', 'application/json') + .set('Authorization', 'Bearer ' + accessToken) + .expect(expectedStatus) +} + +function updateMyUser (options: { + url: string + accessToken: string, + currentPassword?: string, + newPassword?: string, + nsfwPolicy?: NSFWPolicyType, + email?: string, + autoPlayVideo?: boolean + displayName?: string, + description?: string +}) { + const path = '/api/v1/users/me' + + const toSend = {} + if (options.currentPassword !== undefined && options.currentPassword !== null) toSend['currentPassword'] = options.currentPassword + if (options.newPassword !== undefined && options.newPassword !== null) toSend['password'] = options.newPassword + if (options.nsfwPolicy !== undefined && options.nsfwPolicy !== null) toSend['nsfwPolicy'] = options.nsfwPolicy + if (options.autoPlayVideo !== undefined && options.autoPlayVideo !== null) toSend['autoPlayVideo'] = options.autoPlayVideo + if (options.email !== undefined && options.email !== null) toSend['email'] = options.email + if (options.description !== undefined && options.description !== null) toSend['description'] = options.description + if (options.displayName !== undefined && options.displayName !== null) toSend['displayName'] = options.displayName + + return makePutBodyRequest({ + url: options.url, + path, + token: options.accessToken, + fields: toSend, + statusCodeExpected: 204 + }) +} + +function updateMyAvatar (options: { + url: string, + accessToken: string, + fixture: string +}) { + const path = '/api/v1/users/me/avatar/pick' + + return updateAvatarRequest(Object.assign(options, { path })) +} + +function updateUser (options: { + url: string + userId: number, + accessToken: string, + email?: string, + videoQuota?: number, + videoQuotaDaily?: number, + role?: UserRole +}) { + const path = '/api/v1/users/' + options.userId + + const toSend = {} + if (options.email !== undefined && options.email !== null) toSend['email'] = options.email + if (options.videoQuota !== undefined && options.videoQuota !== null) toSend['videoQuota'] = options.videoQuota + if (options.videoQuotaDaily !== undefined && options.videoQuotaDaily !== null) toSend['videoQuotaDaily'] = options.videoQuotaDaily + if (options.role !== undefined && options.role !== null) toSend['role'] = options.role + + return makePutBodyRequest({ + url: options.url, + path, + token: options.accessToken, + fields: toSend, + statusCodeExpected: 204 + }) +} + +function askResetPassword (url: string, email: string) { + const path = '/api/v1/users/ask-reset-password' + + return makePostBodyRequest({ + url, + path, + fields: { email }, + statusCodeExpected: 204 + }) +} + +function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { + const path = '/api/v1/users/' + userId + '/reset-password' + + return makePostBodyRequest({ + url, + path, + fields: { password, verificationString }, + statusCodeExpected + }) +} + +function askSendVerifyEmail (url: string, email: string) { + const path = '/api/v1/users/ask-send-verify-email' + + return makePostBodyRequest({ + url, + path, + fields: { email }, + statusCodeExpected: 204 + }) +} + +function verifyEmail (url: string, userId: number, verificationString: string, statusCodeExpected = 204) { + const path = '/api/v1/users/' + userId + '/verify-email' + + return makePostBodyRequest({ + url, + path, + fields: { verificationString }, + statusCodeExpected + }) +} + +// --------------------------------------------------------------------------- + +export { + createUser, + registerUser, + getMyUserInformation, + getMyUserVideoRating, + deleteMe, + getMyUserVideoQuotaUsed, + getUsersList, + getUsersListPaginationAndSort, + removeUser, + updateUser, + updateMyUser, + getUserInformation, + blockUser, + unblockUser, + askResetPassword, + resetPassword, + updateMyAvatar, + askSendVerifyEmail, + verifyEmail +} -- cgit v1.2.3