Skip to content

Commit

Permalink
fix(api): test token-based auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Aug 20, 2024
1 parent 48db007 commit f8ae9fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
10 changes: 8 additions & 2 deletions withny/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func (c *Client) Login(ctx context.Context) (err error) {
if saved.Username != "" {
creds, err = c.LoginWithUserPassword(ctx, saved.Username, saved.Password)
} else if saved.Token != "" {
creds.Token = saved.Token
// Hijack the client token to override authorization header
c.credentials.Token = saved.Token
c.credentials.TokenType = "Bearer"
c.credentials.RefreshToken = saved.RefreshToken
creds, err = c.LoginWithRefreshToken(ctx, saved.RefreshToken)
}
}
Expand All @@ -122,7 +125,10 @@ func (c *Client) Login(ctx context.Context) (err error) {
if saved.Username != "" {
creds, err = c.LoginWithUserPassword(ctx, saved.Username, saved.Password)
} else if saved.Token != "" {
creds.Token = saved.Token
// Hijack the client token to override authorization header
c.credentials.Token = saved.Token
c.credentials.TokenType = "Bearer"
c.credentials.RefreshToken = saved.RefreshToken
creds, err = c.LoginWithRefreshToken(ctx, saved.RefreshToken)
}
default:
Expand Down
39 changes: 32 additions & 7 deletions withny/api/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ import (
"github.com/Darkness4/withny-dl/utils/secret"
"github.com/Darkness4/withny-dl/withny/api"
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/require"
)

func TestLogin(t *testing.T) {
func init() {
log.Logger = log.Logger.With().Caller().Logger()
_ = godotenv.Load(".env")
_ = godotenv.Load(".env.test")
}

func TestClient(t *testing.T) {
// Arrange
jar, err := cookiejar.New(&cookiejar.Options{})
require.NoError(t, err)
Expand Down Expand Up @@ -52,7 +59,7 @@ func TestLogin(t *testing.T) {
)
require.NoError(t, err)
client.SetCredentials(res)
time.Sleep(2 * time.Second)
time.Sleep(5 * time.Second)
res2, err := client.LoginWithRefreshToken(
context.Background(),
res.RefreshToken,
Expand All @@ -77,6 +84,29 @@ func TestLogin(t *testing.T) {
require.Greater(t, time2.Time.Unix(), time.Time.Unix())
})

t.Run("Token-based authentication", func(t *testing.T) {
// Act
res, err := client.LoginWithUserPassword(
context.Background(),
saved.Username, saved.Password,
)
require.NoError(t, err)

time.Sleep(5 * time.Second)

static := secret.Static{
SavedCredentials: api.SavedCredentials{
Token: res.Token,
RefreshToken: res.RefreshToken,
},
}
client := api.NewClient(hclient, &static)
err = client.Login(context.Background())

// Assert
require.NoError(t, err)
})

t.Run("Get user", func(t *testing.T) {
// Act
const fixture = "admin"
Expand Down Expand Up @@ -147,8 +177,3 @@ func TestLogin(t *testing.T) {
require.Greater(t, len(playlists), 0)
})
}

func init() {
_ = godotenv.Load(".env")
_ = godotenv.Load(".env.test")
}

0 comments on commit f8ae9fc

Please sign in to comment.