Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
[v1.7] Fix Runtime Name Validation (#1896)
Browse files Browse the repository at this point in the history
* Fix Runtime Name Validation (#1895)

* Initial commit

* Add test

* Bunp director version

* Adjust tests

Co-authored-by: StefanCholakov <[email protected]>

* Bunp director version

* Improvements

* Fix imports

* Add additional validation

* Remove unnecessary function

* Fix imports

* Fix validation message in test

* Remove assert for error message

Co-authored-by: StefanCholakov <[email protected]>
  • Loading branch information
desislavaa and StefanCholakov authored Jun 10, 2021
1 parent 8958440 commit c502c17
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
4 changes: 2 additions & 2 deletions chart/compass/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ global:
version: "PR-1828"
director:
dir:
version: "PR-1861"
version: "PR-1896"
gateway:
dir:
version: "PR-1850"
Expand Down Expand Up @@ -85,7 +85,7 @@ global:
version: "PR-31"
e2e_tests:
dir:
version: "PR-1845"
version: "PR-1896"
isLocalEnv: false
oauth2:
host: oauth2
Expand Down
8 changes: 6 additions & 2 deletions components/director/pkg/graphql/runtime_validation.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package graphql

import (
validation "github.com/go-ozzo/ozzo-validation"
"regexp"

validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation"
)

var runtimeNameRgx = regexp.MustCompile(`^[a-zA-Z0-9-._]+$`)

func (i RuntimeInput) Validate() error {
return validation.ValidateStruct(&i,
validation.Field(&i.Name, validation.Required, inputvalidation.DNSName),
validation.Field(&i.Name, validation.Required, validation.RuneLength(1, appNameLengthLimit), validation.Match(runtimeNameRgx)),
validation.Field(&i.Description, validation.RuneLength(0, descriptionStringLengthLimit)),
validation.Field(&i.Labels, inputvalidation.EachKey(validation.Required, validation.Match(alphanumericUnderscoreRegexp))),
)
Expand Down
14 changes: 12 additions & 2 deletions components/director/pkg/graphql/runtime_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ func TestRuntimeInput_Validate_Name(t *testing.T) {
Value: inputvalidationtest.ValidName,
ExpectedValid: true,
},
{
Name: "Expected valid with digit",
Value: inputvalidationtest.ValidRuntimeNameWithDigit,
ExpectedValid: true,
},
{
Name: "Invalid - Empty",
Value: inputvalidationtest.EmptyString,
ExpectedValid: false,
},
{
Name: "Invalid - Invalid Name",
Value: inputvalidationtest.InvalidName,
Name: "Invalid - Too long",
Value: inputvalidationtest.InValidRuntimeNameTooLong,
ExpectedValid: false,
},
{
Name: "Invalid - invalid characters",
Value: inputvalidationtest.InValidRuntimeNameInvalidCharacters,
ExpectedValid: false,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ var (
)

const (
EmptyString = ""
ValidName = "thi5-1npu7.15-valid"
InvalidName = "0iNvALiD"
ValidURL = "https://kyma-project.io"
InvalidURL = "http:/kyma-projectio/path/"
EmptyString = ""
ValidName = "thi5-1npu7.15-valid"
ValidRuntimeNameWithDigit = "0thi5-1npu7.15_valid"
InValidRuntimeNameTooLong = "123aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaааа"
InValidRuntimeNameInvalidCharacters = "123 456"
InvalidName = "0iNvALiD"
ValidURL = "https://kyma-project.io"
InvalidURL = "http:/kyma-projectio/path/"
)
66 changes: 56 additions & 10 deletions tests/director/tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,74 @@ import (

// Runtime Validation

func TestCreateRuntime_Validation(t *testing.T) {
func TestCreateRuntime_ValidationSuccess(t *testing.T) {
// GIVEN
ctx := context.Background()
tenantId := tenant.TestTenants.GetDefaultTenantID()

invalidInput := graphql.RuntimeInput{
Name: "0invalid",
runtimeIn := graphql.RuntimeInput{
Name: "012345Myaccount_Runtime",
}
inputString, err := testctx.Tc.Graphqlizer.RuntimeInputToGQL(invalidInput)
inputString, err := testctx.Tc.Graphqlizer.RuntimeInputToGQL(runtimeIn)
require.NoError(t, err)
var result graphql.Runtime
request := fixtures.FixRegisterRuntimeRequest(inputString)
// WHEN
err = testctx.Tc.RunOperation(ctx, dexGraphQLClient, request, &result)
defer fixtures.UnregisterRuntime(t, ctx, dexGraphQLClient, tenantId, result.ID)

// THEN
require.NoError(t, err)
}

func TestCreateRuntime_ValidationFailure(t *testing.T) {
// GIVEN
ctx := context.Background()
tenantId := tenant.TestTenants.GetDefaultTenantID()

runtimeIn := graphql.RuntimeInput{
Name: "012345Myaccount_Runtime_aaaaaaaaaaaaаа",
}
inputString, err := testctx.Tc.Graphqlizer.RuntimeInputToGQL(runtimeIn)
require.NoError(t, err)
var result graphql.Runtime
request := fixtures.FixRegisterRuntimeRequest(inputString)
// WHEN
err = testctx.Tc.RunOperation(ctx, dexGraphQLClient, request, &result)
if err == nil {
defer fixtures.UnregisterRuntime(t, ctx, dexGraphQLClient, tenantId, result.ID)
}

// THEN
require.Error(t, err)
assert.EqualError(t, err, "graphql: Invalid data RuntimeInput [name=cannot start with digit]")
}

func TestUpdateRuntime_Validation(t *testing.T) {
func TestUpdateRuntime_ValidationSuccess(t *testing.T) {
// GIVEN
ctx := context.Background()

tenantId := tenant.TestTenants.GetDefaultTenantID()

input := fixtures.FixRuntimeInput("validation-test-rtm")
rtm := fixtures.RegisterRuntimeFromInputWithinTenant(t, ctx, dexGraphQLClient, tenantId, &input)
defer fixtures.UnregisterRuntime(t, ctx, dexGraphQLClient, tenantId, rtm.ID)

runtimeIn := graphql.RuntimeInput{
Name: "012345Myaccount_Runtime",
}
inputString, err := testctx.Tc.Graphqlizer.RuntimeInputToGQL(runtimeIn)
require.NoError(t, err)
var result graphql.Runtime
request := fixtures.FixUpdateRuntimeRequest(rtm.ID, inputString)

// WHEN
err = testctx.Tc.RunOperation(ctx, dexGraphQLClient, request, &result)

// THEN
require.NoError(t, err)
}

func TestUpdateRuntime_ValidationFailure(t *testing.T) {
// GIVEN
ctx := context.Background()

Expand All @@ -48,10 +95,10 @@ func TestUpdateRuntime_Validation(t *testing.T) {
rtm := fixtures.RegisterRuntimeFromInputWithinTenant(t, ctx, dexGraphQLClient, tenantId, &input)
defer fixtures.UnregisterRuntime(t, ctx, dexGraphQLClient, tenantId, rtm.ID)

invalidInput := graphql.RuntimeInput{
Name: "0invalid",
runtimeIn := graphql.RuntimeInput{
Name: "012345Myaccount_Runtime_aaaaaaaaaaaaаа",
}
inputString, err := testctx.Tc.Graphqlizer.RuntimeInputToGQL(invalidInput)
inputString, err := testctx.Tc.Graphqlizer.RuntimeInputToGQL(runtimeIn)
require.NoError(t, err)
var result graphql.Runtime
request := fixtures.FixUpdateRuntimeRequest(rtm.ID, inputString)
Expand All @@ -61,7 +108,6 @@ func TestUpdateRuntime_Validation(t *testing.T) {

// THEN
require.Error(t, err)
assert.EqualError(t, err, "graphql: Invalid data RuntimeInput [name=cannot start with digit]")
}

// Label Definition Validation
Expand Down

0 comments on commit c502c17

Please sign in to comment.