| 1 |
package pg |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/url" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
) |
| 9 |
|
| 10 |
func TestOpen_InvalidScheme(t *testing.T) { |
| 11 |
_, err := Open("postgresql+psycopg2://user:pass@localhost/db") |
| 12 |
require.Error(t, err) |
| 13 |
require.Contains(t, err.Error(), `invalid database URL scheme "postgresql+psycopg2"`) |
| 14 |
require.Contains(t, err.Error(), "*****") |
| 15 |
require.NotContains(t, err.Error(), "pass") |
| 16 |
} |
| 17 |
|
| 18 |
func TestOpen_InvalidURL(t *testing.T) { |
| 19 |
_, err := Open("not a valid url\x00") |
| 20 |
require.Error(t, err) |
| 21 |
require.Contains(t, err.Error(), "invalid database URL") |
| 22 |
} |
| 23 |
|
| 24 |
func TestCensorPassword(t *testing.T) { |
| 25 |
tests := []struct { |
| 26 |
name string |
| 27 |
url string |
| 28 |
expected string |
| 29 |
}{ |
| 30 |
{ |
| 31 |
name: "with password", |
| 32 |
url: "postgres://user:secret@localhost/db", |
| 33 |
expected: "postgres://user:*****@localhost/db", |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "without password", |
| 37 |
url: "postgres://localhost/db", |
| 38 |
expected: "postgres://localhost/db", |
| 39 |
}, |
| 40 |
{ |
| 41 |
name: "user only", |
| 42 |
url: "postgres://user@localhost/db", |
| 43 |
expected: "postgres://user@localhost/db", |
| 44 |
}, |
| 45 |
} |
| 46 |
for _, tt := range tests { |
| 47 |
t.Run(tt.name, func(t *testing.T) { |
| 48 |
u, err := url.Parse(tt.url) |
| 49 |
require.NoError(t, err) |
| 50 |
require.Equal(t, tt.expected, censorPassword(u)) |
| 51 |
}) |
| 52 |
} |
| 53 |
} |