pgweb/pkg/connection/connection_string_test.go

175 lines
4.8 KiB
Go
Raw Normal View History

2015-04-30 11:47:07 -05:00
package connection
2014-12-17 21:32:50 -06:00
import (
"fmt"
2018-09-13 23:22:25 -05:00
"net/url"
2014-12-17 21:32:50 -06:00
"os/user"
"testing"
2015-04-30 12:09:29 -05:00
"github.com/sosedoff/pgweb/pkg/command"
2014-12-17 21:32:50 -06:00
"github.com/stretchr/testify/assert"
)
func Test_Invalid_Url(t *testing.T) {
2015-04-30 12:09:29 -05:00
opts := command.Options{}
2014-12-17 21:32:50 -06:00
examples := []string{
"postgre://foobar",
2018-09-13 22:25:15 -05:00
"tcp://blah",
2014-12-17 21:32:50 -06:00
"foobar",
}
for _, val := range examples {
2019-11-02 13:00:23 -05:00
opts.URL = val
str, err := BuildStringFromOptions(opts)
2014-12-17 21:32:50 -06:00
assert.Equal(t, "", str)
assert.Error(t, err)
assert.Equal(t, "Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode", err.Error())
}
}
func Test_Valid_Url(t *testing.T) {
url := "postgres://myhost/database"
2019-11-02 13:00:23 -05:00
str, err := BuildStringFromOptions(command.Options{URL: url})
2014-12-17 21:32:50 -06:00
assert.Equal(t, nil, err)
assert.Equal(t, url, str)
}
func Test_Url_And_Ssl_Flag(t *testing.T) {
str, err := BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://myhost/database",
2014-12-17 21:32:50 -06:00
Ssl: "disable",
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://myhost/database?sslmode=disable", str)
}
func Test_Localhost_Url_And_No_Ssl_Flag(t *testing.T) {
str, err := BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://localhost/database",
2014-12-17 21:32:50 -06:00
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://localhost/database?sslmode=disable", str)
str, err = BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://127.0.0.1/database",
2014-12-17 21:32:50 -06:00
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://127.0.0.1/database?sslmode=disable", str)
}
func Test_Localhost_Url_And_Ssl_Flag(t *testing.T) {
str, err := BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://localhost/database",
2014-12-17 21:32:50 -06:00
Ssl: "require",
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://localhost/database?sslmode=require", str)
str, err = BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://127.0.0.1/database",
2014-12-17 21:32:50 -06:00
Ssl: "require",
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://127.0.0.1/database?sslmode=require", str)
}
func Test_Localhost_Url_And_Ssl_Arg(t *testing.T) {
str, err := BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://localhost/database?sslmode=require",
2014-12-17 21:32:50 -06:00
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://localhost/database?sslmode=require", str)
str, err = BuildStringFromOptions(command.Options{
2019-11-02 13:00:23 -05:00
URL: "postgres://127.0.0.1/database?sslmode=require",
2014-12-17 21:32:50 -06:00
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://127.0.0.1/database?sslmode=require", str)
}
2020-02-11 10:58:35 -06:00
func Test_ExtendedSSLFlags(t *testing.T) {
str, err := BuildStringFromOptions(command.Options{
URL: "postgres://localhost/database?sslmode=require&sslcert=cert&sslkey=key&sslrootcert=ca",
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://localhost/database?sslcert=cert&sslkey=key&sslmode=require&sslrootcert=ca", str)
}
2014-12-17 21:32:50 -06:00
func Test_Flag_Args(t *testing.T) {
str, err := BuildStringFromOptions(command.Options{
2014-12-17 21:32:50 -06:00
Host: "host",
Port: 5432,
User: "user",
Pass: "password",
DbName: "db",
})
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://user:password@host:5432/db", str)
}
func Test_Localhost(t *testing.T) {
2015-04-30 12:09:29 -05:00
opts := command.Options{
2014-12-17 21:32:50 -06:00
Host: "localhost",
Port: 5432,
User: "user",
Pass: "password",
DbName: "db",
}
str, err := BuildStringFromOptions(opts)
2014-12-17 21:32:50 -06:00
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=disable", str)
opts.Host = "127.0.0.1"
str, err = BuildStringFromOptions(opts)
2014-12-17 21:32:50 -06:00
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://user:password@127.0.0.1:5432/db?sslmode=disable", str)
}
func Test_Localhost_And_Ssl(t *testing.T) {
2015-04-30 12:09:29 -05:00
opts := command.Options{
2020-02-11 10:58:35 -06:00
Host: "localhost",
Port: 5432,
User: "user",
Pass: "password",
DbName: "db",
Ssl: "require",
SslKey: "keyPath",
SslCert: "certPath",
SslRootCert: "caPath",
2014-12-17 21:32:50 -06:00
}
str, err := BuildStringFromOptions(opts)
2014-12-17 21:32:50 -06:00
assert.Equal(t, nil, err)
2020-02-11 10:58:35 -06:00
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslcert=certPath&sslkey=keyPath&sslmode=require&sslrootcert=caPath", str)
2014-12-17 21:32:50 -06:00
}
func Test_No_User(t *testing.T) {
2015-04-30 12:09:29 -05:00
opts := command.Options{Host: "host", Port: 5432, DbName: "db"}
2014-12-17 21:32:50 -06:00
u, _ := user.Current()
str, err := BuildStringFromOptions(opts)
2018-09-13 23:22:25 -05:00
userAndPass := url.UserPassword(u.Username, "").String()
2014-12-17 21:32:50 -06:00
assert.Equal(t, nil, err)
2018-09-13 23:22:25 -05:00
assert.Equal(t, fmt.Sprintf("postgres://%s@host:5432/db", userAndPass), str)
2014-12-17 21:32:50 -06:00
}
func Test_Port(t *testing.T) {
2015-04-30 12:09:29 -05:00
opts := command.Options{Host: "host", User: "user", Port: 5000, DbName: "db"}
str, err := BuildStringFromOptions(opts)
2014-12-17 21:32:50 -06:00
assert.Equal(t, nil, err)
assert.Equal(t, "postgres://user:@host:5000/db", str)
2014-12-17 21:32:50 -06:00
}
2014-12-17 21:56:15 -06:00
func Test_Blank(t *testing.T) {
2015-04-30 12:09:29 -05:00
assert.Equal(t, true, IsBlank(command.Options{}))
assert.Equal(t, false, IsBlank(command.Options{Host: "host", User: "user"}))
assert.Equal(t, false, IsBlank(command.Options{Host: "host", User: "user", DbName: "db"}))
2019-11-02 13:00:23 -05:00
assert.Equal(t, false, IsBlank(command.Options{URL: "url"}))
2014-12-17 21:56:15 -06:00
}