...

Source file src/github.com/go-openapi/runtime/security/authorizer_test.go

Documentation: github.com/go-openapi/runtime/security

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package security
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestAuthorized(t *testing.T) {
    27  	authorizer := Authorized()
    28  
    29  	err := authorizer.Authorize(nil, nil)
    30  	require.NoError(t, err)
    31  }
    32  
    33  func TestAuthenticator(t *testing.T) {
    34  	r, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
    35  	require.NoError(t, err)
    36  
    37  	t.Run("with HttpAuthenticator", func(t *testing.T) {
    38  		auth := HttpAuthenticator(func(_ *http.Request) (bool, interface{}, error) { return true, "test", nil })
    39  
    40  		t.Run("authenticator should work on *http.Request", func(t *testing.T) {
    41  			isAuth, user, err := auth.Authenticate(r)
    42  			require.NoError(t, err)
    43  			assert.True(t, isAuth)
    44  			assert.Equal(t, "test", user)
    45  		})
    46  
    47  		t.Run("authenticator should work on *ScopedAuthRequest", func(t *testing.T) {
    48  			scoped := &ScopedAuthRequest{Request: r}
    49  
    50  			isAuth, user, err := auth.Authenticate(scoped)
    51  			require.NoError(t, err)
    52  			assert.True(t, isAuth)
    53  			assert.Equal(t, "test", user)
    54  		})
    55  
    56  		t.Run("authenticator should return false on other inputs", func(t *testing.T) {
    57  			isAuth, user, err := auth.Authenticate("")
    58  			require.NoError(t, err)
    59  			assert.False(t, isAuth)
    60  			assert.Empty(t, user)
    61  		})
    62  	})
    63  
    64  	t.Run("with ScopedAuthenticator", func(t *testing.T) {
    65  		auth := ScopedAuthenticator(func(_ *ScopedAuthRequest) (bool, interface{}, error) { return true, "test", nil })
    66  
    67  		t.Run("authenticator should work on *ScopedAuthRequest", func(t *testing.T) {
    68  			scoped := &ScopedAuthRequest{Request: r}
    69  
    70  			isAuth, user, err := auth.Authenticate(scoped)
    71  			require.NoError(t, err)
    72  			assert.True(t, isAuth)
    73  			assert.Equal(t, "test", user)
    74  		})
    75  
    76  		t.Run("authenticator should return false on other inputs", func(t *testing.T) {
    77  			isAuth, user, err := auth.Authenticate("")
    78  			require.NoError(t, err)
    79  			assert.False(t, isAuth)
    80  			assert.Empty(t, user)
    81  		})
    82  	})
    83  }
    84  

View as plain text