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

     1  package graphql_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestDocumentInput_Validate_Title(t *testing.T) {
    13  	testCases := []struct {
    14  		Name          string
    15  		Value         string
    16  		ExpectedValid bool
    17  	}{
    18  		{
    19  			Name:          "ExpectedValid",
    20  			Value:         "ExpectedValid",
    21  			ExpectedValid: true,
    22  		},
    23  		{
    24  			Name:          "Empty string",
    25  			Value:         inputvalidationtest.EmptyString,
    26  			ExpectedValid: false,
    27  		},
    28  		{
    29  			Name:          "String longer than 128 chars",
    30  			Value:         inputvalidationtest.String129Long,
    31  			ExpectedValid: false,
    32  		},
    33  	}
    34  
    35  	for _, testCase := range testCases {
    36  		t.Run(testCase.Name, func(t *testing.T) {
    37  			//GIVEN
    38  			doc := fixValidDocument()
    39  			doc.Title = testCase.Value
    40  			// WHEN
    41  			err := doc.Validate()
    42  			// THEN
    43  			if testCase.ExpectedValid {
    44  				require.NoError(t, err)
    45  			} else {
    46  				require.Error(t, err)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestDocumentInput_Validate_DisplayName(t *testing.T) {
    53  	testCases := []struct {
    54  		Name          string
    55  		Value         string
    56  		ExpectedValid bool
    57  	}{
    58  		{
    59  			Name:          "ExpectedValid",
    60  			Value:         "ExpectedValid",
    61  			ExpectedValid: true,
    62  		},
    63  		{
    64  			Name:          "Empty string",
    65  			Value:         inputvalidationtest.EmptyString,
    66  			ExpectedValid: false,
    67  		},
    68  		{
    69  			Name:          "String longer than 128 chars",
    70  			Value:         inputvalidationtest.String129Long,
    71  			ExpectedValid: false,
    72  		},
    73  	}
    74  
    75  	for _, testCase := range testCases {
    76  		t.Run(testCase.Name, func(t *testing.T) {
    77  			//GIVEN
    78  			doc := fixValidDocument()
    79  			doc.DisplayName = testCase.Value
    80  			// WHEN
    81  			err := doc.Validate()
    82  			// THEN
    83  			if testCase.ExpectedValid {
    84  				require.NoError(t, err)
    85  			} else {
    86  				require.Error(t, err)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestDocumentInput_Validate_Description(t *testing.T) {
    93  	testCases := []struct {
    94  		Name          string
    95  		Value         string
    96  		ExpectedValid bool
    97  	}{
    98  		{
    99  			Name:          "ExpectedValid",
   100  			Value:         "this is a valid description",
   101  			ExpectedValid: true,
   102  		},
   103  		{
   104  			Name:          "Empty string",
   105  			Value:         inputvalidationtest.EmptyString,
   106  			ExpectedValid: false,
   107  		},
   108  		{
   109  			Name:          "String longer than 2000 chars",
   110  			Value:         inputvalidationtest.String2001Long,
   111  			ExpectedValid: false,
   112  		},
   113  	}
   114  
   115  	for _, testCase := range testCases {
   116  		t.Run(testCase.Name, func(t *testing.T) {
   117  			//GIVEN
   118  			doc := fixValidDocument()
   119  			doc.Description = testCase.Value
   120  			// WHEN
   121  			err := doc.Validate()
   122  			// THEN
   123  			if testCase.ExpectedValid {
   124  				require.NoError(t, err)
   125  			} else {
   126  				require.Error(t, err)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestDocumentInput_Validate_Format(t *testing.T) {
   133  	testCases := []struct {
   134  		Name          string
   135  		Value         graphql.DocumentFormat
   136  		ExpectedValid bool
   137  	}{
   138  		{
   139  			Name:          "ExpectedValid",
   140  			Value:         "MARKDOWN",
   141  			ExpectedValid: true,
   142  		},
   143  		{
   144  			Name:          "Invalid",
   145  			Value:         "INVALID",
   146  			ExpectedValid: false,
   147  		},
   148  		{
   149  			Name:          "Empty string",
   150  			Value:         inputvalidationtest.EmptyString,
   151  			ExpectedValid: false,
   152  		},
   153  	}
   154  
   155  	for _, testCase := range testCases {
   156  		t.Run(testCase.Name, func(t *testing.T) {
   157  			//GIVEN
   158  			doc := fixValidDocument()
   159  			doc.Format = testCase.Value
   160  			// WHEN
   161  			err := doc.Validate()
   162  			// THEN
   163  			if testCase.ExpectedValid {
   164  				require.NoError(t, err)
   165  			} else {
   166  				require.Error(t, err)
   167  			}
   168  		})
   169  	}
   170  }
   171  
   172  func TestDocumentInput_Validate_Kind(t *testing.T) {
   173  	testCases := []struct {
   174  		Name          string
   175  		Value         *string
   176  		ExpectedValid bool
   177  	}{
   178  		{
   179  			Name:          "ExpectedValid",
   180  			Value:         str.Ptr("ExpectedValid"),
   181  			ExpectedValid: true,
   182  		},
   183  		{
   184  			Name:          "ExpectedValid nil pointer",
   185  			Value:         nil,
   186  			ExpectedValid: true,
   187  		},
   188  		{
   189  			Name:          "Empty string",
   190  			Value:         str.Ptr(inputvalidationtest.EmptyString),
   191  			ExpectedValid: true,
   192  		},
   193  		{
   194  			Name:          "String longer than 256 chars",
   195  			Value:         str.Ptr(inputvalidationtest.String257Long),
   196  			ExpectedValid: false,
   197  		},
   198  	}
   199  
   200  	for _, testCase := range testCases {
   201  		t.Run(testCase.Name, func(t *testing.T) {
   202  			//GIVEN
   203  			doc := fixValidDocument()
   204  			doc.Kind = testCase.Value
   205  			// WHEN
   206  			err := doc.Validate()
   207  			// THEN
   208  			if testCase.ExpectedValid {
   209  				require.NoError(t, err)
   210  			} else {
   211  				require.Error(t, err)
   212  			}
   213  		})
   214  	}
   215  }
   216  
   217  func TestDocumentInput_Validate_Data(t *testing.T) {
   218  	testCases := []struct {
   219  		Name          string
   220  		Value         *graphql.CLOB
   221  		ExpectedValid bool
   222  	}{
   223  		{
   224  			Name:          "ExpectedValid",
   225  			Value:         fixCLOB("ExpectedValid"),
   226  			ExpectedValid: true,
   227  		},
   228  		{
   229  			Name:          "ExpectedValid nil pointer",
   230  			Value:         nil,
   231  			ExpectedValid: true,
   232  		},
   233  		{
   234  			Name:          "String longer than 256 chars",
   235  			Value:         fixCLOB(inputvalidationtest.String257Long),
   236  			ExpectedValid: true,
   237  		},
   238  		{
   239  
   240  			Name:          "Empty string",
   241  			Value:         fixCLOB(inputvalidationtest.EmptyString),
   242  			ExpectedValid: false,
   243  		},
   244  	}
   245  
   246  	for _, testCase := range testCases {
   247  		t.Run(testCase.Name, func(t *testing.T) {
   248  			//GIVEN
   249  			doc := fixValidDocument()
   250  			doc.Data = testCase.Value
   251  			// WHEN
   252  			err := doc.Validate()
   253  			// THEN
   254  			if testCase.ExpectedValid {
   255  				require.NoError(t, err)
   256  			} else {
   257  				require.Error(t, err)
   258  			}
   259  		})
   260  	}
   261  }
   262  
   263  func TestDocumentInput_Validate_FetchRequest(t *testing.T) {
   264  	validObj := fixValidFetchRequestInput()
   265  	testCases := []struct {
   266  		Name          string
   267  		Value         *graphql.FetchRequestInput
   268  		ExpectedValid bool
   269  	}{
   270  		{
   271  			Name:          "ExpectedValid",
   272  			Value:         &validObj,
   273  			ExpectedValid: true,
   274  		},
   275  		{
   276  			Name:          "ExpectedValid, nil value",
   277  			Value:         nil,
   278  			ExpectedValid: true,
   279  		},
   280  		{
   281  			Name:          "Invalid object",
   282  			Value:         &graphql.FetchRequestInput{URL: "test-string"},
   283  			ExpectedValid: false,
   284  		},
   285  	}
   286  
   287  	for _, testCase := range testCases {
   288  		t.Run(testCase.Name, func(t *testing.T) {
   289  			//GIVEN
   290  			doc := fixValidDocument()
   291  			doc.FetchRequest = testCase.Value
   292  			// WHEN
   293  			err := doc.Validate()
   294  			// THEN
   295  			if testCase.ExpectedValid {
   296  				require.NoError(t, err)
   297  			} else {
   298  				require.Error(t, err)
   299  			}
   300  		})
   301  	}
   302  }
   303  
   304  func fixValidDocument() graphql.DocumentInput {
   305  	return graphql.DocumentInput{
   306  		Title:       "title",
   307  		DisplayName: "name",
   308  		Description: "desc",
   309  		Format:      "MARKDOWN",
   310  		Data:        fixCLOB("data"),
   311  		Kind:        str.Ptr("kind"),
   312  	}
   313  }
   314  
   315  func fixCLOB(data string) *graphql.CLOB {
   316  	tmp := graphql.CLOB(data)
   317  	return &tmp
   318  }