github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/org_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/v2action"
     8  	"code.cloudfoundry.org/cli/actor/v3action"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    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("org Command", func() {
    21  	var (
    22  		cmd             OrgCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeOrgActor
    27  		fakeActorV3     *v6fakes.FakeOrgActorV3
    28  		binaryName      string
    29  		executeErr      error
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeOrgActor)
    37  		fakeActorV3 = new(v6fakes.FakeOrgActorV3)
    38  
    39  		cmd = OrgCommand{
    40  			UI:          testUI,
    41  			Config:      fakeConfig,
    42  			SharedActor: fakeSharedActor,
    43  			Actor:       fakeActor,
    44  			ActorV3:     fakeActorV3,
    45  		}
    46  
    47  		binaryName = "faceman"
    48  		fakeConfig.BinaryNameReturns(binaryName)
    49  		cmd.RequiredArgs.Organization = "some-org"
    50  	})
    51  
    52  	JustBeforeEach(func() {
    53  		executeErr = cmd.Execute(nil)
    54  	})
    55  
    56  	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  	When("the --guid flag is provided", func() {
    73  		BeforeEach(func() {
    74  			cmd.GUID = true
    75  		})
    76  
    77  		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  		When("getting the org returns an error", func() {
    99  			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  			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  	When("the --guid flag is not provided", func() {
   137  		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  			When("the v3 actor does not exist", 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  			When("API version is above isolation segments minimum version", func() {
   179  				BeforeEach(func() {
   180  					fakeActorV3.CloudControllerAPIVersionReturns(ccversion.MinVersionIsolationSegmentV3)
   181  				})
   182  
   183  				When("something", func() {
   184  					BeforeEach(func() {
   185  						fakeActorV3.GetIsolationSegmentsByOrganizationReturns(
   186  							[]v3action.IsolationSegment{
   187  								{
   188  									Name: "isolation-segment-1",
   189  									GUID: "default-isolation-segment-guid",
   190  								}, {
   191  									Name: "isolation-segment-2",
   192  									GUID: "some-other-isolation-segment-guid",
   193  								},
   194  							},
   195  							v3action.Warnings{"warning-3", "warning-4"},
   196  							nil)
   197  					})
   198  
   199  					It("displays warnings and a table with org domains, org quota, spaces and isolation segments", func() {
   200  						Expect(executeErr).To(BeNil())
   201  
   202  						Expect(testUI.Out).To(Say(`Getting info for org %s as some-user\.\.\.`, cmd.RequiredArgs.Organization))
   203  						Expect(testUI.Err).To(Say("warning-1"))
   204  						Expect(testUI.Err).To(Say("warning-2"))
   205  						Expect(testUI.Err).To(Say("warning-3"))
   206  						Expect(testUI.Err).To(Say("warning-4"))
   207  
   208  						Expect(testUI.Out).To(Say(`name:\s+%s`, cmd.RequiredArgs.Organization))
   209  						Expect(testUI.Out).To(Say(`domains:\s+a-shared.com, b-private.com, c-shared.com, d-private.com`))
   210  						Expect(testUI.Out).To(Say(`quota:\s+some-quota`))
   211  						Expect(testUI.Out).To(Say(`spaces:\s+space1, space2`))
   212  						Expect(testUI.Out).To(Say(`isolation segments:\s+isolation-segment-1 \(default\), isolation-segment-2`))
   213  
   214  						Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   215  
   216  						Expect(fakeActor.GetOrganizationSummaryByNameCallCount()).To(Equal(1))
   217  						orgName := fakeActor.GetOrganizationSummaryByNameArgsForCall(0)
   218  						Expect(orgName).To(Equal("some-org"))
   219  
   220  						Expect(fakeActorV3.GetIsolationSegmentsByOrganizationCallCount()).To(Equal(1))
   221  						orgGuid := fakeActorV3.GetIsolationSegmentsByOrganizationArgsForCall(0)
   222  						Expect(orgGuid).To(Equal("some-org-guid"))
   223  					})
   224  				})
   225  
   226  				When("getting the org isolation segments returns an error", func() {
   227  					var expectedErr error
   228  
   229  					BeforeEach(func() {
   230  						expectedErr = errors.New("get org iso segs error")
   231  						fakeActorV3.GetIsolationSegmentsByOrganizationReturns(
   232  							nil,
   233  							v3action.Warnings{"get iso seg warning"},
   234  							expectedErr)
   235  					})
   236  
   237  					It("returns the error and all warnings", func() {
   238  						Expect(executeErr).To(MatchError(expectedErr))
   239  						Expect(testUI.Err).To(Say("get iso seg warning"))
   240  					})
   241  				})
   242  			})
   243  
   244  			When("api version is below isolation segments minimum version", func() {
   245  				BeforeEach(func() {
   246  					fakeActorV3.CloudControllerAPIVersionReturns(ccversion.MinV3ClientVersion)
   247  				})
   248  
   249  				It("displays warnings and a table with org domains, org quota, spaces and isolation segments", func() {
   250  					Expect(executeErr).To(BeNil())
   251  
   252  					Expect(testUI.Out).To(Say(`Getting info for org %s as some-user\.\.\.`, cmd.RequiredArgs.Organization))
   253  					Expect(testUI.Err).To(Say("warning-1"))
   254  					Expect(testUI.Err).To(Say("warning-2"))
   255  
   256  					Expect(testUI.Out).To(Say(`name:\s+%s`, cmd.RequiredArgs.Organization))
   257  					Expect(testUI.Out).To(Say(`domains:\s+a-shared.com, b-private.com, c-shared.com, d-private.com`))
   258  					Expect(testUI.Out).To(Say(`quota:\s+some-quota`))
   259  					Expect(testUI.Out).To(Say(`spaces:\s+space1, space2`))
   260  					Expect(testUI.Out).ToNot(Say("isolation segments:"))
   261  
   262  					Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   263  
   264  					Expect(fakeActor.GetOrganizationSummaryByNameCallCount()).To(Equal(1))
   265  					orgName := fakeActor.GetOrganizationSummaryByNameArgsForCall(0)
   266  					Expect(orgName).To(Equal("some-org"))
   267  				})
   268  			})
   269  		})
   270  
   271  		When("getting the current user returns an error", func() {
   272  			var expectedErr error
   273  
   274  			BeforeEach(func() {
   275  				expectedErr = errors.New("getting current user error")
   276  				fakeConfig.CurrentUserReturns(
   277  					configv3.User{},
   278  					expectedErr)
   279  			})
   280  
   281  			It("returns the error", func() {
   282  				Expect(executeErr).To(MatchError(expectedErr))
   283  			})
   284  		})
   285  
   286  		When("getting the org summary returns an error", func() {
   287  			When("the error is translatable", func() {
   288  				BeforeEach(func() {
   289  					fakeActor.GetOrganizationSummaryByNameReturns(
   290  						v2action.OrganizationSummary{},
   291  						v2action.Warnings{"warning-1", "warning-2"},
   292  						actionerror.OrganizationNotFoundError{Name: "some-org"})
   293  				})
   294  
   295  				It("returns a translatable error and outputs all warnings", func() {
   296  					Expect(executeErr).To(MatchError(actionerror.OrganizationNotFoundError{Name: "some-org"}))
   297  
   298  					Expect(testUI.Err).To(Say("warning-1"))
   299  					Expect(testUI.Err).To(Say("warning-2"))
   300  				})
   301  			})
   302  
   303  			When("the error is not translatable", func() {
   304  				var expectedErr error
   305  
   306  				BeforeEach(func() {
   307  					expectedErr = errors.New("get org error")
   308  					fakeActor.GetOrganizationSummaryByNameReturns(
   309  						v2action.OrganizationSummary{},
   310  						v2action.Warnings{"warning-1", "warning-2"},
   311  						expectedErr)
   312  				})
   313  
   314  				It("returns the error and all warnings", func() {
   315  					Expect(executeErr).To(MatchError(expectedErr))
   316  
   317  					Expect(testUI.Err).To(Say("warning-1"))
   318  					Expect(testUI.Err).To(Say("warning-2"))
   319  				})
   320  			})
   321  		})
   322  	})
   323  })