github.com/greenpau/go-authcrunch@v1.1.4/pkg/acl/path_test.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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 acl
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/internal/tests"
    19  	"testing"
    20  )
    21  
    22  func TestMatchPathBasedACL(t *testing.T) {
    23  	testcases := []struct {
    24  		name             string
    25  		pattern          string
    26  		matchedPaths     []string
    27  		mismatchedPaths  []string
    28  		nullifyRegex     bool
    29  		wantMatchedFalse bool
    30  	}{
    31  		{
    32  			name:    "match path based acl with max depth",
    33  			pattern: "/*/media/**",
    34  			matchedPaths: []string{
    35  				"/app/media/icon.png",
    36  				"/app/media/icon~png",
    37  				"/app/media/assets/icon.png",
    38  				"/app/media/assets/images/icon.png",
    39  			},
    40  			mismatchedPaths: []string{
    41  				"/app/assets/media/icon.png",
    42  				"/app/assets/media/assets/icon.png",
    43  				"/app/assets/media/assets/images/icon.png",
    44  				"/media/icon.png",
    45  			},
    46  		},
    47  		{
    48  			name:    "match path based acl with limited depth",
    49  			pattern: "/*/media/*",
    50  			matchedPaths: []string{
    51  				"/app/media/icon.png",
    52  				"/app/media/icon~png",
    53  			},
    54  			mismatchedPaths: []string{
    55  				"/app/media/assets/images/icon.png",
    56  				"/app/media/assets/icon.png",
    57  				"/app/assets/media/icon.png",
    58  				"/app/assets/media/assets/icon.png",
    59  				"/app/assets/media/assets/images/icon.png",
    60  				"/media/icon.png",
    61  			},
    62  		},
    63  		{
    64  			name:    "validate empty pattern",
    65  			pattern: "",
    66  			matchedPaths: []string{
    67  				"/app/media/icon.png",
    68  			},
    69  			mismatchedPaths: []string{
    70  				"/app/media/assets/images/icon.png",
    71  			},
    72  			wantMatchedFalse: true,
    73  		},
    74  		{
    75  			name:    "validate exact match",
    76  			pattern: "/app/media/icon.png",
    77  			matchedPaths: []string{
    78  				"/app/media/icon.png",
    79  			},
    80  		},
    81  		{
    82  			name:    "validate exact mismatch",
    83  			pattern: "/app/media/icon.png",
    84  			matchedPaths: []string{
    85  				"/app/media/icon1.png",
    86  			},
    87  			wantMatchedFalse: true,
    88  		},
    89  		{
    90  			name:    "validate invalid regex",
    91  			pattern: "(.*!",
    92  			matchedPaths: []string{
    93  				"/app/media/icon1.png",
    94  			},
    95  			wantMatchedFalse: true,
    96  		},
    97  		{
    98  			name:    "validate nullified regex cache",
    99  			pattern: "^foo.*",
   100  			matchedPaths: []string{
   101  				"foobar",
   102  			},
   103  			nullifyRegex:     true,
   104  			wantMatchedFalse: true,
   105  		},
   106  	}
   107  
   108  	for _, tc := range testcases {
   109  		t.Run(tc.name, func(t *testing.T) {
   110  			want := make(map[string]interface{})
   111  			got := make(map[string]interface{})
   112  			if tc.nullifyRegex {
   113  				pathACLPatterns[tc.pattern] = nil
   114  			}
   115  			for _, p := range tc.matchedPaths {
   116  				if tc.wantMatchedFalse {
   117  					want[p] = false
   118  				} else {
   119  					want[p] = true
   120  				}
   121  				got[p] = MatchPathBasedACL(tc.pattern, p)
   122  			}
   123  			for _, p := range tc.mismatchedPaths {
   124  				want[p] = false
   125  				got[p] = MatchPathBasedACL(tc.pattern, p)
   126  			}
   127  			tests.EvalObjects(t, "output", want, got)
   128  		})
   129  	}
   130  }