diff --git a/provider/telegram.go b/provider/telegram.go index 178c9bd..7b51872 100644 --- a/provider/telegram.go +++ b/provider/telegram.go @@ -192,9 +192,15 @@ func (th *TelegramHandler) LoginHandler(w http.ResponseWriter, r *http.Request) token, err := randToken() if err != nil { rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, err, "failed to generate code") + return } th.requests.Lock() + if th.requests.data == nil { + th.requests.Unlock() + rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, errors.New("run goroutine is not running"), "failed to process login request") + return + } th.requests.data[token] = tgAuthRequest{ expires: time.Now().Add(tgAuthRequestLifetime), } diff --git a/provider/telegram_test.go b/provider/telegram_test.go index efea4a3..19e11e5 100644 --- a/provider/telegram_test.go +++ b/provider/telegram_test.go @@ -12,8 +12,9 @@ import ( "testing" "time" - authtoken "github.com/go-pkgz/auth/token" "github.com/stretchr/testify/assert" + + authtoken "github.com/go-pkgz/auth/token" ) // same across all tests @@ -21,6 +22,28 @@ var botInfoFunc = func(ctx context.Context) (*botInfo, error) { return &botInfo{Username: "my_auth_bot"}, nil } +func TestTgLoginHandlerErrors(t *testing.T) { + tg := TelegramHandler{ + ProviderName: "telegram", + ErrorMsg: "❌ Invalid auth request. Please try clicking link again.", + SuccessMsg: "✅ You have successfully authenticated!", + Telegram: NewTelegramAPI("test", http.DefaultClient), + } + + r := httptest.NewRequest("GET", "/login", nil) + w := httptest.NewRecorder() + tg.LoginHandler(w, r) + assert.Equal(t, 500, w.Code, "request should succeed") + + var resp = struct { + Error string `json:"error"` + }{} + + err := json.Unmarshal(w.Body.Bytes(), &resp) + assert.Nil(t, err) + assert.Equal(t, "failed to process login request", resp.Error) +} + func TestTelegramUnconfirmedRequest(t *testing.T) { m := &TelegramAPIMock{ GetUpdatesFunc: func(ctx context.Context) (*telegramUpdate, error) {