Skip to content

Commit

Permalink
Support for bearer token and first test
Browse files Browse the repository at this point in the history
  • Loading branch information
paketeserrano committed Jan 31, 2025
1 parent 0c9cb07 commit 20676f0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions upcloud/service/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/client"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/dnaeon/go-vcr/recorder"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -70,6 +71,50 @@ func TestToken(t *testing.T) {
})
}

func TestClientWithToken(t *testing.T) {
expires := time.Date(2025, 12, 1, 0, 0, 0, 0, time.UTC)
tokenRequest := request.CreateTokenRequest{
Name: "my_1st_token",
ExpiresAt: expires,
AllowedIPRanges: []string{"0.0.0.0/0", "::/0"},
CanCreateSubTokens: true,
}

// Create client that retries the initial token
user, password := getCredentials()
clt := client.New(user, password)
svc := New(clt)

// Get the initial token
token, err := svc.CreateToken(context.Background(), &tokenRequest)
require.NoError(t, err)

// Create a new client with the initial token
authCfg := client.WithBearerAuth(token.APIToken)
cltWithToken := client.New("", "", authCfg)

// Create a new service with the client with the initial token
svcWithToken := New(cltWithToken)

account, err := svcWithToken.GetAccount(context.Background())
require.NoError(t, err)

// Print account details
t.Logf("Account: %+v", account)

if account.UserName != user {
t.Errorf("TestGetAccount expected %s, got %s", user, account.UserName)
}

assert.NotZero(t, account.ResourceLimits.Cores)
assert.NotZero(t, account.ResourceLimits.Memory)
assert.NotZero(t, account.ResourceLimits.Networks)
assert.NotZero(t, account.ResourceLimits.PublicIPv6)
assert.NotZero(t, account.ResourceLimits.StorageHDD)
assert.NotZero(t, account.ResourceLimits.StorageSSD)

Check failure on line 115 in upcloud/service/token_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not properly formatted (gofumpt)
}

Check failure on line 116 in upcloud/service/token_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary trailing newline (whitespace)

func cleanupTokenFunc(t *testing.T, svc *Service, id string) func() {
return func() {
if err := svc.DeleteToken(context.Background(), &request.DeleteTokenRequest{ID: id}); err != nil {
Expand Down

0 comments on commit 20676f0

Please sign in to comment.