mirror of
https://github.com/henrygd/beszel.git
synced 2026-10-01 05:47:48 +02:00
test: preserve fixture errors and verify complete certificate pools (#2462)
This commit is contained in:
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// marshal to json and return an io.Reader (for use in ApiScenario.Body)
|
||||
@@ -184,7 +185,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"min": 10,
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
},
|
||||
AfterTestFunc: func(t testing.TB, app *pbTests.TestApp, res *http.Response) {
|
||||
alerts, _ := app.CountRecords("alerts")
|
||||
@@ -209,7 +210,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"overwrite": false,
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "CPU",
|
||||
"system": system1.Id,
|
||||
@@ -243,7 +244,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"overwrite": true,
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "CPU",
|
||||
"system": system2.Id,
|
||||
@@ -271,7 +272,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"systems": []string{system1.Id},
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "CPU",
|
||||
"system": system1.Id,
|
||||
@@ -300,7 +301,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"systems": []string{system1.Id},
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "CPU",
|
||||
"system": system1.Id,
|
||||
@@ -329,7 +330,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"systems": []string{system1.Id, system2.Id},
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
for _, systemId := range []string{system1.Id, system2.Id} {
|
||||
_, err := beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "Memory",
|
||||
@@ -363,7 +364,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"systems": []string{system1.Id},
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "CPU", "system": system1.Id, "user": user2.Id, "value": 80,
|
||||
})
|
||||
@@ -388,7 +389,7 @@ func TestUserAlertsApi(t *testing.T) {
|
||||
"systems": []string{system2.Id},
|
||||
}),
|
||||
BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) {
|
||||
beszelTests.ClearCollection(t, app, "alerts")
|
||||
require.NoError(t, beszelTests.ClearCollection(t, app, "alerts"))
|
||||
for _, user := range []string{user1.Id, user2.Id} {
|
||||
beszelTests.CreateRecord(app, "alerts", map[string]any{
|
||||
"name": "CPU",
|
||||
@@ -430,19 +431,21 @@ func TestSendTestNotification(t *testing.T) {
|
||||
localURL := "generic+" + server.URL
|
||||
|
||||
readonlyUser, err := beszelTests.CreateUserWithRole(hub, "readonly@example.com", "password123", "readonly")
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
readonlyToken, err := readonlyUser.NewAuthToken()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
userToken, err := user.NewAuthToken()
|
||||
require.NoError(t, err, "Failed to create user auth token")
|
||||
|
||||
adminUser, err := beszelTests.CreateUserWithRole(hub, "admin@example.com", "password123", "admin")
|
||||
assert.NoError(t, err, "Failed to create admin user")
|
||||
require.NoError(t, err, "Failed to create admin user")
|
||||
adminUserToken, err := adminUser.NewAuthToken()
|
||||
require.NoError(t, err, "Failed to create admin auth token")
|
||||
|
||||
superuser, err := beszelTests.CreateSuperuser(hub, "superuser@example.com", "password123")
|
||||
assert.NoError(t, err, "Failed to create superuser")
|
||||
require.NoError(t, err, "Failed to create superuser")
|
||||
superuserToken, err := superuser.NewAuthToken()
|
||||
assert.NoError(t, err, "Failed to create superuser auth token")
|
||||
require.NoError(t, err, "Failed to create superuser auth token")
|
||||
|
||||
testAppFactory := func(t testing.TB) *pbTests.TestApp {
|
||||
return hub.TestApp
|
||||
|
||||
@@ -99,6 +99,7 @@ func TestApiRoutesAuthentication(t *testing.T) {
|
||||
adminUser, err := beszelTests.CreateUserWithRole(hub, "admin@example.com", "password123", "admin")
|
||||
require.NoError(t, err, "Failed to create admin user")
|
||||
adminUserToken, err := adminUser.NewAuthToken()
|
||||
require.NoError(t, err, "Failed to create admin auth token")
|
||||
|
||||
readOnlyUser, err := beszelTests.CreateUserWithRole(hub, "readonly@example.com", "password123", "readonly")
|
||||
require.NoError(t, err, "Failed to create readonly user")
|
||||
|
||||
@@ -318,6 +318,7 @@ func TestDeleteOldAlertsHistoryEdgeCases(t *testing.T) {
|
||||
"status": "up",
|
||||
"users": []string{user.Id},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create only 5 alerts (well below threshold)
|
||||
for i := range 5 {
|
||||
|
||||
@@ -112,6 +112,9 @@ func CreateRecord(app core.App, collectionName string, fields map[string]any) (*
|
||||
|
||||
func ClearCollection(t testing.TB, app core.App, collectionName string) error {
|
||||
_, err := app.DB().NewQuery(fmt.Sprintf("DELETE from %s", collectionName)).Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
recordCount, err := app.CountRecords(collectionName)
|
||||
assert.EqualValues(t, recordCount, 0, "should have 0 records after clearing")
|
||||
return err
|
||||
|
||||
26
internal/tests/hub_test.go
Normal file
26
internal/tests/hub_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
//go:build testing
|
||||
|
||||
package tests
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestClearCollection(t *testing.T) {
|
||||
hub, err := NewTestHub(t.TempDir())
|
||||
require.NoError(t, err)
|
||||
defer hub.Cleanup()
|
||||
|
||||
_, err = CreateUser(hub, "clear@example.com", "password123")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, ClearCollection(t, hub, "users"))
|
||||
|
||||
// An empty view can be counted, but SQLite rejects DELETE against it.
|
||||
view := core.NewViewCollection("empty_users")
|
||||
view.ViewQuery = "SELECT id FROM users"
|
||||
require.NoError(t, hub.Save(view))
|
||||
require.ErrorContains(t, ClearCollection(t, hub, "empty_users"), "cannot modify empty_users because it is a view")
|
||||
}
|
||||
Reference in New Issue
Block a user