Skip to content

Commit

Permalink
Prevent runtime panic in case telegram not initialised
Browse files Browse the repository at this point in the history
Fix for #90
  • Loading branch information
paskal committed Aug 6, 2021
1 parent e0d94d7 commit 855e27b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions provider/telegram.go
Expand Up @@ -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),
}
Expand Down
25 changes: 24 additions & 1 deletion provider/telegram_test.go
Expand Up @@ -12,15 +12,38 @@ 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
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) {
Expand Down

0 comments on commit 855e27b

Please sign in to comment.