From 6f7ef18084389c8e525109b01e58cfeb742239a4 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 13 Jul 2023 12:48:44 +0100 Subject: [PATCH] Add account password login integration with global setup --- .../src/account-api/api/AccountInternalAPI.ts | 4 ++- qa-core/src/account-api/api/apis/AuthAPI.ts | 32 +++++++++++++++++++ qa-core/src/account-api/api/apis/index.ts | 1 + qa-core/src/jest/globalSetup.ts | 3 +- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 qa-core/src/account-api/api/apis/AuthAPI.ts diff --git a/qa-core/src/account-api/api/AccountInternalAPI.ts b/qa-core/src/account-api/api/AccountInternalAPI.ts index a5abdcbccd..3813ad2c9e 100644 --- a/qa-core/src/account-api/api/AccountInternalAPI.ts +++ b/qa-core/src/account-api/api/AccountInternalAPI.ts @@ -1,15 +1,17 @@ import AccountInternalAPIClient from "./AccountInternalAPIClient" -import { AccountAPI, LicenseAPI } from "./apis" +import { AccountAPI, LicenseAPI, AuthAPI } from "./apis" import { State } from "../../types" export default class AccountInternalAPI { client: AccountInternalAPIClient + auth: AuthAPI accounts: AccountAPI licenses: LicenseAPI constructor(state: State) { this.client = new AccountInternalAPIClient(state) + this.auth = new AuthAPI(this.client) this.accounts = new AccountAPI(this.client) this.licenses = new LicenseAPI(this.client) } diff --git a/qa-core/src/account-api/api/apis/AuthAPI.ts b/qa-core/src/account-api/api/apis/AuthAPI.ts new file mode 100644 index 0000000000..4d2dee6a0d --- /dev/null +++ b/qa-core/src/account-api/api/apis/AuthAPI.ts @@ -0,0 +1,32 @@ +import { Response } from "node-fetch" +import AccountInternalAPIClient from "../AccountInternalAPIClient" +import { APIRequestOpts } from "../../../types" + +export default class AuthAPI { + client: AccountInternalAPIClient + + constructor(client: AccountInternalAPIClient) { + this.client = client + } + + async login( + email: string, + password: string, + opts: APIRequestOpts = { doExpect: true } + ): Promise<[Response, string]> { + const [response, json] = await this.client.post( + `/api/auth/login`, + { + body: { + username: email, + password: password, + }, + } + ) + 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/api/apis/index.ts b/qa-core/src/account-api/api/apis/index.ts index b8a1dc7b4a..1137ac3e36 100644 --- a/qa-core/src/account-api/api/apis/index.ts +++ b/qa-core/src/account-api/api/apis/index.ts @@ -1,2 +1,3 @@ +export { default as AuthAPI } from "./AuthAPI" export { default as AccountAPI } from "./AccountAPI" export { default as LicenseAPI } from "./LicenseAPI" diff --git a/qa-core/src/jest/globalSetup.ts b/qa-core/src/jest/globalSetup.ts index 040a65ecef..f73ea1011e 100644 --- a/qa-core/src/jest/globalSetup.ts +++ b/qa-core/src/jest/globalSetup.ts @@ -68,8 +68,7 @@ async function loginAsAdmin() { } async function loginAsAccount(account: CreateAccountRequest) { - const [res, cookie] = await internalApi.auth.login( - account.tenantId, + const [res, cookie] = await accountsApi.auth.login( account.email, account.password, API_OPTS