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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
package integration
import (
"context"
"testing"
"time"
. "github.com/linode/linodego"
)
func TestToken_GetMissing(t *testing.T) {
client, teardown := createTestClient(t, "fixtures/TestToken_GetMissing")
defer teardown()
doesNotExist := 123
i, err := client.GetToken(context.Background(), doesNotExist)
if err == nil {
t.Errorf("should have received an error requesting a missing token, got %v", i)
}
e, ok := err.(*Error)
if !ok {
t.Errorf("should have received an Error requesting a missing token, got %v", e)
}
if e.Code != 404 {
t.Errorf("should have received a 404 Code requesting a missing token, got %v", e.Code)
}
}
func TestToken_GetFound(t *testing.T) {
tokenTTLSeconds := 120
ttl := time.Now().UTC().Add(time.Second * time.Duration(tokenTTLSeconds))
client, token, teardown, err := setupProfileToken(t, "fixtures/TestToken_GetFound", &ttl)
defer teardown()
if err != nil {
t.Errorf("Error creating test token: %s", err)
}
i, err := client.GetToken(context.Background(), token.ID)
if err != nil {
t.Errorf("Error getting token, expected struct, got %v and error %v", i, err)
}
if i.ID != token.ID {
t.Errorf("Expected a specific token, but got a different one %v", i)
}
assertDateSet(t, i.Created)
updateOpts := i.GetUpdateOptions()
if updateOpts.Label != i.Label {
t.Errorf("Expected matching Label from GetUpdateOptions, got: %v", updateOpts)
}
createOpts := i.GetCreateOptions()
if createOpts.Expiry == nil {
t.Errorf("Expected non-nil Expiry from GetCreateOptions, got: %v", createOpts)
}
}
func TestToken_GetNoExpiry(t *testing.T) {
client, token, teardown, err := setupProfileToken(t, "fixtures/TestToken_GetNoExpiry", nil)
defer teardown()
if err != nil {
t.Errorf("Error creating test token: %s", err)
}
i, err := client.GetToken(context.Background(), token.ID)
if err != nil {
t.Errorf("Error getting token, expected struct, got %v and error %v", i, err)
}
if i.ID != token.ID {
t.Errorf("Expected a specific token, but got a different one %v", i)
}
createOpts := i.GetCreateOptions()
if createOpts.Expiry != nil && createOpts.Expiry.Year() != 2999 {
t.Errorf("Expected \"never\" expiring timestamp from GetCreateOptions, got: %v", createOpts)
}
}
func TestTokens_Update(t *testing.T) {
tokenTTLSeconds := 120
ttl := time.Now().UTC().Add(time.Second * time.Duration(tokenTTLSeconds))
client, token, teardown, err := setupProfileToken(t, "fixtures/TestTokens_Update", &ttl)
defer teardown()
if err != nil {
t.Errorf("Error creating test token: %s", err)
}
createOpts := token.GetCreateOptions()
if createOpts.Expiry == nil {
t.Errorf("Expected non-nil Expiry from GetCreateOptions, got: %v", createOpts)
}
updateOpts := token.GetUpdateOptions()
if updateOpts.Label != token.Label {
t.Errorf("Expected matching Label from GetUpdateOptions, got: %v", updateOpts)
}
updateOpts.Label = updateOpts.Label + "_renamed"
i, err := client.UpdateToken(context.Background(), token.ID, updateOpts)
if err != nil {
t.Errorf("Error updating token: %s", err)
}
if i.Label != updateOpts.Label {
t.Errorf("Expected token label to be changed, but found %v", i)
}
}
func TestTokens_List(t *testing.T) {
tokenTTLSeconds := 120
ttl := time.Now().UTC().Add(time.Second * time.Duration(tokenTTLSeconds))
client, _, teardown, err := setupProfileToken(t, "fixtures/TestTokens_List", &ttl)
defer teardown()
if err != nil {
t.Errorf("Error creating test token: %s", err)
}
i, err := client.ListTokens(context.Background(), nil)
if err != nil {
t.Errorf("Error listing tokens, expected struct, got error %v", err)
}
if len(i) == 0 {
t.Errorf("Expected a list of tokens, but got none %v", i)
}
}
func setupProfileToken(t *testing.T, fixturesYaml string, ttl *time.Time) (*Client, *Token, func(), error) {
t.Helper()
client, fixtureTeardown := createTestClient(t, fixturesYaml)
// This scope must be <= the scope used for testing
limitedTestScope := "linodes:read_only"
createOpts := TokenCreateOptions{
Label: "linodego-token-test",
Expiry: ttl,
Scopes: limitedTestScope,
}
token, err := client.CreateToken(context.Background(), createOpts)
if err != nil {
t.Fatalf("Error creating test Token: %s", err)
}
teardown := func() {
if token != nil {
if err := client.DeleteToken(context.Background(), token.ID); err != nil {
t.Errorf("Error deleting test Token: %s", err)
}
}
fixtureTeardown()
}
return client, token, teardown, err
}
|