tests: use testify assert in context and health controller

This commit is contained in:
Stavros
2026-03-26 16:54:54 +02:00
parent 5219d5c2be
commit b2ab3d0f37
2 changed files with 13 additions and 52 deletions

View File

@@ -10,6 +10,7 @@ import (
"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" "github.com/steveiliop56/tinyauth/internal/utils"
"github.com/stretchr/testify/assert"
) )
func TestContextController(t *testing.T) { func TestContextController(t *testing.T) {
@@ -53,13 +54,8 @@ func TestContextController(t *testing.T) {
OAuthAutoRedirect: controllerConfig.OAuthAutoRedirect, OAuthAutoRedirect: controllerConfig.OAuthAutoRedirect,
WarningsEnabled: controllerConfig.WarningsEnabled, WarningsEnabled: controllerConfig.WarningsEnabled,
} }
bytes, err := json.Marshal(expectedAppContextResponse) bytes, err := json.Marshal(expectedAppContextResponse)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to marshal expected response: %v", err)
}
return string(bytes) return string(bytes)
}(), }(),
}, },
@@ -72,13 +68,8 @@ func TestContextController(t *testing.T) {
Status: 401, Status: 401,
Message: "Unauthorized", Message: "Unauthorized",
} }
bytes, err := json.Marshal(expectedUserContextResponse) bytes, err := json.Marshal(expectedUserContextResponse)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to marshal expected response: %v", err)
}
return string(bytes) return string(bytes)
}(), }(),
}, },
@@ -106,13 +97,8 @@ func TestContextController(t *testing.T) {
Email: utils.CompileUserEmail("johndoe", controllerConfig.CookieDomain), Email: utils.CompileUserEmail("johndoe", controllerConfig.CookieDomain),
Provider: "local", Provider: "local",
} }
bytes, err := json.Marshal(expectedUserContextResponse) bytes, err := json.Marshal(expectedUserContextResponse)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to marshal expected response: %v", err)
}
return string(bytes) return string(bytes)
}(), }(),
}, },
@@ -135,20 +121,12 @@ func TestContextController(t *testing.T) {
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
request, err := http.NewRequest("GET", test.path, nil) request, err := http.NewRequest("GET", test.path, nil)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
router.ServeHTTP(recorder, request) router.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK { assert.Equal(t, recorder.Result().StatusCode, http.StatusOK)
t.Fatalf("Expected status code 200, got %d", recorder.Code) assert.Equal(t, test.expected, recorder.Body.String())
}
if recorder.Body.String() != test.expected {
t.Fatalf("Expected response body %s, got %s", test.expected, recorder.Body.String())
}
}) })
} }
} }

View File

@@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/steveiliop56/tinyauth/internal/controller" "github.com/steveiliop56/tinyauth/internal/controller"
"github.com/stretchr/testify/assert"
) )
func TestHealthController(t *testing.T) { func TestHealthController(t *testing.T) {
@@ -26,13 +27,8 @@ func TestHealthController(t *testing.T) {
"status": 200, "status": 200,
"message": "Healthy", "message": "Healthy",
} }
bytes, err := json.Marshal(expectedHealthResponse) bytes, err := json.Marshal(expectedHealthResponse)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to marshal expected response: %v", err)
}
return string(bytes) return string(bytes)
}(), }(),
}, },
@@ -45,13 +41,8 @@ func TestHealthController(t *testing.T) {
"status": 200, "status": 200,
"message": "Healthy", "message": "Healthy",
} }
bytes, err := json.Marshal(expectedHealthResponse) bytes, err := json.Marshal(expectedHealthResponse)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to marshal expected response: %v", err)
}
return string(bytes) return string(bytes)
}(), }(),
}, },
@@ -69,20 +60,12 @@ func TestHealthController(t *testing.T) {
recorder := httptest.NewRecorder() recorder := httptest.NewRecorder()
request, err := http.NewRequest(test.method, test.path, nil) request, err := http.NewRequest(test.method, test.path, nil)
assert.NoError(t, err)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
router.ServeHTTP(recorder, request) router.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK { assert.Equal(t, http.StatusOK, recorder.Code)
t.Fatalf("Expected status code 200, got %d", recorder.Code) assert.Equal(t, test.expected, recorder.Body.String())
}
if recorder.Body.String() != test.expected {
t.Fatalf("Expected response body %s, got %s", test.expected, recorder.Body.String())
}
}) })
} }
} }