github.com/pivotal-cf/go-pivnet/v6@v6.0.2/dependency_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 - dependency 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 dependency specifiers", func() {
    55  			response := pivnet.DependencySpecifiersResponse{
    56  				DependencySpecifiers: []pivnet.DependencySpecifier{
    57  					{
    58  						ID:        9876,
    59  						Specifier: "1.2.*",
    60  						Product: pivnet.Product{
    61  							ID:   23,
    62  							Name: "Product 23",
    63  						},
    64  					},
    65  					{
    66  						ID:        8765,
    67  						Specifier: "2.3.*",
    68  						Product: pivnet.Product{
    69  							ID:   23,
    70  							Name: "Product 23",
    71  						},
    72  					},
    73  				},
    74  			}
    75  
    76  			server.AppendHandlers(
    77  				ghttp.CombineHandlers(
    78  					ghttp.VerifyRequest("GET", fmt.Sprintf(
    79  						"%s/products/%s/releases/%d/dependency_specifiers",
    80  						apiPrefix,
    81  						productSlug,
    82  						releaseID,
    83  					)),
    84  					ghttp.RespondWithJSONEncoded(http.StatusOK, response),
    85  				),
    86  			)
    87  
    88  			dependencySpecifiers, err := client.DependencySpecifiers.List(productSlug, releaseID)
    89  			Expect(err).NotTo(HaveOccurred())
    90  
    91  			Expect(dependencySpecifiers).To(HaveLen(2))
    92  			Expect(dependencySpecifiers[0].ID).To(Equal(9876))
    93  			Expect(dependencySpecifiers[1].ID).To(Equal(8765))
    94  		})
    95  
    96  		Context("when the server responds with a non-2XX status code", func() {
    97  			var (
    98  				body []byte
    99  			)
   100  
   101  			BeforeEach(func() {
   102  				body = []byte(`{"message":"foo message"}`)
   103  			})
   104  
   105  			BeforeEach(func() {
   106  				server.AppendHandlers(
   107  					ghttp.CombineHandlers(
   108  						ghttp.VerifyRequest("GET", fmt.Sprintf(
   109  							"%s/products/%s/releases/%d/dependency_specifiers",
   110  							apiPrefix,
   111  							productSlug,
   112  							releaseID,
   113  						)),
   114  						ghttp.RespondWith(http.StatusTeapot, body),
   115  					),
   116  				)
   117  			})
   118  
   119  			It("returns an error", func() {
   120  				_, err := client.DependencySpecifiers.List(productSlug, releaseID)
   121  				Expect(err.Error()).To(ContainSubstring("foo message"))
   122  			})
   123  		})
   124  
   125  		Context("when the json unmarshalling fails with error", func() {
   126  			It("forwards the error", func() {
   127  				server.AppendHandlers(
   128  					ghttp.CombineHandlers(
   129  						ghttp.VerifyRequest("GET", fmt.Sprintf(
   130  							"%s/products/%s/releases/%d/dependency_specifiers",
   131  							apiPrefix,
   132  							productSlug,
   133  							releaseID,
   134  						)),
   135  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   136  					),
   137  				)
   138  
   139  				_, err := client.DependencySpecifiers.List(productSlug, releaseID)
   140  				Expect(err).To(HaveOccurred())
   141  
   142  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   143  			})
   144  		})
   145  	})
   146  
   147  	Describe("Get", func() {
   148  		var (
   149  			dependencySpecifierID int
   150  		)
   151  
   152  		BeforeEach(func() {
   153  			dependencySpecifierID = 1234
   154  		})
   155  
   156  		It("returns the dependency specifier", func() {
   157  			response := pivnet.DependencySpecifierResponse{
   158  				DependencySpecifier: pivnet.DependencySpecifier{
   159  					ID:        dependencySpecifierID,
   160  					Specifier: "1.2.*",
   161  					Product: pivnet.Product{
   162  						ID:   23,
   163  						Name: "Product 23",
   164  					},
   165  				},
   166  			}
   167  
   168  			server.AppendHandlers(
   169  				ghttp.CombineHandlers(
   170  					ghttp.VerifyRequest("GET", fmt.Sprintf(
   171  						"%s/products/%s/releases/%d/dependency_specifiers/%d",
   172  						apiPrefix,
   173  						productSlug,
   174  						releaseID,
   175  						dependencySpecifierID,
   176  					)),
   177  					ghttp.RespondWithJSONEncoded(http.StatusOK, response),
   178  				),
   179  			)
   180  
   181  			dependencySpecifier, err := client.DependencySpecifiers.Get(
   182  				productSlug,
   183  				releaseID,
   184  				dependencySpecifierID,
   185  			)
   186  			Expect(err).NotTo(HaveOccurred())
   187  
   188  			Expect(dependencySpecifier.ID).To(Equal(dependencySpecifierID))
   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  			BeforeEach(func() {
   201  				server.AppendHandlers(
   202  					ghttp.CombineHandlers(
   203  						ghttp.VerifyRequest("GET", fmt.Sprintf(
   204  							"%s/products/%s/releases/%d/dependency_specifiers/%d",
   205  							apiPrefix,
   206  							productSlug,
   207  							releaseID,
   208  							dependencySpecifierID,
   209  						)),
   210  						ghttp.RespondWith(http.StatusTeapot, body),
   211  					),
   212  				)
   213  			})
   214  
   215  			It("returns an error", func() {
   216  				_, err := client.DependencySpecifiers.Get(
   217  					productSlug,
   218  					releaseID,
   219  					dependencySpecifierID,
   220  				)
   221  				Expect(err.Error()).To(ContainSubstring("foo message"))
   222  			})
   223  		})
   224  
   225  		Context("when the json unmarshalling fails with error", func() {
   226  			It("forwards the error", func() {
   227  				server.AppendHandlers(
   228  					ghttp.CombineHandlers(
   229  						ghttp.VerifyRequest("GET", fmt.Sprintf(
   230  							"%s/products/%s/releases/%d/dependency_specifiers/%d",
   231  							apiPrefix,
   232  							productSlug,
   233  							releaseID,
   234  							dependencySpecifierID,
   235  						)),
   236  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   237  					),
   238  				)
   239  
   240  				_, err := client.DependencySpecifiers.Get(
   241  					productSlug,
   242  					releaseID,
   243  					dependencySpecifierID,
   244  				)
   245  				Expect(err).To(HaveOccurred())
   246  
   247  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   248  			})
   249  		})
   250  	})
   251  
   252  	Describe("Create", func() {
   253  		var (
   254  			dependentProductSlug string
   255  			specifier            string
   256  		)
   257  
   258  		BeforeEach(func() {
   259  			dependentProductSlug = "other-product"
   260  			specifier = "1.5.*"
   261  		})
   262  
   263  		It("creates the dependency specifier", func() {
   264  			expectedRequestBody := fmt.Sprintf(
   265  				`{"dependency_specifier":{"product_slug":"%s","specifier":"%s"}}`,
   266  				dependentProductSlug,
   267  				specifier,
   268  			)
   269  
   270  			response := pivnet.DependencySpecifierResponse{
   271  				DependencySpecifier: pivnet.DependencySpecifier{
   272  					ID:        1234,
   273  					Specifier: specifier,
   274  					Product: pivnet.Product{
   275  						ID:   23,
   276  						Name: dependentProductSlug,
   277  					},
   278  				},
   279  			}
   280  
   281  			server.AppendHandlers(
   282  				ghttp.CombineHandlers(
   283  					ghttp.VerifyRequest("POST", fmt.Sprintf(
   284  						"%s/products/%s/releases/%d/dependency_specifiers",
   285  						apiPrefix,
   286  						productSlug,
   287  						releaseID,
   288  					)),
   289  					ghttp.VerifyJSON(expectedRequestBody),
   290  					ghttp.RespondWithJSONEncoded(http.StatusCreated, response),
   291  				),
   292  			)
   293  
   294  			dependencySpecifier, err := client.DependencySpecifiers.Create(
   295  				productSlug,
   296  				releaseID,
   297  				dependentProductSlug,
   298  				specifier,
   299  			)
   300  			Expect(err).NotTo(HaveOccurred())
   301  			Expect(dependencySpecifier.ID).To(Equal(1234))
   302  			Expect(dependencySpecifier.Specifier).To(Equal(specifier))
   303  		})
   304  
   305  		Context("when the server responds with a non-2XX status code", func() {
   306  			var (
   307  				body []byte
   308  			)
   309  
   310  			BeforeEach(func() {
   311  				body = []byte(`{"message":"foo message"}`)
   312  			})
   313  
   314  			BeforeEach(func() {
   315  				server.AppendHandlers(
   316  					ghttp.CombineHandlers(
   317  						ghttp.VerifyRequest("POST", fmt.Sprintf(
   318  							"%s/products/%s/releases/%d/dependency_specifiers",
   319  							apiPrefix,
   320  							productSlug,
   321  							releaseID,
   322  						)),
   323  						ghttp.RespondWith(http.StatusTeapot, body),
   324  					),
   325  				)
   326  			})
   327  
   328  			It("returns an error", func() {
   329  				_, err := client.DependencySpecifiers.Create(
   330  					productSlug,
   331  					releaseID,
   332  					dependentProductSlug,
   333  					specifier,
   334  				)
   335  				Expect(err.Error()).To(ContainSubstring("foo message"))
   336  			})
   337  		})
   338  
   339  		Context("when the json unmarshalling fails with error", func() {
   340  			It("forwards the error", func() {
   341  				server.AppendHandlers(
   342  					ghttp.CombineHandlers(
   343  						ghttp.VerifyRequest("POST", fmt.Sprintf(
   344  							"%s/products/%s/releases/%d/dependency_specifiers",
   345  							apiPrefix,
   346  							productSlug,
   347  							releaseID,
   348  						)),
   349  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   350  					),
   351  				)
   352  
   353  				_, err := client.DependencySpecifiers.Create(
   354  					productSlug,
   355  					releaseID,
   356  					dependentProductSlug,
   357  					specifier,
   358  				)
   359  				Expect(err).To(HaveOccurred())
   360  
   361  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   362  			})
   363  		})
   364  	})
   365  
   366  	Describe("Delete", func() {
   367  		var (
   368  			dependencySpecifierID int
   369  		)
   370  
   371  		BeforeEach(func() {
   372  			dependencySpecifierID = 1234
   373  		})
   374  
   375  		It("deletes the dependency specifier", func() {
   376  			server.AppendHandlers(
   377  				ghttp.CombineHandlers(
   378  					ghttp.VerifyRequest("DELETE", fmt.Sprintf(
   379  						"%s/products/%s/releases/%d/dependency_specifiers/%d",
   380  						apiPrefix,
   381  						productSlug,
   382  						releaseID,
   383  						dependencySpecifierID,
   384  					)),
   385  					ghttp.RespondWithJSONEncoded(http.StatusNoContent, nil),
   386  				),
   387  			)
   388  
   389  			err := client.DependencySpecifiers.Delete(
   390  				productSlug,
   391  				releaseID,
   392  				dependencySpecifierID,
   393  			)
   394  			Expect(err).NotTo(HaveOccurred())
   395  		})
   396  
   397  		Context("when the server responds with a non-2XX status code", func() {
   398  			var (
   399  				body []byte
   400  			)
   401  
   402  			BeforeEach(func() {
   403  				body = []byte(`{"message":"foo message"}`)
   404  			})
   405  
   406  			BeforeEach(func() {
   407  				server.AppendHandlers(
   408  					ghttp.CombineHandlers(
   409  						ghttp.VerifyRequest("DELETE", fmt.Sprintf(
   410  							"%s/products/%s/releases/%d/dependency_specifiers/%d",
   411  							apiPrefix,
   412  							productSlug,
   413  							releaseID,
   414  							dependencySpecifierID,
   415  						)),
   416  						ghttp.RespondWith(http.StatusTeapot, body),
   417  					),
   418  				)
   419  			})
   420  
   421  			It("returns an error", func() {
   422  				err := client.DependencySpecifiers.Delete(
   423  					productSlug,
   424  					releaseID,
   425  					dependencySpecifierID,
   426  				)
   427  				Expect(err.Error()).To(ContainSubstring("foo message"))
   428  			})
   429  		})
   430  
   431  		Context("when the json unmarshalling fails with error", func() {
   432  			It("forwards the error", func() {
   433  				server.AppendHandlers(
   434  					ghttp.CombineHandlers(
   435  						ghttp.VerifyRequest("DELETE", fmt.Sprintf(
   436  							"%s/products/%s/releases/%d/dependency_specifiers/%d",
   437  							apiPrefix,
   438  							productSlug,
   439  							releaseID,
   440  							dependencySpecifierID,
   441  						)),
   442  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   443  					),
   444  				)
   445  
   446  				err := client.DependencySpecifiers.Delete(
   447  					productSlug,
   448  					releaseID,
   449  					dependencySpecifierID,
   450  				)
   451  				Expect(err).To(HaveOccurred())
   452  
   453  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   454  			})
   455  		})
   456  	})
   457  })