github.com/pivotal-cf/go-pivnet/v6@v6.0.2/products_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 - product", 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 29 BeforeEach(func() { 30 server = ghttp.NewServer() 31 apiAddress = server.URL() 32 userAgent = "pivnet-resource/0.1.0 (some-url)" 33 34 fakeLogger = &loggerfakes.FakeLogger{} 35 fakeAccessTokenService = &gopivnetfakes.FakeAccessTokenService{} 36 newClientConfig = pivnet.ClientConfig{ 37 Host: apiAddress, 38 UserAgent: userAgent, 39 } 40 client = pivnet.NewClient(fakeAccessTokenService, newClientConfig, fakeLogger) 41 }) 42 43 AfterEach(func() { 44 server.Close() 45 }) 46 47 Describe("Get", func() { 48 var ( 49 slug = "my-product" 50 s3Directory = "my-product/path" 51 isPksProduct = true 52 ) 53 54 Context("when the product can be found", func() { 55 It("returns the located product", func() { 56 response := fmt.Sprintf(`{"id": 3, "slug": "%s"}`, slug) 57 58 server.AppendHandlers( 59 ghttp.CombineHandlers( 60 ghttp.VerifyRequest("GET", fmt.Sprintf( 61 "%s/products/%s", 62 apiPrefix, 63 slug)), 64 ghttp.RespondWith(http.StatusOK, response), 65 ), 66 ) 67 68 product, err := client.Products.Get(slug) 69 Expect(err).NotTo(HaveOccurred()) 70 Expect(product.Slug).To(Equal(slug)) 71 Expect(product.S3Directory).To(BeNil()) 72 }) 73 74 Context("when the product includes the s3Directory", func() { 75 It("contains the s3 prefix path", func() { 76 response := fmt.Sprintf(`{"id": 3, "slug": "%s", "s3_directory": { "path": "%s" }}`, slug, s3Directory) 77 78 server.AppendHandlers( 79 ghttp.CombineHandlers( 80 ghttp.VerifyRequest("GET", fmt.Sprintf( 81 "%s/products/%s", 82 apiPrefix, 83 slug)), 84 ghttp.RespondWith(http.StatusOK, response), 85 ), 86 ) 87 88 product, err := client.Products.Get(slug) 89 Expect(err).NotTo(HaveOccurred()) 90 Expect(product.S3Directory).NotTo(BeNil()) 91 Expect(product.S3Directory.Path).To(Equal(s3Directory)) 92 }) 93 }) 94 95 Context("when the product is installable on PKS", func() { 96 It("contains installs_on_pks field", func() { 97 response := fmt.Sprintf(`{"id": 3, "slug": "%s", "s3_directory": { "path": "%s" }, "installs_on_pks": %t}`, slug, s3Directory, isPksProduct) 98 99 server.AppendHandlers( 100 ghttp.CombineHandlers( 101 ghttp.VerifyRequest("GET", fmt.Sprintf( 102 "%s/products/%s", 103 apiPrefix, 104 slug)), 105 ghttp.RespondWith(http.StatusOK, response), 106 ), 107 ) 108 109 product, err := client.Products.Get(slug) 110 Expect(err).NotTo(HaveOccurred()) 111 Expect(product.S3Directory).NotTo(BeNil()) 112 Expect(product.S3Directory.Path).To(Equal(s3Directory)) 113 Expect(product.InstallsOnPks).To(Equal(isPksProduct)) 114 }) 115 }) 116 }) 117 118 Context("when the server responds with a non-2XX status code", func() { 119 var ( 120 body []byte 121 ) 122 123 BeforeEach(func() { 124 body = []byte(`{"message":"foo message"}`) 125 }) 126 127 It("returns an error", func() { 128 server.AppendHandlers( 129 ghttp.CombineHandlers( 130 ghttp.VerifyRequest("GET", fmt.Sprintf( 131 "%s/products/%s", 132 apiPrefix, 133 slug, 134 )), 135 ghttp.RespondWith(http.StatusTeapot, body), 136 ), 137 ) 138 139 _, err := client.Products.Get(slug) 140 Expect(err.Error()).To(ContainSubstring("foo message")) 141 }) 142 }) 143 144 Context("when the json unmarshalling fails with error", func() { 145 It("forwards the error", func() { 146 server.AppendHandlers( 147 ghttp.CombineHandlers( 148 ghttp.VerifyRequest("GET", fmt.Sprintf( 149 "%s/products/%s", 150 apiPrefix, 151 slug, 152 )), 153 ghttp.RespondWith(http.StatusTeapot, "%%%"), 154 ), 155 ) 156 157 _, err := client.Products.Get(slug) 158 Expect(err).To(HaveOccurred()) 159 160 Expect(err.Error()).To(ContainSubstring("invalid character")) 161 }) 162 }) 163 }) 164 165 Describe("List", func() { 166 var ( 167 slug = "my-product" 168 ) 169 170 Context("when the products can be found", func() { 171 It("returns the products", func() { 172 response := fmt.Sprintf(`{"products":[{"id": 3, "slug": "%s"},{"id": 4, "slug": "bar"}]}`, slug) 173 174 server.AppendHandlers( 175 ghttp.CombineHandlers( 176 ghttp.VerifyRequest("GET", fmt.Sprintf( 177 "%s/products", 178 apiPrefix)), 179 ghttp.RespondWith(http.StatusOK, response), 180 ), 181 ) 182 183 products, err := client.Products.List() 184 Expect(err).NotTo(HaveOccurred()) 185 186 Expect(products).To(HaveLen(2)) 187 Expect(products[0].Slug).To(Equal(slug)) 188 }) 189 }) 190 191 Context("when the server responds with a non-2XX status code", func() { 192 var ( 193 body []byte 194 ) 195 196 BeforeEach(func() { 197 body = []byte(`{"message":"foo message"}`) 198 }) 199 200 It("returns an error", func() { 201 server.AppendHandlers( 202 ghttp.CombineHandlers( 203 ghttp.VerifyRequest("GET", fmt.Sprintf( 204 "%s/products", 205 apiPrefix, 206 )), 207 ghttp.RespondWith(http.StatusTeapot, body), 208 ), 209 ) 210 211 _, err := client.Products.List() 212 Expect(err.Error()).To(ContainSubstring("foo message")) 213 }) 214 }) 215 216 Context("when the json unmarshalling fails with error", func() { 217 It("forwards the error", func() { 218 server.AppendHandlers( 219 ghttp.CombineHandlers( 220 ghttp.VerifyRequest("GET", fmt.Sprintf( 221 "%s/products", 222 apiPrefix, 223 )), 224 ghttp.RespondWith(http.StatusTeapot, "%%%"), 225 ), 226 ) 227 228 _, err := client.Products.List() 229 Expect(err).To(HaveOccurred()) 230 231 Expect(err.Error()).To(ContainSubstring("invalid character")) 232 }) 233 }) 234 }) 235 236 Describe("SlugAlias", func() { 237 Context("when product can be found", func() { 238 var ( 239 productSlug = "product-slug" 240 productSlugAlias = "product-slug-alias" 241 ) 242 243 It("returns list of all the slugs associated to the product", func() { 244 response := fmt.Sprintf(`{"slugs": ["%[1]v", "%[2]v"], "current_slug": "%[1]v"}`, productSlug, productSlugAlias) 245 246 server.AppendHandlers( 247 ghttp.CombineHandlers( 248 ghttp.VerifyRequest("GET", fmt.Sprintf( 249 "%s/products/%s/slug_alias", 250 apiPrefix, 251 productSlug)), 252 ghttp.RespondWith(http.StatusOK, response), 253 ), 254 ) 255 256 slugAliasResponse, err := client.Products.SlugAlias(productSlug) 257 Expect(err).NotTo(HaveOccurred()) 258 Expect(slugAliasResponse.Slugs).To(HaveLen(2)) 259 Expect(slugAliasResponse.Slugs).To(ConsistOf(productSlugAlias, productSlug)) 260 Expect(slugAliasResponse.CurrentSlug).To(Equal(productSlug)) 261 }) 262 }) 263 264 Context("when the server responds with a non-2XX status code", func() { 265 var ( 266 body []byte 267 ) 268 269 BeforeEach(func() { 270 body = []byte(`{"message":"foo message"}`) 271 }) 272 273 It("returns an error", func() { 274 server.AppendHandlers( 275 ghttp.CombineHandlers( 276 ghttp.VerifyRequest("GET", fmt.Sprintf( 277 "%s/products/%s/slug_alias", 278 apiPrefix, 279 productSlug, 280 )), 281 ghttp.RespondWith(http.StatusTeapot, body), 282 ), 283 ) 284 285 _, err := client.Products.SlugAlias(productSlug) 286 Expect(err.Error()).To(ContainSubstring("foo message")) 287 }) 288 }) 289 }) 290 })