mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-03-28 01:17:54 +00:00
tests: use testify assert in context and health controller
This commit is contained in:
@@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user