test: cover chat moderation and the embed endpoints

- CanModerateMessage: group conversations only, chat members only
- parseEmbedRefs, embedKey, escapeLike
- /chat/embeds and /chat/embeds/resolve: 401, 422 before the
  database, 503 without one
This commit is contained in:
2026-10-26 16:05:33 +01:00
parent eea171cee6
commit ac4c2e6f73
3 changed files with 233 additions and 0 deletions

View File

@@ -96,6 +96,35 @@ func TestChatContext(t *testing.T) {
t.Error("CanActFor is not self-only")
}
// Moderators delete other people's messages in group conversations
// (# ogólny) only, and only while they are chat-group members too
// (D-10, D-24).
for _, mc := range []struct {
name string
userID uint
convType string
want bool
}{
{"moderator in group", 7, "group", true},
{"moderator in dm", 7, "dm", false},
{"moderator, unknown type", 7, "", false},
{"non-moderator in group", 8, "group", false},
{"moderator outside the chat group", 9, "group", false},
{"no user", 0, "group", false},
} {
got, err := c.CanModerateMessage(ctx, mc.userID, mc.convType)
if err != nil || got != mc.want {
t.Errorf("%s: CanModerateMessage = %v, %v, want %v", mc.name, got, err, mc.want)
}
}
if _, err := c.CanModerateMessage(ctx, 13, "group"); err == nil {
t.Error("CanModerateMessage with a failing check returned no error")
}
noMods := classes.NewChatContext(&gorm.DB{}, "studenci", "").WithMembership(members)
if ok, err := noMods.CanModerateMessage(ctx, 7, "group"); ok || err != nil {
t.Errorf("CanModerateMessage without a moderator group = %v, %v, want false", ok, err)
}
load := classes.ChatContextLoader(func() string { return "studenci" }, func() string { return "moderatorzy" })
if got, err := load(ctx, &gorm.DB{}, 2); got != nil || err != nil {
t.Errorf("loader(2) = %v, %v, want nil, nil", got, err)