From d8d4686e01f3ee71fdf9e3de55cd3931d92f54b2 Mon Sep 17 00:00:00 2001 From: Mitch-Budibase Date: Thu, 13 Jul 2023 12:36:50 +0100 Subject: [PATCH] Account Portal API Testing --- packages/backend-core/src/constants/misc.ts | 2 + .../src/account-api/api/apis/AccountAPI.ts | 55 +++++++++++++++++++ .../account-api/config/TestConfiguration.ts | 34 ++++++++++++ qa-core/src/account-api/fixtures/accounts.ts | 20 +++++++ qa-core/src/account-api/fixtures/index.ts | 1 + .../account-api/tests/accounts/create.spec.ts | 20 +++++++ .../account-api/tests/accounts/delete.spec.ts | 25 +++++++++ .../account-api/tests/accounts/search.spec.ts | 22 ++++++++ .../tests/accounts/validate.spec.ts | 27 +++++++++ .../account-api/tests/accounts/verify.spec.ts | 24 ++++++++ 10 files changed, 230 insertions(+) create mode 100644 qa-core/src/account-api/config/TestConfiguration.ts create mode 100644 qa-core/src/account-api/fixtures/accounts.ts create mode 100644 qa-core/src/account-api/fixtures/index.ts create mode 100644 qa-core/src/account-api/tests/accounts/create.spec.ts create mode 100644 qa-core/src/account-api/tests/accounts/delete.spec.ts create mode 100644 qa-core/src/account-api/tests/accounts/search.spec.ts create mode 100644 qa-core/src/account-api/tests/accounts/validate.spec.ts create mode 100644 qa-core/src/account-api/tests/accounts/verify.spec.ts diff --git a/packages/backend-core/src/constants/misc.ts b/packages/backend-core/src/constants/misc.ts index ba2533cf4a..0c68798164 100644 --- a/packages/backend-core/src/constants/misc.ts +++ b/packages/backend-core/src/constants/misc.ts @@ -20,6 +20,8 @@ export enum Header { TYPE = "x-budibase-type", PREVIEW_ROLE = "x-budibase-role", TENANT_ID = "x-budibase-tenant-id", + VERIFICATION_CODE = "x-budibase-verification-code", + RETURN_VERIFICATION_CODE = "x-budibase-return-verification-code", TOKEN = "x-budibase-token", CSRF_TOKEN = "x-csrf-token", CORRELATION_ID = "x-budibase-correlation-id", diff --git a/qa-core/src/account-api/api/apis/AccountAPI.ts b/qa-core/src/account-api/api/apis/AccountAPI.ts index fc6f6caecb..0e457c07e8 100644 --- a/qa-core/src/account-api/api/apis/AccountAPI.ts +++ b/qa-core/src/account-api/api/apis/AccountAPI.ts @@ -72,4 +72,59 @@ export default class AccountAPI { } return response } + + async verifyAccount( + verificationCode: string, + opts: APIRequestOpts = { doExpect: true } + ): Promise { + const [response, json] = await this.client.post( + `/api/accounts/verify`, + { + body: { verificationCode }, + } + ) + if (opts.doExpect) { + expect(response).toHaveStatusCode(200) + } + return response + } + + async verifyAccountSendEmail( + email: string, + opts: APIRequestOpts = { doExpect: true } + ): Promise { + const [response, json] = await this.client.post( + `/api/accounts/verify/send`, + { + body: { email }, + } + ) + if (opts.doExpect) { + expect(response).toHaveStatusCode(200) + } + return response + } + + async search( + searchType: string, + search: 'email' | 'tenantId', + opts: APIRequestOpts = { doExpect: true } + ): Promise { + let body: { email?: string; tenantId?: string } = {} + + if (search === 'email') { + body.email = searchType; + } else if (search === 'tenantId') { + body.tenantId = searchType; + } + + const [response, json] = await this.client.post( + `/api/accounts/search`, + {body: body} + ) + if (opts.doExpect) { + expect(response).toHaveStatusCode(200) + } + return response + } } diff --git a/qa-core/src/account-api/config/TestConfiguration.ts b/qa-core/src/account-api/config/TestConfiguration.ts new file mode 100644 index 0000000000..827cee42b7 --- /dev/null +++ b/qa-core/src/account-api/config/TestConfiguration.ts @@ -0,0 +1,34 @@ +import { AccountInternalAPI } from "../api" +import { BudibaseTestConfiguration } from "../../shared" + +export default class TestConfiguration extends BudibaseTestConfiguration { + // apis + api: AccountInternalAPI + + context: T + + constructor() { + super() + this.api = new AccountInternalAPI(this.state) + this.context = {} + } + + async beforeAll() { + await super.beforeAll() + await this.setApiKey() + } + + async afterAll() { + await super.afterAll() + } + + async setApiKey() { + const apiKeyResponse = await this.internalApi.self.getApiKey() + this.state.apiKey = apiKeyResponse.apiKey + } + + async setCookieAuth() { + const apiKeyResponse = await this.internalApi.self.getApiKey() + this.state.apiKey = apiKeyResponse.apiKey + } +} diff --git a/qa-core/src/account-api/fixtures/accounts.ts b/qa-core/src/account-api/fixtures/accounts.ts new file mode 100644 index 0000000000..c6c6fa163f --- /dev/null +++ b/qa-core/src/account-api/fixtures/accounts.ts @@ -0,0 +1,20 @@ +import { generator } from "../../shared" +import { Hosting, CreateAccountRequest } from "@budibase/types" + +export const generateAccount = (): CreateAccountRequest => { + const uuid = generator.guid() + + const email = `${uuid}@budibase.com` + const tenant = `tenant${uuid.replace(/-/g, "")}` + + return { + email, + hosting: Hosting.CLOUD, + name: email, + password: uuid, + profession: "software_engineer", + size: "10+", + tenantId: tenant, + tenantName: tenant, + } +} diff --git a/qa-core/src/account-api/fixtures/index.ts b/qa-core/src/account-api/fixtures/index.ts new file mode 100644 index 0000000000..7d09ee1c2c --- /dev/null +++ b/qa-core/src/account-api/fixtures/index.ts @@ -0,0 +1 @@ +export * as accounts from "./accounts" \ No newline at end of file diff --git a/qa-core/src/account-api/tests/accounts/create.spec.ts b/qa-core/src/account-api/tests/accounts/create.spec.ts new file mode 100644 index 0000000000..e46a56e591 --- /dev/null +++ b/qa-core/src/account-api/tests/accounts/create.spec.ts @@ -0,0 +1,20 @@ +import TestConfiguration from "../../config/TestConfiguration" +import * as fixtures from "../../fixtures" + +describe("Account API - Create Account", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + it("Creates a new account", async () => { + await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) + }) +}) \ No newline at end of file diff --git a/qa-core/src/account-api/tests/accounts/delete.spec.ts b/qa-core/src/account-api/tests/accounts/delete.spec.ts new file mode 100644 index 0000000000..22fd39d17f --- /dev/null +++ b/qa-core/src/account-api/tests/accounts/delete.spec.ts @@ -0,0 +1,25 @@ +import TestConfiguration from "../../config/TestConfiguration" +import * as fixtures from "../../fixtures" + +describe("Account API - Delete Account", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + // it("Deletes an account", async () => { + // + // }) + + it("Deletes an account by ID", async () => { + const [response, account] = await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) + await config.api.accounts.delete(account.accountId) + }) +}) \ No newline at end of file diff --git a/qa-core/src/account-api/tests/accounts/search.spec.ts b/qa-core/src/account-api/tests/accounts/search.spec.ts new file mode 100644 index 0000000000..6f4437d119 --- /dev/null +++ b/qa-core/src/account-api/tests/accounts/search.spec.ts @@ -0,0 +1,22 @@ +import TestConfiguration from "../../config/TestConfiguration" +import { generator } from "../../../shared" + +describe("Account API - Search for Account", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + it("Search account by email", async () => { + await config.api.accounts.search(generator.email(), "email") + }) + + it("Search account by tenantId", async () => { + await config.api.accounts.search(generator.word(), "tenantId") + }) +}) \ No newline at end of file diff --git a/qa-core/src/account-api/tests/accounts/validate.spec.ts b/qa-core/src/account-api/tests/accounts/validate.spec.ts new file mode 100644 index 0000000000..56894bb03f --- /dev/null +++ b/qa-core/src/account-api/tests/accounts/validate.spec.ts @@ -0,0 +1,27 @@ +import TestConfiguration from "../../config/TestConfiguration" +import { generator } from "../../../shared" +import * as fixtures from "../../fixtures"; + +describe("Account API - Validate Account", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + const tenant = generator.word({length: 6}) + const email = `${tenant}@budibase.com` + + + it("Validates an email", async () => { + await config.api.accounts.validateEmail(email) + }) + + it("Validates a tenant ID", async () => { + await config.api.accounts.validateTenantId(tenant) + }) +}) \ No newline at end of file diff --git a/qa-core/src/account-api/tests/accounts/verify.spec.ts b/qa-core/src/account-api/tests/accounts/verify.spec.ts new file mode 100644 index 0000000000..d3f2ff1a18 --- /dev/null +++ b/qa-core/src/account-api/tests/accounts/verify.spec.ts @@ -0,0 +1,24 @@ +import TestConfiguration from "../../config/TestConfiguration" +import { generator } from "../../../shared" +import * as fixtures from "../../fixtures"; + +describe("Account API - Verify Account", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + + it("Verify an account", async () => { + await config.api.accounts.verifyAccount() + }) + + it("Send account verification email ", async () => { + await config.api.accounts.verifyAccountSendEmail() + }) +}) \ No newline at end of file