Skip to content

Commit

Permalink
add unban server test
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Dec 6, 2023
1 parent cb7928f commit 1b9fde2
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions app/events/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package events

import (
"context"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -391,3 +392,94 @@ func TestTelegram_transformEntities(t *testing.T) {
),
)
}

func TestTelegramListener_unbanURL(t *testing.T) {
tests := []struct {
name string
url string
secret string
want string
}{
{"empty", "", "", "?user=123&token=d68b50c4f0747630c33bc736bb3087b4c22f19dc645ec63b3bf90760c553e1ae"},
{"test1", "http://localhost/unban", "secret",
"http://localhost/unban?user=123&token=71199ea8c011a49df546451e456ad10b0016566a53c4861bf849ec6b2ad2a0b7"},
{"test2", "http://127.0.0.1:8080/unban", "secret",
"http://127.0.0.1:8080/unban?user=123&token=71199ea8c011a49df546451e456ad10b0016566a53c4861bf849ec6b2ad2a0b7"},
{"test3", "http://127.0.0.1:8080/unban", "secret2",
"http://127.0.0.1:8080/unban?user=123&token=5385a71e8d5b65ea03e3da10175d78028ae59efd58811004e907baf422019b2e"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
listener := TelegramListener{AdminURL: tt.url, AdminSecret: tt.secret}
res := listener.unbanURL(123)
assert.Equal(t, tt.want, res)
})
}

listener := TelegramListener{AdminURL: "http://localhost/unban", AdminSecret: "secret"}
res := listener.unbanURL(123)
assert.Equal(t, "http://localhost/unban?user=123&token=71199ea8c011a49df546451e456ad10b0016566a53c4861bf849ec6b2ad2a0b7", res)
}

func TestTelegramListener_runUnbanServer(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()

mockAPI := &mocks.TbAPIMock{
GetChatFunc: func(config tbapi.ChatInfoConfig) (tbapi.Chat, error) {
return tbapi.Chat{ID: 123}, nil
},
RequestFunc: func(c tbapi.Chattable) (*tbapi.APIResponse, error) {
return &tbapi.APIResponse{}, nil
},
}

mockBot := &mocks.BotMock{}

listener := TelegramListener{
TbAPI: mockAPI,
Bot: mockBot,
AdminListenAddr: ":9900",
AdminURL: "http://localhost:9090",
AdminSecret: "secret",
chatID: 10,
}

done := make(chan struct{})
go func() {
listener.runUnbanServer(ctx)
close(done)
}()

time.Sleep(100 * time.Millisecond) // wait for server to start

t.Run("unban forbidden, wrong token", func(t *testing.T) {
mockAPI.ResetCalls()
req, err := http.NewRequest("GET", "http://localhost:9900/unban?user=123&token=ssss", http.NoBody)
require.NoError(t, err)
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusForbidden, resp.StatusCode)
assert.Equal(t, 0, len(mockAPI.RequestCalls()))
})

t.Run("unban allowed, matched token", func(t *testing.T) {
mockAPI.ResetCalls()
req, err := http.NewRequest("GET",
"http://localhost:9900/unban?user=123&token=71199ea8c011a49df546451e456ad10b0016566a53c4861bf849ec6b2ad2a0b7", http.NoBody)
require.NoError(t, err)
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, 1, len(mockAPI.RequestCalls()))
assert.Equal(t, int64(10), mockAPI.RequestCalls()[0].C.(tbapi.UnbanChatMemberConfig).ChatID)
assert.Equal(t, int64(123), mockAPI.RequestCalls()[0].C.(tbapi.UnbanChatMemberConfig).UserID)
})

<-done
}

0 comments on commit 1b9fde2

Please sign in to comment.