Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions handler/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"time"

"github.com/oullin/handler/payload"
"github.com/oullin/metal/env"
"github.com/oullin/pkg/portal"
)

func TestPingHandler(t *testing.T) {
t.Setenv("PING_USERNAME", "user")
t.Setenv("PING_PASSWORD", "pass")
h := MakePingHandler()
e := env.Ping{Username: "user", Password: "pass"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The credentials used here do not meet the validation requirement of min=16 characters defined in env.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 corresponding req.SetBasicAuth call on line 21.

Suggested change
e := env.Ping{Username: "user", Password: "pass"}
e := env.Ping{Username: "testuser12345678", Password: "testpass12345678"}

h := MakePingHandler(&e)

t.Run("valid credentials", func(t *testing.T) {
req := httptest.NewRequest("GET", "/ping", nil)
Expand All @@ -32,11 +33,8 @@ func TestPingHandler(t *testing.T) {
if resp.Message != "pong" {
t.Fatalf("unexpected message: %s", resp.Message)
}
if _, err := time.Parse("2006-01-02", resp.Date); err != nil {
t.Fatalf("invalid date: %v", err)
}
if _, err := time.Parse("15:04:05", resp.Time); err != nil {
t.Fatalf("invalid time: %v", err)
if _, err := time.Parse(portal.DatesLayout, resp.DateTime); err != nil {
t.Fatalf("invalid datetime: %v", err)
}
})

Expand Down
6 changes: 5 additions & 1 deletion metal/kernel/kernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func validEnvVars(t *testing.T) {
t.Setenv("ENV_SENTRY_DSN", "dsn")
t.Setenv("ENV_SENTRY_CSP", "csp")
t.Setenv("ENV_PUBLIC_ALLOWED_IP", "1.2.3.4")
t.Setenv("PING_USERNAME", "1234567890abcdef")
t.Setenv("PING_PASSWORD", "abcdef1234567890")
}

func TestMakeEnv(t *testing.T) {
Expand Down Expand Up @@ -81,7 +83,9 @@ func TestIgnite(t *testing.T) {
"ENV_HTTP_HOST=localhost\n" +
"ENV_HTTP_PORT=8080\n" +
"ENV_SENTRY_DSN=dsn\n" +
"ENV_SENTRY_CSP=csp\n"
"ENV_SENTRY_CSP=csp\n" +
"PING_USERNAME=1234567890abcdef\n" +
"PING_PASSWORD=abcdef1234567890\n"

f, err := os.CreateTemp("", "envfile")

Expand Down
46 changes: 9 additions & 37 deletions metal/kernel/router_ping_test.go
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"}},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The credentials used for env.Ping do not meet the min=16 characters validation requirement. To maintain consistency with validation rules and other tests, you should use credentials that are at least 16 characters long.

Suggested change
Env: &env.Environment{Ping: env.Ping{Username: "user", Password: "pass"}},
Env: &env.Environment{Ping: env.Ping{Username: "testuser12345678", Password: "testpass12345678"}},

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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Please update the basic auth credentials to match the ones defined in the Router setup, ensuring they meet the 16-character length requirement.

Suggested change
req.SetBasicAuth("user", "pass")
req.SetBasicAuth("testuser12345678", "testpass12345678")

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)
}
})
}
Loading