github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_string_test.go (about)

     1  package inputvalidation_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     9  	"github.com/pkg/errors"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  const (
    14  	dns1123Error = `a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character`
    15  )
    16  
    17  func TestDNSNameValidator_Validate(t *testing.T) {
    18  	// GIVEN
    19  	testError := errors.New(dns1123Error)
    20  
    21  	rule := inputvalidation.DNSName
    22  
    23  	testCases := []struct {
    24  		Name          string
    25  		Input         interface{}
    26  		ExpectedError error
    27  	}{
    28  		{
    29  			Name:          "Valid input",
    30  			Input:         inputvalidationtest.ValidName,
    31  			ExpectedError: nil,
    32  		},
    33  		{
    34  			Name:          "Valid pointer input",
    35  			Input:         str.Ptr(inputvalidationtest.ValidName),
    36  			ExpectedError: nil,
    37  		},
    38  		{
    39  			Name:          "No error when nil string",
    40  			Input:         (*string)(nil),
    41  			ExpectedError: nil,
    42  		},
    43  		{
    44  			Name:          "Error when starts with digit",
    45  			Input:         "0invalid",
    46  			ExpectedError: errors.New("cannot start with digit"),
    47  		},
    48  		{
    49  			Name:          "Error when too long input",
    50  			Input:         inputvalidationtest.String37Long,
    51  			ExpectedError: errors.New("must be no more than 36 characters"),
    52  		},
    53  		{
    54  			Name:          "Error when upper case letter",
    55  			Input:         "Test",
    56  			ExpectedError: testError,
    57  		},
    58  		{
    59  			Name:          "Error when not allowed character",
    60  			Input:         "imiÄ™",
    61  			ExpectedError: testError,
    62  		},
    63  		{
    64  			Name:          "Error when not allowed character #2",
    65  			Input:         "name;",
    66  			ExpectedError: testError,
    67  		},
    68  		{
    69  			Name:          "Error when invalid type",
    70  			Input:         10,
    71  			ExpectedError: errors.New("type has to be a string"),
    72  		},
    73  	}
    74  
    75  	for _, testCase := range testCases {
    76  		t.Run(testCase.Name, func(t *testing.T) {
    77  			// WHEN
    78  			err := rule.Validate(testCase.Input)
    79  			// THEN
    80  			if testCase.ExpectedError == nil {
    81  				require.NoError(t, err)
    82  			} else {
    83  				require.Error(t, err)
    84  				require.Contains(t, err.Error(), testCase.ExpectedError.Error())
    85  			}
    86  		})
    87  	}
    88  }