github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/service_keys_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
     8  	v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    11  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("service-keys Command", func() {
    19  	var (
    20  		cmd             v7.ServiceKeysCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		executeErr      error
    25  		fakeActor       *v7fakes.FakeActor
    26  	)
    27  
    28  	const (
    29  		fakeUserName            = "fake-user-name"
    30  		fakeServiceInstanceName = "fake-service-instance-name"
    31  		fakeSpaceGUID           = "fake-space-guid"
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(NewBuffer(), NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeActor)
    39  
    40  		cmd = v7.ServiceKeysCommand{
    41  			BaseCommand: v7.BaseCommand{
    42  				UI:          testUI,
    43  				Config:      fakeConfig,
    44  				SharedActor: fakeSharedActor,
    45  				Actor:       fakeActor,
    46  			},
    47  		}
    48  
    49  		fakeConfig.TargetedSpaceReturns(configv3.Space{GUID: fakeSpaceGUID})
    50  
    51  		fakeActor.GetCurrentUserReturns(configv3.User{Name: fakeUserName}, nil)
    52  
    53  		fakeActor.GetServiceKeysByServiceInstanceReturns(
    54  			[]resources.ServiceCredentialBinding{
    55  				{GUID: "1", Name: "flopsy", LastOperation: resources.LastOperation{Type: "create", State: "succeeded", Description: "desc-1"}},
    56  				{GUID: "2", Name: "mopsy", LastOperation: resources.LastOperation{Type: "update", State: "failed", Description: "desc-2"}},
    57  				{GUID: "3", Name: "cottontail"},
    58  				{GUID: "4", Name: "peter", LastOperation: resources.LastOperation{Type: "create", State: "in progress"}},
    59  			},
    60  			v7action.Warnings{"fake warning"},
    61  			nil,
    62  		)
    63  
    64  		setPositionalFlags(&cmd, fakeServiceInstanceName)
    65  	})
    66  
    67  	JustBeforeEach(func() {
    68  		executeErr = cmd.Execute(nil)
    69  	})
    70  
    71  	It("checks the user is logged in, and targeting an org and space", func() {
    72  		Expect(executeErr).NotTo(HaveOccurred())
    73  
    74  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    75  		actualOrg, actualSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    76  		Expect(actualOrg).To(BeTrue())
    77  		Expect(actualSpace).To(BeTrue())
    78  	})
    79  
    80  	It("delegates to the actor", func() {
    81  		Expect(fakeActor.GetServiceKeysByServiceInstanceCallCount()).To(Equal(1))
    82  		actualServiceInstanceName, actualSpaceGUID := fakeActor.GetServiceKeysByServiceInstanceArgsForCall(0)
    83  		Expect(actualServiceInstanceName).To(Equal(fakeServiceInstanceName))
    84  		Expect(actualSpaceGUID).To(Equal(fakeSpaceGUID))
    85  	})
    86  
    87  	It("prints an intro, key details, and warnings", func() {
    88  		Expect(executeErr).NotTo(HaveOccurred())
    89  		Expect(testUI.Err).To(Say("fake warning"))
    90  		Expect(testUI.Out).To(SatisfyAll(
    91  			Say(`Getting keys for service instance %s as %s\.\.\.\n`, fakeServiceInstanceName, fakeUserName),
    92  			Say(`\n`),
    93  			Say(`name\s+last operation\s+message\n`),
    94  			Say(`flopsy\s+create succeeded\s+desc-1\n`),
    95  			Say(`mopsy\s+update failed\s+desc-2\n`),
    96  			Say(`cottontail\s*\n`),
    97  			Say(`peter\s+create in progress\s*\n`),
    98  		))
    99  	})
   100  
   101  	When("there are no keys", func() {
   102  		BeforeEach(func() {
   103  			fakeActor.GetServiceKeysByServiceInstanceReturns(
   104  				nil,
   105  				v7action.Warnings{"fake warning"},
   106  				nil,
   107  			)
   108  		})
   109  
   110  		It("prints an intro, message, and warnings", func() {
   111  			Expect(executeErr).NotTo(HaveOccurred())
   112  			Expect(testUI.Err).To(Say("fake warning"))
   113  			Expect(testUI.Out).To(SatisfyAll(
   114  				Say(`Getting keys for service instance %s as %s\.\.\.\n`, fakeServiceInstanceName, fakeUserName),
   115  				Say(`\n`),
   116  				Say(`No service keys for service instance %s\n`, fakeServiceInstanceName),
   117  			))
   118  		})
   119  	})
   120  
   121  	When("checking the target returns an error", func() {
   122  		BeforeEach(func() {
   123  			fakeSharedActor.CheckTargetReturns(errors.New("explode"))
   124  		})
   125  
   126  		It("returns the error", func() {
   127  			Expect(executeErr).To(MatchError("explode"))
   128  		})
   129  	})
   130  
   131  	When("getting the username returns an error", func() {
   132  		BeforeEach(func() {
   133  			fakeActor.GetCurrentUserReturns(configv3.User{}, errors.New("bad thing"))
   134  		})
   135  
   136  		It("returns the error", func() {
   137  			Expect(executeErr).To(MatchError("bad thing"))
   138  		})
   139  	})
   140  
   141  	When("getting the keys returns an error", func() {
   142  		BeforeEach(func() {
   143  			fakeActor.GetServiceKeysByServiceInstanceReturns(
   144  				nil,
   145  				v7action.Warnings{"fake warning"},
   146  				errors.New("boom"),
   147  			)
   148  		})
   149  
   150  		It("returns the error", func() {
   151  			Expect(executeErr).To(MatchError("boom"))
   152  		})
   153  	})
   154  })