github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/cf/api/securitygroups/defaults/staging/staging_test.go (about)

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