Add comprehensive tests for services and middleware
- Implement tests for AuthMiddleware to handle cookie and token authentication. - Create tests for the Importer and Manager in the Caddy package. - Enhance AuthService tests with password change and token validation scenarios. - Introduce tests for CertificateService to validate certificate listing and expiry. - Expand LogService tests to cover log querying and pagination. - Add NotificationService tests for creating, listing, and marking notifications as read. - Implement ProxyHostService tests for CRUD operations and unique domain validation. - Create RemoteServerService tests for CRUD operations. - Add UpdateService tests to mock GitHub API responses for version checking. - Introduce UptimeService tests to check host availability and notifications for down hosts.
This commit is contained in:
@@ -18,7 +18,8 @@ import (
|
||||
func setupTestRouter(t *testing.T) (*gin.Engine, *gorm.DB) {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
dsn := "file:" + t.Name() + "?mode=memory&cache=shared"
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&models.ProxyHost{}, &models.Location{}))
|
||||
|
||||
@@ -113,3 +114,28 @@ func TestProxyHostErrors(t *testing.T) {
|
||||
router.ServeHTTP(delResp, delReq)
|
||||
require.Equal(t, http.StatusNotFound, delResp.Code)
|
||||
}
|
||||
|
||||
func TestProxyHostValidation(t *testing.T) {
|
||||
router, db := setupTestRouter(t)
|
||||
|
||||
// Invalid JSON
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/proxy-hosts", strings.NewReader(`{invalid json}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp := httptest.NewRecorder()
|
||||
router.ServeHTTP(resp, req)
|
||||
require.Equal(t, http.StatusBadRequest, resp.Code)
|
||||
|
||||
// Create a host first
|
||||
host := &models.ProxyHost{
|
||||
UUID: "valid-uuid",
|
||||
DomainNames: "valid.com",
|
||||
}
|
||||
db.Create(host)
|
||||
|
||||
// Update with invalid JSON
|
||||
req = httptest.NewRequest(http.MethodPut, "/api/v1/proxy-hosts/valid-uuid", strings.NewReader(`{invalid json}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp = httptest.NewRecorder()
|
||||
router.ServeHTTP(resp, req)
|
||||
require.Equal(t, http.StatusBadRequest, resp.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user