github.com/sleungcy-sap/cli@v7.1.0+incompatible/command/v7/revisions_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	v7 "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/resources"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("revisions Command", func() {
    22  	var (
    23  		cmd             RevisionsCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		binaryName      string
    29  		executeErr      error
    30  		appName         string
    31  
    32  		out *Buffer
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		out = NewBuffer()
    37  		testUI = ui.NewTestUI(nil, out, NewBuffer())
    38  		fakeConfig = new(commandfakes.FakeConfig)
    39  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    40  		fakeActor = new(v7fakes.FakeActor)
    41  
    42  		cmd = v7.RevisionsCommand{
    43  			BaseCommand: v7.BaseCommand{
    44  				UI:          testUI,
    45  				Config:      fakeConfig,
    46  				SharedActor: fakeSharedActor,
    47  				Actor:       fakeActor,
    48  			},
    49  		}
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  		appName = "some-app"
    53  
    54  		cmd.RequiredArgs.AppName = appName
    55  	})
    56  
    57  	JustBeforeEach(func() {
    58  		executeErr = cmd.Execute(nil)
    59  	})
    60  
    61  	When("checking target fails", func() {
    62  		BeforeEach(func() {
    63  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    64  		})
    65  
    66  		It("returns an error", func() {
    67  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    68  
    69  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    70  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    71  			Expect(checkTargetedOrg).To(BeTrue())
    72  			Expect(checkTargetedSpace).To(BeTrue())
    73  		})
    74  	})
    75  
    76  	When("the user is logged in, an org is targeted and a space is targeted", func() {
    77  		BeforeEach(func() {
    78  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    79  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    80  		})
    81  
    82  		When("getting the current user returns an error", func() {
    83  			BeforeEach(func() {
    84  				fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error"))
    85  			})
    86  
    87  			It("returns the error", func() {
    88  				Expect(executeErr).To(MatchError("some-error"))
    89  			})
    90  		})
    91  
    92  		When("getting the current user succeeds", func() {
    93  			BeforeEach(func() {
    94  				fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    95  			})
    96  
    97  			When("getting the revisions", func() {
    98  				BeforeEach(func() {
    99  					revisions := []resources.Revision{
   100  						{
   101  							Version:     3,
   102  							GUID:        "A68F13F7-7E5E-4411-88E8-1FAC54F73F50",
   103  							Description: "On a different note",
   104  							CreatedAt:   "2020-03-10T17:11:58Z",
   105  							Deployable:  true,
   106  						},
   107  						{
   108  							Version:     2,
   109  							GUID:        "A89F8259-D32B-491A-ABD6-F100AC42D74C",
   110  							Description: "Something else",
   111  							CreatedAt:   "2020-03-08T12:43:30Z",
   112  							Deployable:  true,
   113  						},
   114  						{
   115  							Version:     1,
   116  							GUID:        "17E0E587-0E53-4A6E-B6AE-82073159F910",
   117  							Description: "Something",
   118  							CreatedAt:   "2020-03-04T13:23:32Z",
   119  							Deployable:  false,
   120  						},
   121  					}
   122  					fakeActor.GetRevisionsByApplicationNameAndSpaceReturns(revisions, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   123  				})
   124  
   125  				It("displays the revisions", func() {
   126  					Expect(executeErr).ToNot(HaveOccurred())
   127  
   128  					Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`))
   129  					Expect(testUI.Out).To(Say("revision   description           deployable   revision guid                          created at"))
   130  					Expect(testUI.Out).To(Say("3          On a different note   true         A68F13F7-7E5E-4411-88E8-1FAC54F73F50   2020-03-10T17:11:58Z"))
   131  					Expect(testUI.Out).To(Say("2          Something else        true         A89F8259-D32B-491A-ABD6-F100AC42D74C   2020-03-08T12:43:30Z"))
   132  					Expect(testUI.Out).To(Say("1          Something             false        17E0E587-0E53-4A6E-B6AE-82073159F910   2020-03-04T13:23:32Z"))
   133  
   134  					Expect(testUI.Err).To(Say("get-warning-1"))
   135  					Expect(testUI.Err).To(Say("get-warning-2"))
   136  
   137  					Expect(fakeActor.GetRevisionsByApplicationNameAndSpaceCallCount()).To(Equal(1))
   138  					appName, spaceGUID := fakeActor.GetRevisionsByApplicationNameAndSpaceArgsForCall(0)
   139  					Expect(appName).To(Equal("some-app"))
   140  					Expect(spaceGUID).To(Equal("some-space-guid"))
   141  				})
   142  
   143  				When("the revisions feature is disabled on the app", func() {
   144  					BeforeEach(func() {
   145  						revisionsFeature := resources.ApplicationFeature{
   146  							Name:    "revisions",
   147  							Enabled: false,
   148  						}
   149  						fakeActor.GetAppFeatureReturns(revisionsFeature, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   150  						fakeApp := resources.Application{
   151  							GUID: "fake-guid",
   152  						}
   153  						fakeActor.GetApplicationByNameAndSpaceReturns(fakeApp, v7action.Warnings{"get-warning-1", "get-warning-2"}, nil)
   154  					})
   155  
   156  					It("displays the revisions with a warning", func() {
   157  						Expect(executeErr).ToNot(HaveOccurred())
   158  
   159  						Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`))
   160  						Expect(testUI.Err).To(Say(`Warning: Revisions for app 'some-app' are disabled. Updates to the app will not create new revisions.`))
   161  
   162  						Expect(testUI.Out).To(Say("revision   description           deployable   revision guid                          created at"))
   163  						Expect(testUI.Out).To(Say("3          On a different note   true         A68F13F7-7E5E-4411-88E8-1FAC54F73F50   2020-03-10T17:11:58Z"))
   164  						Expect(testUI.Out).To(Say("2          Something else        true         A89F8259-D32B-491A-ABD6-F100AC42D74C   2020-03-08T12:43:30Z"))
   165  						Expect(testUI.Out).To(Say("1          Something             false        17E0E587-0E53-4A6E-B6AE-82073159F910   2020-03-04T13:23:32Z"))
   166  
   167  						Expect(testUI.Err).To(Say("get-warning-1"))
   168  						Expect(testUI.Err).To(Say("get-warning-2"))
   169  
   170  						Expect(fakeActor.GetRevisionsByApplicationNameAndSpaceCallCount()).To(Equal(1))
   171  						appName, spaceGUID := fakeActor.GetRevisionsByApplicationNameAndSpaceArgsForCall(0)
   172  						Expect(appName).To(Equal("some-app"))
   173  						Expect(spaceGUID).To(Equal("some-space-guid"))
   174  					})
   175  
   176  				})
   177  
   178  			})
   179  
   180  			When("there are no revisions available", func() {
   181  				BeforeEach(func() {
   182  					fakeActor.GetRevisionsByApplicationNameAndSpaceReturns(
   183  						[]resources.Revision{},
   184  						v7action.Warnings{"get-warning-1", "get-warning-2"},
   185  						nil,
   186  					)
   187  				})
   188  
   189  				It("returns 'no revisions found'", func() {
   190  					Expect(executeErr).ToNot(HaveOccurred())
   191  
   192  					Expect(strings.TrimSpace(string(out.Contents()))).To(Equal(strings.TrimSpace(`
   193  Getting revisions for app some-app in org some-org / space some-space as banana...
   194  
   195  No revisions found
   196  `)))
   197  					Expect(testUI.Err).To(Say("get-warning-1"))
   198  					Expect(testUI.Err).To(Say("get-warning-2"))
   199  				})
   200  			})
   201  
   202  			When("revisions variables returns an unknown error", func() {
   203  				var expectedErr error
   204  				BeforeEach(func() {
   205  					expectedErr = errors.New("some-error")
   206  					fakeActor.GetRevisionsByApplicationNameAndSpaceReturns([]resources.Revision{}, v7action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr)
   207  				})
   208  
   209  				It("returns the error", func() {
   210  					Expect(executeErr).To(Equal(expectedErr))
   211  					Expect(testUI.Out).To(Say(`Getting revisions for app some-app in org some-org / space some-space as banana\.\.\.`))
   212  
   213  					Expect(testUI.Err).To(Say("get-warning-1"))
   214  					Expect(testUI.Err).To(Say("get-warning-2"))
   215  				})
   216  			})
   217  		})
   218  	})
   219  })