github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/users_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"net/http"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    13  
    14  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	"github.com/onsi/gomega/ghttp"
    20  )
    21  
    22  var _ = Describe("UserRepository", func() {
    23  	var (
    24  		client api.UserRepository
    25  
    26  		config     coreconfig.ReadWriter
    27  		ccServer   *ghttp.Server
    28  		uaaServer  *ghttp.Server
    29  		ccGateway  net.Gateway
    30  		uaaGateway net.Gateway
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		ccServer = ghttp.NewServer()
    35  		uaaServer = ghttp.NewServer()
    36  
    37  		config = testconfig.NewRepositoryWithDefaults()
    38  		config.SetAPIEndpoint(ccServer.URL())
    39  		config.SetUaaEndpoint(uaaServer.URL())
    40  		ccGateway = net.NewCloudControllerGateway(config, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    41  		uaaGateway = net.NewUAAGateway(config, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    42  		client = api.NewCloudControllerUserRepository(config, uaaGateway, ccGateway)
    43  	})
    44  
    45  	AfterEach(func() {
    46  		if ccServer != nil {
    47  			ccServer.Close()
    48  		}
    49  		if uaaServer != nil {
    50  			uaaServer.Close()
    51  		}
    52  	})
    53  
    54  	Describe("FindByUsername", func() {
    55  		Context("when a username has multiple origins", func() {
    56  			BeforeEach(func() {
    57  				uaaServer.AppendHandlers(
    58  					ghttp.CombineHandlers(
    59  						ghttp.VerifyRequest("GET", "/Users"),
    60  						ghttp.RespondWith(http.StatusOK, `{
    61  							"resources": [
    62  								{ "id": "user-1-guid", "userName": "some-user" },
    63  								{ "id": "user-2-guid", "userName": "some-user" }
    64  							]}`),
    65  					),
    66  				)
    67  			})
    68  
    69  			It("returns an error", func() {
    70  				_, err := client.FindByUsername("some-user")
    71  				Expect(err).To(MatchError("The user exists in multiple origins."))
    72  			})
    73  		})
    74  	})
    75  
    76  	Describe("ListUsersInOrgForRoleWithNoUAA", func() {
    77  		Context("when there are users in the given org with the given role", func() {
    78  			BeforeEach(func() {
    79  				ccServer.AppendHandlers(
    80  					ghttp.CombineHandlers(
    81  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
    82  						ghttp.VerifyHeader(http.Header{
    83  							"accept": []string{"application/json"},
    84  						}),
    85  						ghttp.RespondWith(http.StatusOK, `{
    86  							"resources":[
    87  							{"metadata": {"guid": "user-1-guid"}, "entity": {}}
    88  							]}`),
    89  					),
    90  				)
    91  			})
    92  
    93  			It("makes a request to CC", func() {
    94  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
    95  				Expect(err).NotTo(HaveOccurred())
    96  				Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
    97  			})
    98  
    99  			It("does not make a request to UAA", func() {
   100  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   101  				Expect(err).NotTo(HaveOccurred())
   102  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   103  			})
   104  
   105  			It("returns the users", func() {
   106  				users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   107  				Expect(err).NotTo(HaveOccurred())
   108  
   109  				Expect(len(users)).To(Equal(1))
   110  				Expect(users[0].GUID).To(Equal("user-1-guid"))
   111  				Expect(users[0].Username).To(BeEmpty())
   112  			})
   113  		})
   114  
   115  		Context("when there are multiple pages of users in the given org with the given role", func() {
   116  			BeforeEach(func() {
   117  				ccServer.AppendHandlers(
   118  					ghttp.CombineHandlers(
   119  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   120  						ghttp.VerifyHeader(http.Header{
   121  							"accept": []string{"application/json"},
   122  						}),
   123  						ghttp.RespondWith(http.StatusOK, `{
   124  								"next_url": "/v2/organizations/org-guid/managers?page=2",
   125  								"resources":[
   126  								{"metadata": {"guid": "user-1-guid"}, "entity": {}}
   127  								]}`),
   128  					),
   129  					ghttp.CombineHandlers(
   130  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers", "page=2"),
   131  						ghttp.VerifyHeader(http.Header{
   132  							"accept": []string{"application/json"},
   133  						}),
   134  						ghttp.RespondWith(http.StatusOK, `{
   135  									"resources":[
   136  									{"metadata": {"guid": "user-2-guid"}, "entity": {"username":"user 2 from cc"}},
   137  									{"metadata": {"guid": "user-3-guid"}, "entity": {"username":"user 3 from cc"}}
   138  									]}`),
   139  					),
   140  				)
   141  			})
   142  
   143  			It("makes a request to CC for each page of results", func() {
   144  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   145  				Expect(err).NotTo(HaveOccurred())
   146  				Expect(ccServer.ReceivedRequests()).To(HaveLen(2))
   147  			})
   148  
   149  			It("does not make a request to UAA", func() {
   150  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   151  				Expect(err).NotTo(HaveOccurred())
   152  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   153  			})
   154  
   155  			It("returns all paginated users", func() {
   156  				users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   157  				Expect(err).NotTo(HaveOccurred())
   158  
   159  				Expect(len(users)).To(Equal(3))
   160  				Expect(users[0].GUID).To(Equal("user-1-guid"))
   161  				Expect(users[0].Username).To(BeEmpty())
   162  				Expect(users[1].GUID).To(Equal("user-2-guid"))
   163  				Expect(users[1].Username).To(Equal("user 2 from cc"))
   164  				Expect(users[2].GUID).To(Equal("user-3-guid"))
   165  				Expect(users[2].Username).To(Equal("user 3 from cc"))
   166  			})
   167  		})
   168  
   169  		Context("when there are no users in the given org with the given role", func() {
   170  			BeforeEach(func() {
   171  				ccServer.AppendHandlers(
   172  					ghttp.CombineHandlers(
   173  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   174  						ghttp.VerifyHeader(http.Header{
   175  							"accept": []string{"application/json"},
   176  						}),
   177  						ghttp.RespondWith(http.StatusOK, `{"resources":[]}`),
   178  					),
   179  				)
   180  			})
   181  
   182  			It("makes a request to CC", func() {
   183  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   184  				Expect(err).NotTo(HaveOccurred())
   185  				Expect(ccServer.ReceivedRequests()).To(HaveLen(1))
   186  			})
   187  
   188  			It("does not make a request to UAA", func() {
   189  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   190  				Expect(err).NotTo(HaveOccurred())
   191  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   192  			})
   193  
   194  			It("returns no users", func() {
   195  				users, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   196  				Expect(err).NotTo(HaveOccurred())
   197  				Expect(len(users)).To(Equal(0))
   198  			})
   199  		})
   200  
   201  		Context("when CC returns an error", func() {
   202  			BeforeEach(func() {
   203  				ccServer.AppendHandlers(
   204  					ghttp.CombineHandlers(
   205  						ghttp.VerifyRequest("GET", "/v2/organizations/org-guid/managers"),
   206  						ghttp.VerifyHeader(http.Header{
   207  							"accept": []string{"application/json"},
   208  						}),
   209  						ghttp.RespondWith(http.StatusGatewayTimeout, nil),
   210  					),
   211  				)
   212  			})
   213  
   214  			It("does not make a request to UAA", func() {
   215  				client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   216  				Expect(uaaServer.ReceivedRequests()).To(BeZero())
   217  			})
   218  
   219  			It("returns an error", func() {
   220  				_, err := client.ListUsersInOrgForRoleWithNoUAA("org-guid", models.RoleOrgManager)
   221  				httpErr, ok := err.(errors.HTTPError)
   222  				Expect(ok).To(BeTrue())
   223  				Expect(httpErr.StatusCode()).To(Equal(http.StatusGatewayTimeout))
   224  			})
   225  		})
   226  	})
   227  })