github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/password/password_test.go (about)

     1  package password_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/net"
    10  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    11  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    12  	testnet "code.cloudfoundry.org/cli/cf/util/testhelpers/net"
    13  
    14  	. "code.cloudfoundry.org/cli/cf/api/password"
    15  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    16  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("CloudControllerPasswordRepository", func() {
    22  	It("updates your password", func() {
    23  		req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    24  			Method:   "PUT",
    25  			Path:     "/Users/my-user-guid/password",
    26  			Matcher:  testnet.RequestBodyMatcher(`{"password":"new-password","oldPassword":"old-password"}`),
    27  			Response: testnet.TestResponse{Status: http.StatusOK},
    28  		})
    29  
    30  		passwordUpdateServer, handler, repo := createPasswordRepo(req)
    31  		defer passwordUpdateServer.Close()
    32  
    33  		apiErr := repo.UpdatePassword("old-password", "new-password")
    34  		Expect(handler).To(HaveAllRequestsCalled())
    35  		Expect(apiErr).NotTo(HaveOccurred())
    36  	})
    37  
    38  	When("the inputs contains special characters", func() {
    39  		It("handles escaping", func() {
    40  			req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    41  				Method:   "PUT",
    42  				Path:     "/Users/my-user-guid/password",
    43  				Matcher:  testnet.RequestBodyMatcher(`{"password":"more-\\-\\b\\\\-crazy","oldPassword":"crazy-\\.\\b-password"}`),
    44  				Response: testnet.TestResponse{Status: http.StatusOK},
    45  			})
    46  
    47  			passwordUpdateServer, handler, repo := createPasswordRepo(req)
    48  			defer passwordUpdateServer.Close()
    49  
    50  			apiErr := repo.UpdatePassword(`crazy-\.\b-password`, `more-\-\b\\-crazy`)
    51  			Expect(handler).To(HaveAllRequestsCalled())
    52  			Expect(apiErr).NotTo(HaveOccurred())
    53  		})
    54  	})
    55  })
    56  
    57  func createPasswordRepo(req testnet.TestRequest) (passwordServer *httptest.Server, handler *testnet.TestHandler, repo Repository) {
    58  	passwordServer, handler = testnet.NewServer([]testnet.TestRequest{req})
    59  
    60  	configRepo := testconfig.NewRepositoryWithDefaults()
    61  	configRepo.SetUaaEndpoint(passwordServer.URL)
    62  	gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    63  	repo = NewCloudControllerRepository(configRepo, gateway)
    64  	return
    65  }