github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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/configuration/core_config"
     6  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     7  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
     8  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
     9  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  
    13  	"github.com/cloudfoundry/cli/cf/command_registry"
    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  		config              core_config.Repository
    23  		deps                command_registry.Dependency
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    28  		ui = new(testterm.FakeUI)
    29  		userRepo = &testapi.FakeUserRepository{}
    30  		config = testconfig.NewRepositoryWithDefaults()
    31  		accessToken, _ := testconfig.EncodeAccessToken(core_config.TokenInfo{
    32  			Username: "current-user",
    33  		})
    34  		config.SetAccessToken(accessToken)
    35  	})
    36  
    37  	updateCommandDependency := func(pluginCall bool) {
    38  		deps.Ui = ui
    39  		deps.Config = config
    40  		deps.RepoLocator = deps.RepoLocator.SetUserRepository(userRepo)
    41  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("create-user").SetDependency(deps, pluginCall))
    42  	}
    43  
    44  	runCommand := func(args ...string) bool {
    45  		return testcmd.RunCliCommand("create-user", args, requirementsFactory, updateCommandDependency, false)
    46  	}
    47  
    48  	It("creates a user", func() {
    49  		runCommand("my-user", "my-password")
    50  
    51  		Expect(ui.Outputs).To(ContainSubstrings(
    52  			[]string{"Creating user", "my-user"},
    53  			[]string{"OK"},
    54  			[]string{"TIP"},
    55  		))
    56  
    57  		Expect(userRepo.CreateUserUsername).To(Equal("my-user"))
    58  	})
    59  
    60  	Context("when creating the user returns an error", func() {
    61  		It("prints a warning when the given user already exists", func() {
    62  			userRepo.CreateUserExists = true
    63  
    64  			runCommand("my-user", "my-password")
    65  
    66  			Expect(ui.WarnOutputs).To(ContainSubstrings(
    67  				[]string{"already exists"},
    68  			))
    69  
    70  			Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"FAILED"}))
    71  		})
    72  		It("fails when any error other than alreadyExists is returned", func() {
    73  			userRepo.CreateUserReturnsHttpError = true
    74  
    75  			runCommand("my-user", "my-password")
    76  
    77  			Expect(ui.Outputs).To(ContainSubstrings(
    78  				[]string{"Forbidden"},
    79  			))
    80  
    81  			Expect(ui.Outputs).To(ContainSubstrings([]string{"FAILED"}))
    82  
    83  		})
    84  	})
    85  
    86  	It("fails when no arguments are passed", func() {
    87  		Expect(runCommand()).To(BeFalse())
    88  	})
    89  
    90  	It("fails when the user is not logged in", func() {
    91  		requirementsFactory.LoginSuccess = false
    92  
    93  		Expect(runCommand("my-user", "my-password")).To(BeFalse())
    94  	})
    95  })