github.com/greenpau/go-authcrunch@v1.1.4/pkg/redirects/redirect_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 TestNewRedirectURIMatchConfig(t *testing.T) {
    26  	testcases := []struct {
    27  		name      string
    28  		config    []string
    29  		want      map[string]interface{}
    30  		shouldErr bool
    31  		err       error
    32  	}{
    33  		{
    34  			name:   "text exact domain and exact path match",
    35  			config: []string{"exact", "authcrunch.com", "exact", "/path/to"},
    36  			want: map[string]interface{}{
    37  				"domain_match_type": "exact",
    38  				"domain":            "authcrunch.com",
    39  				"path_match_type":   "exact",
    40  				"path":              "/path/to",
    41  			},
    42  		},
    43  		{
    44  			name:      "test undefined redirect uri domain",
    45  			config:    []string{"exact", " ", "exact", "/path/to"},
    46  			shouldErr: true,
    47  			err:       fmt.Errorf("undefined redirect uri domain"),
    48  		},
    49  		{
    50  			name:      "test undefined redirect uri domain name match type",
    51  			config:    []string{"", "authcrunch.com", "exact", "/path/to"},
    52  			shouldErr: true,
    53  			err:       fmt.Errorf("undefined redirect uri domain name match type"),
    54  		},
    55  		{
    56  			name:      "test unsupported redirect uri domain name match type",
    57  			config:    []string{"foo", "authcrunch.com", "exact", "/path/to"},
    58  			shouldErr: true,
    59  			err:       fmt.Errorf("invalid %q redirect uri domain name match type", "foo"),
    60  		},
    61  		{
    62  			name:      "test bad redirect uri domain name regex",
    63  			config:    []string{"regex", "[.", "exact", "/path/to"},
    64  			shouldErr: true,
    65  			err:       fmt.Errorf("error parsing regexp: missing closing ]: `[.`"),
    66  		},
    67  		{
    68  			name:      "test undefined redirect uri path",
    69  			config:    []string{"exact", "authcrunch.com", "exact", " "},
    70  			shouldErr: true,
    71  			err:       fmt.Errorf("undefined redirect uri path"),
    72  		},
    73  		{
    74  			name:      "test undefined redirect uri path match type",
    75  			config:    []string{"exact", "authcrunch.com", "", "/path/to"},
    76  			shouldErr: true,
    77  			err:       fmt.Errorf("undefined redirect uri path match type"),
    78  		},
    79  		{
    80  			name:      "test unsupported redirect uri path match type",
    81  			config:    []string{"exact", "authcrunch.com", "foo", "/path/to"},
    82  			shouldErr: true,
    83  			err:       fmt.Errorf("invalid %q redirect uri path match type", "foo"),
    84  		},
    85  		{
    86  			name:      "test bad redirect uri path regex",
    87  			config:    []string{"regex", "authcrunch.rocks", "exact", "[."},
    88  			shouldErr: true,
    89  			err:       fmt.Errorf("error parsing regexp: missing closing ]: `[.`"),
    90  		},
    91  		/*
    92  			{
    93  				name: "test invalid config",
    94  				shouldErr: true,
    95  				err:       fmt.Errorf("TBD"),
    96  			},
    97  		*/
    98  	}
    99  	for _, tc := range testcases {
   100  		t.Run(tc.name, func(t *testing.T) {
   101  			got := make(map[string]interface{})
   102  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
   103  			msgs = append(msgs, fmt.Sprintf("config:\n%v", tc.config))
   104  
   105  			c, err := NewRedirectURIMatchConfig(tc.config[0], tc.config[1], tc.config[2], tc.config[3])
   106  			if tests.EvalErrWithLog(t, err, "Match", tc.shouldErr, tc.err, msgs) {
   107  				return
   108  			}
   109  
   110  			got["domain_match_type"] = c.DomainMatchType
   111  			got["domain"] = c.Domain
   112  			got["path_match_type"] = c.PathMatchType
   113  			got["path"] = c.Path
   114  
   115  			tests.EvalObjectsWithLog(t, "Output", tc.want, got, msgs)
   116  		})
   117  	}
   118  }