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

     1  package graphql_test
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestFetchRequestInput_Validate_URL(t *testing.T) {
    15  	testCases := []struct {
    16  		Name          string
    17  		Value         string
    18  		ExpectedValid bool
    19  	}{
    20  		{
    21  			Name:          "ExpectedValid",
    22  			Value:         inputvalidationtest.ValidURL,
    23  			ExpectedValid: true,
    24  		},
    25  		{
    26  			Name:          "URL longer than 256",
    27  			Value:         "https://kyma-project.io/" + strings.Repeat("a", 233),
    28  			ExpectedValid: false,
    29  		},
    30  		{
    31  			Name:          "Invalid",
    32  			Value:         "kyma-project",
    33  			ExpectedValid: false,
    34  		},
    35  	}
    36  
    37  	for _, testCase := range testCases {
    38  		t.Run(testCase.Name, func(t *testing.T) {
    39  			//GIVEN
    40  			fr := fixValidFetchRequestInput()
    41  			fr.URL = testCase.Value
    42  			// WHEN
    43  			err := fr.Validate()
    44  			// THEN
    45  			if testCase.ExpectedValid {
    46  				require.NoError(t, err)
    47  			} else {
    48  				require.Error(t, err)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestFetchRequestInput_Validate_Auth(t *testing.T) {
    55  	validObj := fixValidAuthInput()
    56  	testCases := []struct {
    57  		Name          string
    58  		Value         *graphql.AuthInput
    59  		ExpectedValid bool
    60  	}{
    61  		{
    62  			Name:          "ExpectedValid",
    63  			Value:         &validObj,
    64  			ExpectedValid: true,
    65  		},
    66  		{
    67  			Name:          "ExpectedValid nil value",
    68  			Value:         nil,
    69  			ExpectedValid: true,
    70  		},
    71  		{
    72  			Name:          "Invalid - Nested validation error",
    73  			Value:         &graphql.AuthInput{Credential: &graphql.CredentialDataInput{}},
    74  			ExpectedValid: false,
    75  		},
    76  	}
    77  
    78  	for _, testCase := range testCases {
    79  		t.Run(testCase.Name, func(t *testing.T) {
    80  			//GIVEN
    81  			fr := fixValidFetchRequestInput()
    82  			fr.Auth = testCase.Value
    83  			// WHEN
    84  			err := fr.Validate()
    85  			// THEN
    86  			if testCase.ExpectedValid {
    87  				require.NoError(t, err)
    88  			} else {
    89  				require.Error(t, err)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestFetchRequestInput_Validate_Mode(t *testing.T) {
    96  	testCases := []struct {
    97  		Name          string
    98  		Value         *graphql.FetchMode
    99  		ExpectedValid bool
   100  	}{
   101  		{
   102  			Name:          "ExpectedValid",
   103  			Value:         (*graphql.FetchMode)(str.Ptr("SINGLE")),
   104  			ExpectedValid: true,
   105  		},
   106  		{
   107  			Name:          "ExpectedValid nil value",
   108  			Value:         nil,
   109  			ExpectedValid: true,
   110  		},
   111  		{
   112  			Name:          "Invalid object",
   113  			Value:         (*graphql.FetchMode)(str.Ptr("INVALID")),
   114  			ExpectedValid: false,
   115  		},
   116  	}
   117  
   118  	for _, testCase := range testCases {
   119  		t.Run(testCase.Name, func(t *testing.T) {
   120  			//GIVEN
   121  			fr := fixValidFetchRequestInput()
   122  			fr.Mode = testCase.Value
   123  			// WHEN
   124  			err := fr.Validate()
   125  			// THEN
   126  			if testCase.ExpectedValid {
   127  				require.NoError(t, err)
   128  			} else {
   129  				require.Error(t, err)
   130  			}
   131  		})
   132  	}
   133  }
   134  
   135  func TestFetchRequestInput_Validate_Filter(t *testing.T) {
   136  	testCases := []struct {
   137  		Name          string
   138  		Value         *string
   139  		ExpectedValid bool
   140  	}{
   141  		{
   142  			Name:          "ExpectedValid",
   143  			Value:         str.Ptr("this is a valid string"),
   144  			ExpectedValid: true,
   145  		},
   146  		{
   147  			Name:          "ExpectedValid nil pointer",
   148  			Value:         nil,
   149  			ExpectedValid: true,
   150  		},
   151  		{
   152  			Name:          "Empty string",
   153  			Value:         str.Ptr(inputvalidationtest.EmptyString),
   154  			ExpectedValid: false,
   155  		},
   156  		{
   157  			Name:          "String bigger than 256 chars",
   158  			Value:         str.Ptr(inputvalidationtest.String257Long),
   159  			ExpectedValid: false,
   160  		},
   161  	}
   162  
   163  	for _, testCase := range testCases {
   164  		t.Run(testCase.Name, func(t *testing.T) {
   165  			//GIVEN
   166  			fr := fixValidFetchRequestInput()
   167  			fr.Filter = testCase.Value
   168  			// WHEN
   169  			err := fr.Validate()
   170  			// THEN
   171  			if testCase.ExpectedValid {
   172  				require.NoError(t, err)
   173  			} else {
   174  				require.Error(t, err)
   175  			}
   176  		})
   177  	}
   178  }
   179  
   180  func fixValidFetchRequestInput() graphql.FetchRequestInput {
   181  	return graphql.FetchRequestInput{
   182  		URL: "https://kyma-project.io",
   183  	}
   184  }