github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/backends/db_provider/postgres_dbprovider_test.go (about) 1 package db_provider 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/prebid/prebid-server/v2/config" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestConnStringPostgres(t *testing.T) { 12 13 type TLS struct { 14 RootCert string 15 ClientCert string 16 ClientKey string 17 } 18 19 type Params struct { 20 db string 21 host string 22 port int 23 username string 24 password string 25 QueryString string 26 TLS TLS 27 } 28 29 tests := []struct { 30 name string 31 params Params 32 connString string 33 expectedError error 34 }{ 35 { 36 params: Params{ 37 db: "", 38 }, 39 connString: "postgresql://?sslmode=disable", 40 }, 41 { 42 params: Params{ 43 db: "TestDB", 44 }, 45 connString: "postgresql:///TestDB?sslmode=disable", 46 }, 47 { 48 params: Params{ 49 host: "example.com", 50 }, 51 connString: "postgresql://example.com?sslmode=disable", 52 }, 53 { 54 params: Params{ 55 port: 20, 56 }, 57 connString: "postgresql://:20?sslmode=disable", 58 }, 59 { 60 params: Params{ 61 username: "someuser", 62 }, 63 connString: "postgresql://someuser@?sslmode=disable", 64 }, 65 { 66 params: Params{ 67 username: "someuser", 68 password: "somepassword", 69 }, 70 connString: "postgresql://someuser:somepassword@?sslmode=disable", 71 }, 72 { 73 params: Params{ 74 username: "someuser", 75 password: "somepassword:/?#[]@!$&()*+,;=", 76 }, 77 connString: "postgresql://someuser:somepassword%3A%2F%3F%23%5B%5D%40%21%24%26%28%29%2A%2B%2C%3B%3D@?sslmode=disable", 78 }, 79 { 80 params: Params{ 81 db: "TestDB", 82 host: "example.com", 83 port: 20, 84 username: "someuser", 85 password: "somepassword", 86 }, 87 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslmode=disable", 88 }, 89 { 90 params: Params{ 91 db: "TestDB", 92 host: "example.com", 93 port: 20, 94 username: "someuser", 95 password: "somepassword", 96 QueryString: "param=value", 97 }, 98 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslmode=disable¶m=value", 99 }, 100 { 101 params: Params{ 102 db: "TestDB", 103 host: "example.com", 104 port: 20, 105 username: "someuser", 106 password: "somepassword", 107 QueryString: "param=value&sslmode=require", 108 }, 109 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?param=value&sslmode=require", 110 }, 111 { 112 params: Params{ 113 db: "TestDB", 114 host: "example.com", 115 port: 20, 116 username: "someuser", 117 password: "somepassword", 118 TLS: TLS{ 119 RootCert: "root-cert.pem", 120 }, 121 }, 122 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslmode=verify-ca&sslrootcert=root-cert.pem", 123 }, 124 { 125 params: Params{ 126 db: "TestDB", 127 host: "example.com", 128 port: 20, 129 username: "someuser", 130 password: "somepassword", 131 TLS: TLS{ 132 RootCert: "root-cert.pem", 133 ClientCert: "client-cert.pem", 134 ClientKey: "client-key.pem", 135 }, 136 }, 137 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslmode=verify-full&sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem", 138 }, 139 { 140 params: Params{ 141 db: "TestDB", 142 host: "example.com", 143 port: 20, 144 username: "someuser", 145 password: "somepassword", 146 QueryString: "param=value", 147 TLS: TLS{ 148 RootCert: "root-cert.pem", 149 ClientCert: "client-cert.pem", 150 ClientKey: "client-key.pem", 151 }, 152 }, 153 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslmode=verify-full&sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem¶m=value", 154 }, 155 { 156 params: Params{ 157 db: "TestDB", 158 host: "example.com", 159 port: 20, 160 username: "someuser", 161 password: "somepassword", 162 QueryString: "sslmode=prefer", 163 TLS: TLS{ 164 RootCert: "root-cert.pem", 165 ClientCert: "client-cert.pem", 166 ClientKey: "client-key.pem", 167 }, 168 }, 169 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem&sslmode=prefer", 170 }, 171 { 172 params: Params{ 173 db: "TestDB", 174 host: "example.com", 175 port: 20, 176 username: "someuser", 177 password: "somepassword", 178 QueryString: "param=value&sslmode=prefer", 179 TLS: TLS{ 180 RootCert: "root-cert.pem", 181 ClientCert: "client-cert.pem", 182 ClientKey: "client-key.pem", 183 }, 184 }, 185 connString: "postgresql://someuser:somepassword@example.com:20/TestDB?sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem¶m=value&sslmode=prefer", 186 }, 187 { 188 params: Params{ 189 QueryString: "sslrootcert=root-cert.pem", 190 TLS: TLS{ 191 RootCert: "root-cert.pem", 192 ClientCert: "client-cert.pem", 193 ClientKey: "client-key.pem", 194 }, 195 }, 196 connString: "", 197 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 198 }, 199 { 200 params: Params{ 201 QueryString: "sslcert=client-cert.pem", 202 TLS: TLS{ 203 RootCert: "root-cert.pem", 204 ClientCert: "client-cert.pem", 205 ClientKey: "client-key.pem", 206 }, 207 }, 208 connString: "", 209 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 210 }, 211 { 212 params: Params{ 213 QueryString: "sslkey=client-key.pem", 214 TLS: TLS{ 215 RootCert: "root-cert.pem", 216 ClientCert: "client-cert.pem", 217 ClientKey: "client-key.pem", 218 }, 219 }, 220 connString: "", 221 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 222 }, 223 { 224 params: Params{ 225 QueryString: "sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem", 226 TLS: TLS{ 227 RootCert: "root-cert.pem", 228 }, 229 }, 230 connString: "", 231 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 232 }, 233 { 234 params: Params{ 235 QueryString: "sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem", 236 TLS: TLS{ 237 ClientCert: "client-cert.pem", 238 }, 239 }, 240 connString: "", 241 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 242 }, 243 { 244 params: Params{ 245 QueryString: "sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem", 246 TLS: TLS{ 247 ClientKey: "client-key.pem", 248 }, 249 }, 250 connString: "", 251 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 252 }, 253 { 254 params: Params{ 255 QueryString: "sslrootcert=root-cert.pem&sslcert=client-cert.pem&sslkey=client-key.pem", 256 TLS: TLS{ 257 RootCert: "root-cert.pem", 258 ClientCert: "client-cert.pem", 259 ClientKey: "client-key.pem", 260 }, 261 }, 262 connString: "", 263 expectedError: errors.New("TLS cert information must either be specified in the TLS object or the query string but not both."), 264 }, 265 } 266 267 for _, test := range tests { 268 cfg := config.DatabaseConnection{ 269 Database: test.params.db, 270 Host: test.params.host, 271 Port: test.params.port, 272 Username: test.params.username, 273 Password: test.params.password, 274 QueryString: test.params.QueryString, 275 TLS: config.TLS(test.params.TLS), 276 } 277 278 provider := PostgresDbProvider{ 279 cfg: cfg, 280 } 281 282 connString, err := provider.ConnString() 283 assert.Equal(t, test.connString, connString, "Strings did not match") 284 assert.Equal(t, test.expectedError, err) 285 } 286 }