test: Add edge case tests for auth helper functions
- respondMissingScope: nil writer handling (80% to 100%) - attachAPITokenRecord: nil record handling (80% to 100%) - getAPITokenRecordFromRequest: wrong type and missing value (87.5% to 100%)
This commit is contained in:
parent
c71fb694bb
commit
1fdb15c779
1 changed files with 44 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
|
@ -56,3 +57,46 @@ func TestRequireScopeAllowsMatchingScope(t *testing.T) {
|
|||
t.Fatalf("expected status 202 when scope present, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRespondMissingScopeNilWriter(t *testing.T) {
|
||||
// Should not panic when called with nil writer
|
||||
respondMissingScope(nil, "some-scope")
|
||||
}
|
||||
|
||||
func TestAttachAPITokenRecordNilRecord(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
originalCtx := req.Context()
|
||||
|
||||
// Should not panic and should not modify context when record is nil
|
||||
attachAPITokenRecord(req, nil)
|
||||
|
||||
// Context should remain unchanged
|
||||
if req.Context() != originalCtx {
|
||||
t.Fatal("context should not change when attaching nil record")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAPITokenRecordFromRequestWrongType(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
||||
// Attach a value of wrong type to the context
|
||||
ctx := req.Context()
|
||||
ctx = context.WithValue(ctx, contextKeyAPIToken, "not-a-record")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
// Should return nil when type assertion fails
|
||||
record := getAPITokenRecordFromRequest(req)
|
||||
if record != nil {
|
||||
t.Fatal("expected nil when context value is wrong type")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAPITokenRecordFromRequestNoValue(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
||||
// No token attached - should return nil
|
||||
record := getAPITokenRecordFromRequest(req)
|
||||
if record != nil {
|
||||
t.Fatal("expected nil when no token in context")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue