github.com/loafoe/cli@v7.1.0+incompatible/command/v7/enable_org_isolation_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    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("enable-org-isolation Command", func() {
    19  	var (
    20  		cmd              EnableOrgIsolationCommand
    21  		testUI           *ui.UI
    22  		fakeConfig       *commandfakes.FakeConfig
    23  		fakeSharedActor  *commandfakes.FakeSharedActor
    24  		fakeActor        *v7fakes.FakeActor
    25  		binaryName       string
    26  		executeErr       error
    27  		isolationSegment string
    28  		org              string
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = EnableOrgIsolationCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    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("checking target fails", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    59  		})
    60  
    61  		It("returns an error", func() {
    62  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    63  
    64  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    65  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    66  			Expect(checkTargetedOrg).To(BeFalse())
    67  			Expect(checkTargetedSpace).To(BeFalse())
    68  		})
    69  	})
    70  
    71  	When("the user is logged in", func() {
    72  		BeforeEach(func() {
    73  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    74  
    75  			cmd.RequiredArgs.OrganizationName = org
    76  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    77  		})
    78  
    79  		When("the enable is successful", func() {
    80  			BeforeEach(func() {
    81  				fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
    82  			})
    83  
    84  			It("displays the header and ok", func() {
    85  				Expect(executeErr).ToNot(HaveOccurred())
    86  
    87  				Expect(testUI.Out).To(Say("Enabling isolation segment segment1 for org some-org as banana..."))
    88  				Expect(testUI.Out).To(Say("OK"))
    89  
    90  				Expect(testUI.Err).To(Say("I am a warning"))
    91  				Expect(testUI.Err).To(Say("I am also a warning"))
    92  
    93  				Expect(fakeActor.EntitleIsolationSegmentToOrganizationByNameCallCount()).To(Equal(1))
    94  
    95  				isolationSegmentName, orgName := fakeActor.EntitleIsolationSegmentToOrganizationByNameArgsForCall(0)
    96  				Expect(orgName).To(Equal(org))
    97  				Expect(isolationSegmentName).To(Equal(isolationSegment))
    98  			})
    99  		})
   100  
   101  		When("the enable is unsuccessful", func() {
   102  			Context("due to an unexpected error", func() {
   103  				var expectedErr error
   104  
   105  				BeforeEach(func() {
   106  					expectedErr = errors.New("I am an error")
   107  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   108  				})
   109  
   110  				It("displays the header and error", func() {
   111  					Expect(executeErr).To(MatchError(expectedErr))
   112  
   113  					Expect(testUI.Out).To(Say("Enabling isolation segment segment1 for org some-org as banana..."))
   114  
   115  					Expect(testUI.Err).To(Say("I am a warning"))
   116  					Expect(testUI.Err).To(Say("I am also a warning"))
   117  				})
   118  			})
   119  
   120  			When("the isolation segment does not exist", func() {
   121  				BeforeEach(func() {
   122  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v7action.Warnings{"I am a warning", "I am also a warning"}, actionerror.IsolationSegmentNotFoundError{Name: "segment1"})
   123  				})
   124  
   125  				It("displays all warnings and the isolation segment not found error", func() {
   126  					Expect(testUI.Err).To(Say("I am a warning"))
   127  					Expect(testUI.Err).To(Say("I am also a warning"))
   128  					Expect(executeErr).To(MatchError(actionerror.IsolationSegmentNotFoundError{Name: "segment1"}))
   129  				})
   130  			})
   131  
   132  			When("the organization does not exist", func() {
   133  				BeforeEach(func() {
   134  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(
   135  						v7action.Warnings{"I am a warning", "I am also a warning"},
   136  						actionerror.OrganizationNotFoundError{Name: "some-org"})
   137  				})
   138  
   139  				It("displays all warnings and the org not found error", func() {
   140  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   141  				})
   142  			})
   143  
   144  		})
   145  	})
   146  })