mirror of
https://github.com/sstent/go-garth.git
synced 2026-01-25 16:42:28 +00:00
porting - part 12 done
This commit is contained in:
57
garth/client/auth_test.go
Normal file
57
garth/client/auth_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"garmin-connect/garth/testutils"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"garmin-connect/garth/client"
|
||||
"garmin-connect/garth/errors"
|
||||
)
|
||||
|
||||
func TestClient_Login_Success(t *testing.T) {
|
||||
// Create mock SSO server
|
||||
ssoServer := testutils.MockJSONResponse(http.StatusOK, `{
|
||||
"access_token": "test_token",
|
||||
"token_type": "Bearer",
|
||||
"expires_in": 3600
|
||||
}`)
|
||||
defer ssoServer.Close()
|
||||
|
||||
// Create client with test configuration
|
||||
c, err := client.NewClient("example.com")
|
||||
require.NoError(t, err)
|
||||
c.Domain = ssoServer.URL
|
||||
|
||||
// Perform login
|
||||
err = c.Login("test@example.com", "password")
|
||||
|
||||
// Verify login
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Bearer test_token", c.AuthToken)
|
||||
}
|
||||
|
||||
func TestClient_Login_Failure(t *testing.T) {
|
||||
// Create mock SSO server returning error
|
||||
ssoServer := testutils.MockJSONResponse(http.StatusUnauthorized, `{
|
||||
"error": "invalid_credentials"
|
||||
}`)
|
||||
defer ssoServer.Close()
|
||||
|
||||
// Create client with test configuration
|
||||
c, err := client.NewClient("example.com")
|
||||
require.NoError(t, err)
|
||||
c.Domain = ssoServer.URL
|
||||
|
||||
// Perform login
|
||||
err = c.Login("test@example.com", "wrongpassword")
|
||||
|
||||
// Verify error
|
||||
require.Error(t, err)
|
||||
assert.IsType(t, &errors.AuthenticationError{}, err)
|
||||
assert.Contains(t, err.Error(), "SSO login failed")
|
||||
}
|
||||
40
garth/client/client_test.go
Normal file
40
garth/client/client_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package client_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"garmin-connect/garth/testutils"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"garmin-connect/garth/client"
|
||||
)
|
||||
|
||||
func TestClient_GetUserProfile(t *testing.T) {
|
||||
// Create mock server returning user profile
|
||||
server := testutils.MockJSONResponse(http.StatusOK, `{
|
||||
"userName": "testuser",
|
||||
"displayName": "Test User",
|
||||
"fullName": "Test User",
|
||||
"location": "Test Location"
|
||||
}`)
|
||||
defer server.Close()
|
||||
|
||||
// Create client with test configuration
|
||||
c := &client.Client{
|
||||
Domain: server.URL,
|
||||
HTTPClient: &http.Client{Timeout: 5 * time.Second},
|
||||
AuthToken: "Bearer testtoken",
|
||||
}
|
||||
|
||||
// Get user profile
|
||||
profile, err := c.GetUserProfile()
|
||||
|
||||
// Verify response
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "testuser", profile.UserName)
|
||||
assert.Equal(t, "Test User", profile.DisplayName)
|
||||
}
|
||||
Reference in New Issue
Block a user