You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.5 KiB
62 lines
1.5 KiB
package web
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/domagojzecevic/cammonitor/internal/config"
|
|
"github.com/domagojzecevic/cammonitor/internal/db"
|
|
)
|
|
|
|
func TestHealthReturnsOK(t *testing.T) {
|
|
router := NewRouter(nil, nil, nil)
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("expected status %d, got %d", http.StatusOK, response.Code)
|
|
}
|
|
|
|
var body map[string]string
|
|
if err := json.NewDecoder(response.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode response body: %v", err)
|
|
}
|
|
|
|
if body["status"] != "ok" {
|
|
t.Fatalf("expected status ok, got %q", body["status"])
|
|
}
|
|
}
|
|
|
|
func TestAdminUsersRedirectsWithoutSessionCookie(t *testing.T) {
|
|
database, err := db.Open(filepath.Join(t.TempDir(), "cammonitor.db"))
|
|
if err != nil {
|
|
t.Fatalf("open database: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := database.Close(); err != nil {
|
|
t.Fatalf("close database: %v", err)
|
|
}
|
|
})
|
|
|
|
router := NewRouter(&config.Config{SessionTTL: time.Hour}, database, nil)
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/admin/users", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(response, request)
|
|
|
|
if response.Code != http.StatusFound {
|
|
t.Fatalf("expected status %d, got %d", http.StatusFound, response.Code)
|
|
}
|
|
|
|
if location := response.Header().Get("Location"); location != "/login" {
|
|
t.Fatalf("expected redirect to /login, got %q", location)
|
|
}
|
|
}
|
|
|