From 368791648f979736ff84e08eadebcf525165867d Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Wed, 1 May 2024 19:25:43 +0200 Subject: [PATCH] Expand high-level token test. --- token/token_test.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/token/token_test.go b/token/token_test.go index 1bcad8c..7659111 100644 --- a/token/token_test.go +++ b/token/token_test.go @@ -1,30 +1,55 @@ package token import ( - "testing" - "path/filepath" "os" + "path/filepath" + "testing" + "time" ) -func TestBad(t *testing.T) { +func TestToken(t *testing.T) { d := t.TempDir() tokens = state{ filename: filepath.Join(d, "test.jsonl"), } + f, err := os.OpenFile(tokens.filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600, ) if err != nil { t.Fatalf("Create: %v", err) } - defer f.Close() + f.Close() - token, err := Parse("foo", nil) + future := time.Now().Add(time.Hour) + user := "user" + _, err = Add(&Stateful{ + Token: "token", + Group: "group", + Username: &user, + Permissions: []string{"present"}, + Expires: &future, + }) + 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) + } + + token, err = Parse("bad", nil) if err == nil { t.Errorf("Expected error, got %v", token) } - token, err = Parse("foo", []map[string]interface{}{}) + token, err = Parse("bad", []map[string]interface{}{}) if err == nil { t.Errorf("Expected error, got %v", token) }