tgbot_callback_auth_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package tgbot
  2. import (
  3. "testing"
  4. "github.com/mymmrac/telego"
  5. )
  6. func TestAnswerCallbackDeniesPrivilegedActionToNonAdmin(t *testing.T) {
  7. defer func() {
  8. if r := recover(); r != nil {
  9. t.Fatalf("a non-admin callback reached a privileged handler: %v", r)
  10. }
  11. }()
  12. tg := &Tgbot{}
  13. for _, data := range []string{"get_backup", "reset_all_traffics_c", "add_client", "onlines", "inbounds"} {
  14. q := &telego.CallbackQuery{
  15. Data: data,
  16. From: telego.User{ID: 999999},
  17. Message: &telego.Message{Chat: telego.Chat{ID: 1}},
  18. }
  19. tg.answerCallback(q, false)
  20. }
  21. }
  22. func TestIsClientSelfCallback(t *testing.T) {
  23. allowed := []string{"client_traffic", "client_sub_links", "client_qr_links", "client_sub_links alice@x"}
  24. for _, d := range allowed {
  25. if !isClientSelfCallback(d) {
  26. t.Errorf("%q should be a per-user client callback", d)
  27. }
  28. }
  29. denied := []string{"get_backup", "reset_all_traffics_c", "add_client", "onlines", "get_banlogs", "get_usage"}
  30. for _, d := range denied {
  31. if isClientSelfCallback(d) {
  32. t.Errorf("%q is an admin-only callback and must not be treated as per-user", d)
  33. }
  34. }
  35. }