Setup oauth2 utils test
This commit is contained in:
parent
3a142fe7c8
commit
29cf7288fe
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"id": "myrealm",
|
||||||
|
"realm": "myrealm",
|
||||||
|
"enabled": true,
|
||||||
|
"clients": [
|
||||||
|
{
|
||||||
|
"clientId": "my-client",
|
||||||
|
"secret": "my-secret",
|
||||||
|
"directAccessGrantsEnabled": true,
|
||||||
|
"serviceAccountsEnabled": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
import { generator } from "@budibase/backend-core/tests"
|
||||||
|
import { GenericContainer, StartedTestContainer, Wait } from "testcontainers"
|
||||||
|
import sdk from "../../.."
|
||||||
|
import TestConfiguration from "../../../../tests/utilities/TestConfiguration"
|
||||||
|
import { generateToken } from "../utils"
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
|
const config = new TestConfiguration()
|
||||||
|
|
||||||
|
const keyClockImage = `quay.io/keycloak/keycloak@sha256:2ce6c7c70994c70dbbd70b372a5422c3b4eebb32583175eac03751320609e52c`
|
||||||
|
|
||||||
|
const volumePath = path.resolve(__dirname, "docker-volume")
|
||||||
|
|
||||||
|
describe("oauth2 utils", () => {
|
||||||
|
let container: StartedTestContainer
|
||||||
|
|
||||||
|
let keycloakUrl: string
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
await config.init()
|
||||||
|
|
||||||
|
container = await new GenericContainer(keyClockImage)
|
||||||
|
.withName("keycloak_testcontainer")
|
||||||
|
.withExposedPorts(8080)
|
||||||
|
.withBindMounts([
|
||||||
|
{ source: volumePath, target: "/opt/keycloak/data/import/" },
|
||||||
|
])
|
||||||
|
.withCommand(["start-dev", "--import-realm"])
|
||||||
|
.withWaitStrategy(Wait.forLogMessage("Listening on: http://0.0.0.0:8080"))
|
||||||
|
.withStartupTimeout(60000)
|
||||||
|
.start()
|
||||||
|
|
||||||
|
keycloakUrl = `http://${container.getHost()}:${container.getMappedPort(
|
||||||
|
8080
|
||||||
|
)}`
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await container.stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("generateToken", () => {
|
||||||
|
it("successfully generates tokens", async () => {
|
||||||
|
const response = await config.doInContext(config.appId, async () => {
|
||||||
|
const oauthConfig = await sdk.oauth2.create({
|
||||||
|
name: generator.guid(),
|
||||||
|
url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
|
||||||
|
})
|
||||||
|
|
||||||
|
const response = await generateToken(oauthConfig.id)
|
||||||
|
return response
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(response).toBe("")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -9,6 +9,9 @@ export async function generateToken(id: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const resp = await fetch(config.url, { method: "post" })
|
const resp = await fetch(config.url, { method: "post" })
|
||||||
|
if (resp.status !== 200) {
|
||||||
|
throw new Error(`Error fetching oauth2 token: ${resp.statusText}`)
|
||||||
|
}
|
||||||
const jsonResponse = await resp.json()
|
const jsonResponse = await resp.json()
|
||||||
return `${jsonResponse.token_type} ${jsonResponse.access_token}`
|
return `${jsonResponse.token_type} ${jsonResponse.access_token}`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue