tests: fix tests

This commit is contained in:
Stavros
2025-03-18 21:48:26 +02:00
parent 7d1252f3c7
commit 156bba6141
2 changed files with 86 additions and 9 deletions

View File

@@ -5,11 +5,13 @@ import (
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect"
"strings" "strings"
"testing" "testing"
"tinyauth/internal/api" "tinyauth/internal/api"
"tinyauth/internal/auth" "tinyauth/internal/auth"
"tinyauth/internal/docker" "tinyauth/internal/docker"
"tinyauth/internal/handlers"
"tinyauth/internal/hooks" "tinyauth/internal/hooks"
"tinyauth/internal/providers" "tinyauth/internal/providers"
"tinyauth/internal/types" "tinyauth/internal/types"
@@ -19,13 +21,21 @@ import (
// Simple API config for tests // Simple API config for tests
var apiConfig = types.APIConfig{ var apiConfig = types.APIConfig{
Port: 8080, Port: 8080,
Address: "0.0.0.0", Address: "0.0.0.0",
Secret: "super-secret-api-thing-for-tests", // It is 32 chars long Secret: "super-secret-api-thing-for-tests", // It is 32 chars long
AppURL: "http://tinyauth.localhost", CookieSecure: false,
SessionExpiry: 3600,
}
// Simple handlers config for tests
var handlersConfig = types.HandlersConfig{
AppURL: "http://localhost:8080",
Domain: ".localhost",
CookieSecure: false, CookieSecure: false,
SessionExpiry: 3600,
DisableContinue: false, DisableContinue: false,
Title: "Tinyauth",
GenericName: "Generic",
} }
// Cookie // Cookie
@@ -67,8 +77,11 @@ func getAPI(t *testing.T) *api.API {
// Create hooks service // Create hooks service
hooks := hooks.NewHooks(auth, providers) hooks := hooks.NewHooks(auth, providers)
// Create handlers service
handlers := handlers.NewHandlers(handlersConfig, auth, hooks, providers)
// Create API // Create API
api := api.NewAPI(apiConfig, hooks, auth, providers) api := api.NewAPI(apiConfig, handlers)
// Setup routes // Setup routes
api.Init() 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 // Test user context
func TestUserContext(t *testing.T) { func TestUserContext(t *testing.T) {
t.Log("Testing user context") t.Log("Testing user context")

View File

@@ -382,7 +382,7 @@ func (h *Handlers) AppHandler(c *gin.Context) {
// Create app context struct // Create app context struct
appContext := types.AppContext{ appContext := types.AppContext{
Status: 200, Status: 200,
Message: "Ok", Message: "OK",
ConfiguredProviders: configuredProviders, ConfiguredProviders: configuredProviders,
DisableContinue: h.Config.DisableContinue, DisableContinue: h.Config.DisableContinue,
Title: h.Config.Title, Title: h.Config.Title,
@@ -490,7 +490,7 @@ func (h *Handlers) OauthUrlHandler(c *gin.Context) {
// Return tailscale URL (immidiately redirects to the callback) // Return tailscale URL (immidiately redirects to the callback)
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
"message": "Ok", "message": "OK",
"url": fmt.Sprintf("%s/api/oauth/callback/tailscale?%s", h.Config.AppURL, tailscaleQuery.Encode()), "url": fmt.Sprintf("%s/api/oauth/callback/tailscale?%s", h.Config.AppURL, tailscaleQuery.Encode()),
}) })
return return
@@ -499,7 +499,7 @@ func (h *Handlers) OauthUrlHandler(c *gin.Context) {
// Return auth URL // Return auth URL
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
"message": "Ok", "message": "OK",
"url": authURL, "url": authURL,
}) })
} }