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"
"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"
@@ -22,10 +24,18 @@ 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",
CookieSecure: false,
SessionExpiry: 3600,
}
// Simple handlers config for tests
var handlersConfig = types.HandlersConfig{
AppURL: "http://localhost:8080",
Domain: ".localhost",
CookieSecure: false,
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")

View File

@@ -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,
})
}