-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
119 lines (91 loc) · 2.42 KB
/
main_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
package main
import (
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type MockHTTPClient struct {
TestDocument string
StatusCode int
}
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
if m.TestDocument == "" {
return &http.Response{
StatusCode: m.StatusCode,
}, nil
}
reader, _ := os.Open(m.TestDocument)
return &http.Response{
Body: ioutil.NopCloser(reader),
StatusCode: m.StatusCode,
}, nil
}
func TestRequestCertificate_404ErrorHandling(t *testing.T) {
client := &MockHTTPClient{
TestDocument: "test/response/error_404.html",
StatusCode: 404,
}
var csr []byte
id, err := RequestCertificate(client, "", "", csr, &Credentials{})
assert.Equal(t, id, 0)
assert.Error(t, err)
}
func TestRequestCertificate_ErrorInvalidTemplateHandling(t *testing.T) {
client := &MockHTTPClient{
TestDocument: "test/response/error_invalid_template.html",
StatusCode: 200,
}
var csr []byte
id, err := RequestCertificate(client, "", "", csr, &Credentials{})
assert.Equal(t, id, 0)
assert.Error(t, err)
}
func TestRequestCertificate_Success(t *testing.T) {
client := &MockHTTPClient{
TestDocument: "test/response/success.html",
StatusCode: 200,
}
var csr []byte
id, err := RequestCertificate(client, "", "", csr, &Credentials{})
assert.Equal(t, id, 1437)
assert.NoError(t, err)
}
func TestDownloadCertificate_NonOkStatus(t *testing.T) {
client := &MockHTTPClient{
StatusCode: 500,
}
cert, err := DownloadCertificate(client, 0, "", &Credentials{})
assert.Nil(t, cert)
assert.Error(t, err)
}
func TestDownloadCertificate_Success(t *testing.T) {
client := &MockHTTPClient{
TestDocument: "test/no_cert.txt",
StatusCode: 200,
}
cert, err := DownloadCertificate(client, 0, "", &Credentials{})
assert.Equal(t, string(cert), "--- I'M NOT A CERTIFICATE ---")
assert.NoError(t, err)
}
func TestLoadCsr_ErrorOnNonExistentFile(t *testing.T) {
csr, err := LoadCsr("test/nonexistent.txt")
assert.Nil(t, csr)
assert.Error(t, err)
}
func TestLoadCsr_ErrorOnInvalidCSR(t *testing.T) {
csr, err := LoadCsr("test/csr/no_csr.txt")
assert.Nil(t, csr)
assert.Error(t, err)
}
func TestLoadCsr_ErrorOnTruncatedCSR(t *testing.T) {
csr, err := LoadCsr("test/csr/bad_csr.txt")
assert.Nil(t, csr)
assert.Error(t, err)
}
func TestLoadCsr_Success(t *testing.T) {
csr, err := LoadCsr("test/csr/good_csr.txt")
assert.NotNil(t, csr)
assert.NoError(t, err)
}