github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/user/create_user_test.go (about) 1 package user_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/apifakes" 5 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 6 "code.cloudfoundry.org/cli/cf/errors" 7 "code.cloudfoundry.org/cli/cf/requirements" 8 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 9 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 10 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 11 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 15 "code.cloudfoundry.org/cli/cf/commandregistry" 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 ) 18 19 var _ = Describe("Create user command", func() { 20 var ( 21 requirementsFactory *requirementsfakes.FakeFactory 22 ui *testterm.FakeUI 23 userRepo *apifakes.FakeUserRepository 24 config coreconfig.Repository 25 deps commandregistry.Dependency 26 ) 27 28 BeforeEach(func() { 29 requirementsFactory = new(requirementsfakes.FakeFactory) 30 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 31 ui = new(testterm.FakeUI) 32 userRepo = new(apifakes.FakeUserRepository) 33 config = testconfig.NewRepositoryWithDefaults() 34 accessToken, _ := testconfig.EncodeAccessToken(coreconfig.TokenInfo{ 35 Username: "current-user", 36 }) 37 config.SetAccessToken(accessToken) 38 }) 39 40 updateCommandDependency := func(pluginCall bool) { 41 deps.UI = ui 42 deps.Config = config 43 deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo) 44 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-user").SetDependency(deps, pluginCall)) 45 } 46 47 runCommand := func(args ...string) bool { 48 return testcmd.RunCLICommand("create-user", args, requirementsFactory, updateCommandDependency, false, ui) 49 } 50 51 It("creates a user", func() { 52 runCommand("my-user", "my-password") 53 54 Expect(ui.Outputs()).To(ContainSubstrings( 55 []string{"Creating user", "my-user"}, 56 []string{"OK"}, 57 []string{"TIP"}, 58 )) 59 60 userName, password := userRepo.CreateArgsForCall(0) 61 Expect(userName).To(Equal("my-user")) 62 Expect(password).To(Equal("my-password")) 63 }) 64 65 Context("when creating the user returns an error", func() { 66 It("prints a warning when the given user already exists", func() { 67 userRepo.CreateReturns(errors.NewModelAlreadyExistsError("User", "my-user")) 68 69 runCommand("my-user", "my-password") 70 71 Expect(ui.WarnOutputs).To(ContainSubstrings( 72 []string{"already exists"}, 73 )) 74 75 Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"})) 76 }) 77 78 It("fails when any error other than alreadyExists is returned", func() { 79 userRepo.CreateReturns(errors.NewHTTPError(403, "403", "Forbidden")) 80 81 runCommand("my-user", "my-password") 82 83 Expect(ui.Outputs()).To(ContainSubstrings( 84 []string{"Forbidden"}, 85 )) 86 87 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 88 89 }) 90 }) 91 92 It("fails when no arguments are passed", func() { 93 Expect(runCommand()).To(BeFalse()) 94 }) 95 96 It("fails when the user is not logged in", func() { 97 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 98 99 Expect(runCommand("my-user", "my-password")).To(BeFalse()) 100 }) 101 })