github.com/greenpau/go-authcrunch@v1.1.4/pkg/redirects/redirect_match_test.go (about)

     1  // Copyright 2024 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 redirects
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"testing"
    21  
    22  	"github.com/greenpau/go-authcrunch/internal/tests"
    23  )
    24  
    25  func TestMatch(t *testing.T) {
    26  
    27  	testInput1 := "https://authcrunch.com/?redirect_uri=https://authcrunch.com/path/to/login"
    28  
    29  	testcases := []struct {
    30  		name      string
    31  		config    []string
    32  		input     string
    33  		want      map[string]interface{}
    34  		shouldErr bool
    35  		err       error
    36  	}{
    37  		{
    38  			name:   "test matched exact domain and exact path match",
    39  			config: []string{"exact", "authcrunch.com", "exact", "/path/to/login"},
    40  			input:  testInput1,
    41  			want: map[string]interface{}{
    42  				"match":  true,
    43  				"domain": "authcrunch.com",
    44  				"path":   "/path/to/login",
    45  			},
    46  		},
    47  		{
    48  			name:   "test exact domain and partial path match",
    49  			config: []string{"exact", "authcrunch.com", "partial", "/to/"},
    50  			input:  testInput1,
    51  			want: map[string]interface{}{
    52  				"match":  true,
    53  				"domain": "authcrunch.com",
    54  				"path":   "/path/to/login",
    55  			},
    56  		},
    57  		{
    58  			name:   "test exact domain and prefix path match",
    59  			config: []string{"exact", "authcrunch.com", "prefix", "/path/to"},
    60  			input:  testInput1,
    61  			want: map[string]interface{}{
    62  				"match":  true,
    63  				"domain": "authcrunch.com",
    64  				"path":   "/path/to/login",
    65  			},
    66  		},
    67  		{
    68  			name:   "test exact domain and suffix path match",
    69  			config: []string{"exact", "authcrunch.com", "suffix", "/to/login"},
    70  			input:  testInput1,
    71  			want: map[string]interface{}{
    72  				"match":  true,
    73  				"domain": "authcrunch.com",
    74  				"path":   "/path/to/login",
    75  			},
    76  		},
    77  		{
    78  			name:   "test exact domain and regex path match",
    79  			config: []string{"exact", "authcrunch.com", "regex", "/path.*login"},
    80  			input:  testInput1,
    81  			want: map[string]interface{}{
    82  				"match":  true,
    83  				"domain": "authcrunch.com",
    84  				"path":   "/path/to/login",
    85  			},
    86  		},
    87  		{
    88  			name:   "test unmatched path",
    89  			config: []string{"exact", "authcrunch.com", "exact", "foo"},
    90  			input:  testInput1,
    91  			want: map[string]interface{}{
    92  				"match":  false,
    93  				"domain": "authcrunch.com",
    94  				"path":   "/path/to/login",
    95  			},
    96  		},
    97  
    98  		{
    99  			name:   "test partial domain and exact path match",
   100  			config: []string{"partial", "authcrunch.com", "exact", "/path/to/login"},
   101  			input:  testInput1,
   102  			want: map[string]interface{}{
   103  				"match":  true,
   104  				"domain": "authcrunch.com",
   105  				"path":   "/path/to/login",
   106  			},
   107  		},
   108  		{
   109  			name:   "test prefix domain and exact path match",
   110  			config: []string{"prefix", "auth", "exact", "/path/to/login"},
   111  			input:  testInput1,
   112  			want: map[string]interface{}{
   113  				"match":  true,
   114  				"domain": "authcrunch.com",
   115  				"path":   "/path/to/login",
   116  			},
   117  		},
   118  		{
   119  			name:   "test suffix domain and exact path match",
   120  			config: []string{"suffix", "crunch.com", "exact", "/path/to/login"},
   121  			input:  testInput1,
   122  			want: map[string]interface{}{
   123  				"match":  true,
   124  				"domain": "authcrunch.com",
   125  				"path":   "/path/to/login",
   126  			},
   127  		},
   128  		{
   129  			name:   "test regex domain and exact path match",
   130  			config: []string{"regex", "auth.*com", "exact", "/path/to/login"},
   131  			input:  testInput1,
   132  			want: map[string]interface{}{
   133  				"match":  true,
   134  				"domain": "authcrunch.com",
   135  				"path":   "/path/to/login",
   136  			},
   137  		},
   138  		{
   139  			name:   "test unmatched domain",
   140  			config: []string{"exact", "authcrunch.rocks", "exact", "/path/to/login"},
   141  			input:  testInput1,
   142  			want: map[string]interface{}{
   143  				"match":  false,
   144  				"domain": "authcrunch.com",
   145  				"path":   "/path/to/login",
   146  			},
   147  		},
   148  	}
   149  	for _, tc := range testcases {
   150  		t.Run(tc.name, func(t *testing.T) {
   151  			got := make(map[string]interface{})
   152  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
   153  			msgs = append(msgs, fmt.Sprintf("input:\n%s", tc.input))
   154  
   155  			c, err := NewRedirectURIMatchConfig(tc.config[0], tc.config[1], tc.config[2], tc.config[3])
   156  			if err != nil {
   157  				t.Fatal(err)
   158  			}
   159  
   160  			redirURI, err := ParseRedirectURI(tc.input)
   161  			if err != nil {
   162  				t.Fatalf("redirect uri not found in the input: %v", err)
   163  			}
   164  
   165  			got["match"] = Match(redirURI, []*RedirectURIMatchConfig{c})
   166  			got["domain"] = redirURI.Host
   167  			got["path"] = redirURI.Path
   168  
   169  			// got, err := Parse(tc.input)
   170  			if tests.EvalErrWithLog(t, err, "Match", tc.shouldErr, tc.err, msgs) {
   171  				return
   172  			}
   173  
   174  			tests.EvalObjectsWithLog(t, "Output", tc.want, got, msgs)
   175  		})
   176  	}
   177  }