github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/enable_org_isolation_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     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/v3"
    12  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    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              v3.EnableOrgIsolationCommand
    23  		testUI           *ui.UI
    24  		fakeConfig       *commandfakes.FakeConfig
    25  		fakeSharedActor  *commandfakes.FakeSharedActor
    26  		fakeActor        *v3fakes.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(v3fakes.FakeEnableOrgIsolationActor)
    38  
    39  		cmd = v3.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  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	Context("when the API version is below the minimum", func() {
    58  		BeforeEach(func() {
    59  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    60  		})
    61  
    62  		It("returns a MinimumAPIVersionNotMetError", func() {
    63  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    64  				CurrentVersion: "0.0.0",
    65  				MinimumVersion: ccversion.MinVersionIsolationSegmentV3,
    66  			}))
    67  		})
    68  	})
    69  
    70  	Context("when checking target fails", func() {
    71  		BeforeEach(func() {
    72  			fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName})
    73  		})
    74  
    75  		It("returns an error", func() {
    76  			Expect(executeErr).To(MatchError(translatableerror.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  	Context("when the user is logged in", func() {
    86  		BeforeEach(func() {
    87  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    88  
    89  			cmd.RequiredArgs.OrganizationName = org
    90  			cmd.RequiredArgs.IsolationSegmentName = isolationSegment
    91  		})
    92  
    93  		Context("when the enable is successful", func() {
    94  			BeforeEach(func() {
    95  				fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, nil)
    96  			})
    97  
    98  			It("displays the header and ok", func() {
    99  				Expect(executeErr).ToNot(HaveOccurred())
   100  
   101  				Expect(testUI.Out).To(Say("Enabling isolation segment segment1 for org some-org as banana..."))
   102  				Expect(testUI.Out).To(Say("OK"))
   103  
   104  				Expect(testUI.Err).To(Say("I am a warning"))
   105  				Expect(testUI.Err).To(Say("I am also a warning"))
   106  
   107  				Expect(fakeActor.EntitleIsolationSegmentToOrganizationByNameCallCount()).To(Equal(1))
   108  
   109  				isolationSegmentName, orgName := fakeActor.EntitleIsolationSegmentToOrganizationByNameArgsForCall(0)
   110  				Expect(orgName).To(Equal(org))
   111  				Expect(isolationSegmentName).To(Equal(isolationSegment))
   112  			})
   113  		})
   114  
   115  		Context("when the enable is unsuccessful", func() {
   116  			Context("due to an unexpected error", func() {
   117  				var expectedErr error
   118  
   119  				BeforeEach(func() {
   120  					expectedErr = errors.New("I am an error")
   121  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   122  				})
   123  
   124  				It("displays the header and error", func() {
   125  					Expect(executeErr).To(MatchError(expectedErr))
   126  
   127  					Expect(testUI.Out).To(Say("Enabling isolation segment segment1 for org some-org as banana..."))
   128  
   129  					Expect(testUI.Err).To(Say("I am a warning"))
   130  					Expect(testUI.Err).To(Say("I am also a warning"))
   131  				})
   132  			})
   133  
   134  			Context("when the isolation segment does not exist", func() {
   135  				BeforeEach(func() {
   136  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(v3action.Warnings{"I am a warning", "I am also a warning"}, v3action.IsolationSegmentNotFoundError{Name: "segment1"})
   137  				})
   138  
   139  				It("displays all warnings and the isolation segment not found error", func() {
   140  					Expect(testUI.Err).To(Say("I am a warning"))
   141  					Expect(testUI.Err).To(Say("I am also a warning"))
   142  					Expect(executeErr).To(MatchError(translatableerror.IsolationSegmentNotFoundError{Name: "segment1"}))
   143  				})
   144  			})
   145  
   146  			Context("when the organization does not exist", func() {
   147  				BeforeEach(func() {
   148  					fakeActor.EntitleIsolationSegmentToOrganizationByNameReturns(
   149  						v3action.Warnings{"I am a warning", "I am also a warning"},
   150  						v3action.OrganizationNotFoundError{Name: "some-org"})
   151  				})
   152  
   153  				It("displays all warnings and the org not found error", func() {
   154  					Expect(executeErr).To(MatchError(translatableerror.OrganizationNotFoundError{Name: "some-org"}))
   155  				})
   156  			})
   157  
   158  		})
   159  	})
   160  })