From b379759f55a35837b803a3b988674972db2903d1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Jan 2023 09:28:29 +0100 Subject: Add signup approval API tests --- shared/server-commands/server/config-command.ts | 50 ++++++- shared/server-commands/server/server.ts | 3 + shared/server-commands/users/index.ts | 1 + .../server-commands/users/registrations-command.ts | 157 +++++++++++++++++++++ shared/server-commands/users/users-command.ts | 29 ---- 5 files changed, 204 insertions(+), 36 deletions(-) create mode 100644 shared/server-commands/users/registrations-command.ts (limited to 'shared') diff --git a/shared/server-commands/server/config-command.ts b/shared/server-commands/server/config-command.ts index 1c2315ed1..51267b85b 100644 --- a/shared/server-commands/server/config-command.ts +++ b/shared/server-commands/server/config-command.ts @@ -18,6 +18,33 @@ export class ConfigCommand extends AbstractCommand { } } + // --------------------------------------------------------------------------- + + static getEmailOverrideConfig (emailPort: number) { + return { + smtp: { + hostname: '127.0.0.1', + port: emailPort + } + } + } + + // --------------------------------------------------------------------------- + + enableSignup (requiresApproval: boolean) { + return this.updateExistingSubConfig({ + newConfig: { + signup: { + enabled: true, + requiresApproval, + limit: -1 + } + } + }) + } + + // --------------------------------------------------------------------------- + disableImports () { return this.setImportsEnabled(false) } @@ -44,6 +71,16 @@ export class ConfigCommand extends AbstractCommand { }) } + // --------------------------------------------------------------------------- + + enableChannelSync () { + return this.setChannelSyncEnabled(true) + } + + disableChannelSync () { + return this.setChannelSyncEnabled(false) + } + private setChannelSyncEnabled (enabled: boolean) { return this.updateExistingSubConfig({ newConfig: { @@ -56,13 +93,7 @@ export class ConfigCommand extends AbstractCommand { }) } - enableChannelSync () { - return this.setChannelSyncEnabled(true) - } - - disableChannelSync () { - return this.setChannelSyncEnabled(false) - } + // --------------------------------------------------------------------------- enableLive (options: { allowReplay?: boolean @@ -142,6 +173,8 @@ export class ConfigCommand extends AbstractCommand { }) } + // --------------------------------------------------------------------------- + enableStudio () { return this.updateExistingSubConfig({ newConfig: { @@ -152,6 +185,8 @@ export class ConfigCommand extends AbstractCommand { }) } + // --------------------------------------------------------------------------- + getConfig (options: OverrideCommandOptions = {}) { const path = '/api/v1/config' @@ -304,6 +339,7 @@ export class ConfigCommand extends AbstractCommand { signup: { enabled: false, limit: 5, + requiresApproval: true, requiresEmailVerification: false, minimumAge: 16 }, diff --git a/shared/server-commands/server/server.ts b/shared/server-commands/server/server.ts index ae1395a74..793fae3a8 100644 --- a/shared/server-commands/server/server.ts +++ b/shared/server-commands/server/server.ts @@ -18,6 +18,7 @@ import { BlocklistCommand, LoginCommand, NotificationsCommand, + RegistrationsCommand, SubscriptionsCommand, TwoFactorCommand, UsersCommand @@ -147,6 +148,7 @@ export class PeerTubeServer { views?: ViewsCommand twoFactor?: TwoFactorCommand videoToken?: VideoTokenCommand + registrations?: RegistrationsCommand constructor (options: { serverNumber: number } | { url: string }) { if ((options as any).url) { @@ -430,5 +432,6 @@ export class PeerTubeServer { this.views = new ViewsCommand(this) this.twoFactor = new TwoFactorCommand(this) this.videoToken = new VideoTokenCommand(this) + this.registrations = new RegistrationsCommand(this) } } diff --git a/shared/server-commands/users/index.ts b/shared/server-commands/users/index.ts index 1afc02dc1..404756539 100644 --- a/shared/server-commands/users/index.ts +++ b/shared/server-commands/users/index.ts @@ -4,6 +4,7 @@ export * from './blocklist-command' export * from './login' export * from './login-command' export * from './notifications-command' +export * from './registrations-command' export * from './subscriptions-command' export * from './two-factor-command' export * from './users-command' diff --git a/shared/server-commands/users/registrations-command.ts b/shared/server-commands/users/registrations-command.ts new file mode 100644 index 000000000..4e97571f4 --- /dev/null +++ b/shared/server-commands/users/registrations-command.ts @@ -0,0 +1,157 @@ +import { pick } from '@shared/core-utils' +import { HttpStatusCode, ResultList, UserRegistration, UserRegistrationRequest } from '@shared/models' +import { unwrapBody } from '../requests' +import { AbstractCommand, OverrideCommandOptions } from '../shared' + +export class RegistrationsCommand extends AbstractCommand { + + register (options: OverrideCommandOptions & Partial & Pick) { + const { password = 'password', email = options.username + '@example.com' } = options + const path = '/api/v1/users/register' + + return this.postBodyRequest({ + ...options, + + path, + fields: { + ...pick(options, [ 'username', 'displayName', 'channel' ]), + + password, + email + }, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + requestRegistration ( + options: OverrideCommandOptions & Partial & Pick + ) { + const { password = 'password', email = options.username + '@example.com' } = options + const path = '/api/v1/users/registrations/request' + + return unwrapBody(this.postBodyRequest({ + ...options, + + path, + fields: { + ...pick(options, [ 'username', 'displayName', 'channel', 'registrationReason' ]), + + password, + email + }, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + })) + } + + // --------------------------------------------------------------------------- + + accept (options: OverrideCommandOptions & { + id: number + moderationResponse: string + }) { + const { id, moderationResponse } = options + const path = '/api/v1/users/registrations/' + id + '/accept' + + return this.postBodyRequest({ + ...options, + + path, + fields: { moderationResponse }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + reject (options: OverrideCommandOptions & { + id: number + moderationResponse: string + }) { + const { id, moderationResponse } = options + const path = '/api/v1/users/registrations/' + id + '/reject' + + return this.postBodyRequest({ + ...options, + + path, + fields: { moderationResponse }, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + // --------------------------------------------------------------------------- + + delete (options: OverrideCommandOptions & { + id: number + }) { + const { id } = options + const path = '/api/v1/users/registrations/' + id + + return this.deleteRequest({ + ...options, + + path, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + // --------------------------------------------------------------------------- + + list (options: OverrideCommandOptions & { + start?: number + count?: number + sort?: string + search?: string + } = {}) { + const path = '/api/v1/users/registrations' + + return this.getRequestBody>({ + ...options, + + path, + query: pick(options, [ 'start', 'count', 'sort', 'search' ]), + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + // --------------------------------------------------------------------------- + + askSendVerifyEmail (options: OverrideCommandOptions & { + email: string + }) { + const { email } = options + const path = '/api/v1/users/registrations/ask-send-verify-email' + + return this.postBodyRequest({ + ...options, + + path, + fields: { email }, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + + verifyEmail (options: OverrideCommandOptions & { + registrationId: number + verificationString: string + }) { + const { registrationId, verificationString } = options + const path = '/api/v1/users/registrations/' + registrationId + '/verify-email' + + return this.postBodyRequest({ + ...options, + + path, + fields: { + verificationString + }, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } +} diff --git a/shared/server-commands/users/users-command.ts b/shared/server-commands/users/users-command.ts index 811b9685b..8a42fafc8 100644 --- a/shared/server-commands/users/users-command.ts +++ b/shared/server-commands/users/users-command.ts @@ -214,35 +214,6 @@ export class UsersCommand extends AbstractCommand { return this.server.login.getAccessToken({ username, password }) } - register (options: OverrideCommandOptions & { - username: string - password?: string - displayName?: string - email?: string - channel?: { - name: string - displayName: string - } - }) { - const { username, password = 'password', displayName, channel, email = username + '@example.com' } = options - const path = '/api/v1/users/register' - - return this.postBodyRequest({ - ...options, - - path, - fields: { - username, - password, - email, - displayName, - channel - }, - implicitToken: false, - defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 - }) - } - // --------------------------------------------------------------------------- getMyInfo (options: OverrideCommandOptions = {}) { -- cgit v1.2.3