forked from cherryservers/cherrygo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshkeys_test.go
156 lines (120 loc) · 3.3 KB
/
sshkeys_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
package cherrygo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestSSHKey_Get(t *testing.T) {
setup()
defer teardown()
expected := SSHKey{
ID: 1,
Label: "test",
Key: "ssh-rsa AAAAB3NzaC1yc",
Fingerprint: "fb:f0:21:33:e9:26:y3:2e:2e:b4:5c:8a:a6:26:64:ae",
Updated: "2021-04-20 16:40:54",
Created: "2021-04-20 13:40:43",
Href: "/ssh-keys/1",
}
mux.HandleFunc("/v1/ssh-keys/1", func(writer http.ResponseWriter, request *http.Request) {
testMethod(t, request, http.MethodGet)
jsonBytes, _ := json.Marshal(expected)
response := string(jsonBytes)
fmt.Fprint(writer, response)
})
sshKey, _, err := client.SSHKeys.Get(1, nil)
if err != nil {
t.Errorf("SSHKey.List returned %+v", err)
}
if !reflect.DeepEqual(sshKey, expected) {
t.Errorf("SSHKey.List returned %+v, expected %+v", sshKey, expected)
}
}
func TestSSHKey_Create(t *testing.T) {
setup()
defer teardown()
expected := SSHKey{
ID: 1,
Label: "test",
Key: "ssh-rsa AAAAB3NzaC1yc",
Fingerprint: "fb:f0:21:33:e9:26:y3:2e:2e:b4:5c:8a:a6:26:64:ae",
Updated: "2021-04-20 16:40:54",
Created: "2021-04-20 13:40:43",
Href: "/ssh-keys/1",
}
requestBody := map[string]interface{}{
"label": "test",
"key": "ssh-rsa AAAAB3NzaC1yc",
}
mux.HandleFunc("/v1/ssh-keys", func(writer http.ResponseWriter, request *http.Request) {
testMethod(t, request, http.MethodPost)
var v map[string]interface{}
err := json.NewDecoder(request.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
if !reflect.DeepEqual(v, requestBody) {
t.Errorf("Request body\n sent %#v\n expected %#v", v, requestBody)
}
jsonBytes, _ := json.Marshal(expected)
fmt.Fprint(writer, string(jsonBytes))
})
sshCreate := CreateSSHKey{
Label: "test",
Key: "ssh-rsa AAAAB3NzaC1yc",
}
_, _, err := client.SSHKeys.Create(&sshCreate)
if err != nil {
t.Errorf("SSHKey.Create returned %+v", err)
}
}
func TestSSHKey_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/ssh-keys/1", func(writer http.ResponseWriter, request *http.Request) {
testMethod(t, request, http.MethodDelete)
writer.WriteHeader(http.StatusNoContent)
fmt.Fprint(writer)
})
_, _, err := client.SSHKeys.Delete(1)
if err != nil {
t.Errorf("SSHKey.Delete returned %+v", err)
}
}
func TestSSHKey_Update(t *testing.T) {
setup()
defer teardown()
expected := SSHKey{
ID: 1,
Label: "updated label",
}
requestBody := map[string]interface{}{
"label": "updated label",
"key": "ssh-rsa AAAAB3NzaC1ycupdated",
}
mux.HandleFunc("/v1/ssh-keys/1", func(writer http.ResponseWriter, request *http.Request) {
testMethod(t, request, http.MethodPut)
var v map[string]interface{}
err := json.NewDecoder(request.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
if !reflect.DeepEqual(v, requestBody) {
t.Errorf("Request body\n sent %#v\n expected %#v", v, requestBody)
}
jsonBytes, _ := json.Marshal(expected)
fmt.Fprint(writer, string(jsonBytes))
})
label := "updated label"
key := "ssh-rsa AAAAB3NzaC1ycupdated"
sshUpdate := UpdateSSHKey{
Label: &label,
Key: &key,
}
_, _, err := client.SSHKeys.Update(1, &sshUpdate)
if err != nil {
t.Errorf("SSHKey.Update returned %+v", err)
}
}