github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/uaa/auth_test.go (about)

     1  package uaa_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  
     8  	. "code.cloudfoundry.org/cli/api/uaa"
     9  	"code.cloudfoundry.org/cli/api/uaa/constant"
    10  	"code.cloudfoundry.org/cli/api/uaa/uaafakes"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Auth", func() {
    17  	var (
    18  		client *Client
    19  
    20  		fakeConfig *uaafakes.FakeConfig
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeConfig = NewTestConfig()
    25  
    26  		client = NewTestUAAClientAndStore(fakeConfig)
    27  	})
    28  
    29  	Describe("Authenticate", func() {
    30  		var (
    31  			credentials map[string]string
    32  
    33  			origin    string
    34  			grantType constant.GrantType
    35  
    36  			accessToken  string
    37  			refreshToken string
    38  			executeErr   error
    39  		)
    40  
    41  		JustBeforeEach(func() {
    42  			accessToken, refreshToken, executeErr = client.Authenticate(credentials, origin, grantType)
    43  		})
    44  
    45  		When("no errors occur", func() {
    46  			When("the grant type is password", func() {
    47  				var response string
    48  				BeforeEach(func() {
    49  					response = `{
    50  						"access_token":"some-access-token",
    51  						"refresh_token":"some-refresh-token"
    52  					}`
    53  					credentials = map[string]string{
    54  						"username": "some-username",
    55  						"password": "some-password",
    56  					}
    57  					grantType = constant.GrantTypePassword
    58  				})
    59  
    60  				When("origin is not set", func() {
    61  					BeforeEach(func() {
    62  						origin = ""
    63  						server.AppendHandlers(
    64  							CombineHandlers(
    65  								verifyRequestHost(TestAuthorizationResource),
    66  								VerifyRequest(http.MethodPost, "/oauth/token", ""),
    67  								VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
    68  								VerifyHeaderKV("Authorization", "Basic Y2xpZW50LWlkOmNsaWVudC1zZWNyZXQ="),
    69  								VerifyBody([]byte("grant_type=password&password=some-password&username=some-username")),
    70  								RespondWith(http.StatusOK, response),
    71  							))
    72  					})
    73  
    74  					It("authenticates with the credentials provided", func() {
    75  						Expect(executeErr).NotTo(HaveOccurred())
    76  
    77  						Expect(accessToken).To(Equal("some-access-token"))
    78  						Expect(refreshToken).To(Equal("some-refresh-token"))
    79  					})
    80  				})
    81  
    82  				When("origin is set", func() {
    83  					BeforeEach(func() {
    84  						origin = "some-fake-origin"
    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("grant_type=password&password=some-password&username=some-username")),
    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("additional prompts are answered", func() {
   106  					BeforeEach(func() {
   107  						credentials = map[string]string{
   108  							"username":     "some-username",
   109  							"password":     "some-password",
   110  							"mfaCode":      "some-mfa-code",
   111  							"customPrompt": "some-custom-value",
   112  						}
   113  
   114  						expectedValues := url.Values{
   115  							"username":     []string{"some-username"},
   116  							"password":     []string{"some-password"},
   117  							"mfaCode":      []string{"some-mfa-code"},
   118  							"customPrompt": []string{"some-custom-value"},
   119  						}
   120  
   121  						server.AppendHandlers(
   122  							CombineHandlers(
   123  								VerifyForm(expectedValues),
   124  								RespondWith(http.StatusOK, response),
   125  							),
   126  						)
   127  					})
   128  
   129  					It("sends all the prompts to the UAA", func() {
   130  						Expect(executeErr).NotTo(HaveOccurred())
   131  						Expect(accessToken).To(Equal("some-access-token"))
   132  						Expect(refreshToken).To(Equal("some-refresh-token"))
   133  					})
   134  				})
   135  			})
   136  
   137  			When("the grant type is client credentials", func() {
   138  				BeforeEach(func() {
   139  					response := `{
   140  						"access_token":"some-access-token"
   141  					}`
   142  
   143  					credentials = map[string]string{
   144  						"client_id":     "some-client-id",
   145  						"client_secret": "some-client-secret",
   146  					}
   147  					origin = ""
   148  					grantType = constant.GrantTypeClientCredentials
   149  					server.AppendHandlers(
   150  						CombineHandlers(
   151  							verifyRequestHost(TestAuthorizationResource),
   152  							VerifyRequest(http.MethodPost, "/oauth/token"),
   153  							VerifyHeaderKV("Content-Type", "application/x-www-form-urlencoded"),
   154  							VerifyHeaderKV("Authorization"),
   155  							VerifyBody([]byte(fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=%s", credentials["client_id"], credentials["client_secret"], grantType))),
   156  							RespondWith(http.StatusOK, response),
   157  						))
   158  				})
   159  
   160  				It("authenticates with the credentials provided", func() {
   161  					Expect(executeErr).NotTo(HaveOccurred())
   162  
   163  					Expect(accessToken).To(Equal("some-access-token"))
   164  					Expect(refreshToken).To(BeEmpty())
   165  				})
   166  			})
   167  		})
   168  
   169  		When("an error occurs", func() {
   170  			var response string
   171  
   172  			BeforeEach(func() {
   173  				response = `{
   174  						"error": "some-error",
   175  						"error_description": "some-description"
   176  					}`
   177  				server.AppendHandlers(
   178  					CombineHandlers(
   179  						verifyRequestHost(TestAuthorizationResource),
   180  						VerifyRequest(http.MethodPost, "/oauth/token"),
   181  						RespondWith(http.StatusTeapot, response),
   182  					))
   183  			})
   184  
   185  			It("returns the error", func() {
   186  				Expect(executeErr).To(MatchError(RawHTTPStatusError{
   187  					StatusCode:  http.StatusTeapot,
   188  					RawResponse: []byte(response),
   189  				}))
   190  			})
   191  		})
   192  	})
   193  })