github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/security_groups_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2"
     9  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion"
    10  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
    11  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    12  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    13  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    14  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    15  	"github.com/liamawhite/cli-with-i18n/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("security-groups Command", func() {
    22  	var (
    23  		cmd             SecurityGroupsCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v2fakes.FakeSecurityGroupsActor
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v2fakes.FakeSecurityGroupsActor)
    37  
    38  		cmd = SecurityGroupsCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  		fakeConfig.ExperimentalReturns(true)
    48  
    49  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	Context("when getting the current user fails", func() {
    57  		var expectedErr error
    58  
    59  		BeforeEach(func() {
    60  			expectedErr = errors.New("getting user failed")
    61  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    62  		})
    63  
    64  		It("returns the error", func() {
    65  			Expect(executeErr).To(MatchError(expectedErr))
    66  		})
    67  	})
    68  
    69  	Context("when checking target fails", func() {
    70  		BeforeEach(func() {
    71  			fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    72  		})
    73  
    74  		It("returns an error", func() {
    75  			Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName}))
    76  
    77  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    78  			_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    79  			Expect(checkTargetedOrg).To(BeFalse())
    80  			Expect(checkTargetedSpace).To(BeFalse())
    81  		})
    82  	})
    83  
    84  	Context("when the API version is low enough not to support fetching staging", func() {
    85  		BeforeEach(func() {
    86  			fakeActor.CloudControllerAPIVersionReturns("2.36.0")
    87  		})
    88  
    89  		It("makes the fetch indicating that staging should not be included", func() {
    90  			Expect(executeErr).NotTo(HaveOccurred())
    91  
    92  			Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(1))
    93  			Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1))
    94  			Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleArgsForCall(0)).To(BeFalse())
    95  
    96  			Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1))
    97  		})
    98  	})
    99  
   100  	Context("when the API version is high enough to support fetching staging", func() {
   101  		BeforeEach(func() {
   102  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionLifecyleStagingV2)
   103  		})
   104  
   105  		Context("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:     ccv2.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:     ccv2.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:     ccv2.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:     ccv2.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:    ccv2.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:    ccv2.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.CloudControllerAPIVersionCallCount()).To(Equal(1))
   165  				Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1))
   166  				Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleArgsForCall(0)).To(BeTrue())
   167  
   168  				Expect(fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleCallCount()).To(Equal(1))
   169  
   170  				Expect(testUI.Out).To(Say("Getting security groups as some-user\\.\\.\\."))
   171  				Expect(testUI.Out).To(Say("OK\\n\\n"))
   172  				Expect(testUI.Out).To(Say("\\s+name\\s+organization\\s+space\\s+lifecycle"))
   173  				Expect(testUI.Out).To(Say("#0\\s+seg-group-1\\s+org-11\\s+space-111\\s+running"))
   174  				Expect(testUI.Out).To(Say("(?m)\\s+seg-group-1\\s+org-12\\s+space-121\\s+running"))
   175  				Expect(testUI.Out).To(Say("(?m)\\s+seg-group-1\\s+org-12\\s+space-122\\s+staging"))
   176  				Expect(testUI.Out).To(Say("#1\\s+seg-group-2\\s+"))
   177  				Expect(testUI.Out).To(Say("#2\\s+seg-group-3\\s+org-31\\s+space-311\\s+running"))
   178  				Expect(testUI.Out).To(Say("#3\\s+seg-group-4\\s+<all>\\s+<all>\\s+running"))
   179  				Expect(testUI.Out).To(Say("(?m)\\s+seg-group-4\\s+<all>\\s+<all>\\s+staging"))
   180  				Expect(testUI.Err).To(Say("warning-1"))
   181  				Expect(testUI.Err).To(Say("warning-2"))
   182  			})
   183  		})
   184  
   185  		Context("when an error is encountered fetching the security groups", func() {
   186  			BeforeEach(func() {
   187  				fakeActor.GetSecurityGroupsWithOrganizationSpaceAndLifecycleReturns(nil, v2action.Warnings{"warning-1", "warning-2"}, errors.New("generic"))
   188  			})
   189  
   190  			It("returns the error", func() {
   191  				Expect(executeErr).To(MatchError("generic"))
   192  
   193  				Expect(testUI.Err).To(Say("warning-1"))
   194  				Expect(testUI.Err).To(Say("warning-2"))
   195  			})
   196  		})
   197  	})
   198  })