github.com/pivotal-cf/go-pivnet/v6@v6.0.2/upgrade_path_specifiers_test.go (about) 1 package pivnet_test 2 3 import ( 4 "fmt" 5 "github.com/pivotal-cf/go-pivnet/v6/go-pivnetfakes" 6 "net/http" 7 8 "github.com/onsi/gomega/ghttp" 9 "github.com/pivotal-cf/go-pivnet/v6" 10 "github.com/pivotal-cf/go-pivnet/v6/logger" 11 "github.com/pivotal-cf/go-pivnet/v6/logger/loggerfakes" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("PivnetClient - upgrade path specifiers", func() { 18 var ( 19 server *ghttp.Server 20 client pivnet.Client 21 apiAddress string 22 userAgent string 23 24 newClientConfig pivnet.ClientConfig 25 fakeLogger logger.Logger 26 fakeAccessTokenService *gopivnetfakes.FakeAccessTokenService 27 28 productSlug string 29 releaseID int 30 ) 31 32 BeforeEach(func() { 33 server = ghttp.NewServer() 34 apiAddress = server.URL() 35 userAgent = "pivnet-resource/0.1.0 (some-url)" 36 37 productSlug = "some-product" 38 releaseID = 2345 39 40 fakeLogger = &loggerfakes.FakeLogger{} 41 fakeAccessTokenService = &gopivnetfakes.FakeAccessTokenService{} 42 newClientConfig = pivnet.ClientConfig{ 43 Host: apiAddress, 44 UserAgent: userAgent, 45 } 46 client = pivnet.NewClient(fakeAccessTokenService, newClientConfig, fakeLogger) 47 }) 48 49 AfterEach(func() { 50 server.Close() 51 }) 52 53 Describe("List", func() { 54 It("returns the upgrade path specifiers", func() { 55 response := pivnet.UpgradePathSpecifiersResponse{ 56 UpgradePathSpecifiers: []pivnet.UpgradePathSpecifier{ 57 { 58 ID: 9876, 59 Specifier: "1.2.*", 60 }, 61 { 62 ID: 8765, 63 Specifier: "~>2.3.4", 64 }, 65 }, 66 } 67 68 server.AppendHandlers( 69 ghttp.CombineHandlers( 70 ghttp.VerifyRequest("GET", fmt.Sprintf( 71 "%s/products/%s/releases/%d/upgrade_path_specifiers", 72 apiPrefix, 73 productSlug, 74 releaseID, 75 )), 76 ghttp.RespondWithJSONEncoded(http.StatusOK, response), 77 ), 78 ) 79 80 upgradePathSpecifiers, err := client.UpgradePathSpecifiers.List(productSlug, releaseID) 81 Expect(err).NotTo(HaveOccurred()) 82 83 Expect(upgradePathSpecifiers).To(HaveLen(2)) 84 Expect(upgradePathSpecifiers[0].ID).To(Equal(9876)) 85 Expect(upgradePathSpecifiers[1].ID).To(Equal(8765)) 86 }) 87 88 Context("when the server responds with a non-2XX status code", func() { 89 var ( 90 body []byte 91 ) 92 93 BeforeEach(func() { 94 body = []byte(`{"message":"foo message"}`) 95 }) 96 97 BeforeEach(func() { 98 server.AppendHandlers( 99 ghttp.CombineHandlers( 100 ghttp.VerifyRequest("GET", fmt.Sprintf( 101 "%s/products/%s/releases/%d/upgrade_path_specifiers", 102 apiPrefix, 103 productSlug, 104 releaseID, 105 )), 106 ghttp.RespondWith(http.StatusTeapot, body), 107 ), 108 ) 109 }) 110 111 It("returns an error", func() { 112 _, err := client.UpgradePathSpecifiers.List(productSlug, releaseID) 113 Expect(err.Error()).To(ContainSubstring("foo message")) 114 }) 115 }) 116 117 Context("when the json unmarshalling fails with error", func() { 118 It("forwards the error", func() { 119 server.AppendHandlers( 120 ghttp.CombineHandlers( 121 ghttp.VerifyRequest("GET", fmt.Sprintf( 122 "%s/products/%s/releases/%d/upgrade_path_specifiers", 123 apiPrefix, 124 productSlug, 125 releaseID, 126 )), 127 ghttp.RespondWith(http.StatusTeapot, "%%%"), 128 ), 129 ) 130 131 _, err := client.UpgradePathSpecifiers.List(productSlug, releaseID) 132 Expect(err).To(HaveOccurred()) 133 134 Expect(err.Error()).To(ContainSubstring("invalid character")) 135 }) 136 }) 137 }) 138 139 Describe("Get", func() { 140 var ( 141 upgradePathSpecifierID int 142 ) 143 144 BeforeEach(func() { 145 upgradePathSpecifierID = 1234 146 }) 147 148 It("returns the upgrade path specifier", func() { 149 response := pivnet.UpgradePathSpecifierResponse{ 150 UpgradePathSpecifier: pivnet.UpgradePathSpecifier{ 151 ID: upgradePathSpecifierID, 152 Specifier: "1.2.*", 153 }, 154 } 155 156 server.AppendHandlers( 157 ghttp.CombineHandlers( 158 ghttp.VerifyRequest("GET", fmt.Sprintf( 159 "%s/products/%s/releases/%d/upgrade_path_specifiers/%d", 160 apiPrefix, 161 productSlug, 162 releaseID, 163 upgradePathSpecifierID, 164 )), 165 ghttp.RespondWithJSONEncoded(http.StatusOK, response), 166 ), 167 ) 168 169 upgradePathSpecifier, err := client.UpgradePathSpecifiers.Get( 170 productSlug, 171 releaseID, 172 upgradePathSpecifierID, 173 ) 174 Expect(err).NotTo(HaveOccurred()) 175 176 Expect(upgradePathSpecifier.ID).To(Equal(upgradePathSpecifierID)) 177 }) 178 179 Context("when the server responds with a non-2XX status code", func() { 180 var ( 181 body []byte 182 ) 183 184 BeforeEach(func() { 185 body = []byte(`{"message":"foo message"}`) 186 }) 187 188 BeforeEach(func() { 189 server.AppendHandlers( 190 ghttp.CombineHandlers( 191 ghttp.VerifyRequest("GET", fmt.Sprintf( 192 "%s/products/%s/releases/%d/upgrade_path_specifiers/%d", 193 apiPrefix, 194 productSlug, 195 releaseID, 196 upgradePathSpecifierID, 197 )), 198 ghttp.RespondWith(http.StatusTeapot, body), 199 ), 200 ) 201 }) 202 203 It("returns an error", func() { 204 _, err := client.UpgradePathSpecifiers.Get( 205 productSlug, 206 releaseID, 207 upgradePathSpecifierID, 208 ) 209 Expect(err.Error()).To(ContainSubstring("foo message")) 210 }) 211 }) 212 213 Context("when the json unmarshalling fails with error", func() { 214 It("forwards the error", func() { 215 server.AppendHandlers( 216 ghttp.CombineHandlers( 217 ghttp.VerifyRequest("GET", fmt.Sprintf( 218 "%s/products/%s/releases/%d/upgrade_path_specifiers/%d", 219 apiPrefix, 220 productSlug, 221 releaseID, 222 upgradePathSpecifierID, 223 )), 224 ghttp.RespondWith(http.StatusTeapot, "%%%"), 225 ), 226 ) 227 228 _, err := client.UpgradePathSpecifiers.Get( 229 productSlug, 230 releaseID, 231 upgradePathSpecifierID, 232 ) 233 Expect(err).To(HaveOccurred()) 234 235 Expect(err.Error()).To(ContainSubstring("invalid character")) 236 }) 237 }) 238 }) 239 240 Describe("Create", func() { 241 var ( 242 specifier string 243 ) 244 245 BeforeEach(func() { 246 specifier = "1.5.*" 247 }) 248 249 It("creates the upgrade path specifier", func() { 250 expectedRequestBody := fmt.Sprintf( 251 `{"upgrade_path_specifier":{"specifier":"%s"}}`, 252 specifier, 253 ) 254 255 response := pivnet.UpgradePathSpecifierResponse{ 256 UpgradePathSpecifier: pivnet.UpgradePathSpecifier{ 257 ID: 1234, 258 Specifier: specifier, 259 }, 260 } 261 262 server.AppendHandlers( 263 ghttp.CombineHandlers( 264 ghttp.VerifyRequest("POST", fmt.Sprintf( 265 "%s/products/%s/releases/%d/upgrade_path_specifiers", 266 apiPrefix, 267 productSlug, 268 releaseID, 269 )), 270 ghttp.VerifyJSON(expectedRequestBody), 271 ghttp.RespondWithJSONEncoded(http.StatusCreated, response), 272 ), 273 ) 274 275 upgradePathSpecifier, err := client.UpgradePathSpecifiers.Create( 276 productSlug, 277 releaseID, 278 specifier, 279 ) 280 Expect(err).NotTo(HaveOccurred()) 281 Expect(upgradePathSpecifier.ID).To(Equal(1234)) 282 Expect(upgradePathSpecifier.Specifier).To(Equal(specifier)) 283 }) 284 285 Context("when the server responds with a non-2XX status code", func() { 286 var ( 287 body []byte 288 ) 289 290 BeforeEach(func() { 291 body = []byte(`{"message":"foo message"}`) 292 }) 293 294 BeforeEach(func() { 295 server.AppendHandlers( 296 ghttp.CombineHandlers( 297 ghttp.VerifyRequest("POST", fmt.Sprintf( 298 "%s/products/%s/releases/%d/upgrade_path_specifiers", 299 apiPrefix, 300 productSlug, 301 releaseID, 302 )), 303 ghttp.RespondWith(http.StatusTeapot, body), 304 ), 305 ) 306 }) 307 308 It("returns an error", func() { 309 _, err := client.UpgradePathSpecifiers.Create( 310 productSlug, 311 releaseID, 312 specifier, 313 ) 314 Expect(err.Error()).To(ContainSubstring("foo message")) 315 }) 316 }) 317 318 Context("when the json unmarshalling fails with error", func() { 319 It("forwards the error", func() { 320 server.AppendHandlers( 321 ghttp.CombineHandlers( 322 ghttp.VerifyRequest("POST", fmt.Sprintf( 323 "%s/products/%s/releases/%d/upgrade_path_specifiers", 324 apiPrefix, 325 productSlug, 326 releaseID, 327 )), 328 ghttp.RespondWith(http.StatusTeapot, "%%%"), 329 ), 330 ) 331 332 _, err := client.UpgradePathSpecifiers.Create( 333 productSlug, 334 releaseID, 335 specifier, 336 ) 337 Expect(err).To(HaveOccurred()) 338 339 Expect(err.Error()).To(ContainSubstring("invalid character")) 340 }) 341 }) 342 }) 343 344 Describe("Delete", func() { 345 var ( 346 upgradePathSpecifierID int 347 ) 348 349 BeforeEach(func() { 350 upgradePathSpecifierID = 1234 351 }) 352 353 It("deletes the upgrade path specifier", func() { 354 server.AppendHandlers( 355 ghttp.CombineHandlers( 356 ghttp.VerifyRequest("DELETE", fmt.Sprintf( 357 "%s/products/%s/releases/%d/upgrade_path_specifiers/%d", 358 apiPrefix, 359 productSlug, 360 releaseID, 361 upgradePathSpecifierID, 362 )), 363 ghttp.RespondWithJSONEncoded(http.StatusNoContent, nil), 364 ), 365 ) 366 367 err := client.UpgradePathSpecifiers.Delete( 368 productSlug, 369 releaseID, 370 upgradePathSpecifierID, 371 ) 372 Expect(err).NotTo(HaveOccurred()) 373 }) 374 375 Context("when the server responds with a non-2XX status code", func() { 376 var ( 377 body []byte 378 ) 379 380 BeforeEach(func() { 381 body = []byte(`{"message":"foo message"}`) 382 }) 383 384 BeforeEach(func() { 385 server.AppendHandlers( 386 ghttp.CombineHandlers( 387 ghttp.VerifyRequest("DELETE", fmt.Sprintf( 388 "%s/products/%s/releases/%d/upgrade_path_specifiers/%d", 389 apiPrefix, 390 productSlug, 391 releaseID, 392 upgradePathSpecifierID, 393 )), 394 ghttp.RespondWith(http.StatusTeapot, body), 395 ), 396 ) 397 }) 398 399 It("returns an error", func() { 400 err := client.UpgradePathSpecifiers.Delete( 401 productSlug, 402 releaseID, 403 upgradePathSpecifierID, 404 ) 405 Expect(err.Error()).To(ContainSubstring("foo message")) 406 }) 407 }) 408 409 Context("when the json unmarshalling fails with error", func() { 410 It("forwards the error", func() { 411 server.AppendHandlers( 412 ghttp.CombineHandlers( 413 ghttp.VerifyRequest("DELETE", fmt.Sprintf( 414 "%s/products/%s/releases/%d/upgrade_path_specifiers/%d", 415 apiPrefix, 416 productSlug, 417 releaseID, 418 upgradePathSpecifierID, 419 )), 420 ghttp.RespondWith(http.StatusTeapot, "%%%"), 421 ), 422 ) 423 424 err := client.UpgradePathSpecifiers.Delete( 425 productSlug, 426 releaseID, 427 upgradePathSpecifierID, 428 ) 429 Expect(err).To(HaveOccurred()) 430 431 Expect(err.Error()).To(ContainSubstring("invalid character")) 432 }) 433 }) 434 }) 435 })