Fix tests

This commit is contained in:
Adria Navarro 2025-03-19 16:32:53 +01:00
parent 18a431df12
commit cc042e2093
2 changed files with 70 additions and 54 deletions

View File

@ -1,5 +1,6 @@
import { import {
OAuth2Config, OAuth2Config,
OAuth2CredentialsMethod,
PASSWORD_REPLACEMENT, PASSWORD_REPLACEMENT,
UpsertOAuth2ConfigRequest, UpsertOAuth2ConfigRequest,
VirtualDocumentType, VirtualDocumentType,
@ -17,6 +18,7 @@ describe("/oauth2", () => {
url: generator.url(), url: generator.url(),
clientId: generator.guid(), clientId: generator.guid(),
clientSecret: generator.hash(), clientSecret: generator.hash(),
method: generator.pickone(Object.values(OAuth2CredentialsMethod)),
} }
} }
@ -54,6 +56,7 @@ describe("/oauth2", () => {
url: c.url, url: c.url,
clientId: c.clientId, clientId: c.clientId,
clientSecret: PASSWORD_REPLACEMENT, clientSecret: PASSWORD_REPLACEMENT,
method: c.method,
})) }))
), ),
}) })
@ -74,6 +77,7 @@ describe("/oauth2", () => {
url: oauth2Config.url, url: oauth2Config.url,
clientId: oauth2Config.clientId, clientId: oauth2Config.clientId,
clientSecret: PASSWORD_REPLACEMENT, clientSecret: PASSWORD_REPLACEMENT,
method: oauth2Config.method,
}, },
], ],
}) })
@ -93,6 +97,7 @@ describe("/oauth2", () => {
url: oauth2Config.url, url: oauth2Config.url,
clientId: oauth2Config.clientId, clientId: oauth2Config.clientId,
clientSecret: PASSWORD_REPLACEMENT, clientSecret: PASSWORD_REPLACEMENT,
method: oauth2Config.method,
}, },
{ {
id: expectOAuth2ConfigId, id: expectOAuth2ConfigId,
@ -100,6 +105,7 @@ describe("/oauth2", () => {
url: oauth2Config2.url, url: oauth2Config2.url,
clientId: oauth2Config2.clientId, clientId: oauth2Config2.clientId,
clientSecret: PASSWORD_REPLACEMENT, clientSecret: PASSWORD_REPLACEMENT,
method: oauth2Config2.method,
}, },
]) ])
expect(response.configs[0].id).not.toEqual(response.configs[1].id) expect(response.configs[0].id).not.toEqual(response.configs[1].id)
@ -125,6 +131,7 @@ describe("/oauth2", () => {
url: oauth2Config.url, url: oauth2Config.url,
clientId: oauth2Config.clientId, clientId: oauth2Config.clientId,
clientSecret: PASSWORD_REPLACEMENT, clientSecret: PASSWORD_REPLACEMENT,
method: oauth2Config.method,
}, },
]) ])
}) })
@ -161,6 +168,7 @@ describe("/oauth2", () => {
url: configData.url, url: configData.url,
clientId: configData.clientId, clientId: configData.clientId,
clientSecret: PASSWORD_REPLACEMENT, clientSecret: PASSWORD_REPLACEMENT,
method: configData.method,
}, },
]) ])
) )

View File

@ -6,6 +6,7 @@ import { generateToken } from "../utils"
import path from "path" import path from "path"
import { KEYCLOAK_IMAGE } from "../../../../integrations/tests/utils/images" import { KEYCLOAK_IMAGE } from "../../../../integrations/tests/utils/images"
import { startContainer } from "../../../../integrations/tests/utils" import { startContainer } from "../../../../integrations/tests/utils"
import { OAuth2CredentialsMethod } from "@budibase/types"
const config = new TestConfiguration() const config = new TestConfiguration()
@ -41,70 +42,77 @@ describe("oauth2 utils", () => {
keycloakUrl = `http://127.0.0.1:${port}` keycloakUrl = `http://127.0.0.1:${port}`
}) })
describe("generateToken", () => { describe.each(Object.values(OAuth2CredentialsMethod))(
it("successfully generates tokens", async () => { "generateToken (in %s)",
const response = await config.doInContext(config.appId, async () => { method => {
const oauthConfig = await sdk.oauth2.create({ it("successfully generates tokens", async () => {
name: generator.guid(), const response = await config.doInContext(config.appId, async () => {
url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`, const oauthConfig = await sdk.oauth2.create({
clientId: "my-client", name: generator.guid(),
clientSecret: "my-secret", url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
clientId: "my-client",
clientSecret: "my-secret",
method,
})
const response = await generateToken(oauthConfig.id)
return response
}) })
const response = await generateToken(oauthConfig.id) expect(response).toEqual(expect.stringMatching(/^Bearer .+/))
return response
}) })
expect(response).toEqual(expect.stringMatching(/^Bearer .+/)) it("handles wrong urls", async () => {
}) await expect(
config.doInContext(config.appId, async () => {
const oauthConfig = await sdk.oauth2.create({
name: generator.guid(),
url: `${keycloakUrl}/realms/wrong/protocol/openid-connect/token`,
clientId: "my-client",
clientSecret: "my-secret",
method,
})
it("handles wrong urls", async () => { await generateToken(oauthConfig.id)
await expect(
config.doInContext(config.appId, async () => {
const oauthConfig = await sdk.oauth2.create({
name: generator.guid(),
url: `${keycloakUrl}/realms/wrong/protocol/openid-connect/token`,
clientId: "my-client",
clientSecret: "my-secret",
}) })
).rejects.toThrow("Error fetching oauth2 token: Not Found")
})
await generateToken(oauthConfig.id) it("handles wrong client ids", async () => {
}) await expect(
).rejects.toThrow("Error fetching oauth2 token: Not Found") config.doInContext(config.appId, async () => {
}) const oauthConfig = await sdk.oauth2.create({
name: generator.guid(),
url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
clientId: "wrong-client-id",
clientSecret: "my-secret",
method,
})
it("handles wrong client ids", async () => { await generateToken(oauthConfig.id)
await expect(
config.doInContext(config.appId, async () => {
const oauthConfig = await sdk.oauth2.create({
name: generator.guid(),
url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
clientId: "wrong-client-id",
clientSecret: "my-secret",
}) })
).rejects.toThrow(
"Error fetching oauth2 token: Invalid client or Invalid client credentials"
)
})
await generateToken(oauthConfig.id) it("handles wrong secrets", async () => {
}) await expect(
).rejects.toThrow( config.doInContext(config.appId, async () => {
"Error fetching oauth2 token: Invalid client or Invalid client credentials" const oauthConfig = await sdk.oauth2.create({
) name: generator.guid(),
}) url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
clientId: "my-client",
clientSecret: "wrong-secret",
method,
})
it("handles wrong secrets", async () => { await generateToken(oauthConfig.id)
await expect(
config.doInContext(config.appId, async () => {
const oauthConfig = await sdk.oauth2.create({
name: generator.guid(),
url: `${keycloakUrl}/realms/myrealm/protocol/openid-connect/token`,
clientId: "my-client",
clientSecret: "wrong-secret",
}) })
).rejects.toThrow(
await generateToken(oauthConfig.id) "Error fetching oauth2 token: Invalid client or Invalid client credentials"
}) )
).rejects.toThrow( })
"Error fetching oauth2 token: Invalid client or Invalid client credentials" }
) )
})
})
}) })