github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/users_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"time"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api"
    10  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    11  	"code.cloudfoundry.org/cli/cf/errors"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/net"
    14  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    15  
    16  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    17  	testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration"
    18  
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  	"github.com/onsi/gomega/ghttp"
    22  )
    23  
    24  var _ = Describe("UserRepository", func() {
    25  	var (
    26  		client api.UserRepository
    27  
    28  		config     coreconfig.ReadWriter
    29  		ccServer   *ghttp.Server
    30  		uaaServer  *ghttp.Server
    31  		ccGateway  net.Gateway
    32  		uaaGateway net.Gateway
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		ccServer = ghttp.NewServer()
    37  		uaaServer = ghttp.NewServer()
    38  
    39  		config = testconfig.NewRepositoryWithDefaults()
    40  		config.SetAPIEndpoint(ccServer.URL())
    41  		config.SetUaaEndpoint(uaaServer.URL())
    42  		ccGateway = net.NewCloudControllerGateway(config, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    43  		uaaGateway = net.NewUAAGateway(config, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    44  		client = api.NewCloudControllerUserRepository(config, uaaGateway, ccGateway)
    45  	})
    46  
    47  	AfterEach(func() {
    48  		if ccServer != nil {
    49  			ccServer.Close()
    50  		}
    51  		if uaaServer != nil {
    52  			uaaServer.Close()
    53  		}
    54  	})
    55  
    56  	Describe("FindByUsername", func() {
    57  		Context("when a username has multiple origins", func() {
    58  			BeforeEach(func() {
    59  				uaaServer.AppendHandlers(
    60  					ghttp.CombineHandlers(
    61  						ghttp.VerifyRequest("GET", "/Users"),
    62  						ghttp.RespondWith(http.StatusOK, `{
    63  							"resources": [
    64  								{ "id": "user-1-guid", "userName": "some-user" },
    65  								{ "id": "user-2-guid", "userName": "some-user" }
    66  							]}`),
    67  					),
    68  				)
    69  			})
    70  
    71  			It("returns an error", func() {
    72  				_, err := client.FindByUsername("some-user")
    73  				Expect(err).To(MatchError("The user exists in multiple origins."))
    74  			})
    75  		})
    76  	})
    77  
    78  	Describe("ListUsersInOrgForRole", func() {
    79  		Context("when there are no users in the given org with the given role", func() {
    80  			BeforeEach(func() {
    81  				ccServer.AppendHandlers(
    82  					ghttp.CombineHandlers(
    83  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
    84  						ghttp.VerifyHeader(http.Header{
    85  							"accept": []string{"application/json"},
    86  						}),
    87  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
    88  					),
    89  				)
    90  			})
    91  
    92  			It("makes a request to CC", func() {
    93  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
    94  				Expect(err).NotTo(HaveOccurred())
    95  				Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
    96  			})
    97  
    98  			It("returns no users", func() {
    99  				users, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   100  				Expect(err).NotTo(HaveOccurred())
   101  				Expect(len(users)).To(Equal(0))
   102  			})
   103  		})
   104  
   105  		Context("when there are users in the given org with the given role", func() {
   106  			BeforeEach(func() {
   107  				ccServer.AppendHandlers(
   108  					ghttp.CombineHandlers(
   109  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   110  						ghttp.VerifyHeader(http.Header{
   111  							"accept": []string{"application/json"},
   112  						}),
   113  						ghttp.RespondWith(http.StatusOK, `{
   114  							"resources":[
   115  							{"metadata": {"guid": "user-1-guid"}, "entity": {}}
   116  							]}`),
   117  					),
   118  				)
   119  
   120  				uaaServer.AppendHandlers(
   121  					ghttp.CombineHandlers(
   122  						ghttp.VerifyRequest("GET", "/Users", fmt.Sprintf("attributes=id,userName&filter=%s", url.QueryEscape(`ID eq "user-1-guid"`))),
   123  						ghttp.VerifyHeader(http.Header{
   124  							"accept": []string{"application/json"},
   125  						}),
   126  						ghttp.RespondWith(http.StatusOK, `{
   127  							"resources": [
   128  							{ "id": "user-1-guid", "userName": "Super user 1" }
   129  							]}`),
   130  					),
   131  				)
   132  			})
   133  
   134  			It("makes a request to CC", func() {
   135  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   136  				Expect(err).NotTo(HaveOccurred())
   137  				Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   138  			})
   139  
   140  			It("makes a request to UAA", func() {
   141  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   142  				Expect(err).NotTo(HaveOccurred())
   143  				Expect(uaaServer.ReceivedRequests()).To(HaveLen(1))
   144  			})
   145  
   146  			It("returns the users", func() {
   147  				users, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   148  				Expect(err).NotTo(HaveOccurred())
   149  
   150  				Expect(len(users)).To(Equal(1))
   151  				Expect(users[0].GUID).To(Equal("user-1-guid"))
   152  				Expect(users[0].Username).To(Equal("Super user 1"))
   153  			})
   154  		})
   155  
   156  		Context("when there are multiple pages of users in the given org with the given role", func() {
   157  			BeforeEach(func() {
   158  				ccServer.AppendHandlers(
   159  					ghttp.CombineHandlers(
   160  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   161  						ghttp.VerifyHeader(http.Header{
   162  							"accept": []string{"application/json"},
   163  						}),
   164  						ghttp.RespondWith(http.StatusOK, `{
   165  								"next_url": "/v2/organizations/org-guid/managers?page=2",
   166  								"resources":[
   167  								{"metadata": {"guid": "user-1-guid"}, "entity": {}}
   168  								]}`),
   169  					),
   170  					ghttp.CombineHandlers(
   171  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers", "page=2"),
   172  						ghttp.VerifyHeader(http.Header{
   173  							"accept": []string{"application/json"},
   174  						}),
   175  						ghttp.RespondWith(http.StatusOK, `{
   176  								"resources":[
   177  								{"metadata": {"guid": "user-2-guid"}, "entity": {"username":"user 2 from cc"}},
   178  								{"metadata": {"guid": "user-3-guid"}, "entity": {"username":"user 3 from cc"}}
   179  								]}`),
   180  					),
   181  				)
   182  
   183  				uaaServer.AppendHandlers(
   184  					ghttp.CombineHandlers(
   185  						ghttp.VerifyRequest("GET", "/Users", fmt.Sprintf("attributes=id,userName&filter=%s", url.QueryEscape(`ID eq "user-1-guid" or ID eq "user-2-guid" or ID eq "user-3-guid"`))),
   186  						ghttp.VerifyHeader(http.Header{
   187  							"accept": []string{"application/json"},
   188  						}),
   189  						ghttp.RespondWith(http.StatusOK, `{
   190  								"resources": [
   191  								{ "id": "user-1-guid", "userName": "Super user 1" },
   192  								{ "id": "user-2-guid", "userName": "Super user 2" },
   193  								{ "id": "user-3-guid", "userName": "Super user 3" }
   194  								]
   195  							}`),
   196  					),
   197  				)
   198  			})
   199  
   200  			It("makes a request to CC for each page of results", func() {
   201  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   202  				Expect(err).NotTo(HaveOccurred())
   203  				Expect(ccServer.ReceivedRequests()).To(HaveLen(2))
   204  			})
   205  
   206  			It("makes a request to UAA", func() {
   207  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   208  				Expect(err).NotTo(HaveOccurred())
   209  				Expect(uaaServer.ReceivedRequests()).To(HaveLen(1))
   210  			})
   211  
   212  			It("returns all paginated users", func() {
   213  				users, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   214  				Expect(err).NotTo(HaveOccurred())
   215  
   216  				Expect(len(users)).To(Equal(3))
   217  				Expect(users[0].GUID).To(Equal("user-1-guid"))
   218  				Expect(users[0].Username).To(Equal("Super user 1"))
   219  				Expect(users[1].GUID).To(Equal("user-2-guid"))
   220  				Expect(users[1].Username).To(Equal("Super user 2"))
   221  				Expect(users[2].GUID).To(Equal("user-3-guid"))
   222  				Expect(users[2].Username).To(Equal("Super user 3"))
   223  			})
   224  		})
   225  
   226  		Context("when CC returns an error", func() {
   227  			BeforeEach(func() {
   228  				ccServer.AppendHandlers(
   229  					ghttp.CombineHandlers(
   230  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   231  						ghttp.VerifyHeader(http.Header{
   232  							"accept": []string{"application/json"},
   233  						}),
   234  						ghttp.RespondWith(http.StatusGatewayTimeout, nil),
   235  					),
   236  				)
   237  			})
   238  
   239  			It("does not make a request to UAA", func() {
   240  				client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   241  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   242  			})
   243  
   244  			It("returns an error", func() {
   245  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   246  				httpErr, ok := err.(errors.HTTPError)
   247  				Expect(ok).To(BeTrue())
   248  				Expect(httpErr.StatusCode()).To(Equal(http.StatusGatewayTimeout))
   249  			})
   250  		})
   251  
   252  		Context("when the UAA endpoint has not been configured", func() {
   253  			BeforeEach(func() {
   254  				ccServer.AppendHandlers(
   255  					ghttp.CombineHandlers(
   256  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   257  						ghttp.VerifyHeader(http.Header{
   258  							"accept": []string{"application/json"},
   259  						}),
   260  						ghttp.RespondWith(http.StatusOK, `{
   261  							"resources":[
   262  							{"metadata": {"guid": "user-1-guid"}, "entity": {}}
   263  							]}`),
   264  					),
   265  				)
   266  				config.SetUaaEndpoint("")
   267  			})
   268  
   269  			It("returns an error", func() {
   270  				_, err := client.ListUsersInOrgForRole("org-guid", models.RoleOrgManager)
   271  				Expect(err).To(HaveOccurred())
   272  			})
   273  		})
   274  	})
   275  
   276  	Describe("ListUsersInOrgForRoleWithNoUAA", func() {
   277  		Context("when there are users in the given org with the given role", func() {
   278  			BeforeEach(func() {
   279  				ccServer.AppendHandlers(
   280  					ghttp.CombineHandlers(
   281  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   282  						ghttp.VerifyHeader(http.Header{
   283  							"accept": []string{"application/json"},
   284  						}),
   285  						ghttp.RespondWith(http.StatusOK, `{
   286  							"resources":[
   287  							{"metadata": {"guid": "user-1-guid"}, "entity": {}}
   288  							]}`),
   289  					),
   290  				)
   291  			})
   292  
   293  			It("makes a request to CC", func() {
   294  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   295  				Expect(err).NotTo(HaveOccurred())
   296  				Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   297  			})
   298  
   299  			It("does not make a request to UAA", func() {
   300  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   301  				Expect(err).NotTo(HaveOccurred())
   302  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   303  			})
   304  
   305  			It("returns the users", func() {
   306  				users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   307  				Expect(err).NotTo(HaveOccurred())
   308  
   309  				Expect(len(users)).To(Equal(1))
   310  				Expect(users[0].GUID).To(Equal("user-1-guid"))
   311  				Expect(users[0].Username).To(BeEmpty())
   312  			})
   313  		})
   314  
   315  		Context("when there are multiple pages of users in the given org with the given role", func() {
   316  			BeforeEach(func() {
   317  				ccServer.AppendHandlers(
   318  					ghttp.CombineHandlers(
   319  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   320  						ghttp.VerifyHeader(http.Header{
   321  							"accept": []string{"application/json"},
   322  						}),
   323  						ghttp.RespondWith(http.StatusOK, `{
   324  								"next_url": "/v2/organizations/org-guid/managers?page=2",
   325  								"resources":[
   326  								{"metadata": {"guid": "user-1-guid"}, "entity": {}}
   327  								]}`),
   328  					),
   329  					ghttp.CombineHandlers(
   330  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers", "page=2"),
   331  						ghttp.VerifyHeader(http.Header{
   332  							"accept": []string{"application/json"},
   333  						}),
   334  						ghttp.RespondWith(http.StatusOK, `{
   335  									"resources":[
   336  									{"metadata": {"guid": "user-2-guid"}, "entity": {"username":"user 2 from cc"}},
   337  									{"metadata": {"guid": "user-3-guid"}, "entity": {"username":"user 3 from cc"}}
   338  									]}`),
   339  					),
   340  				)
   341  			})
   342  
   343  			It("makes a request to CC for each page of results", func() {
   344  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   345  				Expect(err).NotTo(HaveOccurred())
   346  				Expect(ccServer.ReceivedRequests()).To(HaveLen(2))
   347  			})
   348  
   349  			It("does not make a request to UAA", func() {
   350  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   351  				Expect(err).NotTo(HaveOccurred())
   352  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   353  			})
   354  
   355  			It("returns all paginated users", func() {
   356  				users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   357  				Expect(err).NotTo(HaveOccurred())
   358  
   359  				Expect(len(users)).To(Equal(3))
   360  				Expect(users[0].GUID).To(Equal("user-1-guid"))
   361  				Expect(users[0].Username).To(BeEmpty())
   362  				Expect(users[1].GUID).To(Equal("user-2-guid"))
   363  				Expect(users[1].Username).To(Equal("user 2 from cc"))
   364  				Expect(users[2].GUID).To(Equal("user-3-guid"))
   365  				Expect(users[2].Username).To(Equal("user 3 from cc"))
   366  			})
   367  		})
   368  
   369  		Context("when there are no users in the given org with the given role", func() {
   370  			BeforeEach(func() {
   371  				ccServer.AppendHandlers(
   372  					ghttp.CombineHandlers(
   373  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   374  						ghttp.VerifyHeader(http.Header{
   375  							"accept": []string{"application/json"},
   376  						}),
   377  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   378  					),
   379  				)
   380  			})
   381  
   382  			It("makes a request to CC", func() {
   383  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   384  				Expect(err).NotTo(HaveOccurred())
   385  				Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   386  			})
   387  
   388  			It("does not make a request to UAA", func() {
   389  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   390  				Expect(err).NotTo(HaveOccurred())
   391  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   392  			})
   393  
   394  			It("returns no users", func() {
   395  				users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   396  				Expect(err).NotTo(HaveOccurred())
   397  				Expect(len(users)).To(Equal(0))
   398  			})
   399  		})
   400  
   401  		Context("when CC returns an error", func() {
   402  			BeforeEach(func() {
   403  				ccServer.AppendHandlers(
   404  					ghttp.CombineHandlers(
   405  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   406  						ghttp.VerifyHeader(http.Header{
   407  							"accept": []string{"application/json"},
   408  						}),
   409  						ghttp.RespondWith(http.StatusGatewayTimeout, nil),
   410  					),
   411  				)
   412  			})
   413  
   414  			It("does not make a request to UAA", func() {
   415  				client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   416  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   417  			})
   418  
   419  			It("returns an error", func() {
   420  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   421  				httpErr, ok := err.(errors.HTTPError)
   422  				Expect(ok).To(BeTrue())
   423  				Expect(httpErr.StatusCode()).To(Equal(http.StatusGatewayTimeout))
   424  			})
   425  		})
   426  	})
   427  })