github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/stacks/stacks_test.go (about)

     1  package stacks_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"time"
     7  
     8  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     9  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    10  	"github.com/cloudfoundry/cli/cf/errors"
    11  	"github.com/cloudfoundry/cli/cf/models"
    12  	"github.com/cloudfoundry/cli/cf/net"
    13  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    14  	testnet "github.com/cloudfoundry/cli/testhelpers/net"
    15  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    16  
    17  	. "github.com/cloudfoundry/cli/cf/api/stacks"
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("StacksRepo", func() {
    24  	var (
    25  		testServer  *httptest.Server
    26  		testHandler *testnet.TestHandler
    27  		configRepo  core_config.ReadWriter
    28  		repo        StackRepository
    29  	)
    30  
    31  	setupTestServer := func(reqs ...testnet.TestRequest) {
    32  		testServer, testHandler = testnet.NewServer(reqs)
    33  		configRepo.SetApiEndpoint(testServer.URL)
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		configRepo = testconfig.NewRepositoryWithDefaults()
    38  		configRepo.SetAccessToken("BEARER my_access_token")
    39  
    40  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{})
    41  		repo = NewCloudControllerStackRepository(configRepo, gateway)
    42  	})
    43  
    44  	AfterEach(func() {
    45  		testServer.Close()
    46  	})
    47  
    48  	Describe("FindByName", func() {
    49  		Context("when a stack exists", func() {
    50  			BeforeEach(func() {
    51  				setupTestServer(testnet.TestRequest{
    52  					Method: "GET",
    53  					Path:   "/v2/stacks?q=name%3Alinux",
    54  					Response: testnet.TestResponse{
    55  						Status: http.StatusOK,
    56  						Body: `
    57  				{
    58  					"resources": [
    59  						{
    60  						  "metadata": { "guid": "custom-linux-guid" },
    61  						  "entity": { "name": "custom-linux" }
    62  						}
    63  					]
    64  				}`}})
    65  			})
    66  
    67  			It("finds the stack", func() {
    68  				stack, err := repo.FindByName("linux")
    69  
    70  				Expect(testHandler).To(HaveAllRequestsCalled())
    71  				Expect(err).NotTo(HaveOccurred())
    72  				Expect(stack).To(Equal(models.Stack{
    73  					Name: "custom-linux",
    74  					Guid: "custom-linux-guid",
    75  				}))
    76  			})
    77  		})
    78  
    79  		Context("when a stack does not exist", func() {
    80  			BeforeEach(func() {
    81  				setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    82  					Method: "GET",
    83  					Path:   "/v2/stacks?q=name%3Alinux",
    84  					Response: testnet.TestResponse{
    85  						Status: http.StatusOK,
    86  						Body:   ` { "resources": []}`,
    87  					}}))
    88  			})
    89  
    90  			It("returns an error", func() {
    91  				_, err := repo.FindByName("linux")
    92  
    93  				Expect(testHandler).To(HaveAllRequestsCalled())
    94  				Expect(err).To(BeAssignableToTypeOf(&errors.ModelNotFoundError{}))
    95  			})
    96  		})
    97  	})
    98  
    99  	Describe("FindAll", func() {
   100  		BeforeEach(func() {
   101  			setupTestServer(
   102  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   103  					Method: "GET",
   104  					Path:   "/v2/stacks",
   105  					Response: testnet.TestResponse{
   106  						Status: http.StatusOK,
   107  						Body: `{
   108  							"next_url": "/v2/stacks?page=2",
   109  							"resources": [
   110  								{
   111  									"metadata": {
   112  										"guid": "stack-guid-1",
   113  										"url": "/v2/stacks/stack-guid-1",
   114  										"created_at": "2013-08-31 01:32:40 +0000",
   115  										"updated_at": "2013-08-31 01:32:40 +0000"
   116  									},
   117  									"entity": {
   118  										"name": "lucid64",
   119  										"description": "Ubuntu 10.04"
   120  									}
   121  								}
   122  							]
   123  						}`}}),
   124  
   125  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   126  					Method: "GET",
   127  					Path:   "/v2/stacks",
   128  					Response: testnet.TestResponse{
   129  						Status: http.StatusOK,
   130  						Body: `
   131  						{
   132  							"resources": [
   133  								{
   134  									"metadata": {
   135  										"guid": "stack-guid-2",
   136  										"url": "/v2/stacks/stack-guid-2",
   137  										"created_at": "2013-08-31 01:32:40 +0000",
   138  										"updated_at": "2013-08-31 01:32:40 +0000"
   139  									},
   140  									"entity": {
   141  										"name": "lucid64custom",
   142  										"description": "Fake Ubuntu 10.04"
   143  									}
   144  								}
   145  							]
   146  						}`}}))
   147  		})
   148  
   149  		It("finds all the stacks", func() {
   150  			stacks, err := repo.FindAll()
   151  
   152  			Expect(testHandler).To(HaveAllRequestsCalled())
   153  			Expect(err).NotTo(HaveOccurred())
   154  			Expect(stacks).To(ConsistOf([]models.Stack{
   155  				{
   156  					Guid:        "stack-guid-1",
   157  					Name:        "lucid64",
   158  					Description: "Ubuntu 10.04",
   159  				},
   160  				{
   161  					Guid:        "stack-guid-2",
   162  					Name:        "lucid64custom",
   163  					Description: "Fake Ubuntu 10.04",
   164  				},
   165  			}))
   166  		})
   167  	})
   168  })