diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/store/store.go | 4 | ||||
-rw-r--r-- | server/store/store_test.go | 31 | ||||
-rw-r--r-- | server/util/util.go | 10 | ||||
-rw-r--r-- | server/util/util_test.go | 16 |
4 files changed, 21 insertions, 40 deletions
diff --git a/server/store/store.go b/server/store/store.go index a447e72..8af77e3 100644 --- a/server/store/store.go +++ b/server/store/store.go @@ -5,8 +5,8 @@ import ( "golang.org/x/crypto/ssh" + "github.com/nsheridan/cashier/lib" "github.com/nsheridan/cashier/server/config" - "github.com/nsheridan/cashier/server/util" ) // New returns a new configured database. @@ -54,6 +54,6 @@ func parseCertificate(cert *ssh.Certificate) *CertRecord { Principals: cert.ValidPrincipals, CreatedAt: parseTime(cert.ValidAfter), Expires: parseTime(cert.ValidBefore), - Raw: util.GetPublicKey(cert), + Raw: lib.GetPublicKey(cert), } } diff --git a/server/store/store_test.go b/server/store/store_test.go index dbe2d95..281a614 100644 --- a/server/store/store_test.go +++ b/server/store/store_test.go @@ -3,9 +3,11 @@ package store import ( "crypto/rand" "crypto/rsa" + "database/sql" "io/ioutil" "os" - "os/exec" + "os/user" + "strings" "testing" "time" @@ -15,10 +17,6 @@ import ( "golang.org/x/crypto/ssh" ) -var ( - dbConfig = map[string]string{"username": "user", "password": "passwd", "address": "localhost"} -) - func TestParseCertificate(t *testing.T) { t.Parallel() a := assert.New(t) @@ -93,8 +91,8 @@ func TestMySQLStore(t *testing.T) { if os.Getenv("MYSQL_TEST") == "" { t.Skip("No MYSQL_TEST environment variable") } - dbConfig["type"] = "mysql" - db, err := NewSQLStore(dbConfig) + u, _ := user.Current() + db, err := NewSQLStore(map[string]string{"type": "mysql", "username": u.Username}) if err != nil { t.Error(err) } @@ -106,8 +104,7 @@ func TestMongoStore(t *testing.T) { if os.Getenv("MONGO_TEST") == "" { t.Skip("No MONGO_TEST environment variable") } - dbConfig["type"] = "mongo" - db, err := NewMongoStore(dbConfig) + db, err := NewMongoStore(map[string]string{"type": "mongo"}) if err != nil { t.Error(err) } @@ -121,11 +118,21 @@ func TestSQLiteStore(t *testing.T) { t.Error(err) } defer os.Remove(f.Name()) - // This is so jank. - args := []string{"run", "../../cmd/dbinit/dbinit.go", "-db_type", "sqlite", "-db_path", f.Name()} - if err := exec.Command("go", args...).Run(); err != nil { + + seed, err := ioutil.ReadFile("../../db/seed.sql") + if err != nil { t.Error(err) } + stmts := strings.Split(string(seed), ";") + d, _ := sql.Open("sqlite3", f.Name()) + for _, stmt := range stmts { + if !strings.Contains(stmt, "CREATE TABLE") { + continue + } + d.Exec(stmt) + } + d.Close() + config := map[string]string{"type": "sqlite", "filename": f.Name()} db, err := NewSQLStore(config) if err != nil { diff --git a/server/util/util.go b/server/util/util.go deleted file mode 100644 index 10f5eca..0000000 --- a/server/util/util.go +++ /dev/null @@ -1,10 +0,0 @@ -package util - -import "golang.org/x/crypto/ssh" - -// GetPublicKey marshals a ssh certificate to a string. -func GetPublicKey(cert *ssh.Certificate) string { - marshaled := ssh.MarshalAuthorizedKey(cert) - // Strip trailing newline - return string(marshaled[:len(marshaled)-1]) -} diff --git a/server/util/util_test.go b/server/util/util_test.go deleted file mode 100644 index d294d86..0000000 --- a/server/util/util_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package util - -import ( - "testing" - - "github.com/nsheridan/cashier/testdata" - "golang.org/x/crypto/ssh" -) - -func TestGetPublicKey(t *testing.T) { - t.Parallel() - c, _, _, _, _ := ssh.ParseAuthorizedKey(testdata.Cert) - if GetPublicKey(c.(*ssh.Certificate)) != string(testdata.Cert) { - t.Fail() - } -} |