github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/cf/commands/serviceauthtoken/delete_service_auth_token_test.go (about)

     1  package serviceauthtoken_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/errors"
     8  	"code.cloudfoundry.org/cli/cf/models"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  
    14  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    15  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    16  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    17  
    18  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    19  )
    20  
    21  var _ = Describe("delete-service-auth-token command", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		configRepo          coreconfig.Repository
    25  		authTokenRepo       *apifakes.OldFakeAuthTokenRepo
    26  		requirementsFactory *requirementsfakes.FakeFactory
    27  		deps                commandregistry.Dependency
    28  	)
    29  
    30  	updateCommandDependency := func(pluginCall bool) {
    31  		deps.UI = ui
    32  		deps.RepoLocator = deps.RepoLocator.SetServiceAuthTokenRepository(authTokenRepo)
    33  		deps.Config = configRepo
    34  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete-service-auth-token").SetDependency(deps, pluginCall))
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{Inputs: []string{"y"}}
    39  		authTokenRepo = new(apifakes.OldFakeAuthTokenRepo)
    40  		configRepo = testconfig.NewRepositoryWithDefaults()
    41  		requirementsFactory = new(requirementsfakes.FakeFactory)
    42  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    43  		requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Passing{})
    44  	})
    45  
    46  	runCommand := func(args ...string) bool {
    47  		return testcmd.RunCLICommand("delete-service-auth-token", args, requirementsFactory, updateCommandDependency, false, ui)
    48  	}
    49  
    50  	Describe("requirements", func() {
    51  		It("fails with usage when fewer than two arguments are given", func() {
    52  			runCommand("yurp")
    53  			Expect(ui.Outputs()).To(ContainSubstrings(
    54  				[]string{"Incorrect Usage", "Requires", "arguments"},
    55  			))
    56  		})
    57  
    58  		It("fails when not logged in", func() {
    59  			requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    60  			Expect(runCommand()).To(BeFalse())
    61  		})
    62  
    63  		It("requires CC API version 2.47 or lower", func() {
    64  			requirementsFactory.NewMaxAPIVersionRequirementReturns(requirements.Failing{Message: "max api 2.47"})
    65  			requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    66  			Expect(runCommand("one", "two")).To(BeFalse())
    67  		})
    68  	})
    69  
    70  	Context("when the service auth token exists", func() {
    71  		BeforeEach(func() {
    72  			authTokenRepo.FindByLabelAndProviderServiceAuthTokenFields = models.ServiceAuthTokenFields{
    73  				GUID:     "the-guid",
    74  				Label:    "a label",
    75  				Provider: "a provider",
    76  			}
    77  		})
    78  
    79  		It("deletes the service auth token", func() {
    80  			runCommand("a label", "a provider")
    81  			Expect(ui.Outputs()).To(ContainSubstrings(
    82  				[]string{"Deleting service auth token as", "my-user"},
    83  				[]string{"OK"},
    84  			))
    85  
    86  			Expect(authTokenRepo.FindByLabelAndProviderLabel).To(Equal("a label"))
    87  			Expect(authTokenRepo.FindByLabelAndProviderProvider).To(Equal("a provider"))
    88  			Expect(authTokenRepo.DeletedServiceAuthTokenFields.GUID).To(Equal("the-guid"))
    89  		})
    90  
    91  		It("does nothing when the user does not confirm", func() {
    92  			ui.Inputs = []string{"nope"}
    93  			runCommand("a label", "a provider")
    94  
    95  			Expect(ui.Prompts).To(ContainSubstrings(
    96  				[]string{"Really delete", "service auth token", "a label", "a provider"},
    97  			))
    98  			Expect(ui.Outputs()).To(BeEmpty())
    99  			Expect(authTokenRepo.DeletedServiceAuthTokenFields).To(Equal(models.ServiceAuthTokenFields{}))
   100  		})
   101  
   102  		It("does not prompt the user when the -f flag is given", func() {
   103  			ui.Inputs = []string{}
   104  			runCommand("-f", "a label", "a provider")
   105  
   106  			Expect(ui.Prompts).To(BeEmpty())
   107  			Expect(ui.Outputs()).To(ContainSubstrings(
   108  				[]string{"Deleting"},
   109  				[]string{"OK"},
   110  			))
   111  
   112  			Expect(authTokenRepo.DeletedServiceAuthTokenFields.GUID).To(Equal("the-guid"))
   113  		})
   114  	})
   115  
   116  	Context("when the service auth token does not exist", func() {
   117  		BeforeEach(func() {
   118  			authTokenRepo.FindByLabelAndProviderAPIResponse = errors.NewModelNotFoundError("Service Auth Token", "")
   119  		})
   120  
   121  		It("warns the user when the specified service auth token does not exist", func() {
   122  			runCommand("a label", "a provider")
   123  
   124  			Expect(ui.Outputs()).To(ContainSubstrings(
   125  				[]string{"Deleting service auth token as", "my-user"},
   126  				[]string{"OK"},
   127  			))
   128  
   129  			Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"does not exist"}))
   130  		})
   131  	})
   132  
   133  	Context("when there is an error deleting the service auth token", func() {
   134  		BeforeEach(func() {
   135  			authTokenRepo.FindByLabelAndProviderAPIResponse = errors.New("OH NOES")
   136  		})
   137  
   138  		It("shows the user an error", func() {
   139  			runCommand("a label", "a provider")
   140  			Expect(ui.Outputs()).To(ContainSubstrings(
   141  				[]string{"Deleting service auth token as", "my-user"},
   142  				[]string{"FAILED"},
   143  				[]string{"OH NOES"},
   144  			))
   145  		})
   146  	})
   147  })