github.com/pivotal-cf/go-pivnet/v6@v6.0.2/file_groups_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 - FileGroup", 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("List", func() {
    48  		It("returns all FileGroups", func() {
    49  			response := pivnet.FileGroupsResponse{
    50  				[]pivnet.FileGroup{
    51  					{
    52  						ID:   1234,
    53  						Name: "Some file group",
    54  					},
    55  					{
    56  						ID:   2345,
    57  						Name: "Some other file group",
    58  					},
    59  				},
    60  			}
    61  
    62  			server.AppendHandlers(
    63  				ghttp.CombineHandlers(
    64  					ghttp.VerifyRequest("GET", fmt.Sprintf("%s/products/%s/file_groups", apiPrefix, productSlug)),
    65  					ghttp.RespondWithJSONEncoded(http.StatusOK, response),
    66  				),
    67  			)
    68  
    69  			fileGroups, err := client.FileGroups.List(productSlug)
    70  			Expect(err).NotTo(HaveOccurred())
    71  
    72  			Expect(fileGroups).To(HaveLen(2))
    73  
    74  			Expect(fileGroups[0].ID).To(Equal(fileGroups[0].ID))
    75  			Expect(fileGroups[0].Name).To(Equal(fileGroups[0].Name))
    76  			Expect(fileGroups[1].ID).To(Equal(fileGroups[1].ID))
    77  			Expect(fileGroups[1].Name).To(Equal(fileGroups[1].Name))
    78  		})
    79  
    80  		Context("when the server responds with a non-2XX status code", func() {
    81  			var (
    82  				body []byte
    83  			)
    84  
    85  			BeforeEach(func() {
    86  				body = []byte(`{"message":"foo message"}`)
    87  			})
    88  
    89  			It("returns an error", func() {
    90  				server.AppendHandlers(
    91  					ghttp.CombineHandlers(
    92  						ghttp.VerifyRequest("GET", fmt.Sprintf("%s/products/%s/file_groups", apiPrefix, productSlug)),
    93  						ghttp.RespondWith(http.StatusTeapot, body),
    94  					),
    95  				)
    96  
    97  				_, err := client.FileGroups.List(productSlug)
    98  				Expect(err.Error()).To(ContainSubstring("foo message"))
    99  			})
   100  		})
   101  
   102  		Context("when the json unmarshalling fails with error", func() {
   103  			It("forwards the error", func() {
   104  				server.AppendHandlers(
   105  					ghttp.CombineHandlers(
   106  						ghttp.VerifyRequest("GET", fmt.Sprintf("%s/products/%s/file_groups", apiPrefix, productSlug)),
   107  						ghttp.RespondWith(http.StatusOK, "%%%"),
   108  					),
   109  				)
   110  
   111  				_, err := client.FileGroups.List(productSlug)
   112  				Expect(err).To(HaveOccurred())
   113  
   114  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   115  			})
   116  		})
   117  	})
   118  
   119  	Describe("List for release", func() {
   120  		var (
   121  			productSlug string
   122  			releaseID   int
   123  
   124  			response           interface{}
   125  			responseStatusCode int
   126  		)
   127  
   128  		BeforeEach(func() {
   129  			productSlug = "banana"
   130  			releaseID = 12
   131  
   132  			response = pivnet.FileGroupsResponse{[]pivnet.FileGroup{
   133  				{
   134  					ID:   1234,
   135  					Name: "something",
   136  				},
   137  				{
   138  					ID:   2345,
   139  					Name: "something-else",
   140  				},
   141  			}}
   142  
   143  			responseStatusCode = http.StatusOK
   144  		})
   145  
   146  		JustBeforeEach(func() {
   147  			server.AppendHandlers(
   148  				ghttp.CombineHandlers(
   149  					ghttp.VerifyRequest(
   150  						"GET",
   151  						fmt.Sprintf(
   152  							"%s/products/%s/releases/%d/file_groups",
   153  							apiPrefix,
   154  							productSlug,
   155  							releaseID,
   156  						),
   157  					),
   158  					ghttp.RespondWithJSONEncoded(responseStatusCode, response),
   159  				),
   160  			)
   161  		})
   162  
   163  		It("returns the product file without error", func() {
   164  			fileGroups, err := client.FileGroups.ListForRelease(
   165  				productSlug,
   166  				releaseID,
   167  			)
   168  			Expect(err).NotTo(HaveOccurred())
   169  
   170  			Expect(fileGroups).To(HaveLen(2))
   171  			Expect(fileGroups[0].ID).To(Equal(1234))
   172  		})
   173  
   174  		Context("when the server responds with a non-2XX status code", func() {
   175  			BeforeEach(func() {
   176  				responseStatusCode = http.StatusTeapot
   177  				response = pivnetErr{Message: "foo message"}
   178  			})
   179  
   180  			It("returns an error", func() {
   181  				_, err := client.FileGroups.ListForRelease(
   182  					productSlug,
   183  					releaseID,
   184  				)
   185  				Expect(err).To(HaveOccurred())
   186  
   187  				Expect(err.Error()).To(ContainSubstring("foo message"))
   188  			})
   189  		})
   190  
   191  		Context("when the json unmarshalling fails with error", func() {
   192  			BeforeEach(func() {
   193  				response = "%%%"
   194  			})
   195  
   196  			It("forwards the error", func() {
   197  				_, err := client.FileGroups.ListForRelease(
   198  					productSlug,
   199  					releaseID,
   200  				)
   201  				Expect(err).To(HaveOccurred())
   202  
   203  				Expect(err.Error()).To(ContainSubstring("json"))
   204  			})
   205  		})
   206  	})
   207  
   208  	Describe("Get File group", func() {
   209  		var (
   210  			productSlug string
   211  			fileGroupID int
   212  
   213  			response           interface{}
   214  			responseStatusCode int
   215  		)
   216  
   217  		BeforeEach(func() {
   218  			productSlug = "banana"
   219  			fileGroupID = 1234
   220  
   221  			response = pivnet.FileGroup{
   222  				ID:   fileGroupID,
   223  				Name: "something",
   224  			}
   225  
   226  			responseStatusCode = http.StatusOK
   227  		})
   228  
   229  		JustBeforeEach(func() {
   230  			server.AppendHandlers(
   231  				ghttp.CombineHandlers(
   232  					ghttp.VerifyRequest(
   233  						"GET",
   234  						fmt.Sprintf(
   235  							"%s/products/%s/file_groups/%d",
   236  							apiPrefix,
   237  							productSlug,
   238  							fileGroupID,
   239  						),
   240  					),
   241  					ghttp.RespondWithJSONEncoded(responseStatusCode, response),
   242  				),
   243  			)
   244  		})
   245  
   246  		It("returns the file group without error", func() {
   247  			fileGroup, err := client.FileGroups.Get(
   248  				productSlug,
   249  				fileGroupID,
   250  			)
   251  			Expect(err).NotTo(HaveOccurred())
   252  
   253  			Expect(fileGroup.ID).To(Equal(fileGroupID))
   254  			Expect(fileGroup.Name).To(Equal("something"))
   255  		})
   256  
   257  		Context("when the server responds with a non-2XX status code", func() {
   258  			BeforeEach(func() {
   259  				responseStatusCode = http.StatusTeapot
   260  				response = pivnetErr{Message: "foo message"}
   261  			})
   262  
   263  			It("returns an error", func() {
   264  				_, err := client.FileGroups.Get(
   265  					productSlug,
   266  					fileGroupID,
   267  				)
   268  				Expect(err).To(HaveOccurred())
   269  
   270  				Expect(err.Error()).To(ContainSubstring("foo message"))
   271  			})
   272  		})
   273  
   274  		Context("when the json unmarshalling fails with error", func() {
   275  			BeforeEach(func() {
   276  				response = "%%%"
   277  			})
   278  
   279  			It("forwards the error", func() {
   280  				_, err := client.FileGroups.Get(
   281  					productSlug,
   282  					fileGroupID,
   283  				)
   284  				Expect(err).To(HaveOccurred())
   285  
   286  				Expect(err.Error()).To(ContainSubstring("json"))
   287  			})
   288  		})
   289  	})
   290  
   291  	Describe("Create", func() {
   292  		var (
   293  			name string
   294  
   295  			expectedRequestBody string
   296  
   297  			returnedFileGroup pivnet.FileGroup
   298  		)
   299  
   300  		BeforeEach(func() {
   301  			name = "some name"
   302  
   303  			expectedRequestBody = fmt.Sprintf(
   304  				`{"file_group":{"name":"%s"}}`,
   305  				name,
   306  			)
   307  		})
   308  
   309  		JustBeforeEach(func() {
   310  			returnedFileGroup = pivnet.FileGroup{
   311  				ID:   1234,
   312  				Name: name,
   313  			}
   314  		})
   315  
   316  		It("creates new file group without error", func() {
   317  			server.AppendHandlers(
   318  				ghttp.CombineHandlers(
   319  					ghttp.VerifyRequest("POST", fmt.Sprintf(
   320  						"%s/products/%s/file_groups",
   321  						apiPrefix,
   322  						productSlug,
   323  					)),
   324  					ghttp.VerifyJSON(expectedRequestBody),
   325  					ghttp.RespondWithJSONEncoded(http.StatusCreated, returnedFileGroup),
   326  				),
   327  			)
   328  
   329  			config := pivnet.CreateFileGroupConfig{productSlug, name}
   330  			fileGroup, err := client.FileGroups.Create(config)
   331  			Expect(err).NotTo(HaveOccurred())
   332  
   333  			Expect(fileGroup.ID).To(Equal(returnedFileGroup.ID))
   334  			Expect(fileGroup.Name).To(Equal(name))
   335  		})
   336  
   337  		Context("when the server responds with a non-201 status code", func() {
   338  			var (
   339  				body []byte
   340  			)
   341  
   342  			BeforeEach(func() {
   343  				body = []byte(`{"message":"foo message"}`)
   344  			})
   345  
   346  			It("returns an error", func() {
   347  				server.AppendHandlers(
   348  					ghttp.CombineHandlers(
   349  						ghttp.VerifyRequest("POST", fmt.Sprintf(
   350  							"%s/products/%s/file_groups",
   351  							apiPrefix,
   352  							productSlug,
   353  						)),
   354  						ghttp.RespondWith(http.StatusTeapot, body),
   355  					),
   356  				)
   357  
   358  				config := pivnet.CreateFileGroupConfig{productSlug, name}
   359  				_, err := client.FileGroups.Create(config)
   360  
   361  				Expect(err.Error()).To(ContainSubstring("foo message"))
   362  			})
   363  		})
   364  
   365  		Context("when the json unmarshalling fails with error", func() {
   366  			It("forwards the error", func() {
   367  				server.AppendHandlers(
   368  					ghttp.CombineHandlers(
   369  						ghttp.VerifyRequest("POST", fmt.Sprintf(
   370  							"%s/products/%s/file_groups",
   371  							apiPrefix,
   372  							productSlug,
   373  						)),
   374  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   375  					),
   376  				)
   377  
   378  				config := pivnet.CreateFileGroupConfig{productSlug, name}
   379  				_, err := client.FileGroups.Create(config)
   380  				Expect(err).To(HaveOccurred())
   381  
   382  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   383  			})
   384  		})
   385  	})
   386  
   387  	Describe("Update", func() {
   388  		var (
   389  			fileGroup pivnet.FileGroup
   390  
   391  			expectedRequestBody string
   392  
   393  			response pivnet.FileGroup
   394  		)
   395  
   396  		BeforeEach(func() {
   397  			fileGroup = pivnet.FileGroup{
   398  				ID:   1234,
   399  				Name: "some name",
   400  			}
   401  
   402  			expectedRequestBody = fmt.Sprintf(
   403  				`{"file_group":{"name":"%s"}}`,
   404  				fileGroup.Name,
   405  			)
   406  
   407  			response = fileGroup
   408  		})
   409  
   410  		It("returns without error", func() {
   411  			server.AppendHandlers(
   412  				ghttp.CombineHandlers(
   413  					ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   414  						"%s/products/%s/file_groups/%d",
   415  						apiPrefix,
   416  						productSlug,
   417  						fileGroup.ID,
   418  					)),
   419  					ghttp.VerifyJSON(expectedRequestBody),
   420  					ghttp.RespondWithJSONEncoded(http.StatusOK, response),
   421  				),
   422  			)
   423  
   424  			returned, err := client.FileGroups.Update(productSlug, fileGroup)
   425  			Expect(err).NotTo(HaveOccurred())
   426  
   427  			Expect(returned.ID).To(Equal(fileGroup.ID))
   428  			Expect(returned.Name).To(Equal(fileGroup.Name))
   429  		})
   430  
   431  		Context("when the server responds with a non-200 status code", func() {
   432  			var (
   433  				body []byte
   434  			)
   435  
   436  			BeforeEach(func() {
   437  				body = []byte(`{"message":"foo message"}`)
   438  			})
   439  
   440  			It("returns an error", func() {
   441  				server.AppendHandlers(
   442  					ghttp.CombineHandlers(
   443  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   444  							"%s/products/%s/file_groups/%d",
   445  							apiPrefix,
   446  							productSlug,
   447  							fileGroup.ID,
   448  						)),
   449  						ghttp.RespondWith(http.StatusTeapot, body),
   450  					),
   451  				)
   452  
   453  				_, err := client.FileGroups.Update(productSlug, fileGroup)
   454  
   455  				Expect(err.Error()).To(ContainSubstring("foo message"))
   456  			})
   457  		})
   458  
   459  		Context("when the json unmarshalling fails with error", func() {
   460  			It("forwards the error", func() {
   461  				server.AppendHandlers(
   462  					ghttp.CombineHandlers(
   463  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   464  							"%s/products/%s/file_groups/%d",
   465  							apiPrefix,
   466  							productSlug,
   467  							fileGroup.ID,
   468  						)),
   469  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   470  					),
   471  				)
   472  
   473  				_, err := client.FileGroups.Update(productSlug, fileGroup)
   474  				Expect(err).To(HaveOccurred())
   475  
   476  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   477  			})
   478  		})
   479  	})
   480  
   481  	Describe("Delete File Group", func() {
   482  		var (
   483  			id = 1234
   484  		)
   485  
   486  		It("deletes the file group", func() {
   487  			response := []byte(`{"id":1234}`)
   488  
   489  			server.AppendHandlers(
   490  				ghttp.CombineHandlers(
   491  					ghttp.VerifyRequest(
   492  						"DELETE",
   493  						fmt.Sprintf("%s/products/%s/file_groups/%d", apiPrefix, productSlug, id)),
   494  					ghttp.RespondWith(http.StatusOK, response),
   495  				),
   496  			)
   497  
   498  			fileGroup, err := client.FileGroups.Delete(productSlug, id)
   499  			Expect(err).NotTo(HaveOccurred())
   500  
   501  			Expect(fileGroup.ID).To(Equal(id))
   502  		})
   503  
   504  		Context("when the server responds with a non-2XX status code", func() {
   505  			var (
   506  				body []byte
   507  			)
   508  
   509  			BeforeEach(func() {
   510  				body = []byte(`{"message":"foo message"}`)
   511  			})
   512  
   513  			It("returns an error", func() {
   514  				server.AppendHandlers(
   515  					ghttp.CombineHandlers(
   516  						ghttp.VerifyRequest(
   517  							"DELETE",
   518  							fmt.Sprintf("%s/products/%s/file_groups/%d", apiPrefix, productSlug, id)),
   519  						ghttp.RespondWith(http.StatusTeapot, body),
   520  					),
   521  				)
   522  
   523  				_, err := client.FileGroups.Delete(productSlug, id)
   524  				Expect(err.Error()).To(ContainSubstring("foo message"))
   525  			})
   526  		})
   527  
   528  		Context("when the json unmarshalling fails with error", func() {
   529  			It("forwards the error", func() {
   530  				server.AppendHandlers(
   531  					ghttp.CombineHandlers(
   532  						ghttp.VerifyRequest(
   533  							"DELETE",
   534  							fmt.Sprintf("%s/products/%s/file_groups/%d", apiPrefix, productSlug, id)),
   535  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   536  					),
   537  				)
   538  
   539  				_, err := client.FileGroups.Delete(productSlug, id)
   540  				Expect(err).To(HaveOccurred())
   541  
   542  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   543  			})
   544  		})
   545  	})
   546  
   547  	Describe("Add File Group", func() {
   548  		var (
   549  			productSlug = "some-product"
   550  			releaseID   = 2345
   551  			fileGroupID = 3456
   552  
   553  			expectedRequestBody = `{"file_group":{"id":3456}}`
   554  		)
   555  
   556  		Context("when the server responds with a 204 status code", func() {
   557  			It("returns without error", func() {
   558  				server.AppendHandlers(
   559  					ghttp.CombineHandlers(
   560  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   561  							"%s/products/%s/releases/%d/add_file_group",
   562  							apiPrefix,
   563  							productSlug,
   564  							releaseID,
   565  						)),
   566  						ghttp.VerifyJSON(expectedRequestBody),
   567  						ghttp.RespondWith(http.StatusNoContent, nil),
   568  					),
   569  				)
   570  
   571  				err := client.FileGroups.AddToRelease(
   572  					productSlug,
   573  					releaseID,
   574  					fileGroupID,
   575  				)
   576  				Expect(err).NotTo(HaveOccurred())
   577  			})
   578  		})
   579  
   580  		Context("when the server responds with a non-204 status code", func() {
   581  			var (
   582  				response interface{}
   583  			)
   584  
   585  			BeforeEach(func() {
   586  				response = pivnetErr{Message: "foo message"}
   587  			})
   588  
   589  			It("returns an error", func() {
   590  				server.AppendHandlers(
   591  					ghttp.CombineHandlers(
   592  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   593  							"%s/products/%s/releases/%d/add_file_group",
   594  							apiPrefix,
   595  							productSlug,
   596  							releaseID,
   597  						)),
   598  						ghttp.RespondWithJSONEncoded(http.StatusTeapot, response),
   599  					),
   600  				)
   601  
   602  				err := client.FileGroups.AddToRelease(productSlug, releaseID, fileGroupID)
   603  				Expect(err.Error()).To(ContainSubstring("foo message"))
   604  			})
   605  		})
   606  
   607  		Context("when the json unmarshalling fails with error", func() {
   608  			It("forwards the error", func() {
   609  				server.AppendHandlers(
   610  					ghttp.CombineHandlers(
   611  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   612  							"%s/products/%s/releases/%d/add_file_group",
   613  							apiPrefix,
   614  							productSlug,
   615  							releaseID,
   616  						)),
   617  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   618  					),
   619  				)
   620  
   621  				err := client.FileGroups.AddToRelease(productSlug, releaseID, fileGroupID)
   622  				Expect(err).To(HaveOccurred())
   623  
   624  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   625  			})
   626  		})
   627  	})
   628  
   629  	Describe("Remove File Group", func() {
   630  		var (
   631  			productSlug = "some-product"
   632  			releaseID   = 2345
   633  			fileGroupID = 3456
   634  
   635  			expectedRequestBody = `{"file_group":{"id":3456}}`
   636  		)
   637  
   638  		Context("when the server responds with a 204 status code", func() {
   639  			It("returns without error", func() {
   640  				server.AppendHandlers(
   641  					ghttp.CombineHandlers(
   642  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   643  							"%s/products/%s/releases/%d/remove_file_group",
   644  							apiPrefix,
   645  							productSlug,
   646  							releaseID,
   647  						)),
   648  						ghttp.VerifyJSON(expectedRequestBody),
   649  						ghttp.RespondWith(http.StatusNoContent, nil),
   650  					),
   651  				)
   652  
   653  				err := client.FileGroups.RemoveFromRelease(
   654  					productSlug,
   655  					releaseID,
   656  					fileGroupID,
   657  				)
   658  				Expect(err).NotTo(HaveOccurred())
   659  			})
   660  		})
   661  
   662  		Context("when the server responds with a non-204 status code", func() {
   663  			var (
   664  				response interface{}
   665  			)
   666  
   667  			BeforeEach(func() {
   668  				response = pivnetErr{Message: "foo message"}
   669  			})
   670  
   671  			It("returns an error", func() {
   672  				server.AppendHandlers(
   673  					ghttp.CombineHandlers(
   674  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   675  							"%s/products/%s/releases/%d/remove_file_group",
   676  							apiPrefix,
   677  							productSlug,
   678  							releaseID,
   679  						)),
   680  						ghttp.RespondWithJSONEncoded(http.StatusTeapot, response),
   681  					),
   682  				)
   683  
   684  				err := client.FileGroups.RemoveFromRelease(productSlug, releaseID, fileGroupID)
   685  				Expect(err.Error()).To(ContainSubstring("foo message"))
   686  			})
   687  		})
   688  
   689  		Context("when the json unmarshalling fails with error", func() {
   690  			It("forwards the error", func() {
   691  				server.AppendHandlers(
   692  					ghttp.CombineHandlers(
   693  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   694  							"%s/products/%s/releases/%d/remove_file_group",
   695  							apiPrefix,
   696  							productSlug,
   697  							releaseID,
   698  						)),
   699  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   700  					),
   701  				)
   702  
   703  				err := client.FileGroups.RemoveFromRelease(productSlug, releaseID, fileGroupID)
   704  				Expect(err).To(HaveOccurred())
   705  
   706  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   707  			})
   708  		})
   709  	})
   710  })