github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/org_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v3action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v2"
    11  	"code.cloudfoundry.org/cli/command/v2/v2fakes"
    12  	"code.cloudfoundry.org/cli/util/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("org Command", func() {
    20  	var (
    21  		cmd             OrgCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v2fakes.FakeOrgActor
    26  		fakeActorV3     *v2fakes.FakeOrgActorV3
    27  		binaryName      string
    28  		executeErr      error
    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(v2fakes.FakeOrgActor)
    36  		fakeActorV3 = new(v2fakes.FakeOrgActorV3)
    37  
    38  		cmd = OrgCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  			ActorV3:     fakeActorV3,
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		cmd.RequiredArgs.Organization = "some-org"
    49  		fakeActorV3.CloudControllerAPIVersionReturns("3.12.0")
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	Context("when checking the target fails", func() {
    57  		BeforeEach(func() {
    58  			fakeSharedActor.CheckTargetReturns(
    59  				actionerror.NotLoggedInError{BinaryName: binaryName})
    60  		})
    61  
    62  		It("returns an error", func() {
    63  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    64  
    65  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    66  			targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    67  			Expect(targetedOrganizationRequired).To(Equal(false))
    68  			Expect(targetedSpaceRequired).To(Equal(false))
    69  		})
    70  	})
    71  
    72  	Context("when the --guid flag is provided", func() {
    73  		BeforeEach(func() {
    74  			cmd.GUID = true
    75  		})
    76  
    77  		Context("when no errors occur", func() {
    78  			BeforeEach(func() {
    79  				fakeActor.GetOrganizationByNameReturns(
    80  					v2action.Organization{GUID: "some-org-guid"},
    81  					v2action.Warnings{"warning-1", "warning-2"},
    82  					nil)
    83  			})
    84  
    85  			It("displays the org guid and outputs all warnings", func() {
    86  				Expect(executeErr).ToNot(HaveOccurred())
    87  
    88  				Expect(testUI.Out).To(Say("some-org-guid"))
    89  				Expect(testUI.Err).To(Say("warning-1"))
    90  				Expect(testUI.Err).To(Say("warning-2"))
    91  
    92  				Expect(fakeActor.GetOrganizationByNameCallCount()).To(Equal(1))
    93  				orgName := fakeActor.GetOrganizationByNameArgsForCall(0)
    94  				Expect(orgName).To(Equal("some-org"))
    95  			})
    96  		})
    97  
    98  		Context("when getting the org returns an error", func() {
    99  			Context("when the error is translatable", func() {
   100  				BeforeEach(func() {
   101  					fakeActor.GetOrganizationByNameReturns(
   102  						v2action.Organization{},
   103  						v2action.Warnings{"warning-1", "warning-2"},
   104  						actionerror.OrganizationNotFoundError{Name: "some-org"})
   105  				})
   106  
   107  				It("returns a translatable error and outputs all warnings", func() {
   108  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   109  
   110  					Expect(testUI.Err).To(Say("warning-1"))
   111  					Expect(testUI.Err).To(Say("warning-2"))
   112  				})
   113  			})
   114  
   115  			Context("when the error is not translatable", func() {
   116  				var expectedErr error
   117  
   118  				BeforeEach(func() {
   119  					expectedErr = errors.New("get org error")
   120  					fakeActor.GetOrganizationByNameReturns(
   121  						v2action.Organization{},
   122  						v2action.Warnings{"warning-1", "warning-2"},
   123  						expectedErr)
   124  				})
   125  
   126  				It("returns the error and all warnings", func() {
   127  					Expect(executeErr).To(MatchError(expectedErr))
   128  
   129  					Expect(testUI.Err).To(Say("warning-1"))
   130  					Expect(testUI.Err).To(Say("warning-2"))
   131  				})
   132  			})
   133  		})
   134  	})
   135  
   136  	Context("when the --guid flag is not provided", func() {
   137  		Context("when no errors occur", func() {
   138  			BeforeEach(func() {
   139  				fakeConfig.CurrentUserReturns(
   140  					configv3.User{
   141  						Name: "some-user",
   142  					},
   143  					nil)
   144  
   145  				fakeActor.GetOrganizationSummaryByNameReturns(
   146  					v2action.OrganizationSummary{
   147  						Organization: v2action.Organization{
   148  							Name: "some-org",
   149  							GUID: "some-org-guid",
   150  							DefaultIsolationSegmentGUID: "default-isolation-segment-guid",
   151  						},
   152  						DomainNames: []string{
   153  							"a-shared.com",
   154  							"b-private.com",
   155  							"c-shared.com",
   156  							"d-private.com",
   157  						},
   158  						QuotaName: "some-quota",
   159  						SpaceNames: []string{
   160  							"space1",
   161  							"space2",
   162  						},
   163  					},
   164  					v2action.Warnings{"warning-1", "warning-2"},
   165  					nil)
   166  			})
   167  
   168  			Context("when the v3 actor is nil", func() {
   169  				BeforeEach(func() {
   170  					cmd.ActorV3 = nil
   171  				})
   172  				It("displays the org summary with no isolation segment row", func() {
   173  					Expect(executeErr).To(BeNil())
   174  					Expect(testUI.Out).ToNot(Say("isolation segments:"))
   175  				})
   176  			})
   177  
   178  			Context("when api version is above 3.11.0", func() {
   179  				BeforeEach(func() {
   180  					fakeActorV3.GetIsolationSegmentsByOrganizationReturns(
   181  						[]v3action.IsolationSegment{
   182  							{
   183  								Name: "isolation-segment-1",
   184  								GUID: "default-isolation-segment-guid",
   185  							}, {
   186  								Name: "isolation-segment-2",
   187  								GUID: "some-other-isolation-segment-guid",
   188  							},
   189  						},
   190  						v3action.Warnings{"warning-3", "warning-4"},
   191  						nil)
   192  					fakeActorV3.CloudControllerAPIVersionReturns("3.12.0")
   193  				})
   194  
   195  				It("displays warnings and a table with org domains, org quota, spaces and isolation segments", func() {
   196  					Expect(executeErr).To(BeNil())
   197  
   198  					Expect(testUI.Out).To(Say("Getting info for org %s as some-user\\.\\.\\.", cmd.RequiredArgs.Organization))
   199  					Expect(testUI.Err).To(Say("warning-1"))
   200  					Expect(testUI.Err).To(Say("warning-2"))
   201  					Expect(testUI.Err).To(Say("warning-3"))
   202  					Expect(testUI.Err).To(Say("warning-4"))
   203  
   204  					Expect(testUI.Out).To(Say("name:\\s+%s", cmd.RequiredArgs.Organization))
   205  					Expect(testUI.Out).To(Say("domains:\\s+a-shared.com, b-private.com, c-shared.com, d-private.com"))
   206  					Expect(testUI.Out).To(Say("quota:\\s+some-quota"))
   207  					Expect(testUI.Out).To(Say("spaces:\\s+space1, space2"))
   208  					Expect(testUI.Out).To(Say("isolation segments:\\s+isolation-segment-1 \\(default\\), isolation-segment-2"))
   209  
   210  					Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   211  
   212  					Expect(fakeActor.GetOrganizationSummaryByNameCallCount()).To(Equal(1))
   213  					orgName := fakeActor.GetOrganizationSummaryByNameArgsForCall(0)
   214  					Expect(orgName).To(Equal("some-org"))
   215  
   216  					Expect(fakeActorV3.GetIsolationSegmentsByOrganizationCallCount()).To(Equal(1))
   217  					orgGuid := fakeActorV3.GetIsolationSegmentsByOrganizationArgsForCall(0)
   218  					Expect(orgGuid).To(Equal("some-org-guid"))
   219  				})
   220  			})
   221  
   222  			Context("when api version is below 3.11.0", func() {
   223  				BeforeEach(func() {
   224  					fakeActorV3.CloudControllerAPIVersionReturns("3.10.0")
   225  				})
   226  
   227  				It("displays warnings and a table with org domains, org quota, spaces and isolation segments", func() {
   228  					Expect(executeErr).To(BeNil())
   229  
   230  					Expect(testUI.Out).To(Say("Getting info for org %s as some-user\\.\\.\\.", cmd.RequiredArgs.Organization))
   231  					Expect(testUI.Err).To(Say("warning-1"))
   232  					Expect(testUI.Err).To(Say("warning-2"))
   233  
   234  					Expect(testUI.Out).To(Say("name:\\s+%s", cmd.RequiredArgs.Organization))
   235  					Expect(testUI.Out).To(Say("domains:\\s+a-shared.com, b-private.com, c-shared.com, d-private.com"))
   236  					Expect(testUI.Out).To(Say("quota:\\s+some-quota"))
   237  					Expect(testUI.Out).To(Say("spaces:\\s+space1, space2"))
   238  					Expect(testUI.Out).ToNot(Say("isolation segments:"))
   239  
   240  					Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   241  
   242  					Expect(fakeActor.GetOrganizationSummaryByNameCallCount()).To(Equal(1))
   243  					orgName := fakeActor.GetOrganizationSummaryByNameArgsForCall(0)
   244  					Expect(orgName).To(Equal("some-org"))
   245  				})
   246  			})
   247  		})
   248  
   249  		Context("when getting the current user returns an error", func() {
   250  			var expectedErr error
   251  
   252  			BeforeEach(func() {
   253  				expectedErr = errors.New("getting current user error")
   254  				fakeConfig.CurrentUserReturns(
   255  					configv3.User{},
   256  					expectedErr)
   257  			})
   258  
   259  			It("returns the error", func() {
   260  				Expect(executeErr).To(MatchError(expectedErr))
   261  			})
   262  		})
   263  
   264  		Context("when getting the org summary returns an error", func() {
   265  			Context("when the error is translatable", func() {
   266  				BeforeEach(func() {
   267  					fakeActor.GetOrganizationSummaryByNameReturns(
   268  						v2action.OrganizationSummary{},
   269  						v2action.Warnings{"warning-1", "warning-2"},
   270  						actionerror.OrganizationNotFoundError{Name: "some-org"})
   271  				})
   272  
   273  				It("returns a translatable error and outputs all warnings", func() {
   274  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   275  
   276  					Expect(testUI.Err).To(Say("warning-1"))
   277  					Expect(testUI.Err).To(Say("warning-2"))
   278  				})
   279  			})
   280  
   281  			Context("when the error is not translatable", func() {
   282  				var expectedErr error
   283  
   284  				BeforeEach(func() {
   285  					expectedErr = errors.New("get org error")
   286  					fakeActor.GetOrganizationSummaryByNameReturns(
   287  						v2action.OrganizationSummary{},
   288  						v2action.Warnings{"warning-1", "warning-2"},
   289  						expectedErr)
   290  				})
   291  
   292  				It("returns the error and all warnings", func() {
   293  					Expect(executeErr).To(MatchError(expectedErr))
   294  
   295  					Expect(testUI.Err).To(Say("warning-1"))
   296  					Expect(testUI.Err).To(Say("warning-2"))
   297  				})
   298  			})
   299  		})
   300  
   301  		Context("when getting the org isolation segments returns an error", func() {
   302  			var expectedErr error
   303  
   304  			BeforeEach(func() {
   305  				expectedErr = errors.New("get org iso segs error")
   306  				fakeActorV3.GetIsolationSegmentsByOrganizationReturns(
   307  					nil,
   308  					v3action.Warnings{"get iso seg warning"},
   309  					expectedErr)
   310  			})
   311  
   312  			It("returns the error and all warnings", func() {
   313  				Expect(executeErr).To(MatchError(expectedErr))
   314  				Expect(testUI.Err).To(Say("get iso seg warning"))
   315  			})
   316  		})
   317  	})
   318  })