github.com/pivotal-cf/go-pivnet/v6@v6.0.2/subscription_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 - subscription groups", 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  		response               interface{}
    28  		responseStatusCode     int
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		server = ghttp.NewServer()
    33  		apiAddress = server.URL()
    34  		userAgent = "pivnet-resource/0.1.0 (some-url)"
    35  
    36  		fakeLogger = &loggerfakes.FakeLogger{}
    37  		fakeAccessTokenService = &gopivnetfakes.FakeAccessTokenService{}
    38  		newClientConfig = pivnet.ClientConfig{
    39  			Host:      apiAddress,
    40  			UserAgent: userAgent,
    41  		}
    42  		client = pivnet.NewClient(fakeAccessTokenService, newClientConfig, fakeLogger)
    43  
    44  		responseStatusCode = http.StatusOK
    45  	})
    46  
    47  	AfterEach(func() {
    48  		server.Close()
    49  	})
    50  
    51  	Describe("List", func() {
    52  		It("returns all subscription groups", func() {
    53  			response := `{"subscription_groups": [{"id":2,"name":"subscription group 1"},{"id": 3, "name": "subscription group 2"}]}`
    54  
    55  			server.AppendHandlers(
    56  				ghttp.CombineHandlers(
    57  					ghttp.VerifyRequest("GET", fmt.Sprintf("%s/subscription_groups", apiPrefix)),
    58  					ghttp.RespondWith(http.StatusOK, response),
    59  				),
    60  			)
    61  
    62  			subscriptionGroups, err := client.SubscriptionGroups.List()
    63  			Expect(err).NotTo(HaveOccurred())
    64  
    65  			Expect(subscriptionGroups).To(HaveLen(2))
    66  			Expect(subscriptionGroups[0].ID).To(Equal(2))
    67  			Expect(subscriptionGroups[1].ID).To(Equal(3))
    68  		})
    69  
    70  		Context("when the server responds with a non-2XX status code", func() {
    71  			var (
    72  				body []byte
    73  			)
    74  
    75  			BeforeEach(func() {
    76  				body = []byte(`{"message":"foo message"}`)
    77  			})
    78  
    79  			It("returns an error", func() {
    80  				server.AppendHandlers(
    81  					ghttp.CombineHandlers(
    82  						ghttp.VerifyRequest("GET", fmt.Sprintf("%s/subscription_groups", apiPrefix)),
    83  						ghttp.RespondWith(http.StatusTeapot, body),
    84  					),
    85  				)
    86  
    87  				_, err := client.SubscriptionGroups.List()
    88  				Expect(err.Error()).To(ContainSubstring("foo message"))
    89  			})
    90  		})
    91  
    92  		Context("when the json unmarshalling fails with error", func() {
    93  			It("forwards the error", func() {
    94  				server.AppendHandlers(
    95  					ghttp.CombineHandlers(
    96  						ghttp.VerifyRequest("GET", fmt.Sprintf("%s/subscription_groups", apiPrefix)),
    97  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
    98  					),
    99  				)
   100  
   101  				_, err := client.SubscriptionGroups.List()
   102  				Expect(err).To(HaveOccurred())
   103  
   104  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   105  			})
   106  		})
   107  	})
   108  
   109  	Describe("Get Subscription Group", func() {
   110  		var (
   111  			subscriptionGroupID int
   112  		)
   113  
   114  		BeforeEach(func() {
   115  			subscriptionGroupID = 1234
   116  
   117  			response = pivnet.SubscriptionGroup{
   118  				ID:   subscriptionGroupID,
   119  				Name: "some subscription group",
   120  				Members: []pivnet.SubscriptionGroupMember{
   121  					{
   122  						ID:      4321,
   123  						Name:    "subscription group member 1",
   124  						Email:   "dude@dude.dude",
   125  						IsAdmin: false,
   126  					},
   127  					{
   128  						ID:      9876,
   129  						Name:    "subscription group member 2",
   130  						Email:   "buddy@buddy.buddy",
   131  						IsAdmin: true,
   132  					},
   133  				},
   134  				PendingInvitations: []string{},
   135  				Subscriptions:      []pivnet.SubscriptionGroupSubscription{},
   136  			}
   137  		})
   138  
   139  		JustBeforeEach(func() {
   140  			server.AppendHandlers(
   141  				ghttp.CombineHandlers(
   142  					ghttp.VerifyRequest(
   143  						"GET",
   144  						fmt.Sprintf(
   145  							"%s/subscription_groups/%d",
   146  							apiPrefix,
   147  							subscriptionGroupID,
   148  						),
   149  					),
   150  					ghttp.RespondWithJSONEncoded(responseStatusCode, response),
   151  				),
   152  			)
   153  		})
   154  
   155  		It("returns subscription group without errors", func() {
   156  			_, err := client.SubscriptionGroups.Get(subscriptionGroupID)
   157  
   158  			Expect(err).NotTo(HaveOccurred())
   159  		})
   160  
   161  		Context("when the server responds with a non-2XX status code", func() {
   162  			var (
   163  				body []byte
   164  			)
   165  
   166  			BeforeEach(func() {
   167  				response = pivnetErr{Message: "foo message"}
   168  				responseStatusCode = http.StatusTeapot
   169  			})
   170  
   171  			It("returns an error", func() {
   172  				server.AppendHandlers(
   173  					ghttp.CombineHandlers(
   174  						ghttp.VerifyRequest(
   175  							"GET",
   176  							fmt.Sprintf(
   177  								"%s/subscription_groups/%d",
   178  								apiPrefix,
   179  								subscriptionGroupID,
   180  							),
   181  						),
   182  						ghttp.RespondWith(responseStatusCode, body),
   183  					),
   184  				)
   185  
   186  				_, err := client.SubscriptionGroups.Get(
   187  					subscriptionGroupID,
   188  				)
   189  				Expect(err).To(HaveOccurred())
   190  
   191  				Expect(err.Error()).To(ContainSubstring("foo message"))
   192  			})
   193  		})
   194  	})
   195  
   196  	Describe("AddMember", func() {
   197  		var (
   198  			subscriptionGroupID int
   199  			memberEmailAddress  string
   200  			expectedRequestBody string
   201  		)
   202  
   203  		BeforeEach(func() {
   204  			subscriptionGroupID = 1234
   205  			memberEmailAddress = "dude@dude.dude"
   206  
   207  			response = pivnet.SubscriptionGroup{
   208  				ID:   subscriptionGroupID,
   209  				Name: "some subscription group",
   210  				Members: []pivnet.SubscriptionGroupMember{
   211  					{
   212  						ID:      4321,
   213  						Name:    "subscription group member 1",
   214  						Email:   "dude@dude.dude",
   215  						IsAdmin: false,
   216  					},
   217  					{
   218  						ID:      9876,
   219  						Name:    "subscription group member 2",
   220  						Email:   "buddy@buddy.buddy",
   221  						IsAdmin: true,
   222  					},
   223  				},
   224  				PendingInvitations: []string{},
   225  				Subscriptions:      []pivnet.SubscriptionGroupSubscription{},
   226  			}
   227  
   228  			expectedRequestBody = fmt.Sprintf(
   229  				`{"member":{"email":"%s","admin":false}}`,
   230  				memberEmailAddress,
   231  			)
   232  		})
   233  
   234  		It("should return the changed subscription group when successful", func() {
   235  			server.AppendHandlers(
   236  				ghttp.CombineHandlers(
   237  					ghttp.VerifyRequest(
   238  						"PATCH",
   239  						fmt.Sprintf(
   240  							"%s/subscription_groups/%d/add_member",
   241  							apiPrefix,
   242  							subscriptionGroupID,
   243  						),
   244  					),
   245  					ghttp.VerifyJSON(expectedRequestBody),
   246  					ghttp.RespondWithJSONEncoded(responseStatusCode, response),
   247  				),
   248  			)
   249  
   250  			_, err := client.SubscriptionGroups.AddMember(subscriptionGroupID, memberEmailAddress, "false")
   251  
   252  			Expect(err).NotTo(HaveOccurred())
   253  		})
   254  
   255  		Context("when the server responds with a non-2XX status code", func() {
   256  			var (
   257  				body []byte
   258  			)
   259  
   260  			BeforeEach(func() {
   261  				body = []byte(`{"message":"foo message"}`)
   262  			})
   263  
   264  			It("returns an error", func() {
   265  				server.AppendHandlers(
   266  					ghttp.CombineHandlers(
   267  						ghttp.VerifyRequest("PATCH", fmt.Sprintf("%s/subscription_groups/%d/add_member", apiPrefix, 1234)),
   268  						ghttp.RespondWith(http.StatusTeapot, body),
   269  					),
   270  				)
   271  
   272  				_, err := client.SubscriptionGroups.AddMember(1234, "dude@dude.dude", "false")
   273  				Expect(err.Error()).To(ContainSubstring("foo message"))
   274  			})
   275  		})
   276  
   277  		Context("when the json unmarshalling fails with error", func() {
   278  			It("forwards the error", func() {
   279  				server.AppendHandlers(
   280  					ghttp.CombineHandlers(
   281  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   282  							"%s/subscription_groups/%d/add_member",
   283  							apiPrefix,
   284  							4321,
   285  						)),
   286  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   287  					),
   288  				)
   289  
   290  				_, err := client.SubscriptionGroups.AddMember(4321, memberEmailAddress, "false")
   291  				Expect(err).To(HaveOccurred())
   292  
   293  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   294  			})
   295  		})
   296  	})
   297  
   298  	Describe("RemoveMember", func() {
   299  		var (
   300  			subscriptionGroupID int
   301  			memberEmailAddress  string
   302  			expectedRequestBody string
   303  		)
   304  
   305  		BeforeEach(func() {
   306  			subscriptionGroupID = 1234
   307  			memberEmailAddress = "dude@dude.dude"
   308  
   309  			response = pivnet.SubscriptionGroup{
   310  				ID:   subscriptionGroupID,
   311  				Name: "some subscription group",
   312  				Members: []pivnet.SubscriptionGroupMember{
   313  					{
   314  						ID:      4321,
   315  						Name:    "subscription group member 1",
   316  						Email:   "dude@dude.dude",
   317  						IsAdmin: false,
   318  					},
   319  					{
   320  						ID:      9876,
   321  						Name:    "subscription group member 2",
   322  						Email:   "buddy@buddy.buddy",
   323  						IsAdmin: true,
   324  					},
   325  				},
   326  				PendingInvitations: []string{},
   327  				Subscriptions:      []pivnet.SubscriptionGroupSubscription{},
   328  			}
   329  
   330  			expectedRequestBody = fmt.Sprintf(
   331  				`{"member":{"email":"%s"}}`,
   332  				memberEmailAddress,
   333  			)
   334  		})
   335  
   336  		It("should return the changed subscription group when successful", func() {
   337  			server.AppendHandlers(
   338  				ghttp.CombineHandlers(
   339  					ghttp.VerifyRequest(
   340  						"PATCH",
   341  						fmt.Sprintf(
   342  							"%s/subscription_groups/%d/remove_member",
   343  							apiPrefix,
   344  							subscriptionGroupID,
   345  						),
   346  					),
   347  					ghttp.VerifyJSON(expectedRequestBody),
   348  					ghttp.RespondWithJSONEncoded(responseStatusCode, response),
   349  				),
   350  			)
   351  
   352  			_, err := client.SubscriptionGroups.RemoveMember(subscriptionGroupID, memberEmailAddress)
   353  
   354  			Expect(err).NotTo(HaveOccurred())
   355  		})
   356  
   357  		Context("when the server responds with a non-2XX status code", func() {
   358  			var (
   359  				body []byte
   360  			)
   361  
   362  			BeforeEach(func() {
   363  				body = []byte(`{"message":"foo message"}`)
   364  			})
   365  
   366  			It("returns an error", func() {
   367  				server.AppendHandlers(
   368  					ghttp.CombineHandlers(
   369  						ghttp.VerifyRequest("PATCH", fmt.Sprintf("%s/subscription_groups/%d/remove_member", apiPrefix, 1234)),
   370  						ghttp.RespondWith(http.StatusTeapot, body),
   371  					),
   372  				)
   373  
   374  				_, err := client.SubscriptionGroups.RemoveMember(1234, "dude@dude.dude")
   375  				Expect(err.Error()).To(ContainSubstring("foo message"))
   376  			})
   377  		})
   378  
   379  		Context("when the json unmarshalling fails with error", func() {
   380  			It("forwards the error", func() {
   381  				server.AppendHandlers(
   382  					ghttp.CombineHandlers(
   383  						ghttp.VerifyRequest("PATCH", fmt.Sprintf(
   384  							"%s/subscription_groups/%d/remove_member",
   385  							apiPrefix,
   386  							4321,
   387  						)),
   388  						ghttp.RespondWith(http.StatusTeapot, "%%%"),
   389  					),
   390  				)
   391  
   392  				_, err := client.SubscriptionGroups.RemoveMember(4321, memberEmailAddress)
   393  				Expect(err).To(HaveOccurred())
   394  
   395  				Expect(err.Error()).To(ContainSubstring("invalid character"))
   396  			})
   397  		})
   398  	})
   399  
   400  })