-
Notifications
You must be signed in to change notification settings - Fork 3
[test-improver] Improve tests for auth package #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,9 @@ package auth | |||||||||||||
|
|
||||||||||||||
| import ( | ||||||||||||||
| "testing" | ||||||||||||||
|
|
||||||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||||||
| "github.com/stretchr/testify/require" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| func TestSanitizeForLogging(t *testing.T) { | ||||||||||||||
|
|
@@ -40,13 +43,27 @@ func TestSanitizeForLogging(t *testing.T) { | |||||||||||||
| input: "Bearer my-token-123", | ||||||||||||||
| want: "Bear...", | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Unicode characters", | ||||||||||||||
| input: "key-with-émojis-🔑", | ||||||||||||||
| want: "key-...", | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Very long API key", | ||||||||||||||
| input: "my-super-long-api-key-with-many-characters-12345678901234567890", | ||||||||||||||
| want: "my-s...", | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Special characters", | ||||||||||||||
| input: "key!@#$%^&*()", | ||||||||||||||
| want: "key!...", | ||||||||||||||
| }, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| for _, tt := range tests { | ||||||||||||||
| t.Run(tt.name, func(t *testing.T) { | ||||||||||||||
| if got := sanitizeForLogging(tt.input); got != tt.want { | ||||||||||||||
| t.Errorf("sanitizeForLogging() = %v, want %v", got, tt.want) | ||||||||||||||
| } | ||||||||||||||
| got := sanitizeForLogging(tt.input) | ||||||||||||||
| assert.Equal(t, tt.want, got) | ||||||||||||||
| }) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -87,24 +104,69 @@ func TestParseAuthHeader(t *testing.T) { | |||||||||||||
| wantAgentID: "agent-123", | ||||||||||||||
| wantErr: nil, | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Bearer with multiple spaces", | ||||||||||||||
| authHeader: "Bearer my-token", | ||||||||||||||
| wantAPIKey: " my-token", | ||||||||||||||
| wantAgentID: " my-token", | ||||||||||||||
| wantErr: nil, | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Lowercase bearer (not supported)", | ||||||||||||||
| authHeader: "bearer my-token", | ||||||||||||||
| wantAPIKey: "bearer my-token", | ||||||||||||||
| wantAgentID: "bearer my-token", | ||||||||||||||
| wantErr: nil, | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Agent with multiple spaces", | ||||||||||||||
| authHeader: "Agent agent-id", | ||||||||||||||
| wantAPIKey: " agent-id", | ||||||||||||||
| wantAgentID: " agent-id", | ||||||||||||||
| wantErr: nil, | ||||||||||||||
| }, | ||||||||||||||
| { | ||||||||||||||
| name: "Whitespace only header", | ||||||||||||||
| authHeader: " ", | ||||||||||||||
| wantAPIKey: " ", | ||||||||||||||
| wantAgentID: " ", | ||||||||||||||
| wantErr: nil, | ||||||||||||||
|
Comment on lines
+131
to
+133
|
||||||||||||||
| wantAPIKey: " ", | |
| wantAgentID: " ", | |
| wantErr: nil, | |
| wantAPIKey: "", | |
| wantAgentID: "", | |
| wantErr: ErrMissingAuthHeader, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expected values include a leading space, but the implementation uses
strings.TrimPrefix(authHeader, \"Bearer \")which removes only the prefix "Bearer " (with a single space). With input "Bearer my-token" (two spaces), the result will be " my-token" (one leading space), which matches the expectation. However, this test documents unexpected behavior - tokens/keys with leading whitespace are likely unintended and could cause authentication failures. Consider whether the implementation should trim all leading/trailing whitespace after prefix removal.This issue also appears in the following locations of the same file: