...

Source file src/github.com/ory/fosite/arguments_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  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  type exactTestCase struct {
    31  	args   Arguments
    32  	exact  string
    33  	expect bool
    34  }
    35  
    36  var exactTests = []exactTestCase{
    37  	{
    38  		args:   Arguments{"foo"},
    39  		exact:  "foo",
    40  		expect: true,
    41  	},
    42  	{
    43  		args:   Arguments{"foo", "bar"},
    44  		exact:  "foo",
    45  		expect: false,
    46  	},
    47  	{
    48  		args:   Arguments{"foo", "bar"},
    49  		exact:  "bar",
    50  		expect: false,
    51  	},
    52  	{
    53  		args:   Arguments{"foo", "bar"},
    54  		exact:  "baz",
    55  		expect: false,
    56  	},
    57  	{
    58  		args:   Arguments{},
    59  		exact:  "baz",
    60  		expect: false,
    61  	},
    62  }
    63  
    64  func TestArgumentsExact(t *testing.T) {
    65  	testCases := append(exactTests, []exactTestCase{
    66  		{
    67  			args:   Arguments{"foo", "bar"},
    68  			exact:  "foo bar",
    69  			expect: true,
    70  		},
    71  	}...)
    72  
    73  	for k, c := range testCases {
    74  		assert.Equal(t, c.expect, c.args.Exact(c.exact), "%d", k)
    75  		t.Logf("Passed test case %d", k)
    76  	}
    77  }
    78  
    79  func TestArgumentsExactOne(t *testing.T) {
    80  	testCases := append(exactTests, []exactTestCase{
    81  		{
    82  			args:   Arguments{"foo", "bar"},
    83  			exact:  "foo bar",
    84  			expect: false,
    85  		},
    86  	}...)
    87  
    88  	for k, c := range testCases {
    89  		assert.Equal(t, c.expect, c.args.ExactOne(c.exact), "%d", k)
    90  		t.Logf("Passed test case %d", k)
    91  	}
    92  }
    93  
    94  func TestArgumentsHas(t *testing.T) {
    95  	for k, c := range []struct {
    96  		args   Arguments
    97  		has    []string
    98  		expect bool
    99  	}{
   100  		{
   101  			args:   Arguments{"foo", "bar"},
   102  			has:    []string{"foo", "bar"},
   103  			expect: true,
   104  		},
   105  		{
   106  			args:   Arguments{"foo", "bar"},
   107  			has:    []string{"bar", "foo"},
   108  			expect: true,
   109  		},
   110  		{
   111  			args:   Arguments{"bar", "foo"},
   112  			has:    []string{"foo"},
   113  			expect: true,
   114  		},
   115  		{
   116  			args:   Arguments{"foo", "bar"},
   117  			has:    []string{"bar", "foo", "baz"},
   118  			expect: false,
   119  		},
   120  		{
   121  			args:   Arguments{"foo", "bar"},
   122  			has:    []string{"foo"},
   123  			expect: true,
   124  		},
   125  		{
   126  			args:   Arguments{"foo", "bar"},
   127  			has:    []string{"bar"},
   128  			expect: true,
   129  		},
   130  		{
   131  			args:   Arguments{"foo", "bar"},
   132  			has:    []string{"baz"},
   133  			expect: false,
   134  		},
   135  		{
   136  			args:   Arguments{},
   137  			has:    []string{"baz"},
   138  			expect: false,
   139  		},
   140  	} {
   141  		assert.Equal(t, c.expect, c.args.Has(c.has...), "%d", k)
   142  		t.Logf("Passed test case %d", k)
   143  	}
   144  }
   145  
   146  type matchesTestCase struct {
   147  	args   Arguments
   148  	is     []string
   149  	expect bool
   150  }
   151  
   152  var matchesTests = []matchesTestCase{
   153  	{
   154  		args:   Arguments{},
   155  		is:     []string{},
   156  		expect: true,
   157  	},
   158  	{
   159  		args:   Arguments{"foo", "bar"},
   160  		is:     []string{"foo", "bar"},
   161  		expect: true,
   162  	},
   163  	{
   164  		args:   Arguments{"Foo", "Bar"},
   165  		is:     []string{"Foo", "Bar"},
   166  		expect: true,
   167  	},
   168  	{
   169  		args:   Arguments{"foo", "foo"},
   170  		is:     []string{"foo"},
   171  		expect: false,
   172  	},
   173  	{
   174  		args:   Arguments{"foo", "foo"},
   175  		is:     []string{"bar", "foo"},
   176  		expect: false,
   177  	},
   178  	{
   179  		args:   Arguments{"foo", "bar"},
   180  		is:     []string{"bar", "foo", "baz"},
   181  		expect: false,
   182  	},
   183  	{
   184  		args:   Arguments{"foo", "bar"},
   185  		is:     []string{"foo"},
   186  		expect: false,
   187  	},
   188  	{
   189  		args:   Arguments{"foo", "bar"},
   190  		is:     []string{"bar", "bar"},
   191  		expect: false,
   192  	},
   193  	{
   194  		args:   Arguments{"foo", "bar"},
   195  		is:     []string{"baz"},
   196  		expect: false,
   197  	},
   198  	{
   199  		args:   Arguments{},
   200  		is:     []string{"baz"},
   201  		expect: false,
   202  	},
   203  }
   204  
   205  func TestArgumentsMatchesExact(t *testing.T) {
   206  	testCases := append(matchesTests, []matchesTestCase{
   207  		// should fail if items are out of order
   208  		{
   209  			args:   Arguments{"foo", "bar"},
   210  			is:     []string{"bar", "foo"},
   211  			expect: false,
   212  		},
   213  		// should fail due to case-sensitivity.
   214  		{
   215  			args:   Arguments{"fOo", "bar"},
   216  			is:     []string{"foo", "BaR"},
   217  			expect: false,
   218  		},
   219  		// duplicate items should return allowed.
   220  		{
   221  			args:   Arguments{"foo", "foo"},
   222  			is:     []string{"foo", "foo"},
   223  			expect: true,
   224  		},
   225  	}...)
   226  	for k, c := range testCases {
   227  		assert.Equal(t, c.expect, c.args.MatchesExact(c.is...), "%d", k)
   228  		t.Logf("Passed test case %d", k)
   229  	}
   230  }
   231  
   232  func TestArgumentsMatches(t *testing.T) {
   233  	testCases := append(matchesTests, []matchesTestCase{
   234  		// should match if items are out of order.
   235  		{
   236  			args:   Arguments{"foo", "bar"},
   237  			is:     []string{"bar", "foo"},
   238  			expect: true,
   239  		},
   240  		// should allow case-insensitive matching.
   241  		{
   242  			args:   Arguments{"fOo", "bar"},
   243  			is:     []string{"foo", "BaR"},
   244  			expect: true,
   245  		},
   246  		// should return non-matching if duplicate items exist.
   247  		{
   248  			args:   Arguments{"foo", "bar"},
   249  			is:     []string{"FOO", "FOO", "bar"},
   250  			expect: false,
   251  		},
   252  		{
   253  			args:   Arguments{"foo", "foo"},
   254  			is:     []string{"foo", "foo"},
   255  			expect: false,
   256  		},
   257  	}...)
   258  	for k, c := range testCases {
   259  		assert.Equal(t, c.expect, c.args.Matches(c.is...), "%d", k)
   260  		t.Logf("Passed test case %d", k)
   261  	}
   262  }
   263  
   264  func TestArgumentsOneOf(t *testing.T) {
   265  	for k, c := range []struct {
   266  		args   Arguments
   267  		oneOf  []string
   268  		expect bool
   269  	}{
   270  		{
   271  			args:   Arguments{"baz", "bar"},
   272  			oneOf:  []string{"foo", "bar"},
   273  			expect: true,
   274  		},
   275  		{
   276  			args:   Arguments{"foo", "baz"},
   277  			oneOf:  []string{"foo", "bar"},
   278  			expect: true,
   279  		},
   280  		{
   281  			args:   Arguments{"baz"},
   282  			oneOf:  []string{"foo", "bar"},
   283  			expect: false,
   284  		},
   285  	} {
   286  		assert.Equal(t, c.expect, c.args.HasOneOf(c.oneOf...), "%d", k)
   287  		t.Logf("Passed test case %d", k)
   288  	}
   289  }
   290  

View as plain text