github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/copy_source_test.go (about)

     1  package application_test
     2  
     3  import (
     4  	testApplication "github.com/cloudfoundry/cli/cf/api/applications/fakes"
     5  	testCopyApplication "github.com/cloudfoundry/cli/cf/api/copy_application_source/fakes"
     6  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     7  	testorg "github.com/cloudfoundry/cli/cf/api/organizations/fakes"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  
    14  	. "github.com/cloudfoundry/cli/cf/commands/application"
    15  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    16  	"github.com/cloudfoundry/cli/cf/errors"
    17  
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("CopySource", func() {
    24  
    25  	var (
    26  		ui                  *testterm.FakeUI
    27  		config              core_config.ReadWriter
    28  		requirementsFactory *testreq.FakeReqFactory
    29  		authRepo            *testapi.FakeAuthenticationRepository
    30  		appRepo             *testApplication.FakeApplicationRepository
    31  		copyAppSourceRepo   *testCopyApplication.FakeCopyApplicationSourceRepository
    32  		spaceRepo           *testapi.FakeSpaceRepository
    33  		orgRepo             *testorg.FakeOrganizationRepository
    34  		appRestarter        *testcmd.FakeApplicationRestarter
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    40  		authRepo = &testapi.FakeAuthenticationRepository{}
    41  		appRepo = &testApplication.FakeApplicationRepository{}
    42  		copyAppSourceRepo = &testCopyApplication.FakeCopyApplicationSourceRepository{}
    43  		spaceRepo = &testapi.FakeSpaceRepository{}
    44  		orgRepo = &testorg.FakeOrganizationRepository{}
    45  		appRestarter = &testcmd.FakeApplicationRestarter{}
    46  		config = testconfig.NewRepositoryWithDefaults()
    47  	})
    48  
    49  	runCommand := func(args ...string) bool {
    50  		cmd := NewCopySource(ui, config, authRepo, appRepo, orgRepo, spaceRepo, copyAppSourceRepo, appRestarter)
    51  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    52  	}
    53  
    54  	Describe("requirement failures", func() {
    55  		It("when not logged in", func() {
    56  			requirementsFactory.LoginSuccess = false
    57  			Expect(runCommand("source-app", "target-app")).ToNot(HavePassedRequirements())
    58  		})
    59  
    60  		It("when a space is not targeted", func() {
    61  			requirementsFactory.TargetedSpaceSuccess = false
    62  			Expect(runCommand("source-app", "target-app")).ToNot(HavePassedRequirements())
    63  		})
    64  
    65  		It("when provided too many args", func() {
    66  			Expect(runCommand("source-app", "target-app", "too-much", "app-name")).ToNot(HavePassedRequirements())
    67  		})
    68  	})
    69  
    70  	Describe("Passing requirements", func() {
    71  		BeforeEach(func() {
    72  			requirementsFactory.LoginSuccess = true
    73  			requirementsFactory.TargetedSpaceSuccess = true
    74  		})
    75  
    76  		Context("refreshing the auth token", func() {
    77  			It("makes a call for the app token", func() {
    78  				runCommand("source-app", "target-app")
    79  				Expect(authRepo.RefreshTokenCalled).To(BeTrue())
    80  			})
    81  
    82  			Context("when refreshing the auth token fails", func() {
    83  				BeforeEach(func() {
    84  					authRepo.RefreshTokenError = errors.New("I accidentally the UAA")
    85  				})
    86  
    87  				It("it displays an error", func() {
    88  					runCommand("source-app", "target-app")
    89  					Expect(ui.Outputs).To(ContainSubstrings(
    90  						[]string{"FAILED"},
    91  						[]string{"accidentally the UAA"},
    92  					))
    93  				})
    94  			})
    95  
    96  			Describe("when retrieving the app token succeeds", func() {
    97  				var (
    98  					sourceApp, targetApp models.Application
    99  				)
   100  
   101  				BeforeEach(func() {
   102  					sourceApp = models.Application{
   103  						ApplicationFields: models.ApplicationFields{
   104  							Name: "source-app",
   105  							Guid: "source-app-guid",
   106  						},
   107  					}
   108  					appRepo.ReadReturns.App = sourceApp
   109  
   110  					targetApp = models.Application{
   111  						ApplicationFields: models.ApplicationFields{
   112  							Name: "target-app",
   113  							Guid: "target-app-guid",
   114  						},
   115  					}
   116  					appRepo.ReadFromSpaceReturns(targetApp, nil)
   117  				})
   118  
   119  				Describe("when no parameters are passed", func() {
   120  					It("obtains both the source and target application from the same space", func() {
   121  						runCommand("source-app", "target-app")
   122  
   123  						targetAppName, spaceGuid := appRepo.ReadFromSpaceArgsForCall(0)
   124  						Expect(targetAppName).To(Equal("target-app"))
   125  						Expect(spaceGuid).To(Equal("my-space-guid"))
   126  
   127  						Expect(appRepo.ReadArgs.Name).To(Equal("source-app"))
   128  
   129  						sourceAppGuid, targetAppGuid := copyAppSourceRepo.CopyApplicationArgsForCall(0)
   130  						Expect(sourceAppGuid).To(Equal("source-app-guid"))
   131  						Expect(targetAppGuid).To(Equal("target-app-guid"))
   132  
   133  						appArg, orgName, spaceName := appRestarter.ApplicationRestartArgsForCall(0)
   134  						Expect(appArg).To(Equal(targetApp))
   135  						Expect(orgName).To(Equal(config.OrganizationFields().Name))
   136  						Expect(spaceName).To(Equal(config.SpaceFields().Name))
   137  
   138  						Expect(ui.Outputs).To(ContainSubstrings(
   139  							[]string{"Copying source from app", "source-app", "to target app", "target-app", "in org my-org / space my-space as my-user..."},
   140  							[]string{"Note: this may take some time"},
   141  							[]string{"OK"},
   142  						))
   143  					})
   144  
   145  					Context("Failures", func() {
   146  						It("if we cannot obtain the source application", func() {
   147  							appRepo.ReadReturns.Error = errors.New("could not find source app")
   148  							runCommand("source-app", "target-app")
   149  
   150  							Expect(ui.Outputs).To(ContainSubstrings(
   151  								[]string{"FAILED"},
   152  								[]string{"could not find source app"},
   153  							))
   154  						})
   155  
   156  						It("fails if we cannot obtain the target application", func() {
   157  							appRepo.ReadFromSpaceReturns(models.Application{}, errors.New("could not find target app"))
   158  							runCommand("source-app", "target-app")
   159  
   160  							Expect(ui.Outputs).To(ContainSubstrings(
   161  								[]string{"FAILED"},
   162  								[]string{"could not find target app"},
   163  							))
   164  						})
   165  					})
   166  				})
   167  
   168  				Describe("when a space is provided, but not an org", func() {
   169  					It("send the correct target appplication for the current org and target space", func() {
   170  						spaceRepo.Spaces = []models.Space{
   171  							{
   172  								SpaceFields: models.SpaceFields{
   173  									Name: "space-name",
   174  									Guid: "model-space-guid",
   175  								},
   176  							},
   177  						}
   178  
   179  						runCommand("-s", "space-name", "source-app", "target-app")
   180  
   181  						targetAppName, spaceGuid := appRepo.ReadFromSpaceArgsForCall(0)
   182  						Expect(targetAppName).To(Equal("target-app"))
   183  						Expect(spaceGuid).To(Equal("model-space-guid"))
   184  
   185  						Expect(ui.Outputs).To(ContainSubstrings(
   186  							[]string{"Copying source from app", "source-app", "to target app", "target-app", "in org my-org / space space-name as my-user..."},
   187  							[]string{"Note: this may take some time"},
   188  							[]string{"OK"},
   189  						))
   190  					})
   191  
   192  					Context("Failures", func() {
   193  						It("when we cannot find the provided space", func() {
   194  							spaceRepo.FindByNameErr = true
   195  
   196  							runCommand("-s", "space-name", "source-app", "target-app")
   197  							Expect(ui.Outputs).To(ContainSubstrings(
   198  								[]string{"FAILED"},
   199  								[]string{"Error finding space by name."},
   200  							))
   201  						})
   202  					})
   203  				})
   204  
   205  				Describe("when an org and space name are passed as parameters", func() {
   206  					It("send the correct target application for the space and org", func() {
   207  						orgRepo.FindByNameReturns(models.Organization{
   208  							Spaces: []models.SpaceFields{
   209  								{
   210  									Name: "space-name",
   211  									Guid: "space-guid",
   212  								},
   213  							},
   214  						}, nil)
   215  
   216  						runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   217  
   218  						targetAppName, spaceGuid := appRepo.ReadFromSpaceArgsForCall(0)
   219  						Expect(targetAppName).To(Equal("target-app"))
   220  						Expect(spaceGuid).To(Equal("space-guid"))
   221  
   222  						sourceAppGuid, targetAppGuid := copyAppSourceRepo.CopyApplicationArgsForCall(0)
   223  						Expect(sourceAppGuid).To(Equal("source-app-guid"))
   224  						Expect(targetAppGuid).To(Equal("target-app-guid"))
   225  
   226  						appArg, orgName, spaceName := appRestarter.ApplicationRestartArgsForCall(0)
   227  						Expect(appArg).To(Equal(targetApp))
   228  						Expect(orgName).To(Equal("org-name"))
   229  						Expect(spaceName).To(Equal("space-name"))
   230  
   231  						Expect(ui.Outputs).To(ContainSubstrings(
   232  							[]string{"Copying source from app source-app to target app target-app in org org-name / space space-name as my-user..."},
   233  							[]string{"Note: this may take some time"},
   234  							[]string{"OK"},
   235  						))
   236  					})
   237  
   238  					Context("failures", func() {
   239  						It("cannot just accept an organization and no space", func() {
   240  							runCommand("-o", "org-name", "source-app", "target-app")
   241  
   242  							Expect(ui.Outputs).To(ContainSubstrings(
   243  								[]string{"FAILED"},
   244  								[]string{"Please provide the space within the organization containing the target application"},
   245  							))
   246  						})
   247  
   248  						It("when we cannot find the provided org", func() {
   249  							orgRepo.FindByNameReturns(models.Organization{}, errors.New("Could not find org"))
   250  							runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   251  
   252  							Expect(ui.Outputs).To(ContainSubstrings(
   253  								[]string{"FAILED"},
   254  								[]string{"Could not find org"},
   255  							))
   256  						})
   257  
   258  						It("when the org does not contain the space name provide", func() {
   259  							orgRepo.FindByNameReturns(models.Organization{}, nil)
   260  							runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   261  
   262  							Expect(ui.Outputs).To(ContainSubstrings(
   263  								[]string{"FAILED"},
   264  								[]string{"Could not find space space-name in organization org-name"},
   265  							))
   266  						})
   267  
   268  						It("when the targeted app does not exist in the targeted org and space", func() {
   269  							orgRepo.FindByNameReturns(models.Organization{
   270  								Spaces: []models.SpaceFields{
   271  									{
   272  										Name: "space-name",
   273  										Guid: "space-guid",
   274  									},
   275  								},
   276  							}, nil)
   277  
   278  							appRepo.ReadFromSpaceReturns(models.Application{}, errors.New("could not find app"))
   279  							runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   280  
   281  							Expect(ui.Outputs).To(ContainSubstrings(
   282  								[]string{"FAILED"},
   283  								[]string{"could not find app"},
   284  							))
   285  						})
   286  					})
   287  				})
   288  
   289  				Describe("when the --no-restart flag is passed", func() {
   290  					It("does not restart the target application", func() {
   291  						runCommand("--no-restart", "source-app", "target-app")
   292  						Expect(appRestarter.ApplicationRestartCallCount()).To(Equal(0))
   293  					})
   294  				})
   295  			})
   296  		})
   297  	})
   298  })