Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Account struct {
EUUID string `json:"euuid"`
BillingSource string `json:"billing_source"`
Capabilities []string `json:"capabilities"`
ActiveSince *time.Time `json:"-"`
ActiveSince *time.Time `json:"active_since"`
}

// UnmarshalJSON implements the json.Unmarshaler interface
Expand Down
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+v
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
Expand All @@ -48,7 +46,9 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
Expand Down
1 change: 1 addition & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down
163 changes: 83 additions & 80 deletions test/unit/account_child_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,105 @@ package unit

import (
"context"
"fmt"
"reflect"
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
"github.com/linode/linodego/internal/testutil"
"github.com/stretchr/testify/require"
"time"
)

var testChildAccount = linodego.ChildAccount{
Address1: "123 Main Street",
Address2: "Suite A",
Balance: 200,
BalanceUninvoiced: 145,
Capabilities: []string{
"Linodes",
"NodeBalancers",
"Block Storage",
"Object Storage",
},
City: "Philadelphia",
Company: "Linode LLC",
Country: "US",
CreditCard: &linodego.CreditCard{
Expiry: "11/2022",
LastFour: "1111",
},
Email: "john.smith@linode.com",
EUUID: "E1AF5EEC-526F-487D-B317EBEB34C87D71",
FirstName: "John",
LastName: "Smith",
Phone: "215-555-1212",
State: "PA",
TaxID: "ATU99999999",
Zip: "19102-1234",
}
var fixtures *TestFixtures

func TestAccountChild_list(t *testing.T) {
client := createMockClient(t)

desiredResponse := map[string]any{
"page": 1,
"pages": 1,
"results": 1,
"data": []linodego.ChildAccount{
testChildAccount,
},
}
func TestMain(m *testing.M) {
fixtures = NewTestFixtures()

httpmock.RegisterRegexpResponder(
"GET",
testutil.MockRequestURL("/account/child-accounts"),
httpmock.NewJsonResponderOrPanic(200, &desiredResponse),
)
code := m.Run()

accounts, err := client.ListChildAccounts(context.Background(), nil)
require.NoError(t, err)
os.Exit(code)
}

require.True(t, reflect.DeepEqual(accounts, desiredResponse["data"]))
func TestAccountChild_list(t *testing.T) {
fixtureData, err := fixtures.GetFixture("account_child_list")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("account/child-accounts", fixtureData)

accounts, err := base.Client.ListChildAccounts(context.Background(), nil)
assert.NoError(t, err)

assert.Equal(t, 1, len(accounts), "Expected one child account")

if len(accounts) > 0 {
childAccount := accounts[0]
assert.Equal(t, "123 Main Street", childAccount.Address1)
assert.Equal(t, "Suite A", childAccount.Address2)
assert.Equal(t, float32(200), childAccount.Balance)
assert.Equal(t, float32(145), childAccount.BalanceUninvoiced)
assert.Equal(t, "San Diego", childAccount.City)
assert.Equal(t, "john.smith@linode.com", childAccount.Email)
assert.Equal(t, "858-555-1212", childAccount.Phone)
assert.Equal(t, "CA", childAccount.State)
assert.Equal(t, "92111-1234", childAccount.Zip)
}
}

func TestAccountChild_get(t *testing.T) {
client := createMockClient(t)

httpmock.RegisterRegexpResponder(
"GET",
testutil.MockRequestURL(fmt.Sprintf("/account/child-accounts/%s", testChildAccount.EUUID)),
httpmock.NewJsonResponderOrPanic(200, &testChildAccount),
)

account, err := client.GetChildAccount(context.Background(), testChildAccount.EUUID)
require.NoError(t, err)
fixtureData, err := fixtures.GetFixture("account_child_get")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("account/child-accounts/A1BC2DEF-34GH-567I-J890KLMN12O34P56", fixtureData)

account, err := base.Client.GetChildAccount(context.Background(), "A1BC2DEF-34GH-567I-J890KLMN12O34P56")
assert.NoError(t, err)

assert.Equal(t, "John", account.FirstName)
assert.Equal(t, "Smith", account.LastName)
assert.Equal(t, "123 Main Street", account.Address1)
assert.Equal(t, "Suite A", account.Address2)
assert.Equal(t, float32(200), account.Balance)
assert.Equal(t, float32(145), account.BalanceUninvoiced)
assert.Equal(t, "San Diego", account.City)
assert.Equal(t, "john.smith@linode.com", account.Email)
assert.Equal(t, "858-555-1212", account.Phone)
assert.Equal(t, "CA", account.State)
assert.Equal(t, "92111-1234", account.Zip)
assert.Equal(t, "US", account.Country)
assert.Equal(t, "external", account.BillingSource)
assert.Equal(t, []string{"Linodes", "NodeBalancers", "Block Storage", "Object Storage"}, account.Capabilities)

if account.CreditCard != nil {
assert.Equal(t, "11/2024", account.CreditCard.Expiry)
assert.Equal(t, "1111", account.CreditCard.LastFour)
}

require.True(t, reflect.DeepEqual(*account, testChildAccount))
assert.Equal(t, "A1BC2DEF-34GH-567I-J890KLMN12O34P56", account.EUUID)
}

func TestAccountChild_createToken(t *testing.T) {
client := createMockClient(t)
fixtureData, err := fixtures.GetFixture("account_child_create_token")
assert.NoError(t, err)

desiredResponse := linodego.ChildAccountToken{
ID: 123,
Scopes: "*",
Label: "child_token",
}
var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

httpmock.RegisterRegexpResponder(
"POST",
testutil.MockRequestURL(
fmt.Sprintf("/account/child-accounts/%s/token", testChildAccount.EUUID),
),
httpmock.NewJsonResponderOrPanic(200, &desiredResponse),
)
base.MockPost("account/child-accounts/A1BC2DEF-34GH-567I-J890KLMN12O34P56/token", fixtureData)

token, err := client.CreateChildAccountToken(context.Background(), testChildAccount.EUUID)
require.NoError(t, err)
token, err := base.Client.CreateChildAccountToken(context.Background(), "A1BC2DEF-34GH-567I-J890KLMN12O34P56")
assert.NoError(t, err)

require.True(t, reflect.DeepEqual(*token, desiredResponse))
// Assertions for the created token data
assert.Equal(t, 918, token.ID)
assert.Equal(t, "parent1_1234_2024-05-01T00:01:01", token.Label)
assert.Equal(t, "2024-05-01T00:01:01Z", token.Created.Format(time.RFC3339))
assert.Equal(t, "2024-05-01T00:16:01Z", token.Expiry.Format(time.RFC3339))
assert.Equal(t, "*", token.Scopes)
assert.Equal(t, "abcdefghijklmnop", token.Token)
}
46 changes: 18 additions & 28 deletions test/unit/account_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@ package unit

import (
"context"
"reflect"
"github.com/stretchr/testify/assert"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
)

func TestAccount_getTransfer(t *testing.T) {
client := createMockClient(t)
fixtureData, err := fixtures.GetFixture("account_transfer_get")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

desiredResponse := linodego.AccountTransfer{
Billable: 123,
Quota: 456,
Used: 789,
RegionTransfers: []linodego.AccountTransferRegion{
{
ID: "us-southeast",
Billable: 987,
Quota: 654,
Used: 3211,
},
},
}
base.MockGet("account/transfer", fixtureData)

httpmock.RegisterRegexpResponder("GET", mockRequestURL(t, "/account/transfer"),
httpmock.NewJsonResponderOrPanic(200, &desiredResponse))
transferInfo, err := base.Client.GetAccountTransfer(context.Background())
assert.NoError(t, err)

questions, err := client.GetAccountTransfer(context.Background())
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 0, transferInfo.Billable)
assert.Equal(t, 9141, transferInfo.Quota)
assert.Equal(t, 2, transferInfo.Used)

if !reflect.DeepEqual(*questions, desiredResponse) {
t.Fatalf("actual response does not equal desired response: %s", cmp.Diff(questions, desiredResponse))
}
assert.Len(t, transferInfo.RegionTransfers, 1)
assert.Equal(t, "us-east", transferInfo.RegionTransfers[0].ID)
assert.Equal(t, 0, transferInfo.RegionTransfers[0].Billable)
assert.Equal(t, 5010, transferInfo.RegionTransfers[0].Quota)
assert.Equal(t, 1, transferInfo.RegionTransfers[0].Used)
}
58 changes: 58 additions & 0 deletions test/unit/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package unit

import (
"github.com/jarcoal/httpmock"
"github.com/linode/linodego"
"github.com/linode/linodego/internal/testutil"
"github.com/stretchr/testify/mock"
"net/http"
"testing"
)

type MockResponse struct {
StatusCode int
Body interface{}
}

// ClientBaseCase provides a base for unit tests
type ClientBaseCase struct {
Client *linodego.Client
Mock *mock.Mock
BaseURL string // Base URL up to /v4
}

// SetUp initializes the Linode client using the mock HTTP client
func (c *ClientBaseCase) SetUp(t *testing.T) {
c.Mock = &mock.Mock{}
c.Client = testutil.CreateMockClient(t, linodego.NewClient)
c.BaseURL = "https://api.linode.com/v4/"
}

func (c *ClientBaseCase) TearDown(t *testing.T) {
httpmock.DeactivateAndReset() // Reset HTTP mock after tests
c.Mock.AssertExpectations(t)
}

// MockGet mocks a GET request to the client
func (c *ClientBaseCase) MockGet(path string, response interface{}) {
fullURL := c.BaseURL + path
httpmock.RegisterResponder("GET", fullURL, httpmock.NewJsonResponderOrPanic(http.StatusOK, response))
}

// MockPost mocks a POST request for a given path with the provided response body
func (c *ClientBaseCase) MockPost(path string, response interface{}) {
fullURL := c.BaseURL + path
httpmock.RegisterResponder("POST", fullURL, httpmock.NewJsonResponderOrPanic(http.StatusOK, response))
}

// MockPut mocks a PUT request for a given path with the provided response body
func (c *ClientBaseCase) MockPut(path string, response interface{}) {
fullURL := c.BaseURL + path
httpmock.RegisterResponder("PUT", fullURL, httpmock.NewJsonResponderOrPanic(http.StatusOK, response))
}

// MockDelete mocks a DELETE request for a given path with the provided response body
func (c *ClientBaseCase) MockDelete(path string, response interface{}) {
fullURL := c.BaseURL + path
httpmock.RegisterResponder("DELETE", fullURL, httpmock.NewJsonResponderOrPanic(http.StatusOK, response))
}
Loading