forked from equinixmetal-archive/packngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapikeys_test.go
172 lines (145 loc) · 4.17 KB
/
apikeys_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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package packngo
import (
"testing"
)
func createNTestAPIKeys(n int, projectID string, c *Client, t *testing.T) (idMap map[string]struct{}) {
idMap = make(map[string]struct{})
for i := 0; i < n; i++ {
req := APIKeyCreateRequest{
Description: "PACKNGO_TEST_KEY_DELETE_ME-" + randString8(),
ReadOnly: true,
ProjectID: projectID,
}
key, _, err := c.APIKeys.Create(&req)
if err != nil {
t.Fatalf("errored posting key: %v", err)
}
idMap[key.ID] = struct{}{}
}
return idMap
}
func TestAccAPIKeyListProject(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
nKeys := 10
keyIDs := createNTestAPIKeys(nKeys, projectID, c, t)
if len(keyIDs) != nKeys {
t.Fatalf("Helper function was supposed to create %d keys, created %d", nKeys, len(keyIDs))
}
keyList, _, err := c.APIKeys.ProjectList(projectID, nil)
if err != nil {
t.Fatalf("Error getting list of Project keys %s", err)
}
if len(keyList) != nKeys {
t.Fatalf("Listing should return %d keys, returned %d", nKeys, len(keyList))
}
for kID := range keyIDs {
_, err := c.APIKeys.Delete(kID)
if err != nil {
t.Logf("Failed to delete testing key %s", kID)
}
delete(keyIDs, kID)
}
if len(keyIDs) != 0 {
t.Fatal("Error when sweeping keys after ProjectList test - Seems that not all test keys were deleted")
}
}
func TestAccAPIKeyListUser(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, stopRecord := setup(t)
defer stopRecord()
nKeys := 10
keyIDs := createNTestAPIKeys(nKeys, "", c, t)
if len(keyIDs) != nKeys {
t.Fatalf("Helper function was supposed to create %d keys, created %d", nKeys, len(keyIDs))
}
keyList, _, err := c.APIKeys.UserList(nil)
if err != nil {
t.Fatalf("Error getting list of User keys %s", err)
}
if len(keyList) < nKeys {
t.Fatalf("Listing should return at least %d keys, returned %d", nKeys, len(keyList))
}
for kID := range keyIDs {
_, err := c.APIKeys.Delete(kID)
if err != nil {
t.Logf("Failed to delete testing key %s", kID)
}
delete(keyIDs, kID)
}
if len(keyIDs) != 0 {
t.Fatal("Error when sweeping keys after UserList test - Seems that not all test keys were deleted")
}
}
func TestAccAPIKeyCreateUser(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, stopRecord := setup(t)
defer stopRecord()
req := APIKeyCreateRequest{
Description: "PACKNGO_TEST_KEY_DELETE_ME-" + randString8(),
ReadOnly: true,
}
key, _, err := c.APIKeys.Create(&req)
if err != nil {
t.Fatalf("errored posting key: %v", err)
}
if len(key.User.URL) == 0 {
t.Error("new Key doesn't have User URL set")
}
if len(key.Token) == 0 {
t.Error("new Key doesn't have token set")
}
if key.Description != req.Description {
t.Fatalf("returned key label does not match, want: %v, got: %v", req.Description, key.Description)
}
gotKey, err := c.APIKeys.UserGet(key.ID, nil)
if err != nil {
t.Fatalf("Error getting created User API key: %s", err)
}
if gotKey.Token != key.Token {
t.Fatalf("Strange mismatch in tokens of the same test key")
}
_, err = c.APIKeys.Delete(key.ID)
if err != nil {
t.Fatalf("error deleting key")
}
}
func TestAccAPIKeyCreateProject(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
t.Parallel()
c, projectID, teardown := setupWithProject(t)
defer teardown()
req := APIKeyCreateRequest{
Description: "PACKNGO_TEST_KEY_DELETE_ME-" + randString8(),
ReadOnly: true,
ProjectID: projectID,
}
key, _, err := c.APIKeys.Create(&req)
if err != nil {
t.Fatalf("errored posting key: %v", err)
}
if len(key.Project.URL) == 0 {
t.Error("new Key doesn't have Project URL set")
}
if len(key.Token) == 0 {
t.Error("new Key doesn't have token set")
}
if key.Description != req.Description {
t.Fatalf("returned key label does not match, want: %v, got: %v", req.Description, key.Description)
}
gotKey, err := c.APIKeys.ProjectGet(projectID, key.ID, nil)
if err != nil {
t.Fatalf("Error getting created Project API key: %s", err)
}
if gotKey.Token != key.Token {
t.Fatalf("Strange mismatch in tokens of the same test key")
}
_, err = c.APIKeys.Delete(key.ID)
if err != nil {
t.Fatalf("error deleting key")
}
}