Skip to content

Commit

Permalink
Fix tests to use connection string instead of env
Browse files Browse the repository at this point in the history
  • Loading branch information
adrinr committed May 11, 2023
1 parent 1deb4e7 commit 4c58f28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
16 changes: 8 additions & 8 deletions packages/backend-core/src/db/couch/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ export const getCouchInfo = (connection?: string) => {
const urlInfo = getUrlInfo(connection)
let username
let password
if (env.COUCH_DB_USERNAME) {
// set from env
username = env.COUCH_DB_USERNAME
} else if (urlInfo.auth.username) {
if (urlInfo.auth?.username) {
// set from url
username = urlInfo.auth.username
} else if (env.COUCH_DB_USERNAME) {
// set from env
username = env.COUCH_DB_USERNAME
} else if (!env.isTest()) {
throw new Error("CouchDB username not set")
}
if (env.COUCH_DB_PASSWORD) {
// set from env
password = env.COUCH_DB_PASSWORD
} else if (urlInfo.auth.password) {
if (urlInfo.auth?.password) {
// set from url
password = urlInfo.auth.password
} else if (env.COUCH_DB_PASSWORD) {
// set from env
password = env.COUCH_DB_PASSWORD
} else if (!env.isTest()) {
throw new Error("CouchDB password not set")
}
Expand Down
36 changes: 31 additions & 5 deletions packages/server/src/sdk/tests/datasources/validators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SourceName } from "@budibase/types"
import integrations from "../../../integrations"
import { GenericContainer } from "testcontainers"
import { env } from "@budibase/backend-core"
import { generator } from "@budibase/backend-core/tests"

jest.unmock("pg")
jest.unmock("mysql2/promise")
Expand Down Expand Up @@ -118,18 +118,44 @@ describe("datasource validators", () => {
describe("couchdb", () => {
const validator = integrations.getValidator[SourceName.COUCHDB]!

let url: string

beforeAll(async () => {
const user = generator.first()
const password = generator.hash()

const container = await new GenericContainer("budibase/couchdb")
.withExposedPorts(5984)
.withEnv("COUCHDB_USER", user)
.withEnv("COUCHDB_PASSWORD", password)
.start()

const host = container.getContainerIpAddress()
const port = container.getMappedPort(5984)

await container.exec([
`curl`,
`-u`,
`${user}:${password}`,
`-X`,
`PUT`,
`localhost:5984/db`,
])
url = `http://${user}:${password}@${host}:${port}`
})

it("test valid connection string", async () => {
const result = await validator({
url: env.COUCH_DB_URL,
database: "",
url,
database: "db",
})
expect(result).toBe(true)
})

it("test invalid database", async () => {
const result = await validator({
url: env.COUCH_DB_URL,
database: "db",
url,
database: "random_db",
})
expect(result).toBe(false)
})
Expand Down

0 comments on commit 4c58f28

Please sign in to comment.