github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/running_security_groups_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v7action"
     6  	"code.cloudfoundry.org/cli/command/commandfakes"
     7  	. "code.cloudfoundry.org/cli/command/v7"
     8  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	"code.cloudfoundry.org/cli/util/configv3"
    11  	"code.cloudfoundry.org/cli/util/ui"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  )
    16  
    17  var _ = Describe("Running Security Groups Command", func() {
    18  	var (
    19  		cmd             RunningSecurityGroupsCommand
    20  		testUI          *ui.UI
    21  		fakeConfig      *commandfakes.FakeConfig
    22  		fakeSharedActor *commandfakes.FakeSharedActor
    23  		fakeActor       *v7fakes.FakeActor
    24  		executeErr      error
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    29  		fakeConfig = new(commandfakes.FakeConfig)
    30  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    31  		fakeActor = new(v7fakes.FakeActor)
    32  		fakeConfig.TargetedOrganizationNameReturns("some-org")
    33  
    34  		cmd = RunningSecurityGroupsCommand{
    35  			BaseCommand: BaseCommand{
    36  				UI:          testUI,
    37  				Config:      fakeConfig,
    38  				SharedActor: fakeSharedActor,
    39  				Actor:       fakeActor,
    40  			},
    41  		}
    42  	})
    43  
    44  	JustBeforeEach(func() {
    45  		executeErr = cmd.Execute(nil)
    46  	})
    47  
    48  	When("checking the target fails", func() {
    49  		BeforeEach(func() {
    50  			fakeSharedActor.CheckTargetReturns(
    51  				actionerror.NotLoggedInError{BinaryName: "binaryName"})
    52  		})
    53  
    54  		It("returns an error", func() {
    55  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "binaryName"}))
    56  
    57  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    58  			targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    59  			Expect(targetedOrganizationRequired).To(Equal(false))
    60  			Expect(targetedSpaceRequired).To(Equal(false))
    61  		})
    62  	})
    63  
    64  	When("there are no globally enabled running security groups found", func() {
    65  		BeforeEach(func() {
    66  			fakeConfig.CurrentUserReturns(
    67  				configv3.User{
    68  					Name: "some-user",
    69  				},
    70  				nil)
    71  
    72  			fakeActor.GetGlobalRunningSecurityGroupsReturns(
    73  				[]resources.SecurityGroup{},
    74  				v7action.Warnings{"warning-1", "warning-2"},
    75  				nil,
    76  			)
    77  		})
    78  
    79  		It("returns a translatable error and outputs all warnings", func() {
    80  			Expect(testUI.Out).To(Say("Getting global running security groups as some-user..."))
    81  			Expect(testUI.Out).To(Say("No global running security groups found."))
    82  
    83  			Expect(fakeActor.GetGlobalRunningSecurityGroupsCallCount()).To(Equal(1))
    84  			Expect(testUI.Err).To(Say("warning-1"))
    85  			Expect(testUI.Err).To(Say("warning-2"))
    86  		})
    87  	})
    88  
    89  	When("there are globally enabled running security groups", func() {
    90  		BeforeEach(func() {
    91  			fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    92  		})
    93  
    94  		When("the security group does not have associated rules or spaces", func() {
    95  			BeforeEach(func() {
    96  				fakeActor.GetGlobalRunningSecurityGroupsReturns(
    97  					[]resources.SecurityGroup{{
    98  						Name: "running-security-group-1",
    99  					}, {
   100  						Name: "running-security-group-2",
   101  					}},
   102  					v7action.Warnings{"warning-1", "warning-2"},
   103  					nil)
   104  			})
   105  
   106  			It("displays the security groups and all warnings", func() {
   107  				Expect(executeErr).ToNot(HaveOccurred())
   108  				Expect(fakeActor.GetGlobalRunningSecurityGroupsCallCount()).To(Equal(1))
   109  
   110  				Expect(testUI.Out).To(Say("Getting global running security groups as some-user..."))
   111  				Expect(testUI.Err).To(Say("warning-1"))
   112  				Expect(testUI.Err).To(Say("warning-2"))
   113  
   114  				Expect(testUI.Out).To(Say(`name`))
   115  				Expect(testUI.Out).To(Say(`running-security-group-1`))
   116  				Expect(testUI.Out).To(Say(`running-security-group-2`))
   117  			})
   118  		})
   119  	})
   120  })