github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/serviceauthtoken/delete_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/errors"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  
    12  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    13  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    14  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    15  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    16  
    17  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    18  )
    19  
    20  var _ = Describe("delete-service-auth-token command", func() {
    21  	var (
    22  		ui                  *testterm.FakeUI
    23  		configRepo          core_config.Repository
    24  		authTokenRepo       *testapi.FakeAuthTokenRepo
    25  		requirementsFactory *testreq.FakeReqFactory
    26  		deps                command_registry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.Ui = ui
    31  		deps.RepoLocator = deps.RepoLocator.SetServiceAuthTokenRepository(authTokenRepo)
    32  		deps.Config = configRepo
    33  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("delete-service-auth-token").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		ui = &testterm.FakeUI{Inputs: []string{"y"}}
    38  		authTokenRepo = &testapi.FakeAuthTokenRepo{}
    39  		configRepo = testconfig.NewRepositoryWithDefaults()
    40  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true}
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		return testcmd.RunCliCommand("delete-service-auth-token", args, requirementsFactory, updateCommandDependency, false)
    45  	}
    46  
    47  	Describe("requirements", func() {
    48  		It("fails with usage when fewer than two arguments are given", func() {
    49  			runCommand("yurp")
    50  			Expect(ui.Outputs).To(ContainSubstrings(
    51  				[]string{"Incorrect Usage", "Requires", "arguments"},
    52  			))
    53  		})
    54  
    55  		It("fails when not logged in", func() {
    56  			requirementsFactory.LoginSuccess = false
    57  			Expect(runCommand()).To(BeFalse())
    58  		})
    59  	})
    60  
    61  	Context("when the service auth token exists", func() {
    62  		BeforeEach(func() {
    63  			authTokenRepo.FindByLabelAndProviderServiceAuthTokenFields = models.ServiceAuthTokenFields{
    64  				Guid:     "the-guid",
    65  				Label:    "a label",
    66  				Provider: "a provider",
    67  			}
    68  		})
    69  
    70  		It("deletes the service auth token", func() {
    71  			runCommand("a label", "a provider")
    72  			Expect(ui.Outputs).To(ContainSubstrings(
    73  				[]string{"Deleting service auth token as", "my-user"},
    74  				[]string{"OK"},
    75  			))
    76  
    77  			Expect(authTokenRepo.FindByLabelAndProviderLabel).To(Equal("a label"))
    78  			Expect(authTokenRepo.FindByLabelAndProviderProvider).To(Equal("a provider"))
    79  			Expect(authTokenRepo.DeletedServiceAuthTokenFields.Guid).To(Equal("the-guid"))
    80  		})
    81  
    82  		It("does nothing when the user does not confirm", func() {
    83  			ui.Inputs = []string{"nope"}
    84  			runCommand("a label", "a provider")
    85  
    86  			Expect(ui.Prompts).To(ContainSubstrings(
    87  				[]string{"Really delete", "service auth token", "a label", "a provider"},
    88  			))
    89  			Expect(ui.Outputs).To(BeEmpty())
    90  			Expect(authTokenRepo.DeletedServiceAuthTokenFields).To(Equal(models.ServiceAuthTokenFields{}))
    91  		})
    92  
    93  		It("does not prompt the user when the -f flag is given", func() {
    94  			ui.Inputs = []string{}
    95  			runCommand("-f", "a label", "a provider")
    96  
    97  			Expect(ui.Prompts).To(BeEmpty())
    98  			Expect(ui.Outputs).To(ContainSubstrings(
    99  				[]string{"Deleting"},
   100  				[]string{"OK"},
   101  			))
   102  
   103  			Expect(authTokenRepo.DeletedServiceAuthTokenFields.Guid).To(Equal("the-guid"))
   104  		})
   105  	})
   106  
   107  	Context("when the service auth token does not exist", func() {
   108  		BeforeEach(func() {
   109  			authTokenRepo.FindByLabelAndProviderApiResponse = errors.NewModelNotFoundError("Service Auth Token", "")
   110  		})
   111  
   112  		It("warns the user when the specified service auth token does not exist", func() {
   113  			runCommand("a label", "a provider")
   114  
   115  			Expect(ui.Outputs).To(ContainSubstrings(
   116  				[]string{"Deleting service auth token as", "my-user"},
   117  				[]string{"OK"},
   118  			))
   119  
   120  			Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"does not exist"}))
   121  		})
   122  	})
   123  
   124  	Context("when there is an error deleting the service auth token", func() {
   125  		BeforeEach(func() {
   126  			authTokenRepo.FindByLabelAndProviderApiResponse = errors.New("OH NOES")
   127  		})
   128  
   129  		It("shows the user an error", func() {
   130  			runCommand("a label", "a provider")
   131  			Expect(ui.Outputs).To(ContainSubstrings(
   132  				[]string{"Deleting service auth token as", "my-user"},
   133  				[]string{"FAILED"},
   134  				[]string{"OH NOES"},
   135  			))
   136  		})
   137  	})
   138  })