code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/security_group_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/flag"
     8  	. "code.cloudfoundry.org/cli/command/v7"
     9  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("Security Group Command", func() {
    19  	var (
    20  		cmd             SecurityGroupCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		executeErr      error
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    30  		fakeConfig = new(commandfakes.FakeConfig)
    31  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    32  		fakeActor = new(v7fakes.FakeActor)
    33  		fakeConfig.TargetedOrganizationNameReturns("some-org")
    34  
    35  		cmd = SecurityGroupCommand{
    36  			BaseCommand: BaseCommand{
    37  				UI:          testUI,
    38  				Config:      fakeConfig,
    39  				SharedActor: fakeSharedActor,
    40  				Actor:       fakeActor,
    41  			},
    42  			RequiredArgs: flag.SecurityGroup{SecurityGroup: "some-security-group"},
    43  		}
    44  	})
    45  
    46  	JustBeforeEach(func() {
    47  		executeErr = cmd.Execute(nil)
    48  	})
    49  
    50  	When("checking the target fails", func() {
    51  		BeforeEach(func() {
    52  			fakeSharedActor.CheckTargetReturns(
    53  				actionerror.NotLoggedInError{BinaryName: "binaryName"})
    54  		})
    55  
    56  		It("returns an error", func() {
    57  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "binaryName"}))
    58  
    59  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    60  			targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    61  			Expect(targetedOrganizationRequired).To(Equal(false))
    62  			Expect(targetedSpaceRequired).To(Equal(false))
    63  		})
    64  	})
    65  
    66  	When("getting the security group fails", func() {
    67  		BeforeEach(func() {
    68  			fakeConfig.CurrentUserReturns(
    69  				configv3.User{
    70  					Name: "some-user",
    71  				},
    72  				nil)
    73  
    74  			fakeActor.GetSecurityGroupSummaryReturns(
    75  				v7action.SecurityGroupSummary{},
    76  				v7action.Warnings{"warning-1", "warning-2"},
    77  				actionerror.SecurityGroupNotFoundError{})
    78  		})
    79  
    80  		It("returns a translatable error and outputs all warnings", func() {
    81  			Expect(testUI.Out).To(Say("Getting info for security group some-security-group as some-user..."))
    82  
    83  			Expect(executeErr).To(MatchError(actionerror.SecurityGroupNotFoundError{}))
    84  			Expect(fakeActor.GetSecurityGroupSummaryCallCount()).To(Equal(1))
    85  			Expect(testUI.Err).To(Say("warning-1"))
    86  			Expect(testUI.Err).To(Say("warning-2"))
    87  		})
    88  	})
    89  
    90  	When("getting the security group succeeds", func() {
    91  		BeforeEach(func() {
    92  			fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    93  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org", GUID: "some-org-guid"})
    94  		})
    95  
    96  		When("the security group does not have associated rules or spaces", func() {
    97  			BeforeEach(func() {
    98  				fakeActor.GetSecurityGroupSummaryReturns(
    99  					v7action.SecurityGroupSummary{
   100  						Name:                "some-security-group",
   101  						Rules:               []resources.Rule{},
   102  						SecurityGroupSpaces: []v7action.SecurityGroupSpace{},
   103  					},
   104  					v7action.Warnings{"warning-1", "warning-2"},
   105  					nil)
   106  			})
   107  
   108  			It("displays the security group and all warnings", func() {
   109  				Expect(executeErr).ToNot(HaveOccurred())
   110  				Expect(fakeActor.GetSecurityGroupSummaryCallCount()).To(Equal(1))
   111  				securityGroupName := fakeActor.GetSecurityGroupSummaryArgsForCall(0)
   112  				Expect(securityGroupName).To(Equal("some-security-group"))
   113  
   114  				Expect(testUI.Out).To(Say("Getting info for security group some-security-group as some-user..."))
   115  				Expect(testUI.Err).To(Say("warning-1"))
   116  				Expect(testUI.Err).To(Say("warning-2"))
   117  
   118  				Expect(testUI.Out).To(Say(`name:\s+some-security-group`))
   119  				Expect(testUI.Out).To(Say(`rules:`))
   120  				Expect(testUI.Out).To(Say(`\[\]`))
   121  				Expect(testUI.Out).To(Say(`No spaces assigned`))
   122  			})
   123  		})
   124  
   125  		When("the security group has associated rules and spaces", func() {
   126  			BeforeEach(func() {
   127  				port := "4747"
   128  				description := "Top 8 Friends Only"
   129  
   130  				fakeActor.GetSecurityGroupSummaryReturns(
   131  					v7action.SecurityGroupSummary{
   132  						Name: "some-security-group",
   133  						Rules: []resources.Rule{{
   134  							Description: &description,
   135  							Destination: "130.58.1.2",
   136  							Ports:       &port,
   137  							Protocol:    "tcp",
   138  						}},
   139  						SecurityGroupSpaces: []v7action.SecurityGroupSpace{{
   140  							OrgName:   "obsolete-social-networks",
   141  							SpaceName: "my-space",
   142  						}},
   143  					},
   144  					v7action.Warnings{"warning-1", "warning-2"},
   145  					nil)
   146  			})
   147  
   148  			It("displays the security group and all warnings", func() {
   149  				Expect(executeErr).ToNot(HaveOccurred())
   150  				Expect(fakeActor.GetSecurityGroupSummaryCallCount()).To(Equal(1))
   151  				securityGroupName := fakeActor.GetSecurityGroupSummaryArgsForCall(0)
   152  				Expect(securityGroupName).To(Equal("some-security-group"))
   153  
   154  				Expect(testUI.Out).To(Say("Getting info for security group some-security-group as some-user..."))
   155  				Expect(testUI.Err).To(Say("warning-1"))
   156  				Expect(testUI.Err).To(Say("warning-2"))
   157  
   158  				Expect(testUI.Out).To(Say(`name:\s+some-security-group`))
   159  				Expect(testUI.Out).To(Say(`rules:`))
   160  				Expect(testUI.Out).To(Say(`\[`))
   161  				Expect(testUI.Out).To(Say(`{`))
   162  				Expect(testUI.Out).To(Say(`\s+"protocol":\s+"tcp"`))
   163  				Expect(testUI.Out).To(Say(`\s+"destination":\s+"130.58.1.2"`))
   164  				Expect(testUI.Out).To(Say(`\s+"ports":\s+"4747"`))
   165  				Expect(testUI.Out).To(Say(`\s+"description":\s+"Top 8 Friends Only"`))
   166  				Expect(testUI.Out).To(Say(`}`))
   167  				Expect(testUI.Out).To(Say(`\]`))
   168  				Expect(testUI.Out).To(Say(`organization\s+space`))
   169  				Expect(testUI.Out).To(Say(`obsolete-social-networks\s+my-space`))
   170  			})
   171  		})
   172  	})
   173  })