Migrate UserAPI
This commit is contained in:
parent
149d2c0b29
commit
7fa5dbeec9
|
@ -90,7 +90,7 @@ describe("/users", () => {
|
||||||
})
|
})
|
||||||
await config.api.user.update(
|
await config.api.user.update(
|
||||||
{ ...user, roleId: roles.BUILTIN_ROLE_IDS.POWER },
|
{ ...user, roleId: roles.BUILTIN_ROLE_IDS.POWER },
|
||||||
{ expectStatus: 409 }
|
{ status: 409 }
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,154 +4,79 @@ import {
|
||||||
Flags,
|
Flags,
|
||||||
UserMetadata,
|
UserMetadata,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import { Expectations, TestAPI } from "./base"
|
||||||
import { TestAPI } from "./base"
|
|
||||||
import { DocumentInsertResponse } from "@budibase/nano"
|
import { DocumentInsertResponse } from "@budibase/nano"
|
||||||
|
|
||||||
export class UserAPI extends TestAPI {
|
export class UserAPI extends TestAPI {
|
||||||
constructor(config: TestConfiguration) {
|
|
||||||
super(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch = async (
|
fetch = async (
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
expectations?: Expectations
|
||||||
): Promise<FetchUserMetadataResponse> => {
|
): Promise<FetchUserMetadataResponse> => {
|
||||||
const res = await this.request
|
return await this._get<FetchUserMetadataResponse>("/api/users/metadata", {
|
||||||
.get(`/api/users/metadata`)
|
expectations,
|
||||||
.set(this.config.defaultHeaders())
|
})
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
|
|
||||||
if (res.status !== expectStatus) {
|
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body
|
|
||||||
}
|
}
|
||||||
|
|
||||||
find = async (
|
find = async (
|
||||||
id: string,
|
id: string,
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
expectations?: Expectations
|
||||||
): Promise<FindUserMetadataResponse> => {
|
): Promise<FindUserMetadataResponse> => {
|
||||||
const res = await this.request
|
return await this._get<FindUserMetadataResponse>(
|
||||||
.get(`/api/users/metadata/${id}`)
|
`/api/users/metadata/${id}`,
|
||||||
.set(this.config.defaultHeaders())
|
{
|
||||||
.expect("Content-Type", /json/)
|
expectations,
|
||||||
|
}
|
||||||
if (res.status !== expectStatus) {
|
)
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update = async (
|
update = async (
|
||||||
user: UserMetadata,
|
user: UserMetadata,
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
expectations?: Expectations
|
||||||
): Promise<DocumentInsertResponse> => {
|
): Promise<DocumentInsertResponse> => {
|
||||||
const res = await this.request
|
return await this._put<DocumentInsertResponse>("/api/users/metadata", {
|
||||||
.put(`/api/users/metadata`)
|
body: user,
|
||||||
.set(this.config.defaultHeaders())
|
expectations,
|
||||||
.send(user)
|
})
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
|
|
||||||
if (res.status !== expectStatus) {
|
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body as DocumentInsertResponse
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSelf = async (
|
updateSelf = async (
|
||||||
user: UserMetadata,
|
user: UserMetadata,
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
expectations?: Expectations
|
||||||
): Promise<DocumentInsertResponse> => {
|
): Promise<DocumentInsertResponse> => {
|
||||||
const res = await this.request
|
return await this._post<DocumentInsertResponse>(
|
||||||
.post(`/api/users/metadata/self`)
|
"/api/users/metadata/self",
|
||||||
.set(this.config.defaultHeaders())
|
{
|
||||||
.send(user)
|
body: user,
|
||||||
.expect("Content-Type", /json/)
|
expectations,
|
||||||
|
}
|
||||||
if (res.status !== expectStatus) {
|
)
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body as DocumentInsertResponse
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy = async (
|
destroy = async (
|
||||||
id: string,
|
id: string,
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
expectations?: Expectations
|
||||||
): Promise<{ message: string }> => {
|
): Promise<{ message: string }> => {
|
||||||
const res = await this.request
|
return await this._delete<{ message: string }>(
|
||||||
.delete(`/api/users/metadata/${id}`)
|
`/api/users/metadata/${id}`,
|
||||||
.set(this.config.defaultHeaders())
|
{
|
||||||
.expect("Content-Type", /json/)
|
expectations,
|
||||||
|
}
|
||||||
if (res.status !== expectStatus) {
|
)
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body as { message: string }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setFlag = async (
|
setFlag = async (
|
||||||
flag: string,
|
flag: string,
|
||||||
value: any,
|
value: any,
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
expectations?: Expectations
|
||||||
): Promise<{ message: string }> => {
|
): Promise<{ message: string }> => {
|
||||||
const res = await this.request
|
return await this._post<{ message: string }>(`/api/users/flags`, {
|
||||||
.post(`/api/users/flags`)
|
body: { flag, value },
|
||||||
.set(this.config.defaultHeaders())
|
expectations,
|
||||||
.send({ flag, value })
|
})
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
|
|
||||||
if (res.status !== expectStatus) {
|
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body as { message: string }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getFlags = async (
|
getFlags = async (expectations?: Expectations): Promise<Flags> => {
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
return await this._get<Flags>(`/api/users/flags`, {
|
||||||
): Promise<Flags> => {
|
expectations,
|
||||||
const res = await this.request
|
})
|
||||||
.get(`/api/users/flags`)
|
|
||||||
.set(this.config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
|
|
||||||
if (res.status !== expectStatus) {
|
|
||||||
throw new Error(
|
|
||||||
`Expected status ${expectStatus} but got ${
|
|
||||||
res.status
|
|
||||||
} with body ${JSON.stringify(res.body)}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body as Flags
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue