-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_sshkey_test.go
116 lines (100 loc) · 3.39 KB
/
rest_sshkey_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
package ne
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/equinix/ne-go/internal/api"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
var testSSHPublicKey = SSHPublicKey{
Name: String("testKey"),
Value: String("keyyyyyyyyyyyyyyyyyyyyyyyyy"),
Type: String("RSA"),
ProjectID: String("68ccfd49-39b1-478e-957a-67c72f719d7a"),
}
func TestGetSSHPublicKeys(t *testing.T) {
//given
resp := make([]api.SSHPublicKey, 0)
if err := readJSONData("./test-fixtures/ne_sshpubkeys_get.json", &resp); err != nil {
assert.Fail(t, "Cannot read test response")
}
testHc := setupMockedClient("GET", fmt.Sprintf("%s/ne/v1/publicKeys", baseURL), 200, resp)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
keys, err := c.GetSSHPublicKeys()
//then
assert.NotNil(t, keys, "Returned list of keys is not nil")
assert.Nil(t, err, "Returned error is nil")
assert.Equal(t, len(resp), len(keys), "Number of keys matches")
for i := range keys {
verifySSHPublicKey(t, resp[i], keys[i])
}
}
func TestGetSSHPublicKey(t *testing.T) {
//given
resp := api.SSHPublicKey{}
if err := readJSONData("./test-fixtures/ne_sshpubkey_get.json", &resp); err != nil {
assert.Fail(t, "Cannot read test response")
}
keyUUID := "keyID"
testHc := setupMockedClient("GET", fmt.Sprintf("%s/ne/v1/publicKeys/%s", baseURL, keyUUID), 200, resp)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
key, err := c.GetSSHPublicKey(keyUUID)
//then
assert.NotNil(t, key, "Returned key is not nil")
assert.Nil(t, err, "Returned error is nil")
verifySSHPublicKey(t, resp, *key)
}
func TestCreateSSHPublicKey(t *testing.T) {
//given
newUUID := "e3e5d6ce-5238-4fea-a454-e4a74a7bd060"
key := testSSHPublicKey
req := api.SSHPublicKey{}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/ne/v1/publicKeys", baseURL),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
resp := httpmock.NewStringResponse(201, "")
resp.Header.Add("Location", "/ne/v1/publicKeys/"+newUUID)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
uuid, err := c.CreateSSHPublicKey(key)
//then
assert.Nil(t, err, "Error is not returned")
assert.Equal(t, newUUID, *uuid, "UUID matches")
verifySSHPublicKey(t, req, key)
}
func TestDeleteSSHPublicKey(t *testing.T) {
//given
keyUUID := "keyID"
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("DELETE", fmt.Sprintf("%s/ne/v1/publicKeys/%s", baseURL, keyUUID),
httpmock.NewStringResponder(204, ""))
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
err := c.DeleteSSHPublicKey(keyUUID)
//then
assert.Nil(t, err, "Error is not returned")
}
func verifySSHPublicKey(t *testing.T, apiKey api.SSHPublicKey, key SSHPublicKey) {
assert.Equal(t, apiKey.UUID, key.UUID, "UUID matches")
assert.Equal(t, apiKey.KeyName, key.Name, "Name matches")
assert.Equal(t, apiKey.KeyValue, key.Value, "Value matches")
assert.Equal(t, apiKey.KeyType, key.Type, "Type matches")
assert.Equal(t, apiKey.ProjectID, key.ProjectID, "ProjectID matches")
}