-
-
Notifications
You must be signed in to change notification settings - Fork 0
Align ping handler tests with new auth requirements #97
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,67 +1,39 @@ | ||||||
| package kernel | ||||||
|
|
||||||
| import ( | ||||||
| "fmt" | ||||||
| "net/http" | ||||||
| "net/http/httptest" | ||||||
| "reflect" | ||||||
| "testing" | ||||||
| "time" | ||||||
| "unsafe" | ||||||
|
|
||||||
| "github.com/oullin/metal/env" | ||||||
| "github.com/oullin/pkg/middleware" | ||||||
| "github.com/oullin/pkg/portal" | ||||||
| ) | ||||||
|
|
||||||
| func TestPingRoute_PublicMiddleware(t *testing.T) { | ||||||
| t.Setenv("PING_USERNAME", "user") | ||||||
| t.Setenv("PING_PASSWORD", "pass") | ||||||
| fixedTime := time.Unix(1700000000, 0) | ||||||
| pm := middleware.MakePublicMiddleware("", false) | ||||||
| rv := reflect.ValueOf(&pm).Elem().FieldByName("now") | ||||||
| reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Set(reflect.ValueOf(func() time.Time { return fixedTime })) | ||||||
|
|
||||||
| func TestPingRoute(t *testing.T) { | ||||||
| r := Router{ | ||||||
| Mux: http.NewServeMux(), | ||||||
| Pipeline: middleware.Pipeline{ | ||||||
| PublicMiddleware: pm, | ||||||
| }, | ||||||
| Env: &env.Environment{Ping: env.Ping{Username: "user", Password: "pass"}}, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The credentials used for
Suggested change
|
||||||
| Mux: http.NewServeMux(), | ||||||
| Pipeline: middleware.Pipeline{PublicMiddleware: middleware.MakePublicMiddleware("", false)}, | ||||||
| } | ||||||
| r.Ping() | ||||||
|
|
||||||
| t.Run("request without public headers is unauthorized", func(t *testing.T) { | ||||||
| t.Run("valid credentials", func(t *testing.T) { | ||||||
| req := httptest.NewRequest("GET", "/ping", nil) | ||||||
| req.SetBasicAuth("user", "pass") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| rec := httptest.NewRecorder() | ||||||
| r.Mux.ServeHTTP(rec, req) | ||||||
| if rec.Code != http.StatusUnauthorized { | ||||||
| t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rec.Code) | ||||||
| if rec.Code != http.StatusOK { | ||||||
| t.Fatalf("expected status %d, got %d", http.StatusOK, rec.Code) | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| t.Run("request with public headers but invalid credentials is unauthorized", func(t *testing.T) { | ||||||
| t.Run("invalid credentials", func(t *testing.T) { | ||||||
| req := httptest.NewRequest("GET", "/ping", nil) | ||||||
| req.SetBasicAuth("bad", "creds") | ||||||
| req.Header.Set(portal.RequestIDHeader, "req-1") | ||||||
| req.Header.Set(portal.TimestampHeader, fmt.Sprintf("%d", fixedTime.Unix())) | ||||||
| req.Header.Set("X-Forwarded-For", "1.2.3.4") | ||||||
| rec := httptest.NewRecorder() | ||||||
| r.Mux.ServeHTTP(rec, req) | ||||||
| if rec.Code != http.StatusUnauthorized { | ||||||
| t.Fatalf("expected status %d, got %d", http.StatusUnauthorized, rec.Code) | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| t.Run("request with public headers and valid credentials succeeds", func(t *testing.T) { | ||||||
| req := httptest.NewRequest("GET", "/ping", nil) | ||||||
| req.SetBasicAuth("user", "pass") | ||||||
| req.Header.Set(portal.RequestIDHeader, "req-2") | ||||||
| req.Header.Set(portal.TimestampHeader, fmt.Sprintf("%d", fixedTime.Unix())) | ||||||
| req.Header.Set("X-Forwarded-For", "1.2.3.4") | ||||||
| rec := httptest.NewRecorder() | ||||||
| r.Mux.ServeHTTP(rec, req) | ||||||
| if rec.Code != http.StatusOK { | ||||||
| t.Fatalf("expected status %d, got %d", http.StatusOK, rec.Code) | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
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 credentials used here do not meet the validation requirement of
min=16characters defined inenv.Ping. While this test passes because it doesn't trigger environment validation, using credentials that conform to the validation rules would make the test more robust and consistent. Please update this line and the correspondingreq.SetBasicAuthcall on line 21.