Fix compare.spec.ts.
This commit is contained in:
parent
d7f3ad8a84
commit
55783e17f1
|
@ -0,0 +1,21 @@
|
||||||
|
import { object } from "./utils"
|
||||||
|
import Resource from "./utils/Resource"
|
||||||
|
|
||||||
|
const errorSchema = object({
|
||||||
|
status: {
|
||||||
|
type: "number",
|
||||||
|
description: "The HTTP status code of the error.",
|
||||||
|
},
|
||||||
|
message: {
|
||||||
|
type: "string",
|
||||||
|
description: "A descriptive message about the error.",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
export default new Resource()
|
||||||
|
.setExamples({
|
||||||
|
error: {},
|
||||||
|
})
|
||||||
|
.setSchemas({
|
||||||
|
error: errorSchema,
|
||||||
|
})
|
|
@ -2,11 +2,14 @@ import jestOpenAPI from "jest-openapi"
|
||||||
import { run as generateSchema } from "../../../../../specs/generate"
|
import { run as generateSchema } from "../../../../../specs/generate"
|
||||||
import * as setup from "../../tests/utilities"
|
import * as setup from "../../tests/utilities"
|
||||||
import { generateMakeRequest } from "./utils"
|
import { generateMakeRequest } from "./utils"
|
||||||
import { Table, App, Row, User } from "@budibase/types"
|
import { Table, App, Row } from "@budibase/types"
|
||||||
|
import nock from "nock"
|
||||||
|
import environment from "../../../../environment"
|
||||||
|
|
||||||
const yamlPath = generateSchema()
|
const yamlPath = generateSchema()
|
||||||
jestOpenAPI(yamlPath!)
|
jestOpenAPI(yamlPath!)
|
||||||
|
|
||||||
|
describe("compare", () => {
|
||||||
let config = setup.getConfig()
|
let config = setup.getConfig()
|
||||||
let apiKey: string, table: Table, app: App, makeRequest: any
|
let apiKey: string, table: Table, app: App, makeRequest: any
|
||||||
|
|
||||||
|
@ -19,6 +22,10 @@ beforeAll(async () => {
|
||||||
|
|
||||||
afterAll(setup.afterAll)
|
afterAll(setup.afterAll)
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
nock.cleanAll()
|
||||||
|
})
|
||||||
|
|
||||||
describe("check the applications endpoints", () => {
|
describe("check the applications endpoints", () => {
|
||||||
it("should allow retrieving applications through search", async () => {
|
it("should allow retrieving applications through search", async () => {
|
||||||
const res = await makeRequest("post", "/applications/search")
|
const res = await makeRequest("post", "/applications/search")
|
||||||
|
@ -58,6 +65,10 @@ describe("check the applications endpoints", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should allow deleting an application", async () => {
|
it("should allow deleting an application", async () => {
|
||||||
|
nock(environment.WORKER_URL!)
|
||||||
|
.delete(`/api/global/roles/${config.getProdAppId()}`)
|
||||||
|
.reply(200, {})
|
||||||
|
|
||||||
const res = await makeRequest(
|
const res = await makeRequest(
|
||||||
"delete",
|
"delete",
|
||||||
`/applications/${config.getAppId()}`
|
`/applications/${config.getAppId()}`
|
||||||
|
@ -109,9 +120,13 @@ describe("check the rows endpoints", () => {
|
||||||
let row: Row
|
let row: Row
|
||||||
it("should allow retrieving rows through search", async () => {
|
it("should allow retrieving rows through search", async () => {
|
||||||
table = await config.upsertTable()
|
table = await config.upsertTable()
|
||||||
const res = await makeRequest("post", `/tables/${table._id}/rows/search`, {
|
const res = await makeRequest(
|
||||||
|
"post",
|
||||||
|
`/tables/${table._id}/rows/search`,
|
||||||
|
{
|
||||||
query: {},
|
query: {},
|
||||||
})
|
}
|
||||||
|
)
|
||||||
expect(res).toSatisfyApiSpec()
|
expect(res).toSatisfyApiSpec()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -135,7 +150,10 @@ describe("check the rows endpoints", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should allow retrieving a row", async () => {
|
it("should allow retrieving a row", async () => {
|
||||||
const res = await makeRequest("get", `/tables/${table._id}/rows/${row._id}`)
|
const res = await makeRequest(
|
||||||
|
"get",
|
||||||
|
`/tables/${table._id}/rows/${row._id}`
|
||||||
|
)
|
||||||
expect(res).toSatisfyApiSpec()
|
expect(res).toSatisfyApiSpec()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -148,38 +166,10 @@ describe("check the rows endpoints", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("check the users endpoints", () => {
|
|
||||||
let user: User
|
|
||||||
it("should allow retrieving users through search", async () => {
|
|
||||||
user = await config.createUser()
|
|
||||||
const res = await makeRequest("post", "/users/search")
|
|
||||||
expect(res).toSatisfyApiSpec()
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should allow creating a user", async () => {
|
|
||||||
const res = await makeRequest("post", "/users")
|
|
||||||
expect(res).toSatisfyApiSpec()
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should allow updating a user", async () => {
|
|
||||||
const res = await makeRequest("put", `/users/${user._id}`)
|
|
||||||
expect(res).toSatisfyApiSpec()
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should allow retrieving a user", async () => {
|
|
||||||
const res = await makeRequest("get", `/users/${user._id}`)
|
|
||||||
expect(res).toSatisfyApiSpec()
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should allow deleting a user", async () => {
|
|
||||||
const res = await makeRequest("delete", `/users/${user._id}`)
|
|
||||||
expect(res).toSatisfyApiSpec()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("check the queries endpoints", () => {
|
describe("check the queries endpoints", () => {
|
||||||
it("should allow retrieving queries through search", async () => {
|
it("should allow retrieving queries through search", async () => {
|
||||||
const res = await makeRequest("post", "/queries/search")
|
const res = await makeRequest("post", "/queries/search")
|
||||||
expect(res).toSatisfyApiSpec()
|
expect(res).toSatisfyApiSpec()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
|
@ -2,12 +2,12 @@ import * as setup from "../../tests/utilities"
|
||||||
import { User } from "@budibase/types"
|
import { User } from "@budibase/types"
|
||||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||||
import nock from "nock"
|
import nock from "nock"
|
||||||
import environment from "../../../../environment"
|
|
||||||
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
|
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
|
||||||
|
import { mockWorkerUserAPI } from "./utils"
|
||||||
|
|
||||||
|
describe("public users API", () => {
|
||||||
const config = new TestConfiguration()
|
const config = new TestConfiguration()
|
||||||
let globalUser: User
|
let globalUser: User
|
||||||
let users: Record<string, User> = {}
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await config.init()
|
await config.init()
|
||||||
|
@ -17,50 +17,41 @@ afterAll(setup.afterAll)
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
globalUser = await config.globalUser()
|
globalUser = await config.globalUser()
|
||||||
users[globalUser._id!] = globalUser
|
|
||||||
|
|
||||||
nock.cleanAll()
|
nock.cleanAll()
|
||||||
nock(environment.WORKER_URL!)
|
mockWorkerUserAPI(globalUser)
|
||||||
.get(new RegExp(`/api/global/users/.*`))
|
|
||||||
.reply(200, (uri, body) => {
|
|
||||||
const id = uri.split("/").pop()
|
|
||||||
return users[id!]
|
|
||||||
})
|
|
||||||
.persist()
|
|
||||||
|
|
||||||
nock(environment.WORKER_URL!)
|
|
||||||
.post(`/api/global/users`)
|
|
||||||
.reply(200, (uri, body) => {
|
|
||||||
const newUser = body as User
|
|
||||||
if (!newUser._id) {
|
|
||||||
newUser._id = `us_${generator.guid()}`
|
|
||||||
}
|
|
||||||
users[newUser._id!] = newUser
|
|
||||||
return newUser
|
|
||||||
})
|
|
||||||
.persist()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("check user endpoints", () => {
|
describe("read", () => {
|
||||||
it("should not allow a user to update their own roles", async () => {
|
it("should allow a user to read themselves", async () => {
|
||||||
await config.withUser(globalUser, () =>
|
const user = await config.api.user.find(globalUser._id!)
|
||||||
config.api.public.user.update({
|
expect(user._id).toBe(globalUser._id)
|
||||||
...globalUser,
|
|
||||||
roles: { app_1: "ADMIN" },
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should allow a user to read another user", async () => {
|
||||||
|
const otherUser = await config.api.public.user.create({
|
||||||
|
email: generator.email({ domain: "example.com" }),
|
||||||
|
roles: {},
|
||||||
|
})
|
||||||
|
const user = await config.withUser(globalUser, () =>
|
||||||
|
config.api.public.user.find(otherUser._id!)
|
||||||
)
|
)
|
||||||
const updatedUser = await config.api.user.find(globalUser._id!)
|
expect(user._id).toBe(otherUser._id)
|
||||||
expect(updatedUser.roles?.app_1).toBeUndefined()
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should not allow a user to delete themselves", async () => {
|
|
||||||
await config.withUser(globalUser, () =>
|
|
||||||
config.api.public.user.destroy(globalUser._id!, { status: 405 })
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("role updating on free tier", () => {
|
describe("create", () => {
|
||||||
|
it("can successfully create a new user", async () => {
|
||||||
|
const email = generator.email({ domain: "example.com" })
|
||||||
|
const newUser = await config.api.public.user.create({
|
||||||
|
email,
|
||||||
|
roles: {},
|
||||||
|
})
|
||||||
|
expect(newUser.email).toBe(email)
|
||||||
|
expect(newUser._id).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("role creation on free tier", () => {
|
||||||
it("should not allow 'roles' to be updated", async () => {
|
it("should not allow 'roles' to be updated", async () => {
|
||||||
const newUser = await config.api.public.user.create({
|
const newUser = await config.api.public.user.create({
|
||||||
email: generator.email({ domain: "example.com" }),
|
email: generator.email({ domain: "example.com" }),
|
||||||
|
@ -88,7 +79,7 @@ describe("role updating on free tier", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("role updating on business tier", () => {
|
describe("role creation on business tier", () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
mocks.licenses.useExpandedPublicApi()
|
mocks.licenses.useExpandedPublicApi()
|
||||||
})
|
})
|
||||||
|
@ -119,3 +110,34 @@ describe("role updating on business tier", () => {
|
||||||
expect(newUser.builder?.global).toBe(true)
|
expect(newUser.builder?.global).toBe(true)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("update", () => {
|
||||||
|
it("can update a user", async () => {
|
||||||
|
const updatedUser = await config.api.public.user.update({
|
||||||
|
...globalUser,
|
||||||
|
email: `updated-${globalUser.email}`,
|
||||||
|
})
|
||||||
|
expect(updatedUser.email).toBe(`updated-${globalUser.email}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should not allow a user to update their own roles", async () => {
|
||||||
|
await config.withUser(globalUser, () =>
|
||||||
|
config.api.public.user.update({
|
||||||
|
...globalUser,
|
||||||
|
roles: { app_1: "ADMIN" },
|
||||||
|
})
|
||||||
|
)
|
||||||
|
const updatedUser = await config.api.user.find(globalUser._id!)
|
||||||
|
expect(updatedUser.roles?.app_1).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("delete", () => {
|
||||||
|
it("should not allow a user to delete themselves", async () => {
|
||||||
|
await config.withUser(globalUser, () =>
|
||||||
|
config.api.public.user.destroy(globalUser._id!, { status: 405 })
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import * as setup from "../../tests/utilities"
|
import * as setup from "../../tests/utilities"
|
||||||
import { checkSlashesInUrl } from "../../../../utilities"
|
import { checkSlashesInUrl } from "../../../../utilities"
|
||||||
import supertest from "supertest"
|
import supertest from "supertest"
|
||||||
|
import { User } from "@budibase/types"
|
||||||
|
import environment from "../../../../environment"
|
||||||
|
import nock from "nock"
|
||||||
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
|
|
||||||
export type HttpMethod = "post" | "get" | "put" | "delete" | "patch"
|
export type HttpMethod = "post" | "get" | "put" | "delete" | "patch"
|
||||||
|
|
||||||
|
@ -91,3 +95,43 @@ export function generateMakeRequestWithFormData(
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mockWorkerUserAPI(...seedUsers: User[]) {
|
||||||
|
const users: Record<string, User> = {
|
||||||
|
...seedUsers.reduce((acc, user) => {
|
||||||
|
acc[user._id!] = user
|
||||||
|
return acc
|
||||||
|
}, {} as Record<string, User>),
|
||||||
|
}
|
||||||
|
|
||||||
|
nock(environment.WORKER_URL!)
|
||||||
|
.get(new RegExp(`/api/global/users/.*`))
|
||||||
|
.reply(200, (uri, body) => {
|
||||||
|
const id = uri.split("/").pop()
|
||||||
|
return users[id!]
|
||||||
|
})
|
||||||
|
.persist()
|
||||||
|
|
||||||
|
nock(environment.WORKER_URL!)
|
||||||
|
.post(`/api/global/users`)
|
||||||
|
.reply(200, (uri, body) => {
|
||||||
|
const newUser = body as User
|
||||||
|
if (!newUser._id) {
|
||||||
|
newUser._id = `us_${generator.guid()}`
|
||||||
|
}
|
||||||
|
users[newUser._id!] = newUser
|
||||||
|
return newUser
|
||||||
|
})
|
||||||
|
.persist()
|
||||||
|
|
||||||
|
nock(environment.WORKER_URL!)
|
||||||
|
.put(new RegExp(`/api/global/users/.*`))
|
||||||
|
.reply(200, (uri, body) => {
|
||||||
|
const id = uri.split("/").pop()!
|
||||||
|
const updatedUser = body as User
|
||||||
|
const existingUser = users[id] || {}
|
||||||
|
users[id] = { ...existingUser, ...updatedUser }
|
||||||
|
return users[id]
|
||||||
|
})
|
||||||
|
.persist()
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
|
import jestOpenAPI from "jest-openapi"
|
||||||
|
import { run as generateSchema } from "../../../../specs/generate"
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import TestConfiguration from "../TestConfiguration"
|
||||||
import request, { SuperTest, Test, Response } from "supertest"
|
import request, { SuperTest, Test, Response } from "supertest"
|
||||||
import { ReadStream } from "fs"
|
import { ReadStream } from "fs"
|
||||||
import { getServer } from "../../../app"
|
import { getServer } from "../../../app"
|
||||||
|
|
||||||
|
const yamlPath = generateSchema()
|
||||||
|
jestOpenAPI(yamlPath!)
|
||||||
|
|
||||||
type Headers = Record<string, string | string[] | undefined>
|
type Headers = Record<string, string | string[] | undefined>
|
||||||
type Method = "get" | "post" | "put" | "patch" | "delete"
|
type Method = "get" | "post" | "put" | "patch" | "delete"
|
||||||
|
|
||||||
|
@ -170,10 +175,7 @@ export abstract class TestAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _checkResponse = (
|
protected _checkResponse(response: Response, expectations?: Expectations) {
|
||||||
response: Response,
|
|
||||||
expectations?: Expectations
|
|
||||||
) => {
|
|
||||||
const { status = 200 } = expectations || {}
|
const { status = 200 } = expectations || {}
|
||||||
|
|
||||||
if (response.status !== status) {
|
if (response.status !== status) {
|
||||||
|
@ -259,4 +261,14 @@ export abstract class PublicAPI extends TestAPI {
|
||||||
|
|
||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected _checkResponse(response: Response, expectations?: Expectations) {
|
||||||
|
const checked = super._checkResponse(response, expectations)
|
||||||
|
if (checked.status >= 200 && checked.status < 300) {
|
||||||
|
// We don't seem to have documented our errors yet, so for the time being
|
||||||
|
// we'll only do the schema check for successful responses.
|
||||||
|
expect(checked).toSatisfyApiSpec()
|
||||||
|
}
|
||||||
|
return checked
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,18 @@ import { Expectations, PublicAPI } from "../base"
|
||||||
|
|
||||||
export class UserPublicAPI extends PublicAPI {
|
export class UserPublicAPI extends PublicAPI {
|
||||||
find = async (id: string, expectations?: Expectations): Promise<User> => {
|
find = async (id: string, expectations?: Expectations): Promise<User> => {
|
||||||
return await this._get<User>(`/users/${id}`, { expectations })
|
const response = await this._get<{ data: User }>(`/users/${id}`, {
|
||||||
|
expectations,
|
||||||
|
})
|
||||||
|
return response.data
|
||||||
}
|
}
|
||||||
|
|
||||||
update = async (user: User, expectations?: Expectations): Promise<User> => {
|
update = async (user: User, expectations?: Expectations): Promise<User> => {
|
||||||
return await this._put<User>(`/users/${user._id}`, {
|
const response = await this._put<{ data: User }>(`/users/${user._id}`, {
|
||||||
body: user,
|
body: user,
|
||||||
expectations,
|
expectations,
|
||||||
})
|
})
|
||||||
|
return response.data
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy = async (id: string, expectations?: Expectations): Promise<void> => {
|
destroy = async (id: string, expectations?: Expectations): Promise<void> => {
|
||||||
|
|
Loading…
Reference in New Issue