-
Notifications
You must be signed in to change notification settings - Fork 56
/
ssh_key_test.go
137 lines (109 loc) · 3.54 KB
/
ssh_key_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
package govultr
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestSSHKeyServiceHandler_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/ssh-keys", func(writer http.ResponseWriter, request *http.Request) {
response := `{"ssh_key": {"id": "5f05d5a71fe28","date_created": "2020-07-08 14:18:15","name": "api-test-ssh","ssh_key": "ssh-rsa AF+LbfYYw== [email protected]"}}`
fmt.Fprint(writer, response)
})
sshKey := &SSHKeyReq{
Name: "api-test-ssh",
SSHKey: "ssh-rsa AF+LbfYYw== [email protected]",
}
key, _, err := client.SSHKey.Create(ctx, sshKey)
if err != nil {
t.Errorf("SSHKey.Create returned %+v, expected %+v", err, nil)
}
expected := &SSHKey{
ID: "5f05d5a71fe28",
Name: "api-test-ssh",
SSHKey: "ssh-rsa AF+LbfYYw== [email protected]",
DateCreated: "2020-07-08 14:18:15",
}
if !reflect.DeepEqual(key, expected) {
t.Errorf("SSHKey.Create returned %+v, expected %+v", key, expected)
}
}
func TestSSHKeyServiceHandler_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/ssh-keys/abc123", func(writer http.ResponseWriter, request *http.Request) {
response := `{"ssh_key": {"id": "5f05d5a71fe28","date_created": "2020-07-08 14:18:15","name": "api-test-ssh","ssh_key": "ssh-rsa AF+LbfYYw== [email protected]"}}`
fmt.Fprint(writer, response)
})
key, _, err := client.SSHKey.Get(ctx, "abc123")
if err != nil {
t.Errorf("SSHKey.Get returned %+v, expected %+v", err, nil)
}
expected := &SSHKey{
ID: "5f05d5a71fe28",
Name: "api-test-ssh",
SSHKey: "ssh-rsa AF+LbfYYw== [email protected]",
DateCreated: "2020-07-08 14:18:15",
}
if !reflect.DeepEqual(key, expected) {
t.Errorf("SSHKey.Create returned %+v, expected %+v", key, expected)
}
}
func TestSSHKeyServiceHandler_Update(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/ssh-keys/abc123", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
sshKey := &SSHKeyReq{
Name: "foo",
SSHKey: "ssh-rsa CCCCB3NzaC1yc your_username@hostname",
}
err := client.SSHKey.Update(ctx, "abc123", sshKey)
if err != nil {
t.Errorf("SSHKey.Update returned error: %+v", err)
}
}
func TestSSHKeyServiceHandler_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/ssh-keys/abc123", func(writer http.ResponseWriter, request *http.Request) {
fmt.Fprint(writer)
})
err := client.SSHKey.Delete(ctx, "abc123")
if err != nil {
t.Errorf("SSHKey.Delete returned %+v, expected %+v", err, nil)
}
}
func TestSSHKeyServiceHandler_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/ssh-keys", func(writer http.ResponseWriter, request *http.Request) {
response := `{"ssh_keys": [{"id": "5ed139d1890db","date_created": "2020-05-29 16:35:29","name": "api-test-ssh","ssh_key": "ssh-rsa AAAAB3NzaC1ycYYw== [email protected]"}],"meta": {"total": 8,"links": {"next": "","prev": ""}}}`
fmt.Fprint(writer, response)
})
sshKeys, meta, _, err := client.SSHKey.List(ctx, nil)
if err != nil {
t.Errorf("SSHKey.List returned error: %v", err)
}
expectedSSH := []SSHKey{
{
ID: "5ed139d1890db",
Name: "api-test-ssh",
SSHKey: "ssh-rsa AAAAB3NzaC1ycYYw== [email protected]",
DateCreated: "2020-05-29 16:35:29",
},
}
expectedMeta := &Meta{
Total: 8,
Links: &Links{},
}
if !reflect.DeepEqual(sshKeys, expectedSSH) {
t.Errorf("SSHKey.List ssh-keys returned %+v, expected %+v", sshKeys, expectedSSH)
}
if !reflect.DeepEqual(meta, expectedMeta) {
t.Errorf("SSHKey.List meta returned %+v, expected %+v", meta, expectedMeta)
}
}