2020-05-30 09:52:27 +00:00
|
|
|
package authstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-30 23:14:40 +00:00
|
|
|
"strings"
|
2020-05-30 09:52:27 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore(t *testing.T) {
|
2020-05-30 23:14:40 +00:00
|
|
|
// Note: test output is cached (running twice will not result in two records)
|
2020-05-30 09:52:27 +00:00
|
|
|
|
2020-05-30 23:14:40 +00:00
|
|
|
connStr := "postgres://postgres:postgres@localhost/postgres"
|
2020-06-03 06:17:30 +00:00
|
|
|
if strings.Contains(connStr, "@localhost/") || strings.Contains(connStr, "@localhost:") {
|
2020-05-30 23:14:40 +00:00
|
|
|
connStr += "?sslmode=disable"
|
|
|
|
} else {
|
|
|
|
connStr += "?sslmode=required"
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO url.Parse
|
|
|
|
|
|
|
|
store, err := NewStore(connStr, initSQL)
|
2020-05-30 09:52:27 +00:00
|
|
|
if nil != err {
|
|
|
|
t.Fatal("connection error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
num := "8"
|
|
|
|
slug := num + "-xxx-client"
|
2020-08-13 09:48:21 +00:00
|
|
|
secret := "3-xxxx-zzzz-yyyy"
|
|
|
|
pubkey := ToPublicKeyString(secret)
|
2020-05-30 09:52:27 +00:00
|
|
|
auth1 := &Authorization{
|
|
|
|
Slug: slug,
|
2020-08-13 09:48:21 +00:00
|
|
|
SharedKey: secret,
|
2020-05-30 09:52:27 +00:00
|
|
|
PublicKey: pubkey,
|
|
|
|
}
|
|
|
|
err = store.Add(auth1)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal("add error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auth, err := store.Get(slug)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal("get by slug error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auth, err = store.Get(pubkey)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal("get by pub error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-13 09:53:09 +00:00
|
|
|
auth, err = store.Get(secret)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal("get by key error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 09:52:27 +00:00
|
|
|
auth1.MachinePPID = "a-secretish-id"
|
|
|
|
err = store.Set(auth1)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal("set machine id", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.Delete(auth1)
|
|
|
|
if nil != err {
|
|
|
|
t.Fatal("set machine id", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-13 09:48:21 +00:00
|
|
|
auth, err = store.Get(slug)
|
2020-06-22 06:34:42 +00:00
|
|
|
if nil == err {
|
|
|
|
t.Fatal("should get nothing back")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-30 09:52:27 +00:00
|
|
|
store.Close()
|
|
|
|
|
|
|
|
fmt.Printf("%#v\n", auth)
|
|
|
|
}
|