github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/user/create_user_test.go (about) 1 package user_test 2 3 import ( 4 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 5 . "github.com/cloudfoundry/cli/cf/commands/user" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 8 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 9 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 10 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 14 . "github.com/cloudfoundry/cli/testhelpers/matchers" 15 ) 16 17 var _ = Describe("Create user command", func() { 18 var ( 19 requirementsFactory *testreq.FakeReqFactory 20 ui *testterm.FakeUI 21 userRepo *testapi.FakeUserRepository 22 configRepo core_config.ReadWriter 23 ) 24 25 BeforeEach(func() { 26 requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true} 27 ui = new(testterm.FakeUI) 28 userRepo = &testapi.FakeUserRepository{} 29 configRepo = testconfig.NewRepositoryWithDefaults() 30 accessToken, _ := testconfig.EncodeAccessToken(core_config.TokenInfo{ 31 Username: "current-user", 32 }) 33 configRepo.SetAccessToken(accessToken) 34 }) 35 36 runCommand := func(args ...string) bool { 37 cmd := NewCreateUser(ui, configRepo, userRepo) 38 return testcmd.RunCommand(cmd, args, requirementsFactory) 39 } 40 41 It("creates a user", func() { 42 runCommand("my-user", "my-password") 43 44 Expect(ui.Outputs).To(ContainSubstrings( 45 []string{"Creating user", "my-user", "current-user"}, 46 []string{"OK"}, 47 []string{"TIP"}, 48 )) 49 50 Expect(userRepo.CreateUserUsername).To(Equal("my-user")) 51 }) 52 53 Context("when creating the user returns an error", func() { 54 It("prints a warning when the given user already exists", func() { 55 userRepo.CreateUserExists = true 56 57 runCommand("my-user", "my-password") 58 59 Expect(ui.WarnOutputs).To(ContainSubstrings( 60 []string{"already exists"}, 61 )) 62 63 Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"FAILED"})) 64 }) 65 It("fails when any error other than alreadyExists is returned", func() { 66 userRepo.CreateUserReturnsHttpError = true 67 68 runCommand("my-user", "my-password") 69 70 Expect(ui.Outputs).To(ContainSubstrings( 71 []string{"Forbidden"}, 72 )) 73 74 Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"})) 75 76 }) 77 }) 78 79 It("fails when no arguments are passed", func() { 80 runCommand() 81 Expect(ui.FailedWithUsage).To(BeTrue()) 82 }) 83 84 It("fails when the user is not logged in", func() { 85 requirementsFactory.LoginSuccess = false 86 87 Expect(runCommand("my-user", "my-password")).To(BeFalse()) 88 }) 89 })