From 156bba6141adbb71b749ece694b639553ebc5252 Mon Sep 17 00:00:00 2001 From: Stavros Date: Tue, 18 Mar 2025 21:48:26 +0200 Subject: [PATCH] tests: fix tests --- internal/api/api_test.go | 89 ++++++++++++++++++++++++++++++++--- internal/handlers/handlers.go | 6 +-- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 28c01b2..a110519 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -5,11 +5,13 @@ import ( "io" "net/http" "net/http/httptest" + "reflect" "strings" "testing" "tinyauth/internal/api" "tinyauth/internal/auth" "tinyauth/internal/docker" + "tinyauth/internal/handlers" "tinyauth/internal/hooks" "tinyauth/internal/providers" "tinyauth/internal/types" @@ -19,13 +21,21 @@ import ( // Simple API config for tests var apiConfig = types.APIConfig{ - Port: 8080, - Address: "0.0.0.0", - Secret: "super-secret-api-thing-for-tests", // It is 32 chars long - AppURL: "http://tinyauth.localhost", + Port: 8080, + Address: "0.0.0.0", + Secret: "super-secret-api-thing-for-tests", // It is 32 chars long + CookieSecure: false, + SessionExpiry: 3600, +} + +// Simple handlers config for tests +var handlersConfig = types.HandlersConfig{ + AppURL: "http://localhost:8080", + Domain: ".localhost", CookieSecure: false, - SessionExpiry: 3600, DisableContinue: false, + Title: "Tinyauth", + GenericName: "Generic", } // Cookie @@ -67,8 +77,11 @@ func getAPI(t *testing.T) *api.API { // Create hooks service hooks := hooks.NewHooks(auth, providers) + // Create handlers service + handlers := handlers.NewHandlers(handlersConfig, auth, hooks, providers) + // Create API - api := api.NewAPI(apiConfig, hooks, auth, providers) + api := api.NewAPI(apiConfig, handlers) // Setup routes api.Init() @@ -123,6 +136,70 @@ func TestLogin(t *testing.T) { } } +// Test app context +func TestAppContext(t *testing.T) { + t.Log("Testing app context") + + // Get API + api := getAPI(t) + + // Create recorder + recorder := httptest.NewRecorder() + + // Create request + req, err := http.NewRequest("GET", "/api/app", nil) + + // Check if there was an error + if err != nil { + t.Fatalf("Error creating request: %v", err) + } + + // Set the cookie + req.AddCookie(&http.Cookie{ + Name: "tinyauth", + Value: cookie, + }) + + // Serve the request + api.Router.ServeHTTP(recorder, req) + + // Assert + assert.Equal(t, recorder.Code, http.StatusOK) + + // Read the body of the response + body, bodyErr := io.ReadAll(recorder.Body) + + // Check if there was an error + if bodyErr != nil { + t.Fatalf("Error getting body: %v", bodyErr) + } + + // Unmarshal the body into the user struct + var app types.AppContext + + jsonErr := json.Unmarshal(body, &app) + + // Check if there was an error + if jsonErr != nil { + t.Fatalf("Error unmarshalling body: %v", jsonErr) + } + + // Create tests values + expected := types.AppContext{ + Status: 200, + Message: "OK", + ConfiguredProviders: []string{"username"}, + DisableContinue: false, + Title: "Tinyauth", + GenericName: "Generic", + } + + // We should get the username back + if !reflect.DeepEqual(app, expected) { + t.Fatalf("Expected %v, got %v", expected, app) + } +} + // Test user context func TestUserContext(t *testing.T) { t.Log("Testing user context") diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 79b375b..fc98bd1 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -382,7 +382,7 @@ func (h *Handlers) AppHandler(c *gin.Context) { // Create app context struct appContext := types.AppContext{ Status: 200, - Message: "Ok", + Message: "OK", ConfiguredProviders: configuredProviders, DisableContinue: h.Config.DisableContinue, Title: h.Config.Title, @@ -490,7 +490,7 @@ func (h *Handlers) OauthUrlHandler(c *gin.Context) { // Return tailscale URL (immidiately redirects to the callback) c.JSON(200, gin.H{ "status": 200, - "message": "Ok", + "message": "OK", "url": fmt.Sprintf("%s/api/oauth/callback/tailscale?%s", h.Config.AppURL, tailscaleQuery.Encode()), }) return @@ -499,7 +499,7 @@ func (h *Handlers) OauthUrlHandler(c *gin.Context) { // Return auth URL c.JSON(200, gin.H{ "status": 200, - "message": "Ok", + "message": "OK", "url": authURL, }) }