github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/clients_test.go (about) 1 package api_test 2 3 import ( 4 "fmt" 5 "net/http" 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/net" 11 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 12 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 17 "github.com/onsi/gomega/ghttp" 18 ) 19 20 var _ = Describe("ClientRepository", func() { 21 Describe("ClientExists", func() { 22 var ( 23 client api.ClientRepository 24 uaaServer *ghttp.Server 25 uaaGateway net.Gateway 26 config coreconfig.ReadWriter 27 ) 28 29 BeforeEach(func() { 30 uaaServer = ghttp.NewServer() 31 config = testconfig.NewRepositoryWithDefaults() 32 33 config.SetUaaEndpoint(uaaServer.URL()) 34 uaaGateway = net.NewUAAGateway(config, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 35 client = api.NewCloudControllerClientRepository(config, uaaGateway) 36 }) 37 38 Context("when a client does not exist", func() { 39 var clientID string 40 BeforeEach(func() { 41 clientID = "some-client" 42 43 requestPath := fmt.Sprintf("/oauth/clients/%s", clientID) 44 45 uaaServer.AppendHandlers( 46 ghttp.CombineHandlers( 47 ghttp.VerifyRequest("GET", requestPath), 48 ghttp.RespondWith(http.StatusNotFound, ""), 49 ), 50 ) 51 }) 52 53 It("returns a ModelNotFound error", func() { 54 b, err := client.ClientExists("some-client") 55 Expect(err).To(MatchError(&errors.ModelNotFoundError{ 56 ModelType: "Client", 57 ModelName: "some-client", 58 })) 59 Expect(b).To(BeFalse()) 60 }) 61 }) 62 63 Context("when the active user has insufficient permissions", func() { 64 var clientID string 65 BeforeEach(func() { 66 clientID = "some-client" 67 68 requestPath := fmt.Sprintf("/oauth/clients/%s", clientID) 69 70 uaaServer.AppendHandlers( 71 ghttp.CombineHandlers( 72 ghttp.VerifyRequest("GET", requestPath), 73 ghttp.RespondWith(http.StatusForbidden, ""), 74 ), 75 ) 76 }) 77 78 It("returns an AccessDenied error", func() { 79 b, err := client.ClientExists(clientID) 80 Expect(err).To(MatchError(&errors.AccessDeniedError{})) 81 Expect(b).To(BeFalse()) 82 }) 83 }) 84 85 Context("when a client does exist", func() { 86 var clientID string 87 BeforeEach(func() { 88 clientID = "some-client" 89 90 requestPath := fmt.Sprintf("/oauth/clients/%s", clientID) 91 92 uaaServer.AppendHandlers( 93 ghttp.CombineHandlers( 94 ghttp.VerifyRequest("GET", requestPath), 95 ghttp.RespondWith(http.StatusOK, ""), 96 ), 97 ) 98 }) 99 100 It("returns true and no error", func() { 101 b, err := client.ClientExists("some-client") 102 103 Expect(b).To(BeTrue()) 104 Expect(err).To(BeNil()) 105 106 }) 107 }) 108 109 Context("when getAuthEndpoint fails", func() { 110 var executeErr error 111 112 BeforeEach(func() { 113 executeErr = errors.New("UAA endpoint missing from config file") 114 config.SetUaaEndpoint("") 115 }) 116 117 It("returns that error", func() { 118 b, err := client.ClientExists("some-client") 119 Expect(b).To(BeFalse()) 120 Expect(err).To(MatchError(executeErr)) 121 }) 122 }) 123 }) 124 125 })