github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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/configuration/core_config"
     6  	"github.com/cloudfoundry/cli/cf/models"
     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  
    12  	. "github.com/cloudfoundry/cli/cf/commands/serviceauthtoken"
    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.ReadWriter
    22  		authTokenRepo       *testapi.FakeAuthTokenRepo
    23  		requirementsFactory *testreq.FakeReqFactory
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		ui = &testterm.FakeUI{}
    28  		authTokenRepo = &testapi.FakeAuthTokenRepo{}
    29  		configRepo = testconfig.NewRepositoryWithDefaults()
    30  		requirementsFactory = &testreq.FakeReqFactory{}
    31  	})
    32  
    33  	runCommand := func(args ...string) bool {
    34  		cmd := NewCreateServiceAuthToken(ui, configRepo, authTokenRepo)
    35  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    36  	}
    37  
    38  	Describe("requirements", func() {
    39  		It("fails with usage when not invoked with exactly three args", func() {
    40  			requirementsFactory.LoginSuccess = true
    41  			runCommand("whoops", "i-accidentally-an-arg")
    42  
    43  			Expect(ui.FailedWithUsage).To(BeTrue())
    44  		})
    45  
    46  		It("fails when not logged in", func() {
    47  			Expect(runCommand("just", "enough", "args")).To(BeFalse())
    48  		})
    49  	})
    50  
    51  	Context("when logged in", func() {
    52  		BeforeEach(func() {
    53  			requirementsFactory.LoginSuccess = true
    54  		})
    55  
    56  		It("creates a service auth token, obviously", func() {
    57  			runCommand("a label", "a provider", "a value")
    58  
    59  			Expect(ui.Outputs).To(ContainSubstrings(
    60  				[]string{"Creating service auth token as", "my-user"},
    61  				[]string{"OK"},
    62  			))
    63  
    64  			authToken := models.ServiceAuthTokenFields{}
    65  			authToken.Label = "a label"
    66  			authToken.Provider = "a provider"
    67  			authToken.Token = "a value"
    68  			Expect(authTokenRepo.CreatedServiceAuthTokenFields).To(Equal(authToken))
    69  		})
    70  	})
    71  })