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

     1  package security_groups_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/security_groups"
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("app security group api", func() {
    24  	var (
    25  		testServer  *httptest.Server
    26  		testHandler *testnet.TestHandler
    27  		configRepo  core_config.ReadWriter
    28  		repo        SecurityGroupRepo
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		configRepo = testconfig.NewRepositoryWithDefaults()
    33  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{})
    34  		repo = NewSecurityGroupRepo(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(".Create", func() {
    47  		It("can create an app security group, given some attributes", func() {
    48  			req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    49  				Method: "POST",
    50  				Path:   "/v2/security_groups",
    51  				// FIXME: this matcher depend on the order of the key/value pairs in the map
    52  				Matcher: testnet.RequestBodyMatcher(`{
    53  					"name": "mygroup",
    54  					"rules": [{"my-house": "my-rules"}]
    55  				}`),
    56  				Response: testnet.TestResponse{Status: http.StatusCreated},
    57  			})
    58  			setupTestServer(req)
    59  
    60  			err := repo.Create(
    61  				"mygroup",
    62  				[]map[string]interface{}{{"my-house": "my-rules"}},
    63  			)
    64  
    65  			Expect(err).NotTo(HaveOccurred())
    66  			Expect(testHandler).To(HaveAllRequestsCalled())
    67  		})
    68  	})
    69  
    70  	Describe(".Read", func() {
    71  		It("returns the app security group with the given name", func() {
    72  			setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{
    73  				Method: "GET",
    74  				Path:   "/v2/security_groups?q=name:the-name&inline-relations-depth=2",
    75  				Response: testnet.TestResponse{
    76  					Status: http.StatusOK,
    77  					Body: `
    78  {
    79     "resources": [
    80        {
    81           "metadata": {
    82              "guid": "the-group-guid"
    83           },
    84           "entity": {
    85              "name": "the-name",
    86              "rules": [{"key": "value"}],
    87              "spaces": [
    88                 {
    89                 	  "metadata":{
    90                 	  	"guid": "my-space-guid"
    91                 	  },
    92                    "entity": {
    93                       "name": "my-space",
    94                       "organization": {
    95                          "metadata": {
    96                             "guid": "my-org-guid"
    97                          },
    98                          "entity": {
    99                             "name": "my-org"
   100                          }
   101                       }
   102                    }
   103                 }
   104              ]
   105           }
   106        }
   107     ]
   108  }
   109  					`,
   110  				},
   111  			}))
   112  
   113  			group, err := repo.Read("the-name")
   114  
   115  			Expect(err).ToNot(HaveOccurred())
   116  			Expect(group).To(Equal(models.SecurityGroup{
   117  				SecurityGroupFields: models.SecurityGroupFields{
   118  					Name:  "the-name",
   119  					Guid:  "the-group-guid",
   120  					Rules: []map[string]interface{}{{"key": "value"}},
   121  				},
   122  				Spaces: []models.Space{
   123  					{
   124  						SpaceFields:  models.SpaceFields{Guid: "my-space-guid", Name: "my-space"},
   125  						Organization: models.OrganizationFields{Guid: "my-org-guid", Name: "my-org"},
   126  					},
   127  				},
   128  			}))
   129  		})
   130  
   131  		It("returns a ModelNotFound error if the security group cannot be found", func() {
   132  			setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   133  				Method: "GET",
   134  				Path:   "/v2/security_groups?q=name:the-name&inline-relations-depth=2",
   135  				Response: testnet.TestResponse{
   136  					Status: http.StatusOK,
   137  					Body:   `{"resources": []}`,
   138  				},
   139  			}))
   140  
   141  			_, err := repo.Read("the-name")
   142  
   143  			Expect(err).To(HaveOccurred())
   144  			Expect(err).To(BeAssignableToTypeOf(errors.NewModelNotFoundError("model-type", "description")))
   145  		})
   146  	})
   147  
   148  	Describe(".Delete", func() {
   149  		It("deletes the security group", func() {
   150  			securityGroupGuid := "the-security-group-guid"
   151  			setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   152  				Method: "DELETE",
   153  				Path:   "/v2/security_groups/" + securityGroupGuid,
   154  				Response: testnet.TestResponse{
   155  					Status: http.StatusNoContent,
   156  				},
   157  			}))
   158  
   159  			err := repo.Delete(securityGroupGuid)
   160  
   161  			Expect(err).ToNot(HaveOccurred())
   162  		})
   163  	})
   164  
   165  	Describe(".FindAll", func() {
   166  		It("returns all the security groups", func() {
   167  			setupTestServer(
   168  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   169  					Method: "GET",
   170  					Path:   "/v2/security_groups?inline-relations-depth=2",
   171  					Response: testnet.TestResponse{
   172  						Status: http.StatusOK,
   173  						Body:   firstListItem(),
   174  					},
   175  				}),
   176  				testapi.NewCloudControllerTestRequest(testnet.TestRequest{
   177  					Method: "GET",
   178  					Path:   "/v2/security_groups?inline-relations-depth=2&page=2",
   179  					Response: testnet.TestResponse{
   180  						Status: http.StatusOK,
   181  						Body:   secondListItem(),
   182  					},
   183  				}),
   184  			)
   185  
   186  			groups, err := repo.FindAll()
   187  
   188  			Expect(err).ToNot(HaveOccurred())
   189  			Expect(groups[0]).To(Equal(models.SecurityGroup{
   190  				SecurityGroupFields: models.SecurityGroupFields{
   191  					Name:  "name-71",
   192  					Guid:  "cd186158-b356-474d-9861-724f34f48502",
   193  					Rules: []map[string]interface{}{{"protocol": "udp"}},
   194  				},
   195  				Spaces: []models.Space{},
   196  			}))
   197  			Expect(groups[1]).To(Equal(models.SecurityGroup{
   198  				SecurityGroupFields: models.SecurityGroupFields{
   199  					Name:  "name-72",
   200  					Guid:  "d3374b62-7eac-4823-afbd-460d2bf44c67",
   201  					Rules: []map[string]interface{}{{"destination": "198.41.191.47/1"}},
   202  				},
   203  				Spaces: []models.Space{
   204  					{
   205  						SpaceFields:  models.SpaceFields{Guid: "my-space-guid", Name: "my-space"},
   206  						Organization: models.OrganizationFields{Guid: "my-org-guid", Name: "my-org"},
   207  					},
   208  				},
   209  			}))
   210  		})
   211  	})
   212  })
   213  
   214  func firstListItem() string {
   215  	return `{
   216    "next_url": "/v2/security_groups?inline-relations-depth=2&page=2",
   217    "resources": [
   218      {
   219        "metadata": {
   220          "guid": "cd186158-b356-474d-9861-724f34f48502",
   221          "url": "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502",
   222          "created_at": "2014-06-23T22:55:30+00:00",
   223          "updated_at": null
   224        },
   225        "entity": {
   226          "name": "name-71",
   227          "rules": [
   228            {
   229              "protocol": "udp"
   230            }
   231          ],
   232          "spaces_url": "/v2/security_groups/cd186158-b356-474d-9861-724f34f48502/spaces"
   233        }
   234      }
   235    ]
   236  }`
   237  }
   238  
   239  func secondListItem() string {
   240  	return `{
   241    "next_url": null,
   242    "resources": [
   243      {
   244        "metadata": {
   245          "guid": "d3374b62-7eac-4823-afbd-460d2bf44c67",
   246          "url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67",
   247          "created_at": "2014-06-23T22:55:30+00:00",
   248          "updated_at": null
   249        },
   250        "entity": {
   251          "name": "name-72",
   252          "rules": [
   253            {
   254              "destination": "198.41.191.47/1"
   255            }
   256          ],
   257          "spaces": [
   258                 {
   259                 	  "metadata":{
   260                 	  	"guid": "my-space-guid"
   261                 	  },
   262                    "entity": {
   263                       "name": "my-space",
   264                       "organization": {
   265                          "metadata": {
   266                             "guid": "my-org-guid"
   267                          },
   268                          "entity": {
   269                             "name": "my-org"
   270                          }
   271                       }
   272                    }
   273                 }
   274              ],
   275          "spaces_url": "/v2/security_groups/d3374b62-7eac-4823-afbd-460d2bf44c67/spaces"
   276        }
   277      }
   278    ]
   279  }`
   280  }