github.com/cs3org/reva/v2@v2.27.7/internal/http/interceptors/auth/auth_test.go (about)

     1  // Copyright 2018-2021 CERN
     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  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package auth
    20  
    21  import (
    22  	"testing"
    23  )
    24  
    25  func TestGetCredsForUserAgent(t *testing.T) {
    26  	type test struct {
    27  		userAgent            string
    28  		userAgentMap         map[string]string
    29  		availableCredentials []string
    30  		expected             []string
    31  	}
    32  
    33  	tests := []*test{
    34  		// no user agent we return all available credentials
    35  		{
    36  			userAgent:            "",
    37  			userAgentMap:         map[string]string{},
    38  			availableCredentials: []string{"basic"},
    39  			expected:             []string{"basic"},
    40  		},
    41  
    42  		// map set but user agent not in map
    43  		{
    44  			userAgent:            "curl",
    45  			userAgentMap:         map[string]string{"mirall": "basic"},
    46  			availableCredentials: []string{"basic", "bearer"},
    47  			expected:             []string{"basic", "bearer"},
    48  		},
    49  
    50  		// no user map we return all available credentials
    51  		{
    52  			userAgent:            "mirall",
    53  			userAgentMap:         map[string]string{},
    54  			availableCredentials: []string{"basic"},
    55  			expected:             []string{"basic"},
    56  		},
    57  
    58  		// user agent set but no mapping set we return all credentials
    59  		{
    60  			userAgent:            "mirall",
    61  			userAgentMap:         map[string]string{},
    62  			availableCredentials: []string{"basic"},
    63  			expected:             []string{"basic"},
    64  		},
    65  
    66  		// user mapping set to non available credential, we return all available
    67  		{
    68  			userAgent:            "mirall",
    69  			userAgentMap:         map[string]string{"mirall": "notfound"},
    70  			availableCredentials: []string{"basic", "bearer"},
    71  			expected:             []string{"basic", "bearer"},
    72  		},
    73  
    74  		// user mapping set and we return only desired credential
    75  		{
    76  			userAgent:            "mirall",
    77  			userAgentMap:         map[string]string{"mirall": "bearer"},
    78  			availableCredentials: []string{"basic", "bearer"},
    79  			expected:             []string{"bearer"},
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		got := getCredsForUserAgent(
    85  			test.userAgent,
    86  			test.userAgentMap,
    87  			test.availableCredentials)
    88  
    89  		if !match(got, test.expected) {
    90  			fail(t, got, test.expected)
    91  		}
    92  	}
    93  }
    94  
    95  func match(a, b []string) bool {
    96  	if len(a) != len(b) {
    97  		return false
    98  	}
    99  	for i, v := range a {
   100  		if v != b[i] {
   101  			return false
   102  		}
   103  	}
   104  	return true
   105  }
   106  
   107  func fail(t *testing.T, got, expected []string) {
   108  	t.Fatalf("got: %+v expected: %+v", got, expected)
   109  }