github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/api/uaa/auth_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/constant" 9 "code.cloudfoundry.org/cli/api/uaa/uaafakes" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/ghttp" 13 ) 14 15 var _ = Describe("Auth", func() { 16 var ( 17 client *Client 18 19 fakeConfig *uaafakes.FakeConfig 20 ) 21 22 BeforeEach(func() { 23 fakeConfig = NewTestConfig() 24 25 client = NewTestUAAClientAndStore(fakeConfig) 26 }) 27 28 Describe("Authenticate", func() { 29 var ( 30 identity string 31 secret string 32 origin string 33 grantType constant.GrantType 34 35 accessToken string 36 refreshToken string 37 executeErr error 38 ) 39 40 BeforeEach(func() { 41 identity = "some-identity" 42 secret = "some-secret" 43 }) 44 45 JustBeforeEach(func() { 46 accessToken, refreshToken, executeErr = client.Authenticate(identity, secret, origin, grantType) 47 }) 48 49 When("no errors occur", func() { 50 When("the grant type is password and origin is not set", func() { 51 BeforeEach(func() { 52 response := `{ 53 "access_token":"some-access-token", 54 "refresh_token":"some-refresh-token" 55 }` 56 origin = "" 57 grantType = constant.GrantTypePassword 58 server.AppendHandlers( 59 CombineHandlers( 60 verifyRequestHost(TestAuthorizationResource), 61 VerifyRequest(http.MethodPost, "/oauth/token"), 62 VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"), 63 VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="), 64 VerifyBody([]byte(fmt.Sprintf("grant_type=%s&password=%s&username=%s", grantType, secret, identity))), 65 RespondWith(http.StatusOK, response), 66 )) 67 }) 68 69 It("authenticates with the credentials provided", func() { 70 Expect(executeErr).NotTo(HaveOccurred()) 71 72 Expect(accessToken).To(Equal("some-access-token")) 73 Expect(refreshToken).To(Equal("some-refresh-token")) 74 }) 75 }) 76 77 When("the grant type is password and origin is set", func() { 78 BeforeEach(func() { 79 response := `{ 80 "access_token":"some-access-token", 81 "refresh_token":"some-refresh-token" 82 }` 83 origin = "some-fake-origin" 84 grantType = constant.GrantTypePassword 85 expectedQuery := "login_hint=%7B%22origin%22%3A%22" + origin + "%22%7D" 86 server.AppendHandlers( 87 CombineHandlers( 88 verifyRequestHost(TestAuthorizationResource), 89 VerifyRequest(http.MethodPost, "/oauth/token", expectedQuery), 90 VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"), 91 VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="), 92 VerifyBody([]byte(fmt.Sprintf("grant_type=%s&password=%s&username=%s", grantType, secret, identity))), 93 RespondWith(http.StatusOK, response), 94 )) 95 }) 96 97 It("authenticates with the credentials provided", func() { 98 Expect(executeErr).NotTo(HaveOccurred()) 99 100 Expect(accessToken).To(Equal("some-access-token")) 101 Expect(refreshToken).To(Equal("some-refresh-token")) 102 }) 103 }) 104 105 When("the grant type is client credentials", func() { 106 BeforeEach(func() { 107 response := `{ 108 "access_token":"some-access-token" 109 }` 110 111 origin = "" 112 grantType = constant.GrantTypeClientCredentials 113 server.AppendHandlers( 114 CombineHandlers( 115 verifyRequestHost(TestAuthorizationResource), 116 VerifyRequest(http.MethodPost, "/oauth/token"), 117 VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"), 118 VerifyHeaderKV("Authorization"), 119 VerifyBody([]byte(fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=%s", identity, secret, grantType))), 120 RespondWith(http.StatusOK, response), 121 )) 122 }) 123 124 It("authenticates with the credentials provided", func() { 125 Expect(executeErr).NotTo(HaveOccurred()) 126 127 Expect(accessToken).To(Equal("some-access-token")) 128 Expect(refreshToken).To(BeEmpty()) 129 }) 130 }) 131 }) 132 133 When("an error occurs", func() { 134 var response string 135 136 BeforeEach(func() { 137 response = `{ 138 "error": "some-error", 139 "error_description": "some-description" 140 }` 141 server.AppendHandlers( 142 CombineHandlers( 143 verifyRequestHost(TestAuthorizationResource), 144 VerifyRequest(http.MethodPost, "/oauth/token"), 145 RespondWith(http.StatusTeapot, response), 146 )) 147 }) 148 149 It("returns the error", func() { 150 Expect(executeErr).To(MatchError(RawHTTPStatusError{ 151 StatusCode: http.StatusTeapot, 152 RawResponse: []byte(response), 153 })) 154 }) 155 }) 156 }) 157 })