-
Notifications
You must be signed in to change notification settings - Fork 17
/
eulas_test.go
229 lines (187 loc) · 5.68 KB
/
eulas_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package pivnet_test
import (
"fmt"
"github.com/pivotal-cf/go-pivnet/v7/go-pivnetfakes"
"net/http"
"github.com/onsi/gomega/ghttp"
"github.com/pivotal-cf/go-pivnet/v7"
"github.com/pivotal-cf/go-pivnet/v7/logger"
"github.com/pivotal-cf/go-pivnet/v7/logger/loggerfakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("PivnetClient - EULA", func() {
var (
server *ghttp.Server
client pivnet.Client
token string
apiAddress string
userAgent string
newClientConfig pivnet.ClientConfig
fakeLogger logger.Logger
fakeAccessTokenService *gopivnetfakes.FakeAccessTokenService
)
BeforeEach(func() {
server = ghttp.NewServer()
apiAddress = server.URL()
token = "my-auth-token"
userAgent = "pivnet-resource/0.1.0 (some-url)"
fakeLogger = &loggerfakes.FakeLogger{}
fakeAccessTokenService = &gopivnetfakes.FakeAccessTokenService{}
newClientConfig = pivnet.ClientConfig{
Host: apiAddress,
UserAgent: userAgent,
}
client = pivnet.NewClient(fakeAccessTokenService, newClientConfig, fakeLogger)
})
AfterEach(func() {
server.Close()
})
Describe("List", func() {
It("returns all EULAs", func() {
response := `{"eulas": [{"id":1,"name":"eula1"},{"id": 2,"name":"eula2"}]}`
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", fmt.Sprintf("%s/eulas", apiPrefix)),
ghttp.RespondWith(http.StatusOK, response),
),
)
eulas, err := client.EULA.List()
Expect(err).NotTo(HaveOccurred())
Expect(eulas).To(HaveLen(2))
Expect(eulas[0].ID).To(Equal(1))
Expect(eulas[0].Name).To(Equal("eula1"))
Expect(eulas[1].ID).To(Equal(2))
Expect(eulas[1].Name).To(Equal("eula2"))
})
Context("when the server responds with a non-2XX status code", func() {
var (
body []byte
)
BeforeEach(func() {
body = []byte(`{"message":"foo message"}`)
})
It("returns an error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", fmt.Sprintf("%s/eulas", apiPrefix)),
ghttp.RespondWith(http.StatusTeapot, body),
),
)
_, err := client.EULA.List()
Expect(err.Error()).To(ContainSubstring("foo message"))
})
})
Context("when the json unmarshalling fails with error", func() {
It("forwards the error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", fmt.Sprintf("%s/eulas", apiPrefix)),
ghttp.RespondWith(http.StatusOK, "%%%"),
),
)
_, err := client.EULA.List()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("invalid character"))
})
})
})
Describe("Get", func() {
var (
eulaSlug string
)
BeforeEach(func() {
eulaSlug = "eula_1"
})
It("returns the EULA for the provided eula slug", func() {
response := `{"id":1,"name":"eula1","slug":"eula_1"}`
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", fmt.Sprintf("%s/eulas/%s", apiPrefix, eulaSlug)),
ghttp.RespondWith(http.StatusOK, response),
),
)
eula, err := client.EULA.Get(eulaSlug)
Expect(err).NotTo(HaveOccurred())
Expect(eula.ID).To(Equal(1))
Expect(eula.Name).To(Equal("eula1"))
Expect(eula.Slug).To(Equal(eulaSlug))
})
Context("when the server responds with a non-2XX status code", func() {
var (
body []byte
)
BeforeEach(func() {
body = []byte(`{"message":"foo message"}`)
})
It("returns an error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", fmt.Sprintf("%s/eulas/%s", apiPrefix, eulaSlug)),
ghttp.RespondWith(http.StatusTeapot, body),
),
)
_, err := client.EULA.Get(eulaSlug)
Expect(err.Error()).To(ContainSubstring("foo message"))
})
})
Context("when the json unmarshalling fails with error", func() {
It("forwards the error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", fmt.Sprintf("%s/eulas/%s", apiPrefix, eulaSlug)),
ghttp.RespondWith(http.StatusOK, "%%%"),
),
)
_, err := client.EULA.Get(eulaSlug)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("invalid character"))
})
})
})
Describe("Accept", func() {
var (
releaseID int
productSlug string
EULAAcceptanceURL string
)
BeforeEach(func() {
productSlug = "banana-slug"
releaseID = 42
EULAAcceptanceURL = fmt.Sprintf(apiPrefix+"/products/%s/releases/%d/pivnet_resource_eula_acceptance", productSlug, releaseID)
fakeAccessTokenService.AccessTokenReturns(token, nil)
})
It("accepts the EULA for a given release and product ID", func() {
response := fmt.Sprintf(`{"accepted_at": "2016-01-11","_links":{}}`)
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", EULAAcceptanceURL),
ghttp.VerifyHeaderKV("Authorization", fmt.Sprintf("Token %s", token)),
ghttp.VerifyJSON(`{}`),
ghttp.RespondWith(http.StatusOK, response),
),
)
Expect(client.EULA.Accept(productSlug, releaseID)).To(Succeed())
})
Context("when any other non-200 status code comes back", func() {
var (
body []byte
)
BeforeEach(func() {
body = []byte(`{"message":"foo message"}`)
})
It("returns an error", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", EULAAcceptanceURL),
ghttp.VerifyHeaderKV("Authorization", fmt.Sprintf("Token %s", token)),
ghttp.VerifyJSON(`{}`),
ghttp.RespondWith(http.StatusTeapot, body),
),
)
err := client.EULA.Accept(productSlug, releaseID)
Expect(err.Error()).To(ContainSubstring("foo message"))
})
})
})
})