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

     1  package graphql_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/model"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation/inputvalidationtest"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var (
    14  	validScenario, _      = json.Marshal(model.NewScenariosSchema([]string{"test-scenario"}))
    15  	validSchema           = graphql.JSONSchema(validScenario)
    16  	invalidScenarioSchema = graphql.JSONSchema(`{"type": "string"}`)
    17  	invalidSchema         = graphql.JSONSchema(`{invalid`)
    18  )
    19  
    20  func TestLabelDefinitionInput_Validate(t *testing.T) {
    21  	testCases := []struct {
    22  		Name          string
    23  		Value         graphql.LabelDefinitionInput
    24  		ExpectedValid bool
    25  	}{
    26  		{
    27  			Name:          "ExpectedValid",
    28  			Value:         fixValidLabelDefinitionInput(),
    29  			ExpectedValid: true,
    30  		},
    31  		{
    32  			Name: "ExpectedValid - Schema provided",
    33  			Value: graphql.LabelDefinitionInput{
    34  				Key:    model.ScenariosKey,
    35  				Schema: &validSchema,
    36  			},
    37  			ExpectedValid: true,
    38  		},
    39  		{
    40  			Name: "ExpectedValid - Scenarios schema",
    41  			Value: graphql.LabelDefinitionInput{
    42  				Key:    model.ScenariosKey,
    43  				Schema: fixScenariosSchema(t),
    44  			},
    45  			ExpectedValid: true,
    46  		},
    47  		{
    48  			Name: "Invalid - Invalid schema format",
    49  			Value: graphql.LabelDefinitionInput{
    50  				Key:    "ok",
    51  				Schema: &invalidSchema,
    52  			},
    53  			ExpectedValid: false,
    54  		},
    55  		{
    56  			Name: "Invalid - Scenarios schema invalid format",
    57  			Value: graphql.LabelDefinitionInput{
    58  				Key:    model.ScenariosKey,
    59  				Schema: &invalidSchema,
    60  			},
    61  			ExpectedValid: false,
    62  		},
    63  		{
    64  			Name: "Invalid - Scenarios schema invalid",
    65  			Value: graphql.LabelDefinitionInput{
    66  				Key:    model.ScenariosKey,
    67  				Schema: &invalidScenarioSchema,
    68  			},
    69  			ExpectedValid: false,
    70  		},
    71  		{
    72  			Name: "Invalid - Scenarios schema nil",
    73  			Value: graphql.LabelDefinitionInput{
    74  				Key:    model.ScenariosKey,
    75  				Schema: nil,
    76  			},
    77  			ExpectedValid: false,
    78  		},
    79  		{
    80  			Name: "Invalid - Scenarios schema with enum value which does not meet the regex - enum value contains invalid character",
    81  			Value: graphql.LabelDefinitionInput{
    82  				Key: model.ScenariosKey,
    83  				Schema: jsonSchemaPtr(`{
    84  					"type":        "array",
    85  					"minItems":    1,
    86  					"uniqueItems": true,
    87  					"items": {
    88  						"type": "string",
    89  						"enum": ["DEFAULT", "Abc&Cde"]
    90  					}
    91  				}`),
    92  			},
    93  			ExpectedValid: false,
    94  		},
    95  		{
    96  			Name: "Invalid - Scenarios schema with enum value which does not meet the regex - enum value too long",
    97  			Value: graphql.LabelDefinitionInput{
    98  				Key: model.ScenariosKey,
    99  				Schema: jsonSchemaPtr(`{
   100  					"type":        "array",
   101  					"minItems":    1,
   102  					"uniqueItems": true,
   103  					"items": {
   104  						"type": "string",
   105  						"enum": ["DEFAULT", "Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890Abcdefghijklmnopqrstuvwxyz1234567890"]
   106  					}	
   107  				}`),
   108  			},
   109  			ExpectedValid: false,
   110  		},
   111  	}
   112  
   113  	for _, testCase := range testCases {
   114  		t.Run(testCase.Name, func(t *testing.T) {
   115  			//GIVEN
   116  			sut := testCase.Value
   117  			// WHEN
   118  			err := sut.Validate()
   119  			// THEN
   120  			if testCase.ExpectedValid {
   121  				require.NoError(t, err)
   122  			} else {
   123  				require.Error(t, err)
   124  			}
   125  		})
   126  	}
   127  }
   128  
   129  func TestLabelDefinitionInput_Validate_Key(t *testing.T) {
   130  	testCases := []struct {
   131  		Name          string
   132  		Value         string
   133  		ExpectedValid bool
   134  	}{
   135  		{
   136  			Name:          "ExpectedValid",
   137  			Value:         model.ScenariosKey,
   138  			ExpectedValid: true,
   139  		},
   140  		{
   141  			Name:          "Invalid - Empty",
   142  			Value:         inputvalidationtest.EmptyString,
   143  			ExpectedValid: false,
   144  		},
   145  		{
   146  			Name:          "Invalid - Too long",
   147  			Value:         inputvalidationtest.String257Long,
   148  			ExpectedValid: false,
   149  		},
   150  		{
   151  			Name:          "Invalid - Unsupported characters in key",
   152  			Value:         "not/valid",
   153  			ExpectedValid: false,
   154  		},
   155  	}
   156  
   157  	for _, testCase := range testCases {
   158  		t.Run(testCase.Name, func(t *testing.T) {
   159  			//GIVEN
   160  			sut := fixValidLabelDefinitionInput()
   161  			sut.Key = testCase.Value
   162  			// WHEN
   163  			err := sut.Validate()
   164  			// THEN
   165  			if testCase.ExpectedValid {
   166  				require.NoError(t, err)
   167  			} else {
   168  				require.Error(t, err)
   169  			}
   170  		})
   171  	}
   172  }
   173  
   174  func TestLabelDefinitionInput_Validate_Schema(t *testing.T) {
   175  	testCases := []struct {
   176  		Name          string
   177  		Value         *graphql.JSONSchema
   178  		ExpectedValid bool
   179  	}{
   180  		{
   181  			Name:          "ExpectedValid",
   182  			Value:         &validSchema,
   183  			ExpectedValid: true,
   184  		},
   185  		{
   186  			Name:          "ExpectedInvalid - Nil",
   187  			Value:         (*graphql.JSONSchema)(nil),
   188  			ExpectedValid: false,
   189  		},
   190  	}
   191  
   192  	for _, testCase := range testCases {
   193  		t.Run(testCase.Name, func(t *testing.T) {
   194  			//GIVEN
   195  			sut := fixValidLabelDefinitionInput()
   196  			sut.Schema = testCase.Value
   197  			// WHEN
   198  			err := sut.Validate()
   199  			// THEN
   200  			if testCase.ExpectedValid {
   201  				require.NoError(t, err)
   202  			} else {
   203  				require.Error(t, err)
   204  			}
   205  		})
   206  	}
   207  }
   208  
   209  func fixValidLabelDefinitionInput() graphql.LabelDefinitionInput {
   210  	return graphql.LabelDefinitionInput{
   211  		Key:    model.ScenariosKey,
   212  		Schema: &validSchema,
   213  	}
   214  }
   215  
   216  func jsonSchemaPtr(schema string) *graphql.JSONSchema {
   217  	s := graphql.JSONSchema(schema)
   218  	return &s
   219  }
   220  
   221  func fixScenariosSchema(t *testing.T) *graphql.JSONSchema {
   222  	marshalled, err := json.Marshal(model.NewScenariosSchema([]string{"test-scenario"}))
   223  	require.NoError(t, err)
   224  	return jsonSchemaPtr(string(marshalled))
   225  }