github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/security_groups/defaults/running/running_test.go (about)

     1  package running_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/models"
    11  	"github.com/cloudfoundry/cli/cf/net"
    12  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    13  	testnet "github.com/cloudfoundry/cli/testhelpers/net"
    14  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    15  
    16  	. "github.com/cloudfoundry/cli/cf/api/security_groups/defaults/running"
    17  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("RunningSecurityGroupsRepo", func() {
    23  	var (
    24  		testServer  *httptest.Server
    25  		testHandler *testnet.TestHandler
    26  		configRepo  core_config.ReadWriter
    27  		repo        RunningSecurityGroupsRepo
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		configRepo = testconfig.NewRepositoryWithDefaults()
    32  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{})
    33  		repo = NewRunningSecurityGroupsRepo(configRepo, gateway)
    34  	})
    35  
    36  	AfterEach(func() {
    37  		testServer.Close()
    38  	})
    39  
    40  	setupTestServer := func(reqs ...testnet.TestRequest) {
    41  		testServer, testHandler = testnet.NewServer(reqs)
    42  		configRepo.SetApiEndpoint(testServer.URL)
    43  	}
    44  
    45  	Describe(".BindToRunningSet", func() {
    46  		It("makes a correct request", func() {
    47  			setupTestServer(
    48  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    49  					Method: "PUT",
    50  					Path:   "/v2/config/running_security_groups/a-real-guid",
    51  					Response: testnet.TestResponse{
    52  						Status: http.StatusCreated,
    53  						Body:   bindRunningResponse,
    54  					},
    55  				}),
    56  			)
    57  
    58  			err := repo.BindToRunningSet("a-real-guid")
    59  
    60  			Expect(err).ToNot(HaveOccurred())
    61  			Expect(testHandler).To(HaveAllRequestsCalled())
    62  		})
    63  	})
    64  
    65  	Describe(".UnbindFromRunningSet", func() {
    66  		It("makes a correct request", func() {
    67  			testServer, testHandler = testnet.NewServer([]testnet.TestRequest{
    68  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    69  					Method: "DELETE",
    70  					Path:   "/v2/config/running_security_groups/my-guid",
    71  					Response: testnet.TestResponse{
    72  						Status: http.StatusNoContent,
    73  					},
    74  				}),
    75  			})
    76  
    77  			configRepo.SetApiEndpoint(testServer.URL)
    78  			err := repo.UnbindFromRunningSet("my-guid")
    79  
    80  			Expect(err).ToNot(HaveOccurred())
    81  			Expect(testHandler).To(HaveAllRequestsCalled())
    82  		})
    83  	})
    84  
    85  	Describe(".List", func() {
    86  		It("returns a list of security groups that are the defaults for running", func() {
    87  			setupTestServer(
    88  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    89  					Method: "GET",
    90  					Path:   "/v2/config/running_security_groups",
    91  					Response: testnet.TestResponse{
    92  						Status: http.StatusOK,
    93  						Body:   firstRunningListItem,
    94  					},
    95  				}),
    96  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    97  					Method: "GET",
    98  					Path:   "/v2/config/running_security_groups",
    99  					Response: testnet.TestResponse{
   100  						Status: http.StatusOK,
   101  						Body:   secondRunningListItem,
   102  					},
   103  				}),
   104  			)
   105  
   106  			defaults, err := repo.List()
   107  
   108  			Expect(err).ToNot(HaveOccurred())
   109  			Expect(testHandler).To(HaveAllRequestsCalled())
   110  			Expect(defaults).To(ConsistOf([]models.SecurityGroupFields{
   111  				{
   112  					Name: "name-71",
   113  					Guid: "cd186158-b356-474d-9861-724f34f48502",
   114  					Rules: []map[string]interface{}{{
   115  						"protocol": "udp",
   116  					}},
   117  				},
   118  				{
   119  					Name: "name-72",
   120  					Guid: "d3374b62-7eac-4823-afbd-460d2bf44c67",
   121  					Rules: []map[string]interface{}{{
   122  						"destination": "198.41.191.47/1",
   123  					}},
   124  				},
   125  			}))
   126  		})
   127  	})
   128  })
   129  
   130  var bindRunningResponse string = `{
   131    "metadata": {
   132      "guid": "897341eb-ef31-406f-b57b-414f51583a3a",
   133      "url": "/v2/config/running_security_groups/897341eb-ef31-406f-b57b-414f51583a3a",
   134      "created_at": "2014-06-23T21:43:30+00:00",
   135      "updated_at": "2014-06-23T21:43:30+00:00"
   136    },
   137    "entity": {
   138      "name": "name-904",
   139      "rules": [
   140        {
   141          "protocol": "udp",
   142          "ports": "8080",
   143          "destination": "198.41.191.47/1"
   144        }
   145      ]
   146    }
   147  }`
   148  
   149  var firstRunningListItem string = `{
   150    "next_url": "/v2/config/running_security_groups?page=2",
   151    "resources": [
   152      {
   153        "metadata": {
   154          "guid": "cd186158-b356-474d-9861-724f34f48502",
   155          "url": "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502",
   156          "created_at": "2014-06-23T22:55:30+00:00",
   157          "updated_at": null
   158        },
   159        "entity": {
   160          "name": "name-71",
   161          "rules": [
   162            {
   163              "protocol": "udp"
   164            }
   165          ],
   166          "spaces_url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces"
   167        }
   168      }
   169    ]
   170  }`
   171  
   172  var secondRunningListItem string = `{
   173    "next_url": null,
   174    "resources": [
   175      {
   176        "metadata": {
   177          "guid": "d3374b62-7eac-4823-afbd-460d2bf44c67",
   178          "url": "/v2/config/running_security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67",
   179          "created_at": "2014-06-23T22:55:30+00:00",
   180          "updated_at": null
   181        },
   182        "entity": {
   183          "name": "name-72",
   184          "rules": [
   185            {
   186              "destination": "198.41.191.47/1"
   187            }
   188          ],
   189          "spaces_url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces"
   190        }
   191      }
   192    ]
   193  }`