github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/serviceauthtoken/create_service_auth_token_test.go (about)

     1  package serviceauthtoken_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	"github.com/cloudfoundry/cli/cf/models"
     8  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
     9  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    10  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    11  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    12  
    13  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("create-service-auth-token command", func() {
    19  	var (
    20  		ui                  *testterm.FakeUI
    21  		configRepo          core_config.Repository
    22  		authTokenRepo       *testapi.FakeAuthTokenRepo
    23  		requirementsFactory *testreq.FakeReqFactory
    24  		deps                command_registry.Dependency
    25  	)
    26  
    27  	updateCommandDependency := func(pluginCall bool) {
    28  		deps.Ui = ui
    29  		deps.RepoLocator = deps.RepoLocator.SetServiceAuthTokenRepository(authTokenRepo)
    30  		deps.Config = configRepo
    31  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("create-service-auth-token").SetDependency(deps, pluginCall))
    32  	}
    33  
    34  	BeforeEach(func() {
    35  		ui = &testterm.FakeUI{}
    36  		authTokenRepo = &testapi.FakeAuthTokenRepo{}
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		requirementsFactory = &testreq.FakeReqFactory{}
    39  	})
    40  
    41  	runCommand := func(args ...string) bool {
    42  		return testcmd.RunCliCommand("create-service-auth-token", args, requirementsFactory, updateCommandDependency, false)
    43  	}
    44  
    45  	Describe("requirements", func() {
    46  		It("fails with usage when not invoked with exactly three args", func() {
    47  			requirementsFactory.LoginSuccess = true
    48  			runCommand("whoops", "i-accidentally-an-arg")
    49  
    50  			Expect(ui.Outputs).To(ContainSubstrings(
    51  				[]string{"Incorrect Usage", "Requires", "arguments"},
    52  			))
    53  		})
    54  
    55  		It("fails when not logged in", func() {
    56  			Expect(runCommand("just", "enough", "args")).To(BeFalse())
    57  		})
    58  	})
    59  
    60  	Context("when logged in", func() {
    61  		BeforeEach(func() {
    62  			requirementsFactory.LoginSuccess = true
    63  		})
    64  
    65  		It("creates a service auth token, obviously", func() {
    66  			runCommand("a label", "a provider", "a value")
    67  
    68  			Expect(ui.Outputs).To(ContainSubstrings(
    69  				[]string{"Creating service auth token as", "my-user"},
    70  				[]string{"OK"},
    71  			))
    72  
    73  			authToken := models.ServiceAuthTokenFields{}
    74  			authToken.Label = "a label"
    75  			authToken.Provider = "a provider"
    76  			authToken.Token = "a value"
    77  			Expect(authTokenRepo.CreatedServiceAuthTokenFields).To(Equal(authToken))
    78  		})
    79  	})
    80  })