1
0
Fork 0
mirror of synced 2024-07-03 21:40:55 +12:00

Updates to account endpoint API testing

This commit is contained in:
Mitch-Budibase 2023-07-18 13:48:23 +01:00
parent 278f6f8df3
commit ea91605e2f
9 changed files with 150 additions and 80 deletions

View file

@ -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]
}
}

View file

@ -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!]
}

View file

@ -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)
})
})
})
})

View file

@ -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)
})
})
})

View file

@ -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
})
})
})
})
})
})

View file

@ -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)
})
})
})
})

View file

@ -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()
})
})
})
})

View file

@ -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]
}

View file

@ -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
}