From d55cda0d803c941587081d58fff947432e19a5a0 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 26 Sep 2022 16:54:14 +0100 Subject: [PATCH] internal API test config --- qa-core/.env | 3 +- .../TestConfiguration/InternalAPIClient.ts | 58 +++++++++++++++++++ .../TestConfiguration/applications.ts | 27 +++++++++ .../internal-api/TestConfiguration/auth.ts | 22 +++++++ .../TestConfiguration/generator.ts | 3 + .../internal-api/TestConfiguration/index.ts | 21 +++++++ .../internal-api/TestConfiguration/users.ts | 44 ++++++++++++++ .../internal-api/applications/create.spec.ts | 31 ++++++++++ 8 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 qa-core/src/config/internal-api/TestConfiguration/InternalAPIClient.ts create mode 100644 qa-core/src/config/internal-api/TestConfiguration/applications.ts create mode 100644 qa-core/src/config/internal-api/TestConfiguration/auth.ts create mode 100644 qa-core/src/config/internal-api/TestConfiguration/generator.ts create mode 100644 qa-core/src/config/internal-api/TestConfiguration/index.ts create mode 100644 qa-core/src/config/internal-api/TestConfiguration/users.ts create mode 100644 qa-core/src/tests/internal-api/applications/create.spec.ts diff --git a/qa-core/.env b/qa-core/.env index 36dd0a3656..93b5fde74a 100644 --- a/qa-core/.env +++ b/qa-core/.env @@ -3,4 +3,5 @@ BB_ADMIN_USER_PASSWORD=budibase ENCRYPTED_TEST_PUBLIC_API_KEY=a65722f06bee5caeadc5d7ca2f543a43-d610e627344210c643bb726f COUCH_DB_URL=http://budibase:budibase@localhost:4567 COUCH_DB_USER=budibase -COUCH_DB_PASSWORD=budibase \ No newline at end of file +COUCH_DB_PASSWORD=budibase +JWT_SECRET=test \ No newline at end of file diff --git a/qa-core/src/config/internal-api/TestConfiguration/InternalAPIClient.ts b/qa-core/src/config/internal-api/TestConfiguration/InternalAPIClient.ts new file mode 100644 index 0000000000..36014fac17 --- /dev/null +++ b/qa-core/src/config/internal-api/TestConfiguration/InternalAPIClient.ts @@ -0,0 +1,58 @@ +import env from "../../../environment" +import fetch, { HeadersInit } from "node-fetch" + +type APIMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" + +interface ApiOptions { + method?: APIMethod + body?: object + headers?: HeadersInit | undefined +} + +class InternalAPIClient { + host: string + appId?: string + csrfToken?: string + + constructor(appId?: string) { + if (!env.BUDIBASE_SERVER_URL) { + throw new Error( + "Must set BUDIBASE_SERVER_URL env var" + ) + } + this.host = `${env.BUDIBASE_SERVER_URL}/api` + this.appId = appId + } + + apiCall = + (method: APIMethod) => + async (url = "", options: ApiOptions = {}) => { + const requestOptions = { + method, + body: JSON.stringify(options.body), + headers: { + "x-budibase-app-id": this.appId, + "x-csrf-token": this.csrfToken, + "Content-Type": "application/json", + Accept: "application/json", + ...options.headers, + }, + credentials: "include", + } + + // @ts-ignore + const response = await fetch(`${this.host}${url}`, requestOptions) + if (response.status !== 200) { + console.error(response) + } + return response + } + + post = this.apiCall("POST") + get = this.apiCall("GET") + patch = this.apiCall("PATCH") + del = this.apiCall("DELETE") + put = this.apiCall("PUT") +} + +export default InternalAPIClient \ No newline at end of file diff --git a/qa-core/src/config/internal-api/TestConfiguration/applications.ts b/qa-core/src/config/internal-api/TestConfiguration/applications.ts new file mode 100644 index 0000000000..1ab05d2391 --- /dev/null +++ b/qa-core/src/config/internal-api/TestConfiguration/applications.ts @@ -0,0 +1,27 @@ +import { + Application, +} from "@budibase/server/api/controllers/public/mapping/types" +import { Response } from "node-fetch" +import InternalAPIClient from "./InternalAPIClient" + +export default class AppApi { + api: InternalAPIClient + + constructor(apiClient: InternalAPIClient) { + this.api = apiClient + } + + async create( + body: any + ): Promise<[Response, Application]> { + const response = await this.api.post(`/applications`, { body }) + const json = await response.json() + return [response, json.data] + } + + async read(id: string): Promise<[Response, Application]> { + const response = await this.api.get(`/applications/${id}`) + const json = await response.json() + return [response, json.data] + } +} diff --git a/qa-core/src/config/internal-api/TestConfiguration/auth.ts b/qa-core/src/config/internal-api/TestConfiguration/auth.ts new file mode 100644 index 0000000000..98ecd5ba52 --- /dev/null +++ b/qa-core/src/config/internal-api/TestConfiguration/auth.ts @@ -0,0 +1,22 @@ +import { Response } from "node-fetch" +import InternalAPIClient from "./InternalAPIClient" + +export default class AuthApi { + api: InternalAPIClient + + constructor(apiClient: InternalAPIClient) { + this.api = apiClient + } + + async login(): Promise<[Response, any]> { + const response = await this.api.post(`/global/auth/default/login`, { + body: { + // username: process.env.BB_ADMIN_USER_EMAIL, + // password: process.env.BB_ADMIN_USER_PASSWORD + username: "qa@budibase.com", + password: "budibase" + } + }) + return [response, response.headers.get("set-cookie")] + } +} diff --git a/qa-core/src/config/internal-api/TestConfiguration/generator.ts b/qa-core/src/config/internal-api/TestConfiguration/generator.ts new file mode 100644 index 0000000000..c9395f7e47 --- /dev/null +++ b/qa-core/src/config/internal-api/TestConfiguration/generator.ts @@ -0,0 +1,3 @@ +const Chance = require("chance") + +export default new Chance() diff --git a/qa-core/src/config/internal-api/TestConfiguration/index.ts b/qa-core/src/config/internal-api/TestConfiguration/index.ts new file mode 100644 index 0000000000..69c7e8df96 --- /dev/null +++ b/qa-core/src/config/internal-api/TestConfiguration/index.ts @@ -0,0 +1,21 @@ +import ApplicationApi from "./applications" +import AuthApi from "./auth" +import InternalAPIClient from "./InternalAPIClient" + +export default class TestConfiguration { + applications: ApplicationApi + auth: AuthApi + context: T + + constructor(apiClient: InternalAPIClient) { + this.applications = new ApplicationApi(apiClient) + this.auth = new AuthApi(apiClient) + this.context = {} + } + + async beforeAll() {} + + async afterAll() { + this.context = {} + } +} diff --git a/qa-core/src/config/internal-api/TestConfiguration/users.ts b/qa-core/src/config/internal-api/TestConfiguration/users.ts new file mode 100644 index 0000000000..8e53e4ee7f --- /dev/null +++ b/qa-core/src/config/internal-api/TestConfiguration/users.ts @@ -0,0 +1,44 @@ +import PublicAPIClient from "./InternalAPIClient" +import { + CreateUserParams, + SearchInputParams, + User, +} from "@budibase/server/api/controllers/public/mapping/types" +import { Response } from "node-fetch" +import generateUser from "../fixtures/users" + +export default class UserApi { + api: PublicAPIClient + + constructor(apiClient: PublicAPIClient) { + this.api = apiClient + } + + async seed() { + return this.create(generateUser()) + } + + async create(body: CreateUserParams): Promise<[Response, User]> { + const response = await this.api.post(`/users`, { body }) + const json = await response.json() + return [response, json.data] + } + + async read(id: string): Promise<[Response, User]> { + const response = await this.api.get(`/users/${id}`) + const json = await response.json() + return [response, json.data] + } + + async search(body: SearchInputParams): Promise<[Response, [User]]> { + const response = await this.api.post(`/users/search`, { body }) + const json = await response.json() + return [response, json.data] + } + + async update(id: string, body: User): Promise<[Response, User]> { + const response = await this.api.put(`/users/${id}`, { body }) + const json = await response.json() + return [response, json.data] + } +} diff --git a/qa-core/src/tests/internal-api/applications/create.spec.ts b/qa-core/src/tests/internal-api/applications/create.spec.ts new file mode 100644 index 0000000000..2ebdcd4bec --- /dev/null +++ b/qa-core/src/tests/internal-api/applications/create.spec.ts @@ -0,0 +1,31 @@ +import TestConfiguration from "../../../config/internal-api/TestConfiguration" +import { Application } from "@budibase/server/api/controllers/public/mapping/types" +import InternalAPIClient from "../../../config/internal-api/TestConfiguration/InternalAPIClient" + +describe("Internal API - /applications endpoints", () => { + const api = new InternalAPIClient() + const config = new TestConfiguration(api) + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + it("POST - Can login", async () => { + const [response] = await config.auth.login() + expect(response).toHaveStatusCode(200) + }) + + it("POST - Create an application", async () => { + const [response, app] = await config.applications.create({ + name: "abc123", + url: "/foo", + useTemplate: false + }) + expect(response).toHaveStatusCode(200) + expect(app._id).toBeDefined() + }) +})