github.com/greenpau/go-authcrunch@v1.1.4/pkg/util/validate/login_hint_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 validate
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/internal/tests"
    19  	"github.com/greenpau/go-authcrunch/pkg/errors"
    20  	"testing"
    21  )
    22  
    23  func TestLoginHint(t *testing.T) {
    24  	var testcases = []struct {
    25  		name       string
    26  		loginHint  string
    27  		validators []string
    28  		shouldErr  bool
    29  		err        error
    30  	}{
    31  		{
    32  			name:       "doesn't return an error if the provided login_hint is a valid email",
    33  			loginHint:  "foo@bar.com",
    34  			validators: []string{"email"},
    35  			shouldErr:  false,
    36  			err:        nil,
    37  		},
    38  		{
    39  			name:       "returns an error if the provided login_hint has an invalid domain",
    40  			loginHint:  "foo@",
    41  			validators: []string{"email"},
    42  			shouldErr:  true,
    43  			err:        errors.ErrInvalidLoginHint,
    44  		},
    45  		{
    46  			name:       "returns an error if the provided login_hint has an invalid domain",
    47  			loginHint:  "foo@().com",
    48  			validators: []string{"email"},
    49  			shouldErr:  true,
    50  			err:        errors.ErrInvalidLoginHint,
    51  		},
    52  		{
    53  			name:       "doesn't return an error if the provided login_hint is a valid phone number",
    54  			loginHint:  "+1-555-55 55",
    55  			validators: []string{"phone"},
    56  			shouldErr:  false,
    57  			err:        nil,
    58  		},
    59  		{
    60  			name:       "returns an error if the provided login_hint is not a valid phone number",
    61  			loginHint:  ".5dsa-dasdas55",
    62  			validators: []string{"phone"},
    63  			shouldErr:  true,
    64  			err:        errors.ErrInvalidLoginHint,
    65  		},
    66  		{
    67  			name:       "doesn't return an error if the provided login_hint is a valid alphanumeric string",
    68  			loginHint:  "foobar",
    69  			validators: []string{"alphanumeric"},
    70  			shouldErr:  false,
    71  			err:        nil,
    72  		},
    73  		{
    74  			name:       "returns an error if the provided login_hint is not a valid alphanumeric string",
    75  			loginHint:  "!^$^#&$&abc",
    76  			validators: []string{"alphanumeric"},
    77  			shouldErr:  true,
    78  			err:        errors.ErrInvalidLoginHint,
    79  		},
    80  		{
    81  			name:       "doesn't return an error if the provided login_hint matches one of the validators",
    82  			loginHint:  "+1-555-55 55",
    83  			validators: []string{"email", "phone"},
    84  			shouldErr:  false,
    85  			err:        nil,
    86  		},
    87  		{
    88  			name:       "returns an error if the provided login_hint doesn't match any validator",
    89  			loginHint:  "ma!e^",
    90  			validators: []string{"email", "phone", "alphanumeric"},
    91  			shouldErr:  true,
    92  			err:        errors.ErrInvalidLoginHint,
    93  		},
    94  	}
    95  	for _, tc := range testcases {
    96  		t.Run(tc.name, func(t *testing.T) {
    97  			err := LoginHint(tc.loginHint, tc.validators)
    98  
    99  			if tests.EvalErrWithLog(t, err, nil, tc.shouldErr, tc.err, []string{}) {
   100  				return
   101  			}
   102  		})
   103  	}
   104  }