-
Notifications
You must be signed in to change notification settings - Fork 26
/
ssh_key_test.go
76 lines (66 loc) · 2.41 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
package civogo
import (
"reflect"
"testing"
)
func TestNewSSHKey(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/sshkeys": `{
"result": "success",
"id": "730c960f-a51f-44e5-9c21-bd135d015d12"
}`,
})
defer server.Close()
got, err := client.NewSSHKey("test", "730c960f-a51f-44e5-9c21-bd135d015d12")
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}
expected := &SimpleResponse{Result: "success", ID: "730c960f-a51f-44e5-9c21-bd135d015d12"}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}
func TestListSSHKeys(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/sshkeys": `[
{"id": "12345", "name": "RSA Key", "fingerprint": "SHA256:SS4+2d7Zl1Pt5Bc9af9NubyA0yI+fdopOUlEhUoEna0", "public_key": "ssh-rsa AAAA", "created_at": "2018-01-01T00:00:00Z"},
{"id": "33567", "name": "RSA Key", "fingerprint": "SHA256:SS4+87asdf795Bc9af9NubyA0yI+fdopOUlEhUoEna0", "public_key": "ssh-rsa AABB", "created_at": "2018-01-01T00:00:00Z"}]`,
})
defer server.Close()
got, err := client.ListSSHKeys()
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}
if got[0].ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got[0].ID)
}
if got[0].PublicKey != "ssh-rsa AAAA" {
t.Errorf("Expected %s, got %s", "ssh-rsa AAAA", got[0].PublicKey)
}
}
func TestFindSSHKey(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/sshkeys": `[
{"id": "12345", "name": "RSA Key", "fingerprint": "SHA256:SS4+2d7Zl1Pt5Bc9af9NubyA0yI+fdopOUlEhUoEna0" },
{"id": "233567", "name": "Test", "fingerprint": "SHA256:SS4+87asdf795Bc9af9NubyA0yI+fdopOUlEhUoEna0" }]`,
})
defer server.Close()
got, _ := client.FindSSHKey("34")
if got.ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
got, _ = client.FindSSHKey("RSA")
if got.ID != "12345" {
t.Errorf("Expected %s, got %s", "12345", got.ID)
}
_, err := client.FindSSHKey("23")
if err.Error() != "MultipleMatchesError: unable to find 23 because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find 23 because there were multiple matches", err.Error())
}
_, err = client.FindSSHKey("missing")
if err.Error() != "ZeroMatchesError: unable to find missing, zero matches" {
t.Errorf("Expected %s, got %s", "unable to find missing, zero matches", err.Error())
}
}