221 lines
5.2 KiB
Go
221 lines
5.2 KiB
Go
package bot
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
storagemodel "github.com/tech/sendico/gateway/tgsettle/storage/model"
|
|
mloggerfactory "github.com/tech/sendico/pkg/mlogger/factory"
|
|
"github.com/tech/sendico/pkg/model"
|
|
)
|
|
|
|
type fakeService struct{}
|
|
|
|
func (fakeService) ExecutionDelay() time.Duration {
|
|
return 30 * time.Second
|
|
}
|
|
|
|
func (fakeService) MaxPerOperationLimit() string {
|
|
return "1000000"
|
|
}
|
|
|
|
func (fakeService) GetActiveRequestForAccount(context.Context, string) (*storagemodel.TreasuryRequest, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (fakeService) CreateRequest(context.Context, CreateRequestInput) (*storagemodel.TreasuryRequest, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (fakeService) ConfirmRequest(context.Context, string, string) (*storagemodel.TreasuryRequest, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (fakeService) CancelRequest(context.Context, string, string) (*storagemodel.TreasuryRequest, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func TestRouterUnauthorizedInAllowedChatSendsAccessDenied(t *testing.T) {
|
|
var sent []string
|
|
router := NewRouter(
|
|
mloggerfactory.NewLogger(false),
|
|
fakeService{},
|
|
func(_ context.Context, _ string, text string) error {
|
|
sent = append(sent, text)
|
|
return nil
|
|
},
|
|
nil,
|
|
[]string{"100"},
|
|
map[string]string{"123": "acct-1"},
|
|
)
|
|
handled := router.HandleUpdate(context.Background(), &model.TelegramWebhookUpdate{
|
|
Message: &model.TelegramMessage{
|
|
ChatID: "100",
|
|
FromUserID: "999",
|
|
Text: "/fund",
|
|
},
|
|
})
|
|
if !handled {
|
|
t.Fatalf("expected update to be handled")
|
|
}
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one message, got %d", len(sent))
|
|
}
|
|
if sent[0] != unauthorizedMessage {
|
|
t.Fatalf("unexpected message: %q", sent[0])
|
|
}
|
|
}
|
|
|
|
func TestRouterUnknownChatIsIgnored(t *testing.T) {
|
|
var sent []string
|
|
router := NewRouter(
|
|
mloggerfactory.NewLogger(false),
|
|
fakeService{},
|
|
func(_ context.Context, _ string, text string) error {
|
|
sent = append(sent, text)
|
|
return nil
|
|
},
|
|
nil,
|
|
[]string{"100"},
|
|
map[string]string{"123": "acct-1"},
|
|
)
|
|
handled := router.HandleUpdate(context.Background(), &model.TelegramWebhookUpdate{
|
|
Message: &model.TelegramMessage{
|
|
ChatID: "999",
|
|
FromUserID: "123",
|
|
Text: "/fund",
|
|
},
|
|
})
|
|
if !handled {
|
|
t.Fatalf("expected update to be handled")
|
|
}
|
|
if len(sent) != 0 {
|
|
t.Fatalf("expected no messages, got %d", len(sent))
|
|
}
|
|
}
|
|
|
|
func TestRouterEmptyAllowedChats_AllowsAnyChatForAuthorizedUser(t *testing.T) {
|
|
var sent []string
|
|
router := NewRouter(
|
|
mloggerfactory.NewLogger(false),
|
|
fakeService{},
|
|
func(_ context.Context, _ string, text string) error {
|
|
sent = append(sent, text)
|
|
return nil
|
|
},
|
|
nil,
|
|
nil,
|
|
map[string]string{"123": "acct-1"},
|
|
)
|
|
handled := router.HandleUpdate(context.Background(), &model.TelegramWebhookUpdate{
|
|
Message: &model.TelegramMessage{
|
|
ChatID: "999",
|
|
FromUserID: "123",
|
|
Text: "/fund",
|
|
},
|
|
})
|
|
if !handled {
|
|
t.Fatalf("expected update to be handled")
|
|
}
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one message, got %d", len(sent))
|
|
}
|
|
if sent[0] != "Enter amount:" {
|
|
t.Fatalf("unexpected message: %q", sent[0])
|
|
}
|
|
}
|
|
|
|
func TestRouterEmptyAllowedChats_UnauthorizedUserGetsDenied(t *testing.T) {
|
|
var sent []string
|
|
router := NewRouter(
|
|
mloggerfactory.NewLogger(false),
|
|
fakeService{},
|
|
func(_ context.Context, _ string, text string) error {
|
|
sent = append(sent, text)
|
|
return nil
|
|
},
|
|
nil,
|
|
nil,
|
|
map[string]string{"123": "acct-1"},
|
|
)
|
|
handled := router.HandleUpdate(context.Background(), &model.TelegramWebhookUpdate{
|
|
Message: &model.TelegramMessage{
|
|
ChatID: "777",
|
|
FromUserID: "999",
|
|
Text: "/fund",
|
|
},
|
|
})
|
|
if !handled {
|
|
t.Fatalf("expected update to be handled")
|
|
}
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one message, got %d", len(sent))
|
|
}
|
|
if sent[0] != unauthorizedMessage {
|
|
t.Fatalf("unexpected message: %q", sent[0])
|
|
}
|
|
}
|
|
|
|
func TestRouterStartAuthorizedShowsWelcome(t *testing.T) {
|
|
var sent []string
|
|
router := NewRouter(
|
|
mloggerfactory.NewLogger(false),
|
|
fakeService{},
|
|
func(_ context.Context, _ string, text string) error {
|
|
sent = append(sent, text)
|
|
return nil
|
|
},
|
|
nil,
|
|
nil,
|
|
map[string]string{"123": "acct-1"},
|
|
)
|
|
handled := router.HandleUpdate(context.Background(), &model.TelegramWebhookUpdate{
|
|
Message: &model.TelegramMessage{
|
|
ChatID: "777",
|
|
FromUserID: "123",
|
|
Text: "/start",
|
|
},
|
|
})
|
|
if !handled {
|
|
t.Fatalf("expected update to be handled")
|
|
}
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one message, got %d", len(sent))
|
|
}
|
|
if sent[0] != welcomeMessage {
|
|
t.Fatalf("unexpected message: %q", sent[0])
|
|
}
|
|
}
|
|
|
|
func TestRouterStartUnauthorizedGetsDenied(t *testing.T) {
|
|
var sent []string
|
|
router := NewRouter(
|
|
mloggerfactory.NewLogger(false),
|
|
fakeService{},
|
|
func(_ context.Context, _ string, text string) error {
|
|
sent = append(sent, text)
|
|
return nil
|
|
},
|
|
nil,
|
|
nil,
|
|
map[string]string{"123": "acct-1"},
|
|
)
|
|
handled := router.HandleUpdate(context.Background(), &model.TelegramWebhookUpdate{
|
|
Message: &model.TelegramMessage{
|
|
ChatID: "777",
|
|
FromUserID: "999",
|
|
Text: "/start",
|
|
},
|
|
})
|
|
if !handled {
|
|
t.Fatalf("expected update to be handled")
|
|
}
|
|
if len(sent) != 1 {
|
|
t.Fatalf("expected one message, got %d", len(sent))
|
|
}
|
|
if sent[0] != unauthorizedMessage {
|
|
t.Fatalf("unexpected message: %q", sent[0])
|
|
}
|
|
}
|