github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/runtime_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 TestRuntimeRegisterInput_Validate_Name(t *testing.T) {
    13  	testCases := []struct {
    14  		Name          string
    15  		Value         string
    16  		ExpectedValid bool
    17  	}{
    18  		{
    19  			Name:          "ExpectedValid",
    20  			Value:         inputvalidationtest.ValidName,
    21  			ExpectedValid: true,
    22  		},
    23  		{
    24  			Name:          "Expected valid with digit",
    25  			Value:         inputvalidationtest.ValidRuntimeNameWithDigit,
    26  			ExpectedValid: true,
    27  		},
    28  		{
    29  			Name:          "Invalid - Empty",
    30  			Value:         inputvalidationtest.EmptyString,
    31  			ExpectedValid: false,
    32  		},
    33  		{
    34  			Name:          "Invalid - too long",
    35  			Value:         inputvalidationtest.String257Long,
    36  			ExpectedValid: false,
    37  		},
    38  		{
    39  			Name:          "Invalid - invalid characters",
    40  			Value:         inputvalidationtest.InValidRuntimeNameInvalidCharacters,
    41  			ExpectedValid: false,
    42  		},
    43  	}
    44  
    45  	for _, testCase := range testCases {
    46  		t.Run(testCase.Name, func(t *testing.T) {
    47  			//GIVEN
    48  			sut := fixValidRuntimeRegisterInput()
    49  			sut.Name = testCase.Value
    50  			// WHEN
    51  			err := sut.Validate()
    52  			// THEN
    53  			if testCase.ExpectedValid {
    54  				require.NoError(t, err)
    55  			} else {
    56  				require.Error(t, err)
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestRuntimeRegisterInput_Validate_Description(t *testing.T) {
    63  	testCases := []struct {
    64  		Name          string
    65  		Value         *string
    66  		ExpectedValid bool
    67  	}{
    68  		{
    69  			Name: "ExpectedValid",
    70  			Value: str.Ptr("valid	valid"),
    71  			ExpectedValid: true,
    72  		},
    73  		{
    74  			Name:          "ExpectedValid - Nil",
    75  			Value:         (*string)(nil),
    76  			ExpectedValid: true,
    77  		},
    78  		{
    79  			Name:          "ExpectedValid - Empty",
    80  			Value:         str.Ptr(inputvalidationtest.EmptyString),
    81  			ExpectedValid: true,
    82  		},
    83  		{
    84  			Name:          "Invalid - Too long",
    85  			Value:         str.Ptr(inputvalidationtest.String2001Long),
    86  			ExpectedValid: false,
    87  		},
    88  	}
    89  
    90  	for _, testCase := range testCases {
    91  		t.Run(testCase.Name, func(t *testing.T) {
    92  			//GIVEN
    93  			sut := fixValidRuntimeRegisterInput()
    94  			sut.Description = testCase.Value
    95  			// WHEN
    96  			err := sut.Validate()
    97  			// THEN
    98  			if testCase.ExpectedValid {
    99  				require.NoError(t, err)
   100  			} else {
   101  				require.Error(t, err)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestRuntimeRegisterInput_Validate_Labels(t *testing.T) {
   108  	testCases := []struct {
   109  		Name          string
   110  		Value         graphql.Labels
   111  		ExpectedValid bool
   112  	}{
   113  		{
   114  			Name: "ExpectedValid",
   115  			Value: graphql.Labels{
   116  				"test": "ok",
   117  			},
   118  			ExpectedValid: true,
   119  		},
   120  		{
   121  			Name:          "ExpectedValid - Nil",
   122  			Value:         nil,
   123  			ExpectedValid: true,
   124  		},
   125  		{
   126  			Name: "ExpectedValid - Nil map value",
   127  			Value: graphql.Labels{
   128  				"test": nil,
   129  			},
   130  			ExpectedValid: true,
   131  		},
   132  		{
   133  			Name: "Invalid - Empty map key",
   134  			Value: graphql.Labels{
   135  				inputvalidationtest.EmptyString: "val",
   136  			},
   137  			ExpectedValid: false,
   138  		},
   139  		{
   140  			Name: "Invalid - Unsupported characters in key",
   141  			Value: graphql.Labels{
   142  				"not/valid": "val",
   143  			},
   144  			ExpectedValid: false,
   145  		},
   146  	}
   147  
   148  	for _, testCase := range testCases {
   149  		t.Run(testCase.Name, func(t *testing.T) {
   150  			//GIVEN
   151  			sut := fixValidRuntimeRegisterInput()
   152  			sut.Labels = testCase.Value
   153  			// WHEN
   154  			err := sut.Validate()
   155  			// THEN
   156  			if testCase.ExpectedValid {
   157  				require.NoError(t, err)
   158  			} else {
   159  				require.Error(t, err)
   160  			}
   161  		})
   162  	}
   163  }
   164  
   165  func TestRuntimeRegisterInput_Validate_Webhooks(t *testing.T) {
   166  	testCases := []struct {
   167  		Name          string
   168  		Value         graphql.WebhookType
   169  		ExpectedValid bool
   170  	}{
   171  		{
   172  			Name:          "ExpectedValid",
   173  			Value:         graphql.WebhookTypeConfigurationChanged,
   174  			ExpectedValid: true,
   175  		},
   176  		{
   177  			Name:          "Invalid - Empty",
   178  			Value:         inputvalidationtest.EmptyString,
   179  			ExpectedValid: false,
   180  		},
   181  		{
   182  			Name:          "Invalid - Not enum",
   183  			Value:         "invalid",
   184  			ExpectedValid: false,
   185  		},
   186  	}
   187  
   188  	for _, testCase := range testCases {
   189  		t.Run(testCase.Name, func(t *testing.T) {
   190  			//GIVEN
   191  			sut := fixValidRuntimeRegisterInput()
   192  			webhook := fixValidWebhookInput(inputvalidationtest.ValidURL)
   193  			sut.Webhooks = []*graphql.WebhookInput{&webhook}
   194  			sut.Webhooks[0].Type = testCase.Value
   195  			// WHEN
   196  			err := sut.Validate()
   197  			// THEN
   198  			if testCase.ExpectedValid {
   199  				require.NoError(t, err)
   200  			} else {
   201  				require.Error(t, err)
   202  			}
   203  		})
   204  	}
   205  }
   206  
   207  func TestRuntimeRegisterInput_Validate_Application_Namespace(t *testing.T) {
   208  	testCases := []struct {
   209  		Name          string
   210  		Value         *string
   211  		ExpectedValid bool
   212  	}{
   213  		{
   214  			Name: "ExpectedValid",
   215  			Value: str.Ptr("valid	valid"),
   216  			ExpectedValid: true,
   217  		},
   218  		{
   219  			Name:          "ExpectedValid - Nil",
   220  			Value:         (*string)(nil),
   221  			ExpectedValid: true,
   222  		},
   223  		{
   224  			Name:          "ExpectedValid - Empty",
   225  			Value:         str.Ptr(inputvalidationtest.EmptyString),
   226  			ExpectedValid: true,
   227  		},
   228  		{
   229  			Name:          "Invalid - Too long",
   230  			Value:         str.Ptr(inputvalidationtest.String257Long),
   231  			ExpectedValid: false,
   232  		},
   233  	}
   234  
   235  	for _, testCase := range testCases {
   236  		t.Run(testCase.Name, func(t *testing.T) {
   237  			//GIVEN
   238  			sut := fixValidRuntimeRegisterInput()
   239  			sut.ApplicationNamespace = testCase.Value
   240  			// WHEN
   241  			err := sut.Validate()
   242  			// THEN
   243  			if testCase.ExpectedValid {
   244  				require.NoError(t, err)
   245  			} else {
   246  				require.Error(t, err)
   247  			}
   248  		})
   249  	}
   250  }
   251  
   252  func fixValidRuntimeRegisterInput() graphql.RuntimeRegisterInput {
   253  	return graphql.RuntimeRegisterInput{
   254  		Name: inputvalidationtest.ValidName,
   255  	}
   256  }
   257  
   258  func TestRuntimeUpdateInput_Validate_Name(t *testing.T) {
   259  	testCases := []struct {
   260  		Name          string
   261  		Value         string
   262  		ExpectedValid bool
   263  	}{
   264  		{
   265  			Name:          "ExpectedValid",
   266  			Value:         inputvalidationtest.ValidName,
   267  			ExpectedValid: true,
   268  		},
   269  		{
   270  			Name:          "Expected valid with digit",
   271  			Value:         inputvalidationtest.ValidRuntimeNameWithDigit,
   272  			ExpectedValid: true,
   273  		},
   274  		{
   275  			Name:          "Invalid - Empty",
   276  			Value:         inputvalidationtest.EmptyString,
   277  			ExpectedValid: false,
   278  		},
   279  		{
   280  			Name:          "Invalid - too long",
   281  			Value:         inputvalidationtest.String257Long,
   282  			ExpectedValid: false,
   283  		},
   284  		{
   285  			Name:          "Invalid - invalid characters",
   286  			Value:         inputvalidationtest.InValidRuntimeNameInvalidCharacters,
   287  			ExpectedValid: false,
   288  		},
   289  	}
   290  
   291  	for _, testCase := range testCases {
   292  		t.Run(testCase.Name, func(t *testing.T) {
   293  			//GIVEN
   294  			sut := fixValidRuntimeUpdateInput()
   295  			sut.Name = testCase.Value
   296  			// WHEN
   297  			err := sut.Validate()
   298  			// THEN
   299  			if testCase.ExpectedValid {
   300  				require.NoError(t, err)
   301  			} else {
   302  				require.Error(t, err)
   303  			}
   304  		})
   305  	}
   306  }
   307  
   308  func TestRuntimeUpdateInput_Validate_Description(t *testing.T) {
   309  	testCases := []struct {
   310  		Name          string
   311  		Value         *string
   312  		ExpectedValid bool
   313  	}{
   314  		{
   315  			Name: "ExpectedValid",
   316  			Value: str.Ptr("valid	valid"),
   317  			ExpectedValid: true,
   318  		},
   319  		{
   320  			Name:          "ExpectedValid - Nil",
   321  			Value:         (*string)(nil),
   322  			ExpectedValid: true,
   323  		},
   324  		{
   325  			Name:          "ExpectedValid - Empty",
   326  			Value:         str.Ptr(inputvalidationtest.EmptyString),
   327  			ExpectedValid: true,
   328  		},
   329  		{
   330  			Name:          "Invalid - Too long",
   331  			Value:         str.Ptr(inputvalidationtest.String2001Long),
   332  			ExpectedValid: false,
   333  		},
   334  	}
   335  
   336  	for _, testCase := range testCases {
   337  		t.Run(testCase.Name, func(t *testing.T) {
   338  			//GIVEN
   339  			sut := fixValidRuntimeUpdateInput()
   340  			sut.Description = testCase.Value
   341  			// WHEN
   342  			err := sut.Validate()
   343  			// THEN
   344  			if testCase.ExpectedValid {
   345  				require.NoError(t, err)
   346  			} else {
   347  				require.Error(t, err)
   348  			}
   349  		})
   350  	}
   351  }
   352  
   353  func TestRuntimeUpdateInput_Validate_Labels(t *testing.T) {
   354  	testCases := []struct {
   355  		Name          string
   356  		Value         graphql.Labels
   357  		ExpectedValid bool
   358  	}{
   359  		{
   360  			Name: "ExpectedValid",
   361  			Value: graphql.Labels{
   362  				"test": "ok",
   363  			},
   364  			ExpectedValid: true,
   365  		},
   366  		{
   367  			Name:          "ExpectedValid - Nil",
   368  			Value:         nil,
   369  			ExpectedValid: true,
   370  		},
   371  		{
   372  			Name: "ExpectedValid - Nil map value",
   373  			Value: graphql.Labels{
   374  				"test": nil,
   375  			},
   376  			ExpectedValid: true,
   377  		},
   378  		{
   379  			Name: "Invalid - Empty map key",
   380  			Value: graphql.Labels{
   381  				inputvalidationtest.EmptyString: "val",
   382  			},
   383  			ExpectedValid: false,
   384  		},
   385  		{
   386  			Name: "Invalid - Unsupported characters in key",
   387  			Value: graphql.Labels{
   388  				"not/valid": "val",
   389  			},
   390  			ExpectedValid: false,
   391  		},
   392  	}
   393  
   394  	for _, testCase := range testCases {
   395  		t.Run(testCase.Name, func(t *testing.T) {
   396  			//GIVEN
   397  			sut := fixValidRuntimeUpdateInput()
   398  			sut.Labels = testCase.Value
   399  			// WHEN
   400  			err := sut.Validate()
   401  			// THEN
   402  			if testCase.ExpectedValid {
   403  				require.NoError(t, err)
   404  			} else {
   405  				require.Error(t, err)
   406  			}
   407  		})
   408  	}
   409  }
   410  
   411  func TestRuntimeUpdateInput_Validate_Application_Namespace(t *testing.T) {
   412  	testCases := []struct {
   413  		Name          string
   414  		Value         *string
   415  		ExpectedValid bool
   416  	}{
   417  		{
   418  			Name: "ExpectedValid",
   419  			Value: str.Ptr("valid	valid"),
   420  			ExpectedValid: true,
   421  		},
   422  		{
   423  			Name:          "ExpectedValid - Nil",
   424  			Value:         (*string)(nil),
   425  			ExpectedValid: true,
   426  		},
   427  		{
   428  			Name:          "ExpectedValid - Empty",
   429  			Value:         str.Ptr(inputvalidationtest.EmptyString),
   430  			ExpectedValid: true,
   431  		},
   432  		{
   433  			Name:          "Invalid - Too long",
   434  			Value:         str.Ptr(inputvalidationtest.String257Long),
   435  			ExpectedValid: false,
   436  		},
   437  	}
   438  
   439  	for _, testCase := range testCases {
   440  		t.Run(testCase.Name, func(t *testing.T) {
   441  			//GIVEN
   442  			sut := fixValidRuntimeUpdateInput()
   443  			sut.ApplicationNamespace = testCase.Value
   444  			// WHEN
   445  			err := sut.Validate()
   446  			// THEN
   447  			if testCase.ExpectedValid {
   448  				require.NoError(t, err)
   449  			} else {
   450  				require.Error(t, err)
   451  			}
   452  		})
   453  	}
   454  }
   455  
   456  func fixValidRuntimeUpdateInput() graphql.RuntimeUpdateInput {
   457  	return graphql.RuntimeUpdateInput{
   458  		Name: inputvalidationtest.ValidName,
   459  	}
   460  }