-
Notifications
You must be signed in to change notification settings - Fork 3
/
account_test.go
101 lines (86 loc) · 2.78 KB
/
account_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package scalr
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAccountRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
t.Run("account exists", func(t *testing.T) {
account, err := client.Accounts.Read(ctx, defaultAccountID)
require.NoError(t, err)
assert.Equal(t, defaultAccountID, account.ID)
assert.Equal(t, defaultAccountName, account.Name)
assert.Equal(t, []string{}, account.AllowedIPs)
})
t.Run("account does not exist", func(t *testing.T) {
var accId = "notexisting"
_, err := client.Accounts.Read(ctx, accId)
assert.Equal(
t,
fmt.Sprintf("Account with ID '%s' not found or user unauthorized.", accId),
err.Error(),
)
})
t.Run("with invalid acc ID", func(t *testing.T) {
r, err := client.Accounts.Read(ctx, badIdentifier)
assert.Nil(t, r)
assert.EqualError(t, err, "invalid value for account ID")
})
}
func TestAccountUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
defer func() {
options := AccountUpdateOptions{
AllowedIPs: &[]string{},
}
if _, err := client.Accounts.Update(ctx, defaultAccountID, options); err != nil {
t.Errorf("Error resetting allowed ips for account! "+
"The full error is shown below.\n\n"+
"Account: %s\nError: %s", defaultAccountID, err)
}
}()
t.Run("valid allowed ips", func(t *testing.T) {
options := AccountUpdateOptions{
AllowedIPs: &[]string{"0.0.0.0/0", "192.168.0.0/24"},
}
account, err := client.Accounts.Update(ctx, defaultAccountID, options)
require.NoError(t, err)
for i, ip := range account.AllowedIPs {
assert.Equal(t, (*options.AllowedIPs)[i], ip)
}
account, err = client.Accounts.Read(ctx, defaultAccountID)
require.NoError(t, err)
for i, ip := range account.AllowedIPs {
assert.Equal(t, (*options.AllowedIPs)[i], ip)
}
})
t.Run("invalid allowed ips", func(t *testing.T) {
options := AccountUpdateOptions{
AllowedIPs: &[]string{"127.0.00"},
}
account, err := client.Accounts.Update(ctx, defaultAccountID, options)
assert.Nil(t, account)
assert.EqualError(t, err, "Invalid Attribute\n\nvalue is not a valid IPv4 network")
})
t.Run("invalid allowed ips ipv6", func(t *testing.T) {
options := AccountUpdateOptions{
AllowedIPs: &[]string{"FE80:CD00:0000:0CDE:1257:0000:211E:729C"},
}
account, err := client.Accounts.Update(ctx, defaultAccountID, options)
assert.Nil(t, account)
assert.EqualError(t, err, "Invalid Attribute\n\nvalue is not a valid IPv4 network")
})
t.Run("reset allowed ips", func(t *testing.T) {
options := AccountUpdateOptions{
AllowedIPs: &[]string{},
}
account, err := client.Accounts.Update(ctx, defaultAccountID, options)
require.NoError(t, err)
assert.Equal(t, []string{}, account.AllowedIPs)
})
}