Files
sm-bm-plugin/controllers/api/chat_test.go
Jakub Zych 07d67be1e7 test: cover the chat access policy and bootstrap guards
ChatAccess admits only chat-group members to context bm/1 and manages
nothing; the chat context oversees nothing and its loader knows only
id 1; /chat and /chat/members need a caller and a database. The
migration count rises to 15 for the chat group seed.
2026-10-26 15:42:53 +01:00

47 lines
1.6 KiB
Go

package api_test
import (
"net/http"
"testing"
"git.golem15.com/golem15/summercms/modules/bouncer"
"git.golem15.com/jakub/sm-bm-plugin/controllers/api"
"gorm.io/gorm"
)
// chatNoDB is a chat-enabled Handlers whose database is never published.
func chatNoDB() *api.Handlers {
return &api.Handlers{DB: func() *gorm.DB { return nil }, ChatGroup: "studenci", ChatModeratorGroup: "moderatorzy"}
}
func TestChatBootstrapUnauthenticated(t *testing.T) {
code, body := serve(t, chatNoDB().Chat, http.MethodGet, "/", nil, nil)
if code != http.StatusUnauthorized || body["error"] != true {
t.Errorf("GET /chat without a principal = %d %v, want 401", code, body)
}
}
func TestChatBootstrapNoDatabase(t *testing.T) {
code, body := serve(t, chatNoDB().Chat, http.MethodGet, "/", &bouncer.Principal{ID: 7}, nil)
if code != http.StatusServiceUnavailable || body["message"] != "Database unavailable." {
t.Errorf("GET /chat without a database = %d %v, want 503 Database unavailable.", code, body)
}
if _, ok := body["data"]; ok {
t.Errorf("GET /chat without a database carries data: %v", body)
}
}
func TestChatMembersUnauthenticated(t *testing.T) {
code, body := serve(t, chatNoDB().ChatMembers, http.MethodGet, "/", nil, nil)
if code != http.StatusUnauthorized || body["error"] != true {
t.Errorf("GET /chat/members without a principal = %d %v, want 401", code, body)
}
}
func TestChatMembersNoDatabase(t *testing.T) {
code, body := serve(t, chatNoDB().ChatMembers, http.MethodGet, "/", &bouncer.Principal{ID: 7}, nil)
if code != http.StatusServiceUnavailable {
t.Errorf("GET /chat/members without a database = %d %v, want 503", code, body)
}
}