2015-04-30 19:47:07 +03:00
|
|
|
package bookmarks
|
2015-03-04 05:44:14 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-11-10 09:21:49 +03:00
|
|
|
"github.com/sosedoff/pgweb/pkg/shared"
|
2015-03-04 05:44:14 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Invalid_Bookmark_Files(t *testing.T) {
|
2015-03-04 17:58:53 +03:00
|
|
|
_, err := readServerConfig("foobar")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2015-04-30 20:09:29 +03:00
|
|
|
_, err = readServerConfig("../../data/invalid.toml")
|
2015-03-04 17:58:53 +03:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "Near line 1, key 'invalid encoding': Near line 2: Expected key separator '=', but got '\\n' instead.", err.Error())
|
2015-03-04 05:44:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Bookmark(t *testing.T) {
|
2015-04-30 20:09:29 +03:00
|
|
|
bookmark, err := readServerConfig("../../data/bookmark.toml")
|
2015-03-04 05:44:14 +03:00
|
|
|
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, "localhost", bookmark.Host)
|
2016-11-10 09:22:12 +03:00
|
|
|
assert.Equal(t, 5432, bookmark.Port)
|
2015-03-04 05:44:14 +03:00
|
|
|
assert.Equal(t, "postgres", bookmark.User)
|
|
|
|
assert.Equal(t, "mydatabase", bookmark.Database)
|
|
|
|
assert.Equal(t, "disable", bookmark.Ssl)
|
|
|
|
assert.Equal(t, "", bookmark.Password)
|
|
|
|
assert.Equal(t, "", bookmark.Url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Bookmark_URL(t *testing.T) {
|
2015-04-30 20:09:29 +03:00
|
|
|
bookmark, err := readServerConfig("../../data/bookmark_url.toml")
|
2015-03-04 05:44:14 +03:00
|
|
|
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, "postgres://username:password@host:port/database?sslmode=disable", bookmark.Url)
|
|
|
|
assert.Equal(t, "", bookmark.Host)
|
2016-11-10 09:22:12 +03:00
|
|
|
assert.Equal(t, 0, bookmark.Port)
|
2015-03-04 05:44:14 +03:00
|
|
|
assert.Equal(t, "", bookmark.User)
|
|
|
|
assert.Equal(t, "", bookmark.Database)
|
|
|
|
assert.Equal(t, "", bookmark.Ssl)
|
|
|
|
assert.Equal(t, "", bookmark.Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Bookmarks_Path(t *testing.T) {
|
2015-04-30 20:09:29 +03:00
|
|
|
assert.NotEqual(t, "/.pgweb/bookmarks", Path())
|
2015-03-04 05:44:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Basename(t *testing.T) {
|
|
|
|
assert.Equal(t, "filename", fileBasename("filename.toml"))
|
|
|
|
assert.Equal(t, "filename", fileBasename("path/filename.toml"))
|
|
|
|
assert.Equal(t, "filename", fileBasename("~/long/path/filename.toml"))
|
|
|
|
assert.Equal(t, "filename", fileBasename("filename"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ReadBookmarks_Invalid(t *testing.T) {
|
2015-04-30 20:09:29 +03:00
|
|
|
bookmarks, err := ReadAll("foobar")
|
2015-03-04 05:44:14 +03:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, 0, len(bookmarks))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ReadBookmarks(t *testing.T) {
|
2015-04-30 20:09:29 +03:00
|
|
|
bookmarks, err := ReadAll("../../data")
|
2015-03-04 05:44:14 +03:00
|
|
|
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, 2, len(bookmarks))
|
|
|
|
}
|
2016-11-10 09:21:49 +03:00
|
|
|
|
2016-11-10 09:22:07 +03:00
|
|
|
func Test_GetBookmark(t *testing.T) {
|
|
|
|
expBookmark := Bookmark{
|
|
|
|
|
|
|
|
Host: "localhost",
|
2016-11-10 09:22:12 +03:00
|
|
|
Port: 5432,
|
2016-11-10 09:22:07 +03:00
|
|
|
User: "postgres",
|
|
|
|
Password: "",
|
|
|
|
Database: "mydatabase",
|
|
|
|
Ssl: "disable",
|
|
|
|
}
|
|
|
|
b, err := GetBookmark("../../data", "bookmark")
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, expBookmark, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = GetBookmark("../../data", "bar")
|
|
|
|
assert.Equal(t, ErrNonExistingBookmark("bar"), err)
|
|
|
|
|
|
|
|
_, err = GetBookmark("foo", "bookmark")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:21:49 +03:00
|
|
|
func Test_Bookmark_SSHInfoIsEmpty(t *testing.T) {
|
|
|
|
emptySSH := shared.SSHInfo{
|
|
|
|
Host: "",
|
|
|
|
Port: "",
|
|
|
|
User: "",
|
|
|
|
}
|
|
|
|
populatedSSH := shared.SSHInfo{
|
|
|
|
Host: "localhost",
|
|
|
|
Port: "8080",
|
|
|
|
User: "postgres",
|
|
|
|
}
|
|
|
|
|
|
|
|
b := Bookmark{Ssh: emptySSH}
|
|
|
|
assert.True(t, b.SSHInfoIsEmpty())
|
|
|
|
|
|
|
|
b.Ssh = populatedSSH
|
|
|
|
assert.False(t, b.SSHInfoIsEmpty())
|
|
|
|
}
|