) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = config deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(authRepo) commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("auth").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{} config = testconfig.NewRepositoryWithDefaults() requirementsFactory = new(requirementsfakes.FakeFactory) authRepo = new(authenticationfakes.FakeRepository) authRepo.AuthenticateStub = func(credentials map[string]string) error { config.SetAccessToken("my-access-token") config.SetRefreshToken("my-refresh-token") return nil } fakeLogger = new(tracefakes.FakePrinter) deps = commandregistry.NewDependency(os.Stdout, fakeLogger, "") }) Describe("requirements", func() { It("fails with usage when given too few arguments", func() { testcmd.RunCLICommand("auth", []string{}, requirementsFactory, updateCommandDependency, false, ui) Expect(ui.Outputs()).To(ContainSubstrings( []string{"Incorrect Usage", "Requires", "arguments"}, ))
config.SetAPIEndpoint("http://api.the-endpoint") Expect(config.APIEndpoint()).To(Equal("http://api.the-endpoint")) config.SetAPIVersion("3") Expect(config.APIVersion()).To(Equal("3")) config.SetAuthenticationEndpoint("http://auth.the-endpoint") Expect(config.AuthenticationEndpoint()).To(Equal("http://auth.the-endpoint")) config.SetLoggregatorEndpoint("http://loggregator.the-endpoint") Expect(config.LoggregatorEndpoint()).To(Equal("http://loggregator.the-endpoint")) config.SetUaaEndpoint("http://uaa.the-endpoint") Expect(config.UaaEndpoint()).To(Equal("http://uaa.the-endpoint")) config.SetAccessToken("the-token") Expect(config.AccessToken()).To(Equal("the-token")) config.SetSSHOAuthClient("oauth-client-id") Expect(config.SSHOAuthClient()).To(Equal("oauth-client-id")) config.SetRefreshToken("the-token") Expect(config.RefreshToken()).To(Equal("the-token")) organization := models.OrganizationFields{Name: "the-org"} config.SetOrganizationFields(organization) Expect(config.OrganizationFields()).To(Equal(organization)) space := models.SpaceFields{Name: "the-space"} config.SetSpaceFields(space) Expect(config.SpaceFields()).To(Equal(space))
}) }) Describe("when there is no api endpoint set", func() { BeforeEach(func() { requirementsFactory.NewAPIEndpointRequirementReturns(requirements.Failing{Message: "no api set"}) }) It("fails requirements", func() { Expect(callTarget([]string{})).To(BeFalse()) }) }) Describe("when the user is not logged in", func() { BeforeEach(func() { config.SetAccessToken("") requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) }) It("prints the target info when no org or space is specified", func() { Expect(callTarget([]string{})).To(BeFalse()) Expect(ui.ShowConfigurationCalled).To(BeTrue()) }) It("fails requirements when targeting a space or org", func() { Expect(callTarget([]string{"-o", "some-crazy-org-im-not-in"})).To(BeFalse()) Expect(callTarget([]string{"-s", "i-love-space"})).To(BeFalse()) }) })
updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = config commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("logout").SetDependency(deps, pluginCall)) } BeforeEach(func() { org := models.OrganizationFields{} org.Name = "MyOrg" space := models.SpaceFields{} space.Name = "MySpace" config = testconfig.NewRepository() config.SetAccessToken("MyAccessToken") config.SetOrganizationFields(org) config.SetSpaceFields(space) ui = &testterm.FakeUI{} testcmd.RunCLICommand("logout", []string{}, nil, updateCommandDependency, false, ui) }) It("clears access token from the config", func() { Expect(config.AccessToken()).To(Equal("")) }) It("clears organization fields from the config", func() { Expect(config.OrganizationFields()).To(Equal(models.OrganizationFields{})) })
commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-user").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{Inputs: []string{"y"}} userRepo = new(apifakes.FakeUserRepository) requirementsFactory = new(requirementsfakes.FakeFactory) requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) configRepo = testconfig.NewRepositoryWithDefaults() token, err := testconfig.EncodeAccessToken(coreconfig.TokenInfo{ UserGUID: "admin-user-guid", Username: "******", }) Expect(err).ToNot(HaveOccurred()) configRepo.SetAccessToken(token) }) runCommand := func(args ...string) bool { return testcmd.RunCLICommand("delete-user", args, requirementsFactory, updateCommandDependency, false, ui) } Describe("requirements", func() { It("fails when not logged in", func() { requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) Expect(runCommand("my-user")).To(BeFalse()) }) It("fails with usage when no arguments are given", func() { runCommand()
Expect(err).ToNot(HaveOccurred()) Expect(result).To(BeTrue()) }) }) Context(".IsLoggedIn", func() { BeforeEach(func() { rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) err := rpcService.Start() Expect(err).ToNot(HaveOccurred()) pingCli(rpcService.Port()) }) It("returns the IsLoggedIn setting in config", func() { config.SetAccessToken("Logged-In-Token") client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port()) Expect(err).ToNot(HaveOccurred()) var result bool err = client.Call("CliRpcCmd.IsLoggedIn", "", &result) Expect(err).ToNot(HaveOccurred()) Expect(result).To(BeTrue()) }) }) Context(".HasOrganization and .HasSpace ", func() { BeforeEach(func() { rpcService, err = NewRpcService(nil, nil, config, api.RepositoryLocator{}, nil, nil, nil, rpc.DefaultServer) err := rpcService.Start() Expect(err).ToNot(HaveOccurred())
keepGoing = cb(s) if !keepGoing { return nil } } return nil } } BeforeEach(func() { Flags = []string{} Config = testconfig.NewRepository() ui = &testterm.FakeUI{} authRepo = new(authenticationfakes.FakeRepository) authRepo.AuthenticateStub = func(credentials map[string]string) error { Config.SetAccessToken("my_access_token") Config.SetRefreshToken("my_refresh_token") return nil } endpointRepo = new(coreconfigfakes.FakeEndpointRepository) minCLIVersion = "1.0.0" minRecommendedCLIVersion = "1.0.0" org = models.Organization{} org.Name = "my-new-org" org.GUID = "my-new-org-guid" orgRepo = &organizationsfakes.FakeOrganizationRepository{} orgRepo.ListOrgsReturns([]models.Organization{org}, nil) space := models.Space{}
ui *testterm.FakeUI userRepo *apifakes.FakeUserRepository config coreconfig.Repository deps commandregistry.Dependency ) BeforeEach(func() { requirementsFactory = new(requirementsfakes.FakeFactory) requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) ui = new(testterm.FakeUI) userRepo = new(apifakes.FakeUserRepository) config = testconfig.NewRepositoryWithDefaults() accessToken, _ := testconfig.EncodeAccessToken(coreconfig.TokenInfo{ Username: "******", }) config.SetAccessToken(accessToken) }) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = config deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo) commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-user").SetDependency(deps, pluginCall)) } runCommand := func(args ...string) bool { return testcmd.RunCLICommand("create-user", args, requirementsFactory, updateCommandDependency, false, ui) } It("creates a user", func() { runCommand("my-user", "my-password")