diff --git a/internal/entities/command_deployment.go b/internal/entities/command_deployment.go index 0bf940303..94574964a 100644 --- a/internal/entities/command_deployment.go +++ b/internal/entities/command_deployment.go @@ -10,14 +10,13 @@ import ( "github.com/spf13/cobra" - "github.com/newrelic/newrelic-client-go/v2/pkg/nrtime" - + "github.com/newrelic/newrelic-cli/internal/client" "github.com/newrelic/newrelic-cli/internal/output" "github.com/newrelic/newrelic-cli/internal/utils" + "github.com/newrelic/newrelic-cli/internal/utils/validation" "github.com/newrelic/newrelic-client-go/v2/pkg/changetracking" "github.com/newrelic/newrelic-client-go/v2/pkg/common" - - "github.com/newrelic/newrelic-cli/internal/client" + "github.com/newrelic/newrelic-client-go/v2/pkg/nrtime" ) var ( @@ -68,6 +67,10 @@ The deployment command marks a change for a New Relic entity log.Fatal("--version cannot be empty") } + if _, err := validation.DecodeEntityGuid(entityGUID); err != nil { + log.Fatal(err) + } + var ( attrs map[string]interface{} err error diff --git a/internal/utils/validation/entity_guid_validator_test.go b/internal/utils/validation/entity_guid_validator_test.go new file mode 100644 index 000000000..2dcbfbb6b --- /dev/null +++ b/internal/utils/validation/entity_guid_validator_test.go @@ -0,0 +1,72 @@ +package validation + +import ( + "encoding/base64" + "fmt" + "strconv" + "strings" + "testing" +) + +func TestDecodeEntityGuid_Valid(t *testing.T) { + entity := GenericEntity{ + AccountId: 12345, + Domain: "test_domain", + EntityType: "user", + DomainId: "abc123", + } + encodedGuid := encodeEntity(entity) + + decodedEntity, err := DecodeEntityGuid(encodedGuid) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + if decodedEntity.AccountId != entity.AccountId || + decodedEntity.Domain != entity.Domain || + decodedEntity.EntityType != entity.EntityType || + decodedEntity.DomainId != entity.DomainId { + t.Errorf("Decoded entity does not match original entity") + } +} + +func encodeEntity(entity GenericEntity) string { + parts := []string{ + strconv.FormatInt(entity.AccountId, 10), + entity.Domain, + entity.EntityType, + entity.DomainId, + } + return base64.StdEncoding.EncodeToString([]byte(strings.Join(parts, DELIMITER))) +} + +func TestDecodeEntityGuid_MissingDelimiter(t *testing.T) { + invalidGuid := "invalidentityguid" + + _, err := DecodeEntityGuid(invalidGuid) + + if err != invalidEntityGuidErr { + t.Errorf("Expected error 'invalid entity GUID format', got %v", err) + } +} + +func TestDecodeEntityGuid_LessThanFourParts(t *testing.T) { + invalidGuid := base64.StdEncoding.EncodeToString([]byte("account|domain")) + + _, err := DecodeEntityGuid(invalidGuid) + + expectedErrorMessage := fmt.Sprintf("invalid entity GUID format: expected at least 4 parts delimited by '%s': %s", DELIMITER, invalidGuid) + if err.Error() != expectedErrorMessage { + t.Errorf("Expected error message: %s, got %v", expectedErrorMessage, err) + } +} + +func TestDecodeEntityGuid_EmptyEntityType(t *testing.T) { + encodedGuid := base64.StdEncoding.EncodeToString([]byte("12345|domain||domainId")) + + _, err := DecodeEntityGuid(encodedGuid) + + if err != emptyEntityTypeErr { + t.Errorf("Expected error 'entity type is required', got %v", err) + } +}