Stripping credentials from Couch URLs + test cases.
This commit is contained in:
parent
d9e36e92d2
commit
cb50cca40b
|
@ -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,
|
||||
|
|
|
@ -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")
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue