github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/v3/repository/repository_test.go (about)

     1  package repository_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     7  	"github.com/cloudfoundry/cli/cf/v3/models"
     8  	"github.com/cloudfoundry/cli/cf/v3/repository"
     9  	"github.com/cloudfoundry/cli/testhelpers/configuration"
    10  
    11  	ccClientFakes "github.com/cloudfoundry/go-ccapi/v3/client/fakes"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Repository", func() {
    18  	var (
    19  		r        repository.Repository
    20  		ccClient *ccClientFakes.FakeClient
    21  		config   coreconfig.ReadWriter
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		ccClient = &ccClientFakes.FakeClient{}
    26  		config = configuration.NewRepositoryWithDefaults()
    27  		r = repository.NewRepository(config, ccClient)
    28  	})
    29  
    30  	Describe("GetApplications", func() {
    31  		It("tries to get applications from CC with a token handler", func() {
    32  			r.GetApplications()
    33  			Expect(ccClient.GetApplicationsCallCount()).To(Equal(1))
    34  		})
    35  
    36  		Context("when the client has updated tokens", func() {
    37  			BeforeEach(func() {
    38  				ccClient.TokensUpdatedReturns(true)
    39  				ccClient.GetUpdatedTokensReturns("updated-access-token", "updated-refresh-token")
    40  			})
    41  
    42  			It("stores the new tokens in the config", func() {
    43  				r.GetApplications()
    44  				Expect(config.AccessToken()).To(Equal("updated-access-token"))
    45  				Expect(config.RefreshToken()).To(Equal("updated-refresh-token"))
    46  			})
    47  		})
    48  
    49  		Context("when getting the applications succeeds", func() {
    50  			BeforeEach(func() {
    51  				ccClient.GetApplicationsReturns(getApplicationsJSON, nil)
    52  			})
    53  
    54  			It("returns a slice of application model objects", func() {
    55  				applications, err := r.GetApplications()
    56  				Expect(err).NotTo(HaveOccurred())
    57  				Expect(applications).To(Equal([]models.V3Application{
    58  					{
    59  						Name:                  "app-1-name",
    60  						DesiredState:          "STOPPED",
    61  						TotalDesiredInstances: 1,
    62  						Links: models.Links{
    63  							Processes: models.Link{
    64  								Href: "/v3/apps/app-1-guid/processes",
    65  							},
    66  							Routes: models.Link{
    67  								Href: "/v3/apps/app-1-guid/routes",
    68  							},
    69  						},
    70  					},
    71  					{
    72  						Name:                  "app-2-name",
    73  						DesiredState:          "RUNNING",
    74  						TotalDesiredInstances: 2,
    75  						Links: models.Links{
    76  							Processes: models.Link{
    77  								Href: "/v3/apps/app-2-guid/processes",
    78  							},
    79  							Routes: models.Link{
    80  								Href: "/v3/apps/app-2-guid/routes",
    81  							},
    82  						},
    83  					},
    84  				}))
    85  			})
    86  		})
    87  
    88  		Context("when getting the applications returns JSON that cannot be parsed", func() {
    89  			BeforeEach(func() {
    90  				ccClient.GetApplicationsReturns([]byte(`:bad_json:`), nil)
    91  			})
    92  
    93  			It("returns a slice of application model objects", func() {
    94  				_, err := r.GetApplications()
    95  				Expect(err).To(HaveOccurred())
    96  			})
    97  		})
    98  	})
    99  
   100  	Describe("GetProcesses", func() {
   101  		It("tries to get processes from CC with a token handler", func() {
   102  			r.GetProcesses("/the-path")
   103  			Expect(ccClient.GetResourcesCallCount()).To(Equal(1))
   104  			Expect(ccClient.GetResourcesArgsForCall(0)).To(Equal("/the-path"))
   105  		})
   106  
   107  		Context("when the client has updated tokens", func() {
   108  			BeforeEach(func() {
   109  				ccClient.TokensUpdatedReturns(true)
   110  				ccClient.GetUpdatedTokensReturns("updated-access-token", "updated-refresh-token")
   111  			})
   112  
   113  			It("stores the new tokens in the config", func() {
   114  				r.GetProcesses("/the-path")
   115  				Expect(config.AccessToken()).To(Equal("updated-access-token"))
   116  				Expect(config.RefreshToken()).To(Equal("updated-refresh-token"))
   117  			})
   118  		})
   119  
   120  		Context("when getting the processes fails", func() {
   121  			BeforeEach(func() {
   122  				ccClient.GetResourcesReturns([]byte{}, errors.New("get-processes-err"))
   123  			})
   124  
   125  			It("returns an error", func() {
   126  				_, err := r.GetProcesses("/the-path")
   127  				Expect(err).To(HaveOccurred())
   128  				Expect(err.Error()).To(Equal("get-processes-err"))
   129  			})
   130  		})
   131  
   132  		Context("when getting the processes succeeds", func() {
   133  			BeforeEach(func() {
   134  				ccClient.GetResourcesReturns(getProcessesJSON, nil)
   135  			})
   136  
   137  			It("returns a slice of procees model objects", func() {
   138  				processes, err := r.GetProcesses("/the-path")
   139  				Expect(err).NotTo(HaveOccurred())
   140  				Expect(processes).To(Equal([]models.V3Process{
   141  					{
   142  						Type:       "web",
   143  						Instances:  1,
   144  						MemoryInMB: 1024,
   145  						DiskInMB:   1024,
   146  					},
   147  					{
   148  						Type:       "web",
   149  						Instances:  2,
   150  						MemoryInMB: 512,
   151  						DiskInMB:   512,
   152  					},
   153  				}))
   154  			})
   155  		})
   156  	})
   157  
   158  	Describe("GetRoutes", func() {
   159  		It("tries to get routes from CC with a token handler", func() {
   160  			r.GetRoutes("/the-path")
   161  			Expect(ccClient.GetResourcesCallCount()).To(Equal(1))
   162  			Expect(ccClient.GetResourcesArgsForCall(0)).To(Equal("/the-path"))
   163  		})
   164  
   165  		Context("when the client has updated tokens", func() {
   166  			BeforeEach(func() {
   167  				ccClient.TokensUpdatedReturns(true)
   168  				ccClient.GetUpdatedTokensReturns("updated-access-token", "updated-refresh-token")
   169  			})
   170  
   171  			It("stores the new tokens in the config", func() {
   172  				r.GetRoutes("/the-path")
   173  				Expect(config.AccessToken()).To(Equal("updated-access-token"))
   174  				Expect(config.RefreshToken()).To(Equal("updated-refresh-token"))
   175  			})
   176  		})
   177  
   178  		Context("when getting the routes fails", func() {
   179  			BeforeEach(func() {
   180  				ccClient.GetResourcesReturns([]byte{}, errors.New("get-routes-err"))
   181  			})
   182  
   183  			It("returns an error", func() {
   184  				_, err := r.GetRoutes("/the-path")
   185  				Expect(err).To(HaveOccurred())
   186  				Expect(err.Error()).To(Equal("get-routes-err"))
   187  			})
   188  		})
   189  
   190  		Context("when getting the routes succeeds", func() {
   191  			BeforeEach(func() {
   192  				ccClient.GetResourcesReturns(getRoutesJSON, nil)
   193  			})
   194  
   195  			It("returns a slice of routes model objects", func() {
   196  				routes, err := r.GetRoutes("/the-path")
   197  				Expect(err).NotTo(HaveOccurred())
   198  				Expect(routes).To(Equal([]models.V3Route{
   199  					{
   200  						Host: "route-1-host",
   201  						Path: "/route-1-path",
   202  					},
   203  					{
   204  						Host: "route-2-host",
   205  						Path: "",
   206  					},
   207  				}))
   208  			})
   209  		})
   210  	})
   211  })
   212  
   213  var getApplicationsJSON = []byte(`[
   214  {
   215  	"guid": "app-1-guid",
   216  	"name": "app-1-name",
   217  	"desired_state": "STOPPED",
   218  	"total_desired_instances": 1,
   219  	"created_at": "1970-01-01T00:00:03Z",
   220  	"lifecycle": {
   221  		"type": "buildpack",
   222  		"data": {
   223  			"buildpack": "app-1-buildpack-name",
   224  			"stack": "app-1-buildpack-stack"
   225  		}
   226  	},
   227  	"environment_variables": {
   228  		"key": "value"
   229  	},
   230  	"links": {
   231  			"self": {
   232  				"href": "/v3/apps/app-1-guid"
   233  			},
   234  			"space": {
   235  				"href": "/v2/spaces/space-guid"
   236  			},
   237  			"processes": {
   238  				"href": "/v3/apps/app-1-guid/processes"
   239  			},
   240  			"routes": {
   241  				"href": "/v3/apps/app-1-guid/routes"
   242  			},
   243  			"packages": {
   244  				"href": "/v3/apps/app-1-guid/packages"
   245  			},
   246  			"droplets": {
   247  				"href": "/v3/apps/app-1-guid/droplets"
   248  			},
   249  			"start": {
   250  				"href": "/v3/apps/app-1-guid/start",
   251  				"method": "PUT"
   252  			},
   253  			"stop": {
   254  				"href": "/v3/apps/app-1-guid/stop",
   255  				"method": "PUT"
   256  			},
   257  			"assign_current_droplet": {
   258  				"href": "/v3/apps/app-1-guid/current_droplet",
   259  				"method": "PUT"
   260  			}
   261  		}
   262  },
   263  {
   264  	"guid": "app-2-guid",
   265  	"name": "app-2-name",
   266  	"desired_state": "RUNNING",
   267  	"total_desired_instances": 2,
   268  	"created_at": "1970-01-01T00:00:03Z",
   269  	"lifecycle": {
   270  		"type": "buildpack",
   271  		"data": {
   272  			"buildpack": "app-2-buildpack-name",
   273  			"stack": "app-2-buildpack-stack"
   274  		}
   275  	},
   276  	"environment_variables": {},
   277  	"links": {
   278  			"self": {
   279  				"href": "/v3/apps/app-2-guid"
   280  			},
   281  			"space": {
   282  				"href": "/v2/spaces/space-guid"
   283  			},
   284  			"processes": {
   285  				"href": "/v3/apps/app-2-guid/processes"
   286  			},
   287  			"routes": {
   288  				"href": "/v3/apps/app-2-guid/routes"
   289  			},
   290  			"packages": {
   291  				"href": "/v3/apps/app-2-guid/packages"
   292  			},
   293  			"droplets": {
   294  				"href": "/v3/apps/app-2-guid/droplets"
   295  			},
   296  			"start": {
   297  				"href": "/v3/apps/app-2-guid/start",
   298  				"method": "PUT"
   299  			},
   300  			"stop": {
   301  				"href": "/v3/apps/app-2-guid/stop",
   302  				"method": "PUT"
   303  			},
   304  			"assign_current_droplet": {
   305  				"href": "/v3/apps/app-2-guid/current_droplet",
   306  				"method": "PUT"
   307  			}
   308  		}
   309  	}
   310  ]`)
   311  
   312  var getProcessesJSON = []byte(`[
   313  	{
   314  	  "guid": "process-1-guid",
   315  	  "type": "web",
   316  	  "command": null,
   317  	  "instances": 1,
   318  	  "memory_in_mb": 1024,
   319  	  "disk_in_mb": 1024,
   320  	  "created_at": "2015-12-22T18:28:11Z",
   321  	  "updated_at": "2015-12-22T18:28:11Z",
   322  	  "links": {
   323  	    "self": {
   324  	      "href": "/v3/processes/process-1-guid"
   325  	    },
   326  	    "scale": {
   327  	      "href": "/v3/processes/process-1-guid/scale",
   328  	      "method": "PUT"
   329  	    },
   330  	    "app": {
   331  	      "href": "/v3/apps/app-1-guid"
   332  	    },
   333  	    "space": {
   334  	      "href": "/v2/spaces/process-1-guid"
   335  	    }
   336  	  }
   337  	},
   338  	{
   339  		"guid": "process-2-guid",
   340  		"type": "web",
   341  		"command": null,
   342  		"instances": 2,
   343  		"memory_in_mb": 512,
   344  		"disk_in_mb": 512,
   345  		"created_at": "2015-12-22T18:28:11Z",
   346  		"updated_at": "2015-12-22T18:28:11Z",
   347  		"links": {
   348  			"self": {
   349  				"href": "/v3/processes/process-2-guid"
   350  			},
   351  			"scale": {
   352  				"href": "/v3/processes/process-2-guid/scale",
   353  				"method": "PUT"
   354  			},
   355  			"app": {
   356  				"href": "/v3/apps/app-2-guid"
   357  			},
   358  			"space": {
   359  				"href": "/v2/spaces/process-2-guid"
   360  			}
   361  		}
   362  	}
   363  ]`)
   364  
   365  var getRoutesJSON = []byte(`
   366  [
   367    {
   368      "guid": "8e1e3d10-5c77-48e7-9c15-38d4c0946a1c",
   369      "host": "route-1-host",
   370      "path": "/route-1-path",
   371      "created_at": "2015-12-22T18:28:01Z",
   372      "updated_at": null,
   373      "links": {
   374        "space": {
   375          "href": "/v2/spaces/be76f146-2139-4589-96a3-3489bfc888d0"
   376        },
   377        "domain": {
   378          "href": "/v2/domains/235d9128-b3d9-4b6b-b1c3-911159590e3c"
   379        }
   380      }
   381    },
   382    {
   383      "guid": "efab3642-71b1-4ce9-ab7b-28515241e090",
   384      "host": "route-2-host",
   385      "path": "",
   386      "created_at": "2015-12-22T18:28:01Z",
   387      "updated_at": null,
   388      "links": {
   389        "space": {
   390          "href": "/v2/spaces/be76f146-2139-4589-96a3-3489bfc888d0"
   391        },
   392        "domain": {
   393          "href": "/v2/domains/3cda59fc-ce99-463e-9fe9-e9a325fb8797"
   394        }
   395      }
   396    }
   397  ]`)