Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantspace committed May 4, 2024
1 parent e6afecd commit 8ca7495
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
34 changes: 19 additions & 15 deletions backend/app/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,26 @@ func handleMessage(bot *telego.Bot, message telego.Message) {

// process commands
if message.Voice == nil && message.Audio == nil && message.Video == nil && message.VideoNote == nil && message.Document == nil && message.Photo == nil && (message.Text == string(EmptyCommand) || strings.HasPrefix(message.Text, "/")) {
if !isPrivate && !strings.Contains(message.Text, "@"+BOT.Name) {
log.Infof("Ignoring public command w/o @mention in channel: %s", chatIDString)
return
}
chatMember, err := bot.GetChatMember(&telego.GetChatMemberParams{
ChatID: chatID,
UserID: message.From.ID,
})
if err != nil {
log.Errorf("Error getting chat member: %v", err)
return
}
if err == nil && !isPrivate && chatMember.MemberStatus() != telego.MemberStatusCreator && chatMember.MemberStatus() != telego.MemberStatusAdministrator {
log.Infof("Ignoring public command from non-admin in channel: %s", chatIDString)
return
if !isPrivate {
if !strings.Contains(message.Text, "@"+BOT.Name) {
log.Infof("Ignoring public command w/o @mention in channel: %s", chatIDString)
return
}

chatMember, err := bot.GetChatMember(&telego.GetChatMemberParams{
ChatID: chatID,
UserID: message.From.ID,
})
if err != nil {
log.Errorf("Error getting chat member: %v", err)
return
}
if err == nil && chatMember.MemberStatus() != telego.MemberStatusCreator && chatMember.MemberStatus() != telego.MemberStatusAdministrator {
log.Infof("Ignoring public command from non-admin in channel: %s", chatIDString)
return
}
}

AllCommandHandlers.handleCommand(ctx, BOT, &message)
return
}
Expand Down
47 changes: 43 additions & 4 deletions backend/app/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ func setupTestDatadog() {
}
}

func getChatMemberFuncAssertion(t *testing.T, expectedChatID int64, expectedUserID int64) func(bot *telego.Bot, params *telego.GetChatMemberParams) (telego.ChatMember, error) {
return func(bot *telego.Bot, params *telego.GetChatMemberParams) (telego.ChatMember, error) {
if params.ChatID.ID != expectedChatID {
t.Errorf("Expected chat ID %d, got %d", expectedChatID, params.ChatID.ID)
}
if params.UserID != expectedUserID {
t.Errorf("Expected user ID %d, got %d", expectedUserID, params.UserID)
}

return &telego.ChatMemberAdministrator{}, nil
}
}

func getSendMessageFuncAssertion(t *testing.T, expectedRegex string, expectedChatID int64) func(bot *telego.Bot, params *telego.SendMessageParams) (*telego.Message, error) {
return func(bot *telego.Bot, params *telego.SendMessageParams) (*telego.Message, error) {
if params.ChatID.ID != expectedChatID {
Expand Down Expand Up @@ -166,8 +179,11 @@ func TestHandlePrivateStartCommandMessage(t *testing.T) {
func TestHandlePublicStartCommandMessage(t *testing.T) {
// arrange
message := telego.Message{
From: &telego.User{
ID: 234,
},
Chat: telego.Chat{
ID: 123,
ID: -123,
Type: "supergroup",
},
Text: "/start@testbot",
Expand All @@ -176,13 +192,23 @@ func TestHandlePublicStartCommandMessage(t *testing.T) {
sendMessagePatch, err := mpatch.PatchInstanceMethodByName(
reflect.TypeOf(BOT.Bot),
"SendMessage",
getSendMessageFuncAssertion(t, "Hi, I'm a bot powered by AI!", 123),
getSendMessageFuncAssertion(t, "Hi, I'm a bot powered by AI!", -123),
)
if err != nil {
t.Fatal(err)
}
defer sendMessagePatch.Unpatch()

getChatMemberPatch, err := mpatch.PatchInstanceMethodByName(
reflect.TypeOf(BOT.Bot),
"GetChatMember",
getChatMemberFuncAssertion(t, -123, 234),
)
if err != nil {
t.Fatal(err)
}
defer getChatMemberPatch.Unpatch()

// act
handleMessage(BOT.Bot, message)
}
Expand All @@ -204,8 +230,11 @@ func TestHandlePublicStartCommandNoMentionMessage(t *testing.T) {
func TestHandlePublicUnknownCommandMessage(t *testing.T) {
// arrange
message := telego.Message{
From: &telego.User{
ID: 234,
},
Chat: telego.Chat{
ID: 123,
ID: -123,
Type: "supergroup",
},
Text: "/destroy@testbot",
Expand All @@ -214,13 +243,23 @@ func TestHandlePublicUnknownCommandMessage(t *testing.T) {
sendMessagePatch, err := mpatch.PatchInstanceMethodByName(
reflect.TypeOf(BOT.Bot),
"SendMessage",
getSendMessageFuncAssertion(t, "Unknown command", 123),
getSendMessageFuncAssertion(t, "Unknown command", -123),
)
if err != nil {
t.Fatal(err)
}
defer sendMessagePatch.Unpatch()

getChatMemberPatch, err := mpatch.PatchInstanceMethodByName(
reflect.TypeOf(BOT.Bot),
"GetChatMember",
getChatMemberFuncAssertion(t, -123, 234),
)
if err != nil {
t.Fatal(err)
}
defer getChatMemberPatch.Unpatch()

// act
handleMessage(BOT.Bot, message)
}

0 comments on commit 8ca7495

Please sign in to comment.