github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/security_groups_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v6"
    11  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("security-groups Command", func() {
    20  	var (
    21  		cmd             SecurityGroupsCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v6fakes.FakeSecurityGroupsActor
    26  		binaryName      string
    27  		executeErr      error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    34  		fakeActor = new(v6fakes.FakeSecurityGroupsActor)
    35  
    36  		cmd = SecurityGroupsCommand{
    37  			UI:          testUI,
    38  			Config:      fakeConfig,
    39  			SharedActor: fakeSharedActor,
    40  			Actor:       fakeActor,
    41  		}
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  		fakeConfig.ExperimentalReturns(true)
    46  
    47  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    48  	})
    49  
    50  	JustBeforeEach(func() {
    51  		executeErr = cmd.Execute(nil)
    52  	})
    53  
    54  	When("checking target fails", func() {
    55  		BeforeEach(func() {
    56  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    57  		})
    58  
    59  		It("returns an error", func() {
    60  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    61  
    62  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    63  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    64  			Expect(checkTargetedOrg).To(BeFalse())
    65  			Expect(checkTargetedSpace).To(BeFalse())
    66  		})
    67  	})
    68  
    69  	When("the user is logged in, and a space and org are targeted", func() {
    70  		BeforeEach(func() {
    71  			fakeConfig.HasTargetedOrganizationReturns(true)
    72  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    73  				GUID: "some-org-guid",
    74  				Name: "some-org",
    75  			})
    76  			fakeConfig.HasTargetedSpaceReturns(true)
    77  			fakeConfig.TargetedSpaceReturns(configv3.Space{
    78  				GUID: "some-space-guid",
    79  				Name: "some-space",
    80  			})
    81  		})
    82  
    83  		When("getting the current user returns an error", func() {
    84  			var expectedErr error
    85  
    86  			BeforeEach(func() {
    87  				expectedErr = errors.New("get current user error")
    88  				fakeConfig.CurrentUserReturns(
    89  					configv3.User{},
    90  					expectedErr)
    91  			})
    92  
    93  			It("returns the error", func() {
    94  				Expect(executeErr).To(MatchError(expectedErr))
    95  			})
    96  		})
    97  
    98  		When("getting the current user does not return an error", func() {
    99  			BeforeEach(func() {
   100  				fakeConfig.CurrentUserReturns(
   101  					configv3.User{Name: "some-user"},
   102  					nil)
   103  			})
   104  
   105  			When("the list of security groups is returned", func() {
   106  				var secGroups []v2action.SecurityGroupWithOrganizationSpaceAndLifecycle
   107  
   108  				BeforeEach(func() {
   109  					secGroups = []v2action.SecurityGroupWithOrganizationSpaceAndLifecycle{
   110  						{
   111  							SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-1"},
   112  							Organization:  &v2action.Organization{Name: "org-11"},
   113  							Space:         &v2action.Space{Name: "space-111"},
   114  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   115  						},
   116  						{
   117  							SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-1"},
   118  							Organization:  &v2action.Organization{Name: "org-12"},
   119  							Space:         &v2action.Space{Name: "space-121"},
   120  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   121  						},
   122  						{
   123  							SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-1"},
   124  							Organization:  &v2action.Organization{Name: "org-12"},
   125  							Space:         &v2action.Space{Name: "space-122"},
   126  							Lifecycle:     constant.SecurityGroupLifecycleStaging,
   127  						},
   128  						{
   129  							SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-2"},
   130  							Organization:  &v2action.Organization{},
   131  							Space:         &v2action.Space{},
   132  						},
   133  						{
   134  							SecurityGroup: &v2action.SecurityGroup{Name: "seg-group-3"},
   135  							Organization:  &v2action.Organization{Name: "org-31"},
   136  							Space:         &v2action.Space{Name: "space-311"},
   137  							Lifecycle:     constant.SecurityGroupLifecycleRunning,
   138  						},
   139  						{
   140  							SecurityGroup: &v2action.SecurityGroup{
   141  								Name:           "seg-group-4",
   142  								RunningDefault: true,
   143  							},
   144  							Organization: &v2action.Organization{Name: ""},
   145  							Space:        &v2action.Space{Name: ""},
   146  							Lifecycle:    constant.SecurityGroupLifecycleRunning,
   147  						},
   148  						{
   149  							SecurityGroup: &v2action.SecurityGroup{
   150  								Name:           "seg-group-4",
   151  								StagingDefault: true,
   152  							},
   153  							Organization: &v2action.Organization{Name: ""},
   154  							Space:        &v2action.Space{Name: ""},
   155  							Lifecycle:    constant.SecurityGroupLifecycleStaging,
   156  						},
   157  					}
   158  					fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleReturns(secGroups, v2action.Warnings{"warning-1", "warning-2"}, nil)
   159  				})
   160  
   161  				It("displays a table containing the security groups, the spaces to which they are bound, the spaces' orgs, and the lifecycle of the app they were assigned to", func() {
   162  					Expect(executeErr).To(BeNil())
   163  
   164  					Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1))
   165  					Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleArgsForCall(0)).To(BeTrue())
   166  
   167  					Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1))
   168  
   169  					Expect(testUI.Out).To(Say(`Getting security groups as some-user\.\.\.`))
   170  					Expect(testUI.Out).To(Say(`OK\n\n`))
   171  					Expect(testUI.Out).To(Say(`\s+name\s+organization\s+space\s+lifecycle`))
   172  					Expect(testUI.Out).To(Say(`#0\s+seg-group-1\s+org-11\s+space-111\s+running`))
   173  					Expect(testUI.Out).To(Say(`(?m)\s+seg-group-1\s+org-12\s+space-121\s+running`))
   174  					Expect(testUI.Out).To(Say(`(?m)\s+seg-group-1\s+org-12\s+space-122\s+staging`))
   175  					Expect(testUI.Out).To(Say(`#1\s+seg-group-2\s+`))
   176  					Expect(testUI.Out).To(Say(`#2\s+seg-group-3\s+org-31\s+space-311\s+running`))
   177  					Expect(testUI.Out).To(Say(`#3\s+seg-group-4\s+<all>\s+<all>\s+running`))
   178  					Expect(testUI.Out).To(Say(`(?m)\s+seg-group-4\s+<all>\s+<all>\s+staging`))
   179  					Expect(testUI.Err).To(Say("warning-1"))
   180  					Expect(testUI.Err).To(Say("warning-2"))
   181  				})
   182  			})
   183  
   184  			When("an error is encountered fetching the security groups", func() {
   185  				BeforeEach(func() {
   186  					fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleReturns(nil, v2action.Warnings{"warning-1", "warning-2"}, errors.New("generic"))
   187  				})
   188  
   189  				It("returns the error", func() {
   190  					Expect(executeErr).To(MatchError("generic"))
   191  
   192  					Expect(testUI.Err).To(Say("warning-1"))
   193  					Expect(testUI.Err).To(Say("warning-2"))
   194  				})
   195  			})
   196  		})
   197  	})
   198  })