1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-13 20:25:57 +01:00
galene/token/token_test.go

57 lines
1 KiB
Go
Raw Permalink Normal View History

package token
import (
"os"
2024-05-01 19:25:43 +02:00
"path/filepath"
"testing"
"time"
)
2024-05-01 19:25:43 +02:00
func TestToken(t *testing.T) {
d := t.TempDir()
tokens = state{
filename: filepath.Join(d, "test.jsonl"),
}
2024-05-01 19:25:43 +02:00
f, err := os.OpenFile(tokens.filename,
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600,
)
if err != nil {
t.Fatalf("Create: %v", err)
}
2024-05-01 19:25:43 +02:00
f.Close()
future := time.Now().Add(time.Hour)
user := "user"
_, err = Update(&Stateful{
2024-05-01 19:25:43 +02:00
Token: "token",
Group: "group",
Username: &user,
Permissions: []string{"present"},
Expires: &future,
}, "")
2024-05-01 19:25:43 +02:00
if err != nil {
t.Fatalf("Add: %v", err)
}
token, err := Parse("token", nil)
if err != nil || token == nil {
t.Fatalf("Parse: %v", err)
}
_, _, err = token.Check("galene.org:8443", "group", &user)
if err != nil {
t.Errorf("Check: %v", err)
}
2024-05-01 19:25:43 +02:00
token, err = Parse("bad", nil)
if err == nil {
t.Errorf("Expected error, got %v", token)
}
2024-05-01 19:25:43 +02:00
token, err = Parse("bad", []map[string]interface{}{})
if err == nil {
t.Errorf("Expected error, got %v", token)
}
}