Skip to content

Commit 8591d05

Browse files
committed
chore: minor adjustments
1 parent d7e1366 commit 8591d05

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

telegram/auth/qrlogin/errors.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package qrlogin
22

33
import (
4-
"errors"
54
"fmt"
65

76
"github.com/gotd/td/tg"
87
)
98

10-
// ErrAlreadyAuthenticated indicates that user is already authenticated
11-
// and no new QR token is needed.
12-
var ErrAlreadyAuthenticated = errors.New("already authenticated")
13-
149
// MigrationNeededError reports that Telegram requested DC migration to continue login.
1510
type MigrationNeededError struct {
1611
MigrateTo *tg.AuthLoginTokenMigrateTo

telegram/auth/qrlogin/qrlogin.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ func (q QR) Auth(
159159
}
160160

161161
// If token is empty, it means AuthLoginTokenSuccess was returned
162-
// and authentication is already complete, but we should wait for the signal
163-
if token.String() == "" {
162+
// and authentication is already complete, but we should wait for the signal.
163+
if token.Empty() {
164164
select {
165165
case <-ctx.Done():
166166
return nil, ctx.Err()
@@ -186,9 +186,9 @@ func (q QR) Auth(
186186
return nil, err
187187
}
188188

189-
// If empty token, it means AuthLoginTokenSuccess was returned
190-
if t.String() == "" {
191-
// QR was scanned and accepted, break out of loop
189+
if t.Empty() {
190+
// If empty token, it means AuthLoginTokenSuccess was returned.
191+
// QR was scanned and accepted, break to import.
192192
break
193193
}
194194

telegram/auth/qrlogin/qrlogin_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestMigrationNeededError_Error(t *testing.T) {
197197
a.Equal("migration to 2 needed", err.Error())
198198
}
199199

200-
// Mock dispatcher that implements the required interface
200+
// Mock dispatcher that implements the required interface.
201201
type mockDispatcher struct {
202202
handler tg.LoginTokenHandler
203203
}
@@ -212,32 +212,32 @@ func TestOnLoginToken(t *testing.T) {
212212
dispatcher := &mockDispatcher{}
213213
loggedIn := OnLoginToken(dispatcher)
214214

215-
// Verify that handler was set
215+
// Verify that handler was set.
216216
a.NotNil(dispatcher.handler)
217217

218218
// Test the handler
219219
ctx := context.Background()
220220
entities := tg.Entities{}
221221
update := &tg.UpdateLoginToken{}
222222

223-
// First call should send to channel
223+
// First call should send to channel.
224224
done := make(chan error, 1)
225225
go func() {
226226
done <- dispatcher.handler(ctx, entities, update)
227227
}()
228228

229-
// Should receive signal
229+
// Should receive signal.
230230
select {
231231
case <-loggedIn:
232232
// Good
233-
case <-time.After(time.Second):
233+
case <-time.After(time.Second * 5):
234234
t.Fatal("should receive signal")
235235
}
236236

237-
// Handler should return nil
237+
// Handler should return nil.
238238
a.NoError(<-done)
239239

240-
// Second call when channel is full should not block
240+
// Second call when channel is full should not block.
241241
err := dispatcher.handler(ctx, entities, update)
242242
a.NoError(err)
243243
}
@@ -246,12 +246,12 @@ func TestToken_Image(t *testing.T) {
246246
a := require.New(t)
247247
token := NewToken([]byte("test_token"), int(time.Now().Unix()))
248248

249-
// Test with valid QR level
249+
// Test with valid QR level.
250250
img, err := token.Image(qr.L)
251251
a.NoError(err)
252252
a.NotNil(img)
253253

254-
// Test with different QR levels
254+
// Test with different QR levels.
255255
levels := []qr.Level{qr.L, qr.M, qr.Q, qr.H}
256256

257257
for _, level := range levels {
@@ -265,7 +265,7 @@ func TestQR_Import_WithMigration(t *testing.T) {
265265
ctx := context.Background()
266266
a := require.New(t)
267267

268-
// Test with migration function
268+
// Test with migration function.
269269
migrateCalled := false
270270
migrate := func(ctx context.Context, dcID int) error {
271271
migrateCalled = true
@@ -279,7 +279,7 @@ func TestQR_Import_WithMigration(t *testing.T) {
279279
User: &tg.User{ID: 10},
280280
}
281281

282-
// First call returns migration needed
282+
// First call returns migration needed.
283283
mock.ExpectCall(&tg.AuthExportLoginTokenRequest{
284284
APIID: constant.TestAppID,
285285
APIHash: constant.TestAppHash,
@@ -302,7 +302,7 @@ func TestQR_Import_MigrationError(t *testing.T) {
302302
ctx := context.Background()
303303
a := require.New(t)
304304

305-
// Test with migration function that returns error
305+
// Test with migration function that returns error,
306306
migrate := func(ctx context.Context, dcID int) error {
307307
return testutil.TestError()
308308
}

telegram/auth/qrlogin/token.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (t Token) String() string {
5959
return base64.URLEncoding.EncodeToString(t.token)
6060
}
6161

62+
// Empty reports whether token is empty.
63+
func (t Token) Empty() bool {
64+
return len(t.token) == 0
65+
}
66+
6267
// URL returns login URL.
6368
//
6469
// See https://core.telegram.org/api/qr-login#exporting-a-login-token.

0 commit comments

Comments
 (0)