diff --git a/qa-core/src/account-api/api/apis/AccountAPI.ts b/qa-core/src/account-api/api/apis/AccountAPI.ts index 37aa3313ba..9450f6b2cd 100644 --- a/qa-core/src/account-api/api/apis/AccountAPI.ts +++ b/qa-core/src/account-api/api/apis/AccountAPI.ts @@ -20,9 +20,6 @@ export default class AccountAPI { body: { email }, } ) - if (opts.doExpect) { - expect(response).toHaveStatusCode(200) - } return response } @@ -36,18 +33,15 @@ export default class AccountAPI { body: { tenantId }, } ) - if (opts.doExpect) { - expect(response).toHaveStatusCode(200) - } return response } async create( body: CreateAccountRequest, - opts: APIRequestOpts = { doExpect: true } + opts: APIRequestOpts & { autoVerify: boolean } = { doExpect: true, autoVerify: true } ): Promise<[Response, Account]> { const headers = { - "no-verify": "1", + "no-verify": opts.autoVerify ? "1" : "0" } const [response, json] = await this.client.post(`/api/accounts`, { body, @@ -59,7 +53,7 @@ export default class AccountAPI { return [response, json] } - async delete(accountID: string) { + async delete(accountID: string, opts: APIRequestOpts = {status:204}) { const [response, json] = await this.client.del( `/api/accounts/${accountID}`, { @@ -67,8 +61,8 @@ export default class AccountAPI { } ) // can't use expect here due to use in global teardown - if (response.status !== 204) { - throw new Error(`Could not delete accountId=${accountID}`) + if (response.status !== opts.status) { + throw new Error(`status: ${response.status} not equal to expected: ${opts.status}`) } return response } @@ -120,18 +114,15 @@ export default class AccountAPI { let body: SearchAccountsRequest = {} if (search === 'email') { - body.email = searchType; + body.email = searchType } else if (search === 'tenantId') { - body.tenantId = searchType; + body.tenantId = searchType } const [response, json] = await this.client.post( `/api/accounts/search`, {body: body} ) - if (opts.doExpect) { - expect(response).toHaveStatusCode(200) - } return [response, json] } } diff --git a/qa-core/src/account-api/api/apis/AuthAPI.ts b/qa-core/src/account-api/api/apis/AuthAPI.ts index 9c375d2b5d..5f2df6d250 100644 --- a/qa-core/src/account-api/api/apis/AuthAPI.ts +++ b/qa-core/src/account-api/api/apis/AuthAPI.ts @@ -23,9 +23,9 @@ export default class AuthAPI { }, } ) - if (opts.doExpect) { - expect(response).toHaveStatusCode(200) - } + // if (opts.doExpect) { + // expect(response).toHaveStatusCode(200) + // } const cookie = response.headers.get("set-cookie") return [response, cookie!] } diff --git a/qa-core/src/account-api/tests/accounts/create.spec.ts b/qa-core/src/account-api/tests/accounts/create.spec.ts index 2c26c5170b..11a243a8ba 100644 --- a/qa-core/src/account-api/tests/accounts/create.spec.ts +++ b/qa-core/src/account-api/tests/accounts/create.spec.ts @@ -12,9 +12,12 @@ describe("Account API - Create Account", () => { await config.afterAll() }) - it("Creates a new account", async () => { - await config.api.accounts.create({ - ...fixtures.accounts.generateAccount() + describe("POST /api/accounts/", () => { + it("Returns 201", async () => { + const [res, account] = await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) + expect(res.status).toBe(201) }) }) -}) \ 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 index 2d476d78c0..c3abeed12c 100644 --- a/qa-core/src/account-api/tests/accounts/delete.spec.ts +++ b/qa-core/src/account-api/tests/accounts/delete.spec.ts @@ -1,5 +1,6 @@ import TestConfiguration from "../../config/TestConfiguration" import * as fixtures from "../../fixtures" +import { generator } from "../../../shared" describe("Account API - Delete Account", () => { const config = new TestConfiguration() @@ -12,29 +13,40 @@ describe("Account API - Delete Account", () => { await config.afterAll() }) - it("Deletes an account", async () => { - await config.doInNewState(async () => { - // Create account - const createAccountRequest = fixtures.accounts.generateAccount() - await config.api.accounts.create(createAccountRequest) + describe("DEL /api/accounts", () => { + it("Returns 204", async () => { + await config.doInNewState(async () => { + // Create account + const createAccountRequest = fixtures.accounts.generateAccount() + await config.api.accounts.create(createAccountRequest) - // Login - Get cookie - await config.login( - createAccountRequest.email, - createAccountRequest.password, - createAccountRequest.tenantId - ) + // Login - Get cookie + await config.login( + createAccountRequest.email, + createAccountRequest.password, + createAccountRequest.tenantId + ) - // Delete account - const res = await config.api.accounts.deleteCurrentAccount() + // Delete account + const res = await config.api.accounts.deleteCurrentAccount() + expect(res.status).toBe(204) + }) + }) + }) + + describe("DEL /api/accounts/{accountId}", () => { + it("Returns 204", async () => { + const [response, account] = await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) + // Delete account by ID + const res = await config.api.accounts.delete(account.accountId) expect(res.status).toBe(204) }) - }) - it("Deletes an account by ID", async () => { - const [response, account] = await config.api.accounts.create({ - ...fixtures.accounts.generateAccount() + it("returns 404 - Account not found", async () => { + const accountId = generator.string() + await config.api.accounts.delete(accountId, {status:404}) }) - 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 index a39cf8e325..d928c23996 100644 --- a/qa-core/src/account-api/tests/accounts/search.spec.ts +++ b/qa-core/src/account-api/tests/accounts/search.spec.ts @@ -12,32 +12,59 @@ describe("Account API - Search for Account", () => { await config.afterAll() }) - describe("POST /api/accounts/search", () => { describe("by tenant", () => { it("returns 200 + empty", async () => { const tenantId = generator.string() - const [res, body] = await config.api.accounts.search(tenantId, "tenantId") + const [res, body] = + await config.api.accounts.search(tenantId, "tenantId") expect(res.status).toBe(200) expect(body.length).toBe(0) }) it("returns 200 + found", async () => { - const [res, body] = await config.api.accounts.search(config.state.tenantId!, "tenantId") + const [res, body] = + await config.api.accounts.search(config.state.tenantId!, "tenantId") expect(res.status).toBe(200) expect(body.length).toBe(1) expect(body[0].tenantId).toBe(config.state.tenantId) }) + + it("returns 400 + error: Invalid body - tenantId is not allowed to be empty", async () => { + const [res, body] = + await config.api.accounts.search("", "tenantId") + expect(body).toEqual({ + message: "Invalid body - \"tenantId\" is not allowed to be empty", + status: 400 + }) + }) }) describe("by email", () => { it("returns 200 + empty", async () => { - await config.api.accounts.search(generator.word(), "email") + const email = generator.email() + const [res, body] = + await config.api.accounts.search(email, "email") + expect(res.status).toBe(200) + expect(body.length).toBe(0) }) it("returns 200 + found", async () => { - await config.api.accounts.search(generator.word(), "email") + const [res, body] = + await config.api.accounts.search(config.state.email!, "email") + expect(res.status).toBe(200) + expect(body.length).toBe(1) + expect(body[0].email).toBe(config.state.email) + }) + + it("returns 400 + error: Invalid body - email is not allowed to be empty", async () => { + const [res, body] = + await config.api.accounts.search("", "email") + expect(body).toEqual({ + message: "Invalid body - \"email\" is not allowed to be empty", + status: 400 + }) }) }) }) -}) \ 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 index 5794949eee..16a32ecbe0 100644 --- a/qa-core/src/account-api/tests/accounts/validate.spec.ts +++ b/qa-core/src/account-api/tests/accounts/validate.spec.ts @@ -1,6 +1,6 @@ import TestConfiguration from "../../config/TestConfiguration" +import * as fixtures from "../../fixtures" import { generator } from "../../../shared" -import * as fixtures from "../../fixtures"; describe("Account API - Validate Account", () => { const config = new TestConfiguration() @@ -13,17 +13,34 @@ describe("Account API - Validate Account", () => { await config.afterAll() }) - const tenant = generator.word({length: 6}) - const email = `${tenant}@budibase.com` + describe("POST /api/accounts/validate/email", () => { + it("Returns 200", async () => { + const email = generator.email() + const res = await config.api.accounts.validateEmail(email) + expect(res.status).toBe(200) + }) - - it("Validates an email", async () => { - - await config.api.accounts.validateEmail(email) + it("returns 400", async () => { + const [response, account] = await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) + const res = await config.api.accounts.validateEmail(account.email) + expect(res.status).toBe(400) + }) }) - it("Validates a tenant ID", async () => { + describe("POST /api/accounts/validate/tenantId", () => { + it("Returns 200", async () => { + const res = await config.api.accounts.validateTenantId("randomtenant") + expect(res.status).toBe(200) + }) - await config.api.accounts.validateTenantId(tenant) + it("Returns 400", async () => { + const [response, account] = await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) + const res = await config.api.accounts.validateTenantId(account.tenantId) + expect(res.status).toBe(400) + }) }) -}) \ 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 index 222c4cb4ea..6c14b4a4d9 100644 --- a/qa-core/src/account-api/tests/accounts/verify.spec.ts +++ b/qa-core/src/account-api/tests/accounts/verify.spec.ts @@ -1,6 +1,5 @@ import TestConfiguration from "../../config/TestConfiguration" -import { generator } from "../../../shared" -import * as fixtures from "../../fixtures"; +import * as fixtures from "../../fixtures" describe("Account API - Verify Account", () => { const config = new TestConfiguration() @@ -13,26 +12,45 @@ describe("Account API - Verify Account", () => { await config.afterAll() }) + describe("POST /api/accounts/verify", () => { + it("returns 200", async () => { + // Create unverified account + const createAccountRequest = fixtures.accounts.generateAccount() + const [res, acc] = await config.api.accounts.create( + createAccountRequest, + { doExpect: true, autoVerify: false }) - it("Verify an account", async () => { - // Create account - await config.api.accounts.create({ - ...fixtures.accounts.generateAccount() + // Attempt to log in using unverified account + const [loginResponse, cookie] = await config.accountsApi.auth.login( + createAccountRequest.email, + createAccountRequest.password, + ) + + // await config.login( + // createAccountRequest.email, + // createAccountRequest.password, + // createAccountRequest.tenantId, + // ) + + // Expect response - cannot login via unverified account + + + // Verify account via code + // await config.api.accounts.verifyAccount() + + // Expect response - login successful }) - // Invite user - - // Verify account via code - await config.api.accounts.verifyAccount() }) - it("Send account verification email ", async () => { - // Create account - await config.api.accounts.create({ - ...fixtures.accounts.generateAccount() - }) - // Invite user + describe("POST /api/accounts/verify/send", () => { + it("Send account verification email ", async () => { + // Create account + await config.api.accounts.create({ + ...fixtures.accounts.generateAccount() + }) - // Verify account via email - await config.api.accounts.verifyAccountSendEmail() + // Verify account via email + //await config.api.accounts.verifyAccountSendEmail() + }) }) -}) \ No newline at end of file +}) diff --git a/qa-core/src/jest/globalSetup.ts b/qa-core/src/jest/globalSetup.ts index e52f8cfe16..e11d0e4423 100644 --- a/qa-core/src/jest/globalSetup.ts +++ b/qa-core/src/jest/globalSetup.ts @@ -14,7 +14,7 @@ logging.LOG_CONTEXT = false const accountsApi = new AccountInternalAPI({}) const internalApi = new BudibaseInternalAPI({}) -const API_OPTS: APIRequestOpts = { doExpect: false } +const API_OPTS: APIRequestOpts = { doExpect: false} // @ts-ignore global.qa = {} @@ -23,7 +23,8 @@ async function createAccount(): Promise<[CreateAccountRequest, Account]> { const account = fixtures.accounts.generateAccount() await accountsApi.accounts.validateEmail(account.email, API_OPTS) await accountsApi.accounts.validateTenantId(account.tenantId, API_OPTS) - const [res, newAccount] = await accountsApi.accounts.create(account, API_OPTS) + const [res, newAccount] = await accountsApi.accounts.create( + account, {...API_OPTS, autoVerify: true}) await updateLicense(newAccount.accountId) return [account, newAccount] } diff --git a/qa-core/src/types/api.ts b/qa-core/src/types/api.ts index c94150d3eb..3d52445280 100644 --- a/qa-core/src/types/api.ts +++ b/qa-core/src/types/api.ts @@ -1,5 +1,6 @@ export interface APIRequestOpts { // in some cases we need to bypass the expect assertion in an api call // e.g. during global setup where jest is not available - doExpect: boolean + doExpect?: boolean + status?: number }