Files
go-garth/auth_test.go
2025-09-05 06:38:19 -07:00

62 lines
1.6 KiB
Go

package garth
import (
"context"
"log"
"os"
"testing"
"time"
"github.com/joho/godotenv"
)
func TestRealAuthentication(t *testing.T) {
// Load environment variables from .env file
if err := godotenv.Load(); err != nil {
t.Fatalf("Error loading .env file: %v", err)
}
// Get credentials from environment
username := os.Getenv("GARMIN_USERNAME")
password := os.Getenv("GARMIN_PASSWORD")
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)
defer cancel()
// Create token storage (using memory storage for this test)
storage := NewMemoryStorage()
// Create authenticator
auth := NewAuthenticator(ClientOptions{
Storage: storage,
TokenURL: "https://connectapi.garmin.com/oauth-service/oauth/token",
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)
}
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)
// 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.Println("Token storage verification successful")
}