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

View File

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