-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsecurity_service_test.go
74 lines (59 loc) · 1.8 KB
/
security_service_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
package api_test
import (
"net/http"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/pivotal-cf/om/api"
)
var _ = Describe("Security", func() {
var (
client *ghttp.Server
service api.Api
)
BeforeEach(func() {
client = ghttp.NewServer()
service = api.New(api.ApiInput{
Client: httpClient{client.URL()},
})
})
AfterEach(func() {
client.Close()
})
It("gets the root CA cert", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api/v0/security/root_ca_certificate"),
ghttp.RespondWith(http.StatusOK, `{"root_ca_certificate_pem": "some-response-cert"}`),
),
)
output, err := service.GetSecurityRootCACertificate()
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal("some-response-cert"))
})
It("returns error if request fails to submit", func() {
client.Close()
_, err := service.GetSecurityRootCACertificate()
Expect(err).To(MatchError(ContainSubstring("failed to submit request")))
})
It("returns error when response contains non-200 status code", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api/v0/security/root_ca_certificate"),
ghttp.RespondWith(http.StatusTeapot, `{}`),
),
)
_, err := service.GetSecurityRootCACertificate()
Expect(err).To(MatchError(ContainSubstring("request failed: unexpected response")))
})
It("returns error if response fails to unmarshal", func() {
client.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api/v0/security/root_ca_certificate"),
ghttp.RespondWith(http.StatusOK, `%%%`),
),
)
_, err := service.GetSecurityRootCACertificate()
Expect(err).To(MatchError(ContainSubstring("failed to unmarshal response: invalid character")))
})
})