mirror of
https://github.com/sstent/go-garth.git
synced 2026-01-31 19:42:01 +00:00
sync 2
This commit is contained in:
63
auth_test.go
63
auth_test.go
@@ -19,12 +19,13 @@ func TestRealAuthentication(t *testing.T) {
|
||||
// Get credentials from environment
|
||||
username := os.Getenv("GARMIN_USERNAME")
|
||||
password := os.Getenv("GARMIN_PASSWORD")
|
||||
mfaToken := os.Getenv("GARMIN_MFA_TOKEN") // Optional MFA token
|
||||
if username == "" || password == "" {
|
||||
t.Fatal("GARMIN_USERNAME or GARMIN_PASSWORD not set in .env")
|
||||
}
|
||||
|
||||
// Add timeout to prevent hanging
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Create token storage (using memory storage for this test)
|
||||
@@ -37,25 +38,51 @@ func TestRealAuthentication(t *testing.T) {
|
||||
Timeout: 30 * time.Second,
|
||||
})
|
||||
|
||||
// Perform authentication with timeout context
|
||||
token, err := auth.Login(ctx, username, password, "")
|
||||
if err != nil {
|
||||
t.Fatalf("Authentication failed: %v", err)
|
||||
// Test authentication with and without MFA
|
||||
testCases := []struct {
|
||||
name string
|
||||
mfaToken string
|
||||
}{
|
||||
{"Without MFA", ""},
|
||||
{"With MFA", mfaToken},
|
||||
}
|
||||
|
||||
log.Printf("Authentication successful! Token details:")
|
||||
log.Printf("Access Token: %s", token.OAuth2.AccessToken)
|
||||
log.Printf("Expires: %s", token.OAuth2.Expiry.Format(time.RFC3339))
|
||||
log.Printf("Refresh Token: %s", token.OAuth2.RefreshToken)
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
// Perform authentication
|
||||
token, err := auth.Login(ctx, username, password, tc.mfaToken)
|
||||
if err != nil {
|
||||
if tc.mfaToken != "" && err.Error() == "MFA required but no token provided" {
|
||||
t.Skip("Skipping MFA test since no token provided")
|
||||
}
|
||||
t.Fatalf("Authentication failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify token storage
|
||||
storedToken, err := storage.GetToken()
|
||||
if err != nil {
|
||||
t.Fatalf("Token storage verification failed: %v", err)
|
||||
}
|
||||
if storedToken.OAuth2.AccessToken != token.OAuth2.AccessToken {
|
||||
t.Fatal("Stored token doesn't match authenticated token")
|
||||
}
|
||||
log.Printf("Authentication successful! Token details:")
|
||||
log.Printf("Access Token: %s", token.OAuth2Token.AccessToken)
|
||||
log.Printf("Expires At: %d", token.OAuth2Token.ExpiresAt)
|
||||
log.Printf("Refresh Token: %s", token.OAuth2Token.RefreshToken)
|
||||
|
||||
log.Println("Token storage verification successful")
|
||||
// Verify token storage
|
||||
storedToken, err := storage.GetToken()
|
||||
if err != nil {
|
||||
t.Fatalf("Token storage verification failed: %v", err)
|
||||
}
|
||||
if storedToken.OAuth2Token.AccessToken != token.OAuth2Token.AccessToken {
|
||||
t.Fatal("Stored token doesn't match authenticated token")
|
||||
}
|
||||
|
||||
log.Println("Token storage verification successful")
|
||||
|
||||
// Test token refresh
|
||||
newToken, err := auth.RefreshToken(ctx, token.OAuth2Token.RefreshToken)
|
||||
if err != nil {
|
||||
t.Fatalf("Token refresh failed: %v", err)
|
||||
}
|
||||
if newToken.OAuth2Token.AccessToken == token.OAuth2Token.AccessToken {
|
||||
t.Fatal("Refreshed token should be different from original")
|
||||
}
|
||||
log.Println("Token refresh successful")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user