internal API test config

This commit is contained in:
Martin McKeaveney 2022-09-26 16:54:14 +01:00
parent 157040502a
commit bfe1004487
8 changed files with 208 additions and 1 deletions

View File

@ -3,4 +3,5 @@ BB_ADMIN_USER_PASSWORD=budibase
ENCRYPTED_TEST_PUBLIC_API_KEY=a65722f06bee5caeadc5d7ca2f543a43-d610e627344210c643bb726f ENCRYPTED_TEST_PUBLIC_API_KEY=a65722f06bee5caeadc5d7ca2f543a43-d610e627344210c643bb726f
COUCH_DB_URL=http://budibase:budibase@localhost:4567 COUCH_DB_URL=http://budibase:budibase@localhost:4567
COUCH_DB_USER=budibase COUCH_DB_USER=budibase
COUCH_DB_PASSWORD=budibase COUCH_DB_PASSWORD=budibase
JWT_SECRET=test

View File

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

View File

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

View File

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

View File

@ -0,0 +1,3 @@
const Chance = require("chance")
export default new Chance()

View File

@ -0,0 +1,21 @@
import ApplicationApi from "./applications"
import AuthApi from "./auth"
import InternalAPIClient from "./InternalAPIClient"
export default class TestConfiguration<T> {
applications: ApplicationApi
auth: AuthApi
context: T
constructor(apiClient: InternalAPIClient) {
this.applications = new ApplicationApi(apiClient)
this.auth = new AuthApi(apiClient)
this.context = <T>{}
}
async beforeAll() {}
async afterAll() {
this.context = <T>{}
}
}

View File

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

View File

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