...

Source file src/github.com/ory/fosite/authorize_request_test.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package fosite
    23  
    24  import (
    25  	"github.com/stretchr/testify/require"
    26  	"net/url"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestAuthorizeRequestURLRegression(t *testing.T) {
    34  	require.Nil(t, NewAuthorizeRequest().RedirectURI)
    35  }
    36  
    37  func TestAuthorizeRequest(t *testing.T) {
    38  	var urlparse = func(rawurl string) *url.URL {
    39  		u, _ := url.Parse(rawurl)
    40  		return u
    41  	}
    42  
    43  	for k, c := range []struct {
    44  		ar           *AuthorizeRequest
    45  		isRedirValid bool
    46  	}{
    47  		{
    48  			ar:           NewAuthorizeRequest(),
    49  			isRedirValid: false,
    50  		},
    51  		{
    52  			ar: &AuthorizeRequest{
    53  				RedirectURI: urlparse("https://foobar"),
    54  			},
    55  			isRedirValid: false,
    56  		},
    57  		{
    58  			ar: &AuthorizeRequest{
    59  				RedirectURI: urlparse("https://foobar"),
    60  				Request: Request{
    61  					Client: &DefaultClient{RedirectURIs: []string{""}},
    62  				},
    63  			},
    64  			isRedirValid: false,
    65  		},
    66  		{
    67  			ar: &AuthorizeRequest{
    68  				Request: Request{
    69  					Client: &DefaultClient{RedirectURIs: []string{""}},
    70  				},
    71  				RedirectURI: urlparse(""),
    72  			},
    73  			isRedirValid: false,
    74  		},
    75  		{
    76  			ar: &AuthorizeRequest{
    77  				Request: Request{
    78  					Client: &DefaultClient{RedirectURIs: []string{""}},
    79  				},
    80  				RedirectURI: urlparse(""),
    81  			},
    82  			isRedirValid: false,
    83  		},
    84  		{
    85  			ar: &AuthorizeRequest{
    86  				RedirectURI: urlparse("https://foobar.com#123"),
    87  				Request: Request{
    88  					Client: &DefaultClient{RedirectURIs: []string{"https://foobar.com#123"}},
    89  				},
    90  			},
    91  			isRedirValid: false,
    92  		},
    93  		{
    94  			ar: &AuthorizeRequest{
    95  				Request: Request{
    96  					Client: &DefaultClient{RedirectURIs: []string{"https://foobar.com"}},
    97  				},
    98  				RedirectURI: urlparse("https://foobar.com#123"),
    99  			},
   100  			isRedirValid: false,
   101  		},
   102  		{
   103  			ar: &AuthorizeRequest{
   104  				Request: Request{
   105  					Client:         &DefaultClient{RedirectURIs: []string{"https://foobar.com/cb"}},
   106  					RequestedAt:    time.Now().UTC(),
   107  					RequestedScope: []string{"foo", "bar"},
   108  				},
   109  				RedirectURI:   urlparse("https://foobar.com/cb"),
   110  				ResponseTypes: []string{"foo", "bar"},
   111  				State:         "foobar",
   112  			},
   113  			isRedirValid: true,
   114  		},
   115  	} {
   116  		assert.Equal(t, c.ar.Client, c.ar.GetClient(), "%d", k)
   117  		assert.Equal(t, c.ar.RedirectURI, c.ar.GetRedirectURI(), "%d", k)
   118  		assert.Equal(t, c.ar.RequestedAt, c.ar.GetRequestedAt(), "%d", k)
   119  		assert.Equal(t, c.ar.ResponseTypes, c.ar.GetResponseTypes(), "%d", k)
   120  		assert.Equal(t, c.ar.RequestedScope, c.ar.GetRequestedScopes(), "%d", k)
   121  		assert.Equal(t, c.ar.State, c.ar.GetState(), "%d", k)
   122  		assert.Equal(t, c.isRedirValid, c.ar.IsRedirectURIValid(), "%d", k)
   123  
   124  		c.ar.GrantScope("foo")
   125  		c.ar.SetSession(&DefaultSession{})
   126  		c.ar.SetRequestedScopes([]string{"foo"})
   127  		assert.True(t, c.ar.GetGrantedScopes().Has("foo"))
   128  		assert.True(t, c.ar.GetRequestedScopes().Has("foo"))
   129  		assert.Equal(t, &DefaultSession{}, c.ar.GetSession())
   130  	}
   131  }
   132  

View as plain text