} var _ = Describe("api command", func() { var ( config configuration.ReadWriter endpointRepo *testapi.FakeEndpointRepo ) BeforeEach(func() { config = testconfig.NewRepository() endpointRepo = &testapi.FakeEndpointRepo{} }) Context("when the api endpoint's ssl certificate is invalid", func() { It("warns the user and prints out a tip", func() { endpointRepo.UpdateEndpointError = errors.NewInvalidSSLCert("https://buttontomatoes.org", "why? no. go away") ui := callApi([]string{"https://buttontomatoes.org"}, config, endpointRepo) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"FAILED"}, {"SSL cert", "https://buttontomatoes.org"}, {"TIP", "--skip-ssl-validation"}, }) }) }) Context("when the user does not provide an endpoint", func() { Context("when the endpoint is set in the config", func() { var ( ui *testterm.FakeUI ctx *cli.Context
l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, testcmd.NewContext("login", Flags), nil) Expect(Config.ApiEndpoint()).To(Equal("api.example.com")) Expect(Config.OrganizationFields().Guid).To(BeEmpty()) Expect(Config.SpaceFields().Guid).To(BeEmpty()) Expect(Config.AccessToken()).To(BeEmpty()) Expect(Config.RefreshToken()).To(BeEmpty()) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"Failed"}, }) }) It("fails when the /v2/info API returns an error", func() { endpointRepo.UpdateEndpointError = errors.NewErrorWithMessage("Server error") ui.Inputs = []string{"api.example.com"} l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, testcmd.NewContext("login", Flags), nil) Expect(Config.ApiEndpoint()).To(BeEmpty()) Expect(Config.OrganizationFields().Guid).To(BeEmpty()) Expect(Config.SpaceFields().Guid).To(BeEmpty()) Expect(Config.AccessToken()).To(BeEmpty()) Expect(Config.RefreshToken()).To(BeEmpty()) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"Failed"}, })
Config.SetSSLDisabled(false) }) ItSucceeds() ItShowsTheTarget() It("stores the API endpoint and the skip-ssl flag", func() { Expect(endpointRepo.UpdateEndpointReceived).To(Equal("https://api.the-server.com")) Expect(Config.IsSSLDisabled()).To(BeTrue()) }) }) Describe("setting api endpoint failed", func() { BeforeEach(func() { Config.SetSSLDisabled(true) endpointRepo.UpdateEndpointError = errors.New("API endpoint not found") }) ItFails() ItDoesntShowTheTarget() It("clears the entire config", func() { Expect(Config.ApiEndpoint()).To(BeEmpty()) Expect(Config.IsSSLDisabled()).To(BeFalse()) Expect(Config.AccessToken()).To(BeEmpty()) Expect(Config.RefreshToken()).To(BeEmpty()) Expect(Config.OrganizationFields().Guid).To(BeEmpty()) Expect(Config.SpaceFields().Guid).To(BeEmpty()) }) }) })
l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, testcmd.NewContext("login", Flags), nil) Expect(Config.ApiEndpoint()).To(Equal("api.example.com")) Expect(Config.OrganizationFields().Guid).To(BeEmpty()) Expect(Config.SpaceFields().Guid).To(BeEmpty()) Expect(Config.AccessToken()).To(BeEmpty()) Expect(Config.RefreshToken()).To(BeEmpty()) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"Failed"}, }) }) It("fails when the /v2/info API returns an error", func() { endpointRepo.UpdateEndpointError = net.NewApiResponseWithMessage("Server error") ui.Inputs = []string{"api.example.com"} l := NewLogin(ui, Config, authRepo, endpointRepo, orgRepo, spaceRepo) testcmd.RunCommand(l, testcmd.NewContext("login", Flags), nil) Expect(Config.ApiEndpoint()).To(BeEmpty()) Expect(Config.OrganizationFields().Guid).To(BeEmpty()) Expect(Config.SpaceFields().Guid).To(BeEmpty()) Expect(Config.AccessToken()).To(BeEmpty()) Expect(Config.RefreshToken()).To(BeEmpty()) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"Failed"}, })