github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/enable_org_isolation_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/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("enable-org-isolation Command", func() {
    21  	var (
    22  		cmd              EnableOrgIsolationCommand
    23  		testUI           *ui.UI
    24  		fakeConfig       *commandfakes.FakeConfig
    25  		fakeSharedActor  *commandfakes.FakeSharedActor
    26  		fakeActor        *v6fakes.FakeEnableOrgIsolationActor
    27  		binaryName       string
    28  		executeErr       error
    29  		isolationSegment string
    30  		org              string
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v6fakes.FakeEnableOrgIsolationActor)
    38  
    39  		cmd = EnableOrgIsolationCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		org = "some-org"
    49  		isolationSegment = "segment1"
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	When("the API version is below the minimum", func() {
    57  		BeforeEach(func() {
    58  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion)
    59  		})
    60  
    61  		It("returns a MinimumAPIVersionNotMetError", func() {
    62  			Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{
    63  				CurrentVersion: ccversion.MinV3ClientVersion,
    64  				MinimumVersion: ccversion.MinVersionIsolationSegmentV3,
    65  			}))
    66  		})
    67  	})
    68  
    69  	When("checking target fails", func() {
    70  		BeforeEach(func() {
    71  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    72  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    77  
    78  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    79  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    80  			Expect(checkTargetedOrg).To(BeFalse())
    81  			Expect(checkTargetedSpace).To(BeFalse())
    82  		})
    83  	})
    84  
    85  	When("the user is logged in", func() {
    86  		BeforeEach(func() {
    87  			fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    88  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    89  
    90  			cmd.RequiredArgs.OrganizationName = org
    91  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    92  		})
    93  
    94  		When("the enable is successful", func() {
    95  			BeforeEach(func() {
    96  				fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil)
    97  			})
    98  
    99  			It("displays the header and ok", func() {
   100  				Expect(executeErr).ToNot(HaveOccurred())
   101  
   102  				Expect(testUI.Out).To(Say("Enabling isolation segment segment1 for org some-org as banana..."))
   103  				Expect(testUI.Out).To(Say("OK"))
   104  
   105  				Expect(testUI.Err).To(Say("I am a warning"))
   106  				Expect(testUI.Err).To(Say("I am also a warning"))
   107  
   108  				Expect(fakeActor.EntitleIsolationSegmentToOrganizationByNameCallCount()).To(Equal(1))
   109  
   110  				isolationSegmentName, orgName := fakeActor.EntitleIsolationSegmentToOrganizationByNameArgsForCall(0)
   111  				Expect(orgName).To(Equal(org))
   112  				Expect(isolationSegmentName).To(Equal(isolationSegment))
   113  			})
   114  		})
   115  
   116  		When("the enable is unsuccessful", func() {
   117  			Context("due to an unexpected error", func() {
   118  				var expectedErr error
   119  
   120  				BeforeEach(func() {
   121  					expectedErr = errors.New("I am an error")
   122  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   123  				})
   124  
   125  				It("displays the header and error", func() {
   126  					Expect(executeErr).To(MatchError(expectedErr))
   127  
   128  					Expect(testUI.Out).To(Say("Enabling isolation segment segment1 for org some-org as banana..."))
   129  
   130  					Expect(testUI.Err).To(Say("I am a warning"))
   131  					Expect(testUI.Err).To(Say("I am also a warning"))
   132  				})
   133  			})
   134  
   135  			When("the isolation segment does not exist", func() {
   136  				BeforeEach(func() {
   137  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentNotFoundError{Name: "segment1"})
   138  				})
   139  
   140  				It("displays all warnings and the isolation segment not found error", func() {
   141  					Expect(testUI.Err).To(Say("I am a warning"))
   142  					Expect(testUI.Err).To(Say("I am also a warning"))
   143  					Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: "segment1"}))
   144  				})
   145  			})
   146  
   147  			When("the organization does not exist", func() {
   148  				BeforeEach(func() {
   149  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(
   150  						v3action.Warnings{"I am a warning", "I am also a warning"},
   151  						actionerror.OrganizationNotFoundError{Name: "some-org"})
   152  				})
   153  
   154  				It("displays all warnings and the org not found error", func() {
   155  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   156  				})
   157  			})
   158  
   159  		})
   160  	})
   161  })