1
0
Fork 0
mirror of synced 2024-07-10 16:55:46 +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 }, body: { email },
} }
) )
if (opts.doExpect) {
expect(response).toHaveStatusCode(200)
}
return response return response
} }
@ -36,18 +33,15 @@ export default class AccountAPI {
body: { tenantId }, body: { tenantId },
} }
) )
if (opts.doExpect) {
expect(response).toHaveStatusCode(200)
}
return response return response
} }
async create( async create(
body: CreateAccountRequest, body: CreateAccountRequest,
opts: APIRequestOpts = { doExpect: true } opts: APIRequestOpts & { autoVerify: boolean } = { doExpect: true, autoVerify: true }
): Promise<[Response, Account]> { ): Promise<[Response, Account]> {
const headers = { const headers = {
"no-verify": "1", "no-verify": opts.autoVerify ? "1" : "0"
} }
const [response, json] = await this.client.post(`/api/accounts`, { const [response, json] = await this.client.post(`/api/accounts`, {
body, body,
@ -59,7 +53,7 @@ export default class AccountAPI {
return [response, json] return [response, json]
} }
async delete(accountID: string) { async delete(accountID: string, opts: APIRequestOpts = {status:204}) {
const [response, json] = await this.client.del( const [response, json] = await this.client.del(
`/api/accounts/${accountID}`, `/api/accounts/${accountID}`,
{ {
@ -67,8 +61,8 @@ export default class AccountAPI {
} }
) )
// can't use expect here due to use in global teardown // can't use expect here due to use in global teardown
if (response.status !== 204) { if (response.status !== opts.status) {
throw new Error(`Could not delete accountId=${accountID}`) throw new Error(`status: ${response.status} not equal to expected: ${opts.status}`)
} }
return response return response
} }
@ -120,18 +114,15 @@ export default class AccountAPI {
let body: SearchAccountsRequest = {} let body: SearchAccountsRequest = {}
if (search === 'email') { if (search === 'email') {
body.email = searchType; body.email = searchType
} else if (search === 'tenantId') { } else if (search === 'tenantId') {
body.tenantId = searchType; body.tenantId = searchType
} }
const [response, json] = await this.client.post( const [response, json] = await this.client.post(
`/api/accounts/search`, `/api/accounts/search`,
{body: body} {body: body}
) )
if (opts.doExpect) {
expect(response).toHaveStatusCode(200)
}
return [response, json] return [response, json]
} }
} }

View file

@ -23,9 +23,9 @@ export default class AuthAPI {
}, },
} }
) )
if (opts.doExpect) { // if (opts.doExpect) {
expect(response).toHaveStatusCode(200) // expect(response).toHaveStatusCode(200)
} // }
const cookie = response.headers.get("set-cookie") const cookie = response.headers.get("set-cookie")
return [response, cookie!] return [response, cookie!]
} }

View file

@ -12,9 +12,12 @@ describe("Account API - Create Account", () => {
await config.afterAll() await config.afterAll()
}) })
it("Creates a new account", async () => { describe("POST /api/accounts/", () => {
await config.api.accounts.create({ it("Returns 201", async () => {
...fixtures.accounts.generateAccount() 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 TestConfiguration from "../../config/TestConfiguration"
import * as fixtures from "../../fixtures" import * as fixtures from "../../fixtures"
import { generator } from "../../../shared"
describe("Account API - Delete Account", () => { describe("Account API - Delete Account", () => {
const config = new TestConfiguration() const config = new TestConfiguration()
@ -12,29 +13,40 @@ describe("Account API - Delete Account", () => {
await config.afterAll() await config.afterAll()
}) })
it("Deletes an account", async () => { describe("DEL /api/accounts", () => {
await config.doInNewState(async () => { it("Returns 204", async () => {
// Create account await config.doInNewState(async () => {
const createAccountRequest = fixtures.accounts.generateAccount() // Create account
await config.api.accounts.create(createAccountRequest) const createAccountRequest = fixtures.accounts.generateAccount()
await config.api.accounts.create(createAccountRequest)
// Login - Get cookie // Login - Get cookie
await config.login( await config.login(
createAccountRequest.email, createAccountRequest.email,
createAccountRequest.password, createAccountRequest.password,
createAccountRequest.tenantId createAccountRequest.tenantId
) )
// Delete account // Delete account
const res = await config.api.accounts.deleteCurrentAccount() 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) expect(res.status).toBe(204)
}) })
})
it("Deletes an account by ID", async () => { it("returns 404 - Account not found", async () => {
const [response, account] = await config.api.accounts.create({ const accountId = generator.string()
...fixtures.accounts.generateAccount() await config.api.accounts.delete(accountId, {status:404})
}) })
await config.api.accounts.delete(account.accountId)
}) })
}) })

View file

@ -12,31 +12,58 @@ describe("Account API - Search for Account", () => {
await config.afterAll() await config.afterAll()
}) })
describe("POST /api/accounts/search", () => { describe("POST /api/accounts/search", () => {
describe("by tenant", () => { describe("by tenant", () => {
it("returns 200 + empty", async () => { it("returns 200 + empty", async () => {
const tenantId = generator.string() 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(res.status).toBe(200)
expect(body.length).toBe(0) expect(body.length).toBe(0)
}) })
it("returns 200 + found", async () => { 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(res.status).toBe(200)
expect(body.length).toBe(1) expect(body.length).toBe(1)
expect(body[0].tenantId).toBe(config.state.tenantId) 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", () => { describe("by email", () => {
it("returns 200 + empty", async () => { 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 () => { 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 TestConfiguration from "../../config/TestConfiguration"
import * as fixtures from "../../fixtures"
import { generator } from "../../../shared" import { generator } from "../../../shared"
import * as fixtures from "../../fixtures";
describe("Account API - Validate Account", () => { describe("Account API - Validate Account", () => {
const config = new TestConfiguration() const config = new TestConfiguration()
@ -13,17 +13,34 @@ describe("Account API - Validate Account", () => {
await config.afterAll() await config.afterAll()
}) })
const tenant = generator.word({length: 6}) describe("POST /api/accounts/validate/email", () => {
const email = `${tenant}@budibase.com` it("Returns 200", async () => {
const email = generator.email()
const res = await config.api.accounts.validateEmail(email)
expect(res.status).toBe(200)
})
it("returns 400", async () => {
it("Validates an email", async () => { const [response, account] = await config.api.accounts.create({
...fixtures.accounts.generateAccount()
await config.api.accounts.validateEmail(email) })
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 TestConfiguration from "../../config/TestConfiguration"
import { generator } from "../../../shared" import * as fixtures from "../../fixtures"
import * as fixtures from "../../fixtures";
describe("Account API - Verify Account", () => { describe("Account API - Verify Account", () => {
const config = new TestConfiguration() const config = new TestConfiguration()
@ -13,26 +12,45 @@ describe("Account API - Verify Account", () => {
await config.afterAll() 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 () => { // Attempt to log in using unverified account
// Create account const [loginResponse, cookie] = await config.accountsApi.auth.login(
await config.api.accounts.create({ createAccountRequest.email,
...fixtures.accounts.generateAccount() 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 () => { describe("POST /api/accounts/verify/send", () => {
// Create account it("Send account verification email ", async () => {
await config.api.accounts.create({ // Create account
...fixtures.accounts.generateAccount() await config.api.accounts.create({
}) ...fixtures.accounts.generateAccount()
// Invite user })
// Verify account via email // Verify account via email
await config.api.accounts.verifyAccountSendEmail() //await config.api.accounts.verifyAccountSendEmail()
})
}) })
}) })

View file

@ -14,7 +14,7 @@ logging.LOG_CONTEXT = false
const accountsApi = new AccountInternalAPI({}) const accountsApi = new AccountInternalAPI({})
const internalApi = new BudibaseInternalAPI({}) const internalApi = new BudibaseInternalAPI({})
const API_OPTS: APIRequestOpts = { doExpect: false } const API_OPTS: APIRequestOpts = { doExpect: false}
// @ts-ignore // @ts-ignore
global.qa = {} global.qa = {}
@ -23,7 +23,8 @@ async function createAccount(): Promise<[CreateAccountRequest, Account]> {
const account = fixtures.accounts.generateAccount() const account = fixtures.accounts.generateAccount()
await accountsApi.accounts.validateEmail(account.email, API_OPTS) await accountsApi.accounts.validateEmail(account.email, API_OPTS)
await accountsApi.accounts.validateTenantId(account.tenantId, 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) await updateLicense(newAccount.accountId)
return [account, newAccount] return [account, newAccount]
} }

View file

@ -1,5 +1,6 @@
export interface APIRequestOpts { export interface APIRequestOpts {
// in some cases we need to bypass the expect assertion in an api call // in some cases we need to bypass the expect assertion in an api call
// e.g. during global setup where jest is not available // e.g. during global setup where jest is not available
doExpect: boolean doExpect?: boolean
status?: number
} }