max-telegram-bridge-bot/admin_test.go
Andrey Lugovskoy 62a4233027 Security hardening and admin check tests
- Webhook paths now include token-derived secret (prevents spoofed updates)
- HTTP server with Read/Write/Idle timeouts (prevents slowloris)
- Shared HTTP client with 60s timeout for all uploads/downloads
- Removed tokens and sensitive data from debug logs
- Retry loop respects context cancellation instead of blocking sleep
- Pending bridge keys expire after 1 hour (migration 000003)
- Increased bridge key entropy from 32 to 64 bits (16 hex chars)
- Docker container runs as non-root user
- Extracted admin check helpers with unit tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-15 15:05:41 +03:00

109 lines
2.3 KiB
Go

package main
import (
"testing"
maxschemes "github.com/max-messenger/max-bot-api-client-go/schemes"
)
func TestIsTgGroup(t *testing.T) {
tests := []struct {
chatType string
want bool
}{
{"group", true},
{"supergroup", true},
{"private", false},
{"channel", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.chatType, func(t *testing.T) {
if got := isTgGroup(tt.chatType); got != tt.want {
t.Errorf("isTgGroup(%q) = %v, want %v", tt.chatType, got, tt.want)
}
})
}
}
func TestIsTgAdmin(t *testing.T) {
tests := []struct {
status string
want bool
}{
{"creator", true},
{"administrator", true},
{"member", false},
{"restricted", false},
{"left", false},
{"kicked", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.status, func(t *testing.T) {
if got := isTgAdmin(tt.status); got != tt.want {
t.Errorf("isTgAdmin(%q) = %v, want %v", tt.status, got, tt.want)
}
})
}
}
func TestIsMaxGroup(t *testing.T) {
tests := []struct {
name string
chatType maxschemes.ChatType
want bool
}{
{"chat", maxschemes.CHAT, true},
{"channel", maxschemes.CHANNEL, true},
{"dialog", maxschemes.DIALOG, false},
{"empty", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isMaxGroup(tt.chatType); got != tt.want {
t.Errorf("isMaxGroup(%q) = %v, want %v", tt.chatType, got, tt.want)
}
})
}
}
func TestIsMaxUserAdmin(t *testing.T) {
admins := []maxschemes.ChatMember{
{UserId: 100, Name: "Owner", IsOwner: true, IsAdmin: true},
{UserId: 200, Name: "Admin", IsAdmin: true},
{UserId: 300, Name: "Bot", IsBot: true, IsAdmin: true},
}
tests := []struct {
name string
userID int64
want bool
}{
{"owner is admin", 100, true},
{"admin is admin", 200, true},
{"bot admin", 300, true},
{"non-admin user", 999, false},
{"zero id", 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isMaxUserAdmin(admins, tt.userID); got != tt.want {
t.Errorf("isMaxUserAdmin(admins, %d) = %v, want %v", tt.userID, got, tt.want)
}
})
}
}
func TestIsMaxUserAdmin_EmptyList(t *testing.T) {
if isMaxUserAdmin(nil, 100) {
t.Error("isMaxUserAdmin(nil, 100) = true, want false")
}
if isMaxUserAdmin([]maxschemes.ChatMember{}, 100) {
t.Error("isMaxUserAdmin([], 100) = true, want false")
}
}