mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 23:16:52 +00:00
* fix_communications_authorised_clients * Telegram: Link config to authorised clients list * Telegram: Prevent multiple spam messages from unauthed user * Telegram: Improve command handling for authenticated users Telegram doesn't allow you to easily fetch the user ID of a user unless they have previously sent you a message and is currently waiting to be processed, or if they message you on the fly once the bot is connected. This ensures that the user ID is stored for future usage upon a single successful auth command. It also fixes the offset as the previous code wouldn't be able to process incoming messages once connected and instead only relay them. * Bump docs * default to UTC time in case bot is run on a server with diff time zones * Enhance config for already upgraded configs --------- Co-authored-by: shanhuhai5739 <shanhu5739@gmail.com>
129 lines
3.1 KiB
Go
129 lines
3.1 KiB
Go
package telegram
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/communications/base"
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
)
|
|
|
|
const (
|
|
testErrNotFound = "Not Found"
|
|
)
|
|
|
|
func TestSetup(t *testing.T) {
|
|
t.Parallel()
|
|
cfg := &config.Config{Communications: base.CommunicationsConfig{
|
|
TelegramConfig: base.TelegramConfig{
|
|
Name: "Telegram",
|
|
Enabled: false,
|
|
Verbose: false,
|
|
VerificationToken: "testest",
|
|
AuthorisedClients: map[string]int64{"sender": 0},
|
|
},
|
|
}}
|
|
commsCfg := cfg.GetCommunicationsConfig()
|
|
var T Telegram
|
|
T.Setup(&commsCfg)
|
|
if T.Name != "Telegram" || T.Enabled || T.Token != "testest" || T.Verbose || len(T.AuthorisedClients) != 1 {
|
|
t.Error("telegram Setup() error, unexpected setup values",
|
|
T.Name,
|
|
T.Enabled,
|
|
T.Token,
|
|
T.Verbose)
|
|
}
|
|
}
|
|
|
|
func TestConnect(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
if err := T.Connect(); err == nil {
|
|
t.Error("expected error")
|
|
}
|
|
}
|
|
|
|
func TestPushEvent(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
err := T.PushEvent(base.Event{})
|
|
if !errors.Is(err, ErrNotConnected) {
|
|
t.Errorf("expected %s, got %s", ErrNotConnected, err)
|
|
}
|
|
|
|
T.Connected = true
|
|
T.AuthorisedClients = map[string]int64{"sender": 0}
|
|
err = T.PushEvent(base.Event{})
|
|
if err != nil {
|
|
t.Errorf("expected nil, got %s", err)
|
|
}
|
|
|
|
T.AuthorisedClients = map[string]int64{"sender": 1337}
|
|
err = T.PushEvent(base.Event{})
|
|
if err.Error() != testErrNotFound {
|
|
t.Errorf("telegram PushEvent() error, expected 'Not found' got '%s'",
|
|
err)
|
|
}
|
|
}
|
|
|
|
func TestHandleMessages(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
chatID := int64(1337)
|
|
err := T.HandleMessages(cmdHelp, chatID)
|
|
if err.Error() != testErrNotFound {
|
|
t.Errorf("telegram HandleMessages() error, expected 'Not found' got '%s'",
|
|
err)
|
|
}
|
|
err = T.HandleMessages(cmdStart, chatID)
|
|
if err.Error() != testErrNotFound {
|
|
t.Errorf("telegram HandleMessages() error, expected 'Not found' got '%s'",
|
|
err)
|
|
}
|
|
err = T.HandleMessages(cmdStatus, chatID)
|
|
if err.Error() != testErrNotFound {
|
|
t.Errorf("telegram HandleMessages() error, expected 'Not found' got '%s'",
|
|
err)
|
|
}
|
|
err = T.HandleMessages("Not a command", chatID)
|
|
if err.Error() != testErrNotFound {
|
|
t.Errorf("telegram HandleMessages() error, expected 'Not found' got '%s'",
|
|
err)
|
|
}
|
|
}
|
|
|
|
func TestGetUpdates(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
if _, err := T.GetUpdates(); err != nil {
|
|
t.Error("telegram GetUpdates() error", err)
|
|
}
|
|
}
|
|
|
|
func TestTestConnection(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
if err := T.TestConnection(); err.Error() != testErrNotFound {
|
|
t.Errorf("received %s, expected: %s", err, testErrNotFound)
|
|
}
|
|
}
|
|
|
|
func TestSendMessage(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
err := T.SendMessage("Test message", int64(1337))
|
|
if err.Error() != testErrNotFound {
|
|
t.Errorf("telegram SendMessage() error, expected 'Not found' got '%s'",
|
|
err)
|
|
}
|
|
}
|
|
|
|
func TestSendHTTPRequest(t *testing.T) {
|
|
t.Parallel()
|
|
var T Telegram
|
|
err := T.SendHTTPRequest("0.0.0.0", nil, nil)
|
|
if err == nil {
|
|
t.Error("telegram SendHTTPRequest() error")
|
|
}
|
|
}
|