github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/uaa/ssh_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	. "code.cloudfoundry.org/cli/api/uaa"
     8  	"code.cloudfoundry.org/cli/api/uaa/uaafakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  var _ = Describe("SSH", func() {
    15  	var (
    16  		client *Client
    17  
    18  		fakeConfig *uaafakes.FakeConfig
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeConfig = NewTestConfig()
    23  
    24  		client = NewTestUAAClientAndStore(fakeConfig)
    25  	})
    26  
    27  	Describe("GetSSHPasscode", func() {
    28  		When("no errors occur", func() {
    29  			var expectedCode string
    30  
    31  			BeforeEach(func() {
    32  				expectedCode = "c0d3"
    33  				locationHeader := http.Header{}
    34  				locationHeader.Add("Location", fmt.Sprintf("http://localhost/redirect/cf?code=%s&state=", expectedCode))
    35  				uaaServer.AppendHandlers(
    36  					CombineHandlers(
    37  						verifyRequestHost(TestUAAResource),
    38  						VerifyRequest(http.MethodGet, "/oauth/authorize", "response_type=code&client_id=ssh-proxy"),
    39  						RespondWith(http.StatusFound, nil, locationHeader),
    40  					))
    41  			})
    42  
    43  			It("returns a ssh passcode", func() {
    44  				code, err := client.GetSSHPasscode("4c3sst0k3n", "ssh-proxy")
    45  				Expect(err).NotTo(HaveOccurred())
    46  				Expect(code).To(Equal(expectedCode))
    47  			})
    48  		})
    49  
    50  		When("an error occurs", func() {
    51  			var response string
    52  
    53  			BeforeEach(func() {
    54  				response = `{
    55  					"error": "some-error",
    56  					"error_description": "some-description"
    57  				}`
    58  				uaaServer.AppendHandlers(
    59  					CombineHandlers(
    60  						verifyRequestHost(TestUAAResource),
    61  						VerifyRequest(http.MethodGet, "/oauth/authorize", "response_type=code&client_id=ssh-proxy"),
    62  						RespondWith(http.StatusBadRequest, response),
    63  					))
    64  			})
    65  
    66  			It("returns an error", func() {
    67  				_, err := client.GetSSHPasscode("4c3sst0k3n", "ssh-proxy")
    68  				Expect(err).To(MatchError(RawHTTPStatusError{
    69  					StatusCode:  http.StatusBadRequest,
    70  					RawResponse: []byte(response),
    71  				}))
    72  			})
    73  		})
    74  	})
    75  })