214 lines
5.0 KiB
Go
214 lines
5.0 KiB
Go
package automation
|
|
|
|
import "testing"
|
|
|
|
func TestValidateConditions_Empty(t *testing.T) {
|
|
err := ValidateConditions(Conditions{})
|
|
if err != nil {
|
|
t.Fatalf("expected no error for empty conditions, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_ValidSingleCondition(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for valid condition, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_InvalidAttribute(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "invalid_attribute",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid attribute, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_InvalidFilterOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "invalid_op",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid filter operator, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_InvalidQueryOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "invalid_op",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid query operator, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_MultipleValidConditions(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
{
|
|
Attribute: "priority",
|
|
FilterOperator: "not_equal",
|
|
Values: []string{"low"},
|
|
QueryOperator: "and",
|
|
},
|
|
{
|
|
Attribute: "assignee_id",
|
|
FilterOperator: "is_present",
|
|
Values: []string{},
|
|
QueryOperator: "or",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for multiple valid conditions, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_MixedValidAndInvalid(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
{
|
|
Attribute: "bogus_attr",
|
|
FilterOperator: "equal",
|
|
Values: []string{"test"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error when one condition has invalid attribute, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AllWhitelistedAttributes(t *testing.T) {
|
|
for attr := range WhitelistedConditionAttributes {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: attr,
|
|
FilterOperator: "equal",
|
|
Values: []string{"test"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Errorf("expected valid for attribute %q, got error: %v", attr, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AllWhitelistedFilterOperators(t *testing.T) {
|
|
for op := range WhitelistedFilterOperators {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: op,
|
|
Values: []string{"test"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Errorf("expected valid for filter_operator %q, got error: %v", op, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AllWhitelistedQueryOperators(t *testing.T) {
|
|
for op := range WhitelistedQueryOperators {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"test"},
|
|
QueryOperator: op,
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Errorf("expected valid for query_operator %q, got error: %v", op, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_ContainsOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "content",
|
|
FilterOperator: "contains",
|
|
Values: []string{"help"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for contains operator, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AttributeChangedOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "attribute_changed",
|
|
Values: []string{"open", "resolved"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for attribute_changed operator, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_DoesNotContainOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "labels",
|
|
FilterOperator: "does_not_contain",
|
|
Values: []string{"spam"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for does_not_contain operator, got: %v", err)
|
|
}
|
|
} |