github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/service_auth_tokens_test.go (about)

     1  package api_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/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    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/util/testhelpers/configuration"
    15  	testnet "code.cloudfoundry.org/cli/util/testhelpers/net"
    16  
    17  	. "code.cloudfoundry.org/cli/cf/api"
    18  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    19  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  )
    23  
    24  var _ = Describe("ServiceAuthTokensRepo", func() {
    25  	var (
    26  		testServer  *httptest.Server
    27  		testHandler *testnet.TestHandler
    28  		configRepo  coreconfig.ReadWriter
    29  		repo        CloudControllerServiceAuthTokenRepository
    30  	)
    31  
    32  	setupTestServer := func(reqs ...testnet.TestRequest) {
    33  		testServer, testHandler = testnet.NewServer(reqs)
    34  		configRepo.SetAPIEndpoint(testServer.URL)
    35  	}
    36  
    37  	BeforeEach(func() {
    38  		configRepo = testconfig.NewRepositoryWithDefaults()
    39  
    40  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    41  		repo = NewCloudControllerServiceAuthTokenRepository(configRepo, gateway)
    42  	})
    43  
    44  	AfterEach(func() {
    45  		testServer.Close()
    46  	})
    47  
    48  	Describe("Create", func() {
    49  		It("creates a service auth token", func() {
    50  			setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    51  				Method:   "POST",
    52  				Path:     "/v2/service_auth_tokens",
    53  				Matcher:  testnet.RequestBodyMatcher(`{"label":"a label","provider":"a provider","token":"a token"}`),
    54  				Response: testnet.TestResponse{Status: http.StatusCreated},
    55  			}))
    56  
    57  			err := repo.Create(models.ServiceAuthTokenFields{
    58  				Label:    "a label",
    59  				Provider: "a provider",
    60  				Token:    "a token",
    61  			})
    62  
    63  			Expect(testHandler).To(HaveAllRequestsCalled())
    64  			Expect(err).NotTo(HaveOccurred())
    65  		})
    66  	})
    67  
    68  	Describe("FindAll", func() {
    69  		var firstServiceAuthTokenRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    70  			Method: "GET",
    71  			Path:   "/v2/service_auth_tokens",
    72  			Response: testnet.TestResponse{
    73  				Status: http.StatusOK,
    74  				Body: `
    75  				{
    76  					"next_url": "/v2/service_auth_tokens?page=2",
    77  					"resources": [
    78  						{
    79  							"metadata": {
    80  								"guid": "mongodb-core-guid"
    81  							},
    82  							"entity": {
    83  								"label": "mongodb",
    84  								"provider": "mongodb-core"
    85  							}
    86  						}
    87  					]
    88  				}`,
    89  			},
    90  		})
    91  
    92  		var secondServiceAuthTokenRequest = apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    93  			Method: "GET",
    94  			Path:   "/v2/service_auth_tokens",
    95  			Response: testnet.TestResponse{
    96  				Status: http.StatusOK,
    97  				Body: `
    98  				{
    99  					"resources": [
   100  						{
   101  							"metadata": {
   102  								"guid": "mysql-core-guid"
   103  							},
   104  							"entity": {
   105  								"label": "mysql",
   106  								"provider": "mysql-core"
   107  							}
   108  						},
   109  						{
   110  							"metadata": {
   111  								"guid": "postgres-core-guid"
   112  							},
   113  							"entity": {
   114  								"label": "postgres",
   115  								"provider": "postgres-core"
   116  							}
   117  						}
   118  					]
   119  				}`,
   120  			},
   121  		})
   122  
   123  		BeforeEach(func() {
   124  			setupTestServer(firstServiceAuthTokenRequest, secondServiceAuthTokenRequest)
   125  		})
   126  
   127  		It("finds all service auth tokens", func() {
   128  			authTokens, err := repo.FindAll()
   129  
   130  			Expect(testHandler).To(HaveAllRequestsCalled())
   131  			Expect(err).NotTo(HaveOccurred())
   132  
   133  			Expect(len(authTokens)).To(Equal(3))
   134  
   135  			Expect(authTokens[0].Label).To(Equal("mongodb"))
   136  			Expect(authTokens[0].Provider).To(Equal("mongodb-core"))
   137  			Expect(authTokens[0].GUID).To(Equal("mongodb-core-guid"))
   138  
   139  			Expect(authTokens[1].Label).To(Equal("mysql"))
   140  			Expect(authTokens[1].Provider).To(Equal("mysql-core"))
   141  			Expect(authTokens[1].GUID).To(Equal("mysql-core-guid"))
   142  		})
   143  	})
   144  
   145  	Describe("FindByLabelAndProvider", func() {
   146  		Context("when the auth token exists", func() {
   147  			BeforeEach(func() {
   148  				setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   149  					Method: "GET",
   150  					Path:   "/v2/service_auth_tokens?q=label%3Aa-label%3Bprovider%3Aa-provider",
   151  					Response: testnet.TestResponse{
   152  						Status: http.StatusOK,
   153  						Body: `{
   154  					"resources": [{
   155  						"metadata": { "guid": "mysql-core-guid" },
   156  						"entity": {
   157  							"label": "mysql",
   158  							"provider": "mysql-core"
   159  						}
   160  					}]}`,
   161  					},
   162  				}))
   163  			})
   164  
   165  			It("returns the auth token", func() {
   166  				serviceAuthToken, err := repo.FindByLabelAndProvider("a-label", "a-provider")
   167  
   168  				Expect(testHandler).To(HaveAllRequestsCalled())
   169  				Expect(err).NotTo(HaveOccurred())
   170  				Expect(serviceAuthToken).To(Equal(models.ServiceAuthTokenFields{
   171  					GUID:     "mysql-core-guid",
   172  					Label:    "mysql",
   173  					Provider: "mysql-core",
   174  				}))
   175  			})
   176  		})
   177  
   178  		Context("when the auth token does not exist", func() {
   179  			BeforeEach(func() {
   180  				setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   181  					Method: "GET",
   182  					Path:   "/v2/service_auth_tokens?q=label%3Aa-label%3Bprovider%3Aa-provider",
   183  					Response: testnet.TestResponse{
   184  						Status: http.StatusOK,
   185  						Body:   `{"resources": []}`},
   186  				}))
   187  			})
   188  
   189  			It("returns a ModelNotFoundError", func() {
   190  				_, err := repo.FindByLabelAndProvider("a-label", "a-provider")
   191  
   192  				Expect(testHandler).To(HaveAllRequestsCalled())
   193  				Expect(err).To(BeAssignableToTypeOf(&errors.ModelNotFoundError{}))
   194  			})
   195  		})
   196  	})
   197  
   198  	Describe("Update", func() {
   199  		It("updates the service auth token", func() {
   200  			setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   201  				Method:   "PUT",
   202  				Path:     "/v2/service_auth_tokens/mysql-core-guid",
   203  				Matcher:  testnet.RequestBodyMatcher(`{"token":"a value"}`),
   204  				Response: testnet.TestResponse{Status: http.StatusOK},
   205  			}))
   206  
   207  			err := repo.Update(models.ServiceAuthTokenFields{
   208  				GUID:  "mysql-core-guid",
   209  				Token: "a value",
   210  			})
   211  
   212  			Expect(testHandler).To(HaveAllRequestsCalled())
   213  			Expect(err).NotTo(HaveOccurred())
   214  		})
   215  	})
   216  
   217  	Describe("Delete", func() {
   218  		It("deletes the service auth token", func() {
   219  
   220  			setupTestServer(apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
   221  				Method:   "DELETE",
   222  				Path:     "/v2/service_auth_tokens/mysql-core-guid",
   223  				Response: testnet.TestResponse{Status: http.StatusOK},
   224  			}))
   225  
   226  			err := repo.Delete(models.ServiceAuthTokenFields{
   227  				GUID: "mysql-core-guid",
   228  			})
   229  
   230  			Expect(testHandler).To(HaveAllRequestsCalled())
   231  			Expect(err).NotTo(HaveOccurred())
   232  		})
   233  	})
   234  })