Stripping credentials from Couch URLs + test cases.

This commit is contained in:
mike12345567 2024-07-03 11:25:21 +01:00
parent d9e36e92d2
commit cb50cca40b
2 changed files with 31 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import env from "../../environment"
export const getCouchInfo = (connection?: string) => {
// clean out any auth credentials
const urlInfo = getUrlInfo(connection)
let username
let password
@ -23,9 +24,16 @@ export const getCouchInfo = (connection?: string) => {
throw new Error("CouchDB password not set")
}
const authCookie = Buffer.from(`${username}:${password}`).toString("base64")
let sqlUrl = env.COUCH_DB_SQL_URL
if (!sqlUrl && urlInfo.url) {
const parsed = new URL(urlInfo.url)
// attempt to connect on default port
sqlUrl = urlInfo.url.replace(parsed.port, "4984")
}
return {
url: urlInfo.url!,
sqlUrl: env.COUCH_DB_SQL_URL,
// clean out any auth credentials
sqlUrl: getUrlInfo(sqlUrl).url,
auth: {
username: username,
password: password,

View File

@ -0,0 +1,22 @@
import env from "../../environment"
import { getCouchInfo } from "../couch"
const MAIN_COUCH_URL = "http://user:test@localhost:5984"
describe("connections", () => {
beforeAll(() => {
env._set("COUCH_DB_SQL_URL", "https://user:test@localhost:4984")
})
it("should strip URL credentials", () => {
const response = getCouchInfo(MAIN_COUCH_URL)
expect(response.url).toBe("http://localhost:5984")
expect(response.sqlUrl).toBe("https://localhost:4984")
})
it("should return separate auth credentials", () => {
const response = getCouchInfo(MAIN_COUCH_URL)
expect(response.auth.username).toBe("user")
expect(response.auth.password).toBe("test")
})
})