2023-11-23 11:08:03 +01:00
import { buildIntegerMatcher , buildStringMatcher , getDefaultConfig , overrideWithEnv } from '../src/config/config'
2020-12-08 11:42:51 +01:00
2022-03-10 12:18:24 +01:00
describe ( 'config' , ( ) = > {
test ( 'overrideWithEnv 1' , ( ) = > {
const defaultConfig = getDefaultConfig ( )
const env = {
2022-05-19 19:18:15 +02:00
CLICKHOUSE_SECURE : 'false' ,
2022-03-10 12:18:24 +01:00
TASK_TIMEOUT : '3008' ,
CLICKHOUSE_HOST : '0.0.0.0' ,
BASE_DIR : undefined ,
}
const config = overrideWithEnv ( getDefaultConfig ( ) , env )
2020-12-08 11:42:51 +01:00
2022-05-19 19:18:15 +02:00
expect ( config . CLICKHOUSE_SECURE ) . toEqual ( false )
2022-03-10 12:18:24 +01:00
expect ( config . TASK_TIMEOUT ) . toEqual ( 3008 )
expect ( config . CLICKHOUSE_HOST ) . toEqual ( '0.0.0.0' )
expect ( config . BASE_DIR ) . toEqual ( defaultConfig . BASE_DIR )
} )
2020-12-08 11:42:51 +01:00
2022-03-10 12:18:24 +01:00
test ( 'overrideWithEnv 2' , ( ) = > {
const env = {
2022-05-19 19:18:15 +02:00
CLICKHOUSE_SECURE : '1' ,
2022-03-10 12:18:24 +01:00
TASK_TIMEOUT : '3008.12' ,
}
const config = overrideWithEnv ( getDefaultConfig ( ) , env )
2020-12-08 11:42:51 +01:00
2022-05-19 19:18:15 +02:00
expect ( config . CLICKHOUSE_SECURE ) . toEqual ( true )
2022-03-10 12:18:24 +01:00
expect ( config . TASK_TIMEOUT ) . toEqual ( 3008.12 )
} )
2022-10-19 16:38:40 +02:00
describe ( 'DATABASE_URL' , ( ) = > {
test ( 'Error if DATABASE_URL is not set AND POSTHOG_DB_NAME is not set' , ( ) = > {
const env = {
DATABASE_URL : '' ,
POSTHOG_DB_NAME : '' ,
}
expect ( ( ) = > overrideWithEnv ( getDefaultConfig ( ) , env ) ) . toThrowError (
'You must specify either DATABASE_URL or the database options POSTHOG_DB_NAME, POSTHOG_DB_USER, POSTHOG_DB_PASSWORD, POSTHOG_POSTGRES_HOST, POSTHOG_POSTGRES_PORT!'
)
} )
2023-02-15 12:02:37 +01:00
test ( 'Set DATABASE_URL to a string composed of URL-encoded connection options if DATABASE_URL is not explictly set' , ( ) = > {
2022-10-19 16:38:40 +02:00
const env = {
DATABASE_URL : '' ,
POSTHOG_DB_NAME : 'mydb' ,
2023-02-15 12:02:37 +01:00
POSTHOG_DB_USER : 'user1@domain' ,
POSTHOG_DB_PASSWORD : 'strong?password' ,
2022-10-19 16:38:40 +02:00
POSTHOG_POSTGRES_HOST : 'my.host' ,
}
const config = overrideWithEnv ( getDefaultConfig ( ) , env )
2023-02-15 12:02:37 +01:00
expect ( config . DATABASE_URL ) . toEqual ( 'postgres://user1%40domain:strong%3Fpassword@my.host:5432/mydb' )
2022-10-19 16:38:40 +02:00
} )
test ( 'DATABASE_URL takes precedence to individual config options' , ( ) = > {
const env = {
DATABASE_URL : 'my_db_url' ,
POSTHOG_DB_NAME : 'mydb' ,
POSTHOG_DB_USER : 'user1' ,
POSTHOG_DB_PASSWORD : 'strongpassword' ,
POSTHOG_POSTGRES_HOST : 'my.host' ,
}
const config = overrideWithEnv ( getDefaultConfig ( ) , env )
expect ( config . DATABASE_URL ) . toEqual ( 'my_db_url' )
} )
} )
2020-12-08 11:42:51 +01:00
} )
2023-09-04 13:40:15 +02:00
describe ( 'buildIntegerMatcher' , ( ) = > {
test ( 'empty input' , ( ) = > {
const matcher = buildIntegerMatcher ( '' , false )
expect ( matcher ( 2 ) ) . toBe ( false )
} )
test ( 'ignores star star when not allowed' , ( ) = > {
const matcher = buildIntegerMatcher ( '*' , false )
expect ( matcher ( 2 ) ) . toBe ( false )
} )
test ( 'matches star when allowed' , ( ) = > {
const matcher = buildIntegerMatcher ( '*' , true )
expect ( matcher ( 2 ) ) . toBe ( true )
} )
test ( 'can match on a single value' , ( ) = > {
const matcher = buildIntegerMatcher ( '2' , true )
expect ( matcher ( 2 ) ) . toBe ( true )
expect ( matcher ( 3 ) ) . toBe ( false )
} )
test ( 'can match on several values' , ( ) = > {
const matcher = buildIntegerMatcher ( '2,3,4' , true )
expect ( matcher ( 2 ) ) . toBe ( true )
expect ( matcher ( 3 ) ) . toBe ( true )
expect ( matcher ( 4 ) ) . toBe ( true )
expect ( matcher ( 5 ) ) . toBe ( false )
} )
} )
2023-11-23 11:08:03 +01:00
describe ( 'buildStringMatcher' , ( ) = > {
test ( 'empty input' , ( ) = > {
const matcher = buildStringMatcher ( '' , false )
expect ( matcher ( 'b' ) ) . toBe ( false )
} )
test ( 'ignores star star when not allowed' , ( ) = > {
const matcher = buildStringMatcher ( '*' , false )
expect ( matcher ( 'b' ) ) . toBe ( false )
} )
test ( 'matches star when allowed' , ( ) = > {
const matcher = buildStringMatcher ( '*' , true )
expect ( matcher ( 'b' ) ) . toBe ( true )
} )
test ( 'can match on a single value' , ( ) = > {
const matcher = buildStringMatcher ( 'b' , true )
expect ( matcher ( 'b' ) ) . toBe ( true )
expect ( matcher ( 'a' ) ) . toBe ( false )
} )
test ( 'can match on several values' , ( ) = > {
const matcher = buildStringMatcher ( 'b,c,d' , true )
expect ( matcher ( 'b' ) ) . toBe ( true )
expect ( matcher ( 'c' ) ) . toBe ( true )
expect ( matcher ( 'd' ) ) . toBe ( true )
expect ( matcher ( 'e' ) ) . toBe ( false )
} )
} )