Skip to content

qrlogin: fix Export method to handle all AuthExportLoginToken response types#1571

Merged
ernado merged 9 commits intogotd:mainfrom
fr0ster:main
Nov 6, 2025
Merged

qrlogin: fix Export method to handle all AuthExportLoginToken response types#1571
ernado merged 9 commits intogotd:mainfrom
fr0ster:main

Conversation

@fr0ster
Copy link
Contributor

@fr0ster fr0ster commented Jun 13, 2025

Problem

The Export method in qrlogin package only handled *tg.AuthLoginToken response type from AuthExportLoginToken API call, but the API can return other types like *tg.AuthLoginTokenSuccess and *tg.AuthLoginTokenMigrateTo, which would cause "unexpected type" errors.

Solution

Added proper handling for all possible response types:

  • AuthLoginToken: normal case, returns new token
  • AuthLoginTokenSuccess: token already accepted, returns appropriate error
  • AuthLoginTokenMigrateTo: migration needed, returns MigrationNeededError

Changes

  • Modified Export method to use switch statement instead of simple type assertion
  • Added proper error handling for each response type
  • Maintains backward compatibility

This fix prevents runtime panics when AuthExportLoginToken returns unexpected (but valid) response types.

@codecov
Copy link

codecov bot commented Jun 14, 2025

Codecov Report

❌ Patch coverage is 35.29412% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.79%. Comparing base (273e74f) to head (8b1801e).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
telegram/auth/qrlogin/qrlogin.go 35.29% 9 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1571      +/-   ##
==========================================
+ Coverage   70.44%   70.79%   +0.35%     
==========================================
  Files         443      443              
  Lines       17920    17934      +14     
==========================================
+ Hits        12623    12696      +73     
+ Misses       4359     4298      -61     
- Partials      938      940       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

fr0ster added 5 commits June 15, 2025 18:23
…sponse types

The Export method now properly handles all possible response types from AuthExportLoginToken:
- AuthLoginToken: normal case, returns new token
- AuthLoginTokenSuccess: token already accepted, returns error
- AuthLoginTokenMigrateTo: migration needed, returns MigrationNeededError

Previously only AuthLoginToken was handled, which could cause unexpected type errors.
AuthLoginTokenSuccess indicates successful authentication, not an error.
Return empty token instead of error when login token was already accepted.
- Add tests for MigrationNeededError.Error method (0% -> 100%)
- Add tests for OnLoginToken function (0% -> 100%)
- Add tests for Token.Image method (0% -> 75%)
- Add tests for Import method with migration scenarios (43.5% -> 82.6%)
- Overall coverage improved from 67.0% to 84.6%
@fr0ster
Copy link
Contributor Author

fr0ster commented Jun 16, 2025

📝 Changes Made

✅ Fixed Commit Messages

Fixed all commit messages to follow Conventional Commits standard as required by commitlint:

  • Changed qrlogin: fix ...fix(qrlogin): ...
  • Changed feat(vscode): ...feat(qrlogin): ... (corrected scope)

📊 Significantly Improved Test Coverage

Added comprehensive tests to address codecov feedback:

Overall coverage: 67.0%84.6% (+17.6%)

Method-specific improvements:

  • MigrationNeededError.Error(): 0%100%
  • OnLoginToken(): 0%100%
  • Token.Image(): 0%75%
  • Import(): 43.5%82.6%

🧪 New Tests Added

  • TestMigrationNeededError_Error - Tests error message formatting
  • TestOnLoginToken - Tests login token handler setup and channel communication
  • TestToken_Image - Tests QR code image generation with different quality levels
  • TestQR_Import_WithMigration - Tests Import method with successful migration
  • TestQR_Import_MigrationError - Tests Import method when migration fails

🎯 Coverage Issues Addressed

This PR addresses the codecov report showing 42.10% patch coverage with 11 missing lines. The new tests specifically target the previously uncovered code paths in:

  • Error handling methods
  • QR code generation
  • Migration scenarios
  • Login token event handling

The commitlint check should now pass ✅ and codecov coverage should be significantly improved 📈

fr0ster added 2 commits June 17, 2025 11:15
…eived

When Export returns empty token (AuthLoginTokenSuccess case), Auth method
should wait for the loggedIn signal before calling Import, ensuring proper
synchronization with the login flow and fixing test failures on macOS Go 1.23.
@fr0ster
Copy link
Contributor Author

fr0ster commented Jul 4, 2025

Anybody home?

@ernado
Copy link
Member

ernado commented Jul 6, 2025

@tdakkota can you please take a look?
Won't it break https://github.com/tdakkota/tgauth/blob/master/qr.go?

@ernado ernado requested review from a user and ernado July 6, 2025 18:53

// ErrAlreadyAuthenticated indicates that user is already authenticated
// and no new QR token is needed.
var ErrAlreadyAuthenticated = errors.New("already authenticated")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this error is unused.

Comment on lines +189 to +193
// If empty token, it means AuthLoginTokenSuccess was returned
if t.String() == "" {
// QR was scanned and accepted, break out of loop
break
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This break would break out of select, not out of the for loop.

The logic itself seems fine to me, but comment should be fixed.

case *tg.AuthLoginTokenSuccess:
// Token was already accepted, authentication successful
// Return empty token since no new token is needed
return Token{}, nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ErrAlreadyAuthenticated should be returned here, I guess.

@ernado ernado merged commit d7e1366 into gotd:main Nov 6, 2025
11 of 14 checks passed
@ernado
Copy link
Member

ernado commented Nov 6, 2025

Mergin this to unblock iyear/tdl#1061

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants