mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-03-25 16:07:54 +00:00
tests: rework tests for context controller
This commit is contained in:
@@ -2,152 +2,153 @@ package controller_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/steveiliop56/tinyauth/internal/config"
|
"github.com/steveiliop56/tinyauth/internal/config"
|
||||||
"github.com/steveiliop56/tinyauth/internal/controller"
|
"github.com/steveiliop56/tinyauth/internal/controller"
|
||||||
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
|
"github.com/steveiliop56/tinyauth/internal/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"gotest.tools/v3/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var contextControllerCfg = controller.ContextControllerConfig{
|
func TestContextController(t *testing.T) {
|
||||||
|
controllerConfig := controller.ContextControllerConfig{
|
||||||
Providers: []controller.Provider{
|
Providers: []controller.Provider{
|
||||||
{
|
{
|
||||||
Name: "Local",
|
Name: "Local",
|
||||||
ID: "local",
|
ID: "local",
|
||||||
OAuth: false,
|
OAuth: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Name: "Google",
|
|
||||||
ID: "google",
|
|
||||||
OAuth: true,
|
|
||||||
},
|
},
|
||||||
},
|
Title: "Tinyauth",
|
||||||
Title: "Test App",
|
AppURL: "https://tinyauth.example.com",
|
||||||
AppURL: "http://localhost:8080",
|
CookieDomain: "example.com",
|
||||||
CookieDomain: "localhost",
|
ForgotPasswordMessage: "foo",
|
||||||
ForgotPasswordMessage: "Contact admin to reset your password.",
|
BackgroundImage: "/background.jpg",
|
||||||
BackgroundImage: "/assets/bg.jpg",
|
OAuthAutoRedirect: "none",
|
||||||
OAuthAutoRedirect: "google",
|
|
||||||
WarningsEnabled: true,
|
WarningsEnabled: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
var contextCtrlTestContext = config.UserContext{
|
tests := []struct {
|
||||||
Username: "testuser",
|
description string
|
||||||
Name: "testuser",
|
middlewares []gin.HandlerFunc
|
||||||
Email: "test@example.com",
|
expected string
|
||||||
IsLoggedIn: true,
|
path string
|
||||||
IsBasicAuth: false,
|
}{
|
||||||
OAuth: false,
|
{
|
||||||
|
description: "Ensure context controller returns app context",
|
||||||
|
middlewares: []gin.HandlerFunc{},
|
||||||
|
path: "/api/context/app",
|
||||||
|
expected: func() string {
|
||||||
|
expectedAppContextResponse := controller.AppContextResponse{
|
||||||
|
Status: 200,
|
||||||
|
Message: "Success",
|
||||||
|
Providers: controllerConfig.Providers,
|
||||||
|
Title: controllerConfig.Title,
|
||||||
|
AppURL: controllerConfig.AppURL,
|
||||||
|
CookieDomain: controllerConfig.CookieDomain,
|
||||||
|
ForgotPasswordMessage: controllerConfig.ForgotPasswordMessage,
|
||||||
|
BackgroundImage: controllerConfig.BackgroundImage,
|
||||||
|
OAuthAutoRedirect: controllerConfig.OAuthAutoRedirect,
|
||||||
|
WarningsEnabled: controllerConfig.WarningsEnabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(expectedAppContextResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to marshal expected response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(bytes)
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Ensure user context returns 401 when unauthorized",
|
||||||
|
middlewares: []gin.HandlerFunc{},
|
||||||
|
path: "/api/context/user",
|
||||||
|
expected: func() string {
|
||||||
|
expectedUserContextResponse := controller.UserContextResponse{
|
||||||
|
Status: 401,
|
||||||
|
Message: "Unauthorized",
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(expectedUserContextResponse)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to marshal expected response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(bytes)
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Ensure user context returns when authorized",
|
||||||
|
middlewares: []gin.HandlerFunc{
|
||||||
|
func(c *gin.Context) {
|
||||||
|
c.Set("context", &config.UserContext{
|
||||||
|
Username: "johndoe",
|
||||||
|
Name: "John Doe",
|
||||||
|
Email: utils.CompileUserEmail("johndoe", controllerConfig.CookieDomain),
|
||||||
|
Provider: "local",
|
||||||
|
IsLoggedIn: true,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "/api/context/user",
|
||||||
|
expected: func() string {
|
||||||
|
expectedUserContextResponse := controller.UserContextResponse{
|
||||||
|
Status: 200,
|
||||||
|
Message: "Success",
|
||||||
|
IsLoggedIn: true,
|
||||||
|
Username: "johndoe",
|
||||||
|
Name: "John Doe",
|
||||||
|
Email: utils.CompileUserEmail("johndoe", controllerConfig.CookieDomain),
|
||||||
Provider: "local",
|
Provider: "local",
|
||||||
TotpPending: false,
|
|
||||||
OAuthGroups: "",
|
|
||||||
TotpEnabled: false,
|
|
||||||
OAuthSub: "",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupContextController(middlewares *[]gin.HandlerFunc) (*gin.Engine, *httptest.ResponseRecorder) {
|
bytes, err := json.Marshal(expectedUserContextResponse)
|
||||||
tlog.NewSimpleLogger().Init()
|
|
||||||
|
|
||||||
// Setup
|
if err != nil {
|
||||||
gin.SetMode(gin.TestMode)
|
t.Fatalf("Failed to marshal expected response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(bytes)
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.description, func(t *testing.T) {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
|
|
||||||
if middlewares != nil {
|
for _, middleware := range test.middlewares {
|
||||||
for _, m := range *middlewares {
|
router.Use(middleware)
|
||||||
router.Use(m)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
group := router.Group("/api")
|
group := router.Group("/api")
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
|
||||||
ctrl := controller.NewContextController(contextControllerCfg, group)
|
contextController := controller.NewContextController(controllerConfig, group)
|
||||||
ctrl.SetupRoutes()
|
contextController.SetupRoutes()
|
||||||
|
|
||||||
return router, recorder
|
recorder := httptest.NewRecorder()
|
||||||
|
|
||||||
|
request, err := http.NewRequest("GET", test.path, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create request: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAppContextHandler(t *testing.T) {
|
router.ServeHTTP(recorder, request)
|
||||||
expectedRes := controller.AppContextResponse{
|
|
||||||
Status: 200,
|
if recorder.Code != http.StatusOK {
|
||||||
Message: "Success",
|
t.Fatalf("Expected status code 200, got %d", recorder.Code)
|
||||||
Providers: contextControllerCfg.Providers,
|
|
||||||
Title: contextControllerCfg.Title,
|
|
||||||
AppURL: contextControllerCfg.AppURL,
|
|
||||||
CookieDomain: contextControllerCfg.CookieDomain,
|
|
||||||
ForgotPasswordMessage: contextControllerCfg.ForgotPasswordMessage,
|
|
||||||
BackgroundImage: contextControllerCfg.BackgroundImage,
|
|
||||||
OAuthAutoRedirect: contextControllerCfg.OAuthAutoRedirect,
|
|
||||||
WarningsEnabled: contextControllerCfg.WarningsEnabled,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
router, recorder := setupContextController(nil)
|
if recorder.Body.String() != test.expected {
|
||||||
req := httptest.NewRequest("GET", "/api/context/app", nil)
|
t.Fatalf("Expected response body %s, got %s", test.expected, recorder.Body.String())
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
|
|
||||||
assert.Equal(t, 200, recorder.Code)
|
|
||||||
|
|
||||||
var ctrlRes controller.AppContextResponse
|
|
||||||
|
|
||||||
err := json.Unmarshal(recorder.Body.Bytes(), &ctrlRes)
|
|
||||||
|
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.DeepEqual(t, expectedRes, ctrlRes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserContextHandler(t *testing.T) {
|
|
||||||
expectedRes := controller.UserContextResponse{
|
|
||||||
Status: 200,
|
|
||||||
Message: "Success",
|
|
||||||
IsLoggedIn: contextCtrlTestContext.IsLoggedIn,
|
|
||||||
Username: contextCtrlTestContext.Username,
|
|
||||||
Name: contextCtrlTestContext.Name,
|
|
||||||
Email: contextCtrlTestContext.Email,
|
|
||||||
Provider: contextCtrlTestContext.Provider,
|
|
||||||
OAuth: contextCtrlTestContext.OAuth,
|
|
||||||
TotpPending: contextCtrlTestContext.TotpPending,
|
|
||||||
OAuthName: contextCtrlTestContext.OAuthName,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test with context
|
|
||||||
router, recorder := setupContextController(&[]gin.HandlerFunc{
|
|
||||||
func(c *gin.Context) {
|
|
||||||
c.Set("context", &contextCtrlTestContext)
|
|
||||||
c.Next()
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
req := httptest.NewRequest("GET", "/api/context/user", nil)
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
|
|
||||||
assert.Equal(t, 200, recorder.Code)
|
|
||||||
|
|
||||||
var ctrlRes controller.UserContextResponse
|
|
||||||
|
|
||||||
err := json.Unmarshal(recorder.Body.Bytes(), &ctrlRes)
|
|
||||||
|
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.DeepEqual(t, expectedRes, ctrlRes)
|
|
||||||
|
|
||||||
// Test no context
|
|
||||||
expectedRes = controller.UserContextResponse{
|
|
||||||
Status: 401,
|
|
||||||
Message: "Unauthorized",
|
|
||||||
IsLoggedIn: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
router, recorder = setupContextController(nil)
|
|
||||||
req = httptest.NewRequest("GET", "/api/context/user", nil)
|
|
||||||
router.ServeHTTP(recorder, req)
|
|
||||||
|
|
||||||
assert.Equal(t, 200, recorder.Code)
|
|
||||||
|
|
||||||
err = json.Unmarshal(recorder.Body.Bytes(), &ctrlRes)
|
|
||||||
|
|
||||||
assert.NilError(t, err)
|
|
||||||
assert.DeepEqual(t, expectedRes, ctrlRes)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user