From 23184c0491efb4623f79ba8f54e5a3a9c6b8ff8c Mon Sep 17 00:00:00 2001 From: Tolga Ozen Date: Thu, 30 Nov 2023 19:44:26 +0300 Subject: [PATCH] test: bundle operation tests added --- internal/validation/validation_test.go | 167 +++++++++++++++++++++++++ 1 file changed, 167 insertions(+) diff --git a/internal/validation/validation_test.go b/internal/validation/validation_test.go index 9fb778047..df8618354 100644 --- a/internal/validation/validation_test.go +++ b/internal/validation/validation_test.go @@ -282,6 +282,20 @@ var _ = Describe("validation", func() { }, } + // Create an invalid test tuple with relation not defined in entity definition + invalidTuple3 := &base.Tuple{ + Subject: &base.Subject{ + Type: "team", + Id: "x", + Relation: "member", + }, + Relation: "member", + Entity: &base.Entity{ + Type: "team", + Id: "x", + }, + } + // Test the function with a valid tuple err := ValidateTuple(entityDef, validTuple1) Expect(err).Should(BeNil()) @@ -305,6 +319,10 @@ var _ = Describe("validation", func() { // Test the function with an invalid tuple with relation not defined in entity definition err = ValidateTuple(entityDef, invalidTuple2) Expect(err).ShouldNot(BeNil()) + + // Test the function with an invalid tuple with relation not defined in entity definition + err = ValidateTuple(entityDef, invalidTuple3) + Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_ENTITY_AND_SUBJECT_CANNOT_BE_EQUAL.String())) }) It("Case 4", func() { @@ -324,6 +342,10 @@ var _ = Describe("validation", func() { Name: "ips", Type: base.AttributeType_ATTRIBUTE_TYPE_STRING_ARRAY, }, + "private": { + Name: "private", + Type: base.AttributeType_ATTRIBUTE_TYPE_UNSPECIFIED, + }, }, } @@ -468,6 +490,16 @@ var _ = Describe("validation", func() { Attributes: []string{}, }) Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_VALIDATION.String())) + + err = ValidateFilters( + &base.TupleFilter{}, &base.AttributeFilter{ + Entity: &base.EntityFilter{ + Type: "organization", + Ids: []string{"1"}, + }, + Attributes: []string{}, + }) + Expect(err).ShouldNot(HaveOccurred()) }) It("Case 8", func() { @@ -489,6 +521,15 @@ var _ = Describe("validation", func() { }) Expect(is).Should(BeFalse()) + is = IsAttributeFilterEmpty(&base.AttributeFilter{ + Entity: &base.EntityFilter{ + Type: "", + Ids: []string{}, + }, + Attributes: []string{"public"}, + }) + Expect(is).Should(BeFalse()) + is = IsAttributeFilterEmpty(&base.AttributeFilter{ Entity: &base.EntityFilter{ Type: "", @@ -515,5 +556,131 @@ var _ = Describe("validation", func() { }) Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_VALIDATION.String())) }) + + It("Case 9", func() { + is := IsTupleFilterEmpty(&base.TupleFilter{ + Entity: &base.EntityFilter{ + Type: "", + Ids: []string{"1"}, + }, + Relation: "admin", + }) + Expect(is).Should(BeFalse()) + + is = IsTupleFilterEmpty(&base.TupleFilter{ + Entity: &base.EntityFilter{ + Type: "", + Ids: []string{}, + }, + Relation: "admin", + }) + Expect(is).Should(BeFalse()) + + is = IsTupleFilterEmpty(&base.TupleFilter{ + Entity: &base.EntityFilter{ + Type: "", + Ids: []string{}, + }, + Relation: "", + Subject: &base.SubjectFilter{ + Type: "user", + }, + }) + Expect(is).Should(BeFalse()) + + is = IsTupleFilterEmpty(&base.TupleFilter{ + Entity: &base.EntityFilter{ + Type: "", + Ids: []string{}, + }, + Relation: "", + Subject: &base.SubjectFilter{ + Type: "", + Ids: []string{"1", "2"}, + }, + }) + Expect(is).Should(BeFalse()) + + is = IsTupleFilterEmpty(&base.TupleFilter{ + Entity: &base.EntityFilter{ + Type: "", + Ids: []string{}, + }, + Relation: "", + Subject: &base.SubjectFilter{ + Type: "", + Ids: []string{}, + Relation: "member", + }, + }) + Expect(is).Should(BeFalse()) + }) + + It("Case 10", func() { + err := ValidateBundleOperation(&base.Operation{ + RelationshipsWrite: []string{ + "organization:{{.organizationID}}#member@user:{{.userID}}", + }, + RelationshipsDelete: []string{ + "organization:{{.organizationID}}#admin@user:{{.userID}}", + }, + AttributesWrite: []string{ + "organization:{{.organizationID}}$public|boolean:true", + }, + AttributesDelete: []string{ + "team:{{.teamID}}$public|boolean:false", + }, + }) + Expect(err).ShouldNot(HaveOccurred()) + + err = ValidateBundleOperation(&base.Operation{ + RelationshipsWrite: []string{ + "organization:{{.organizationID}}#member@user:{{.userID}}", + "organization:{{.organizationID}}#member@user:{{.userID}}", + }, + }) + Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())) + + err = ValidateBundleOperation(&base.Operation{ + RelationshipsDelete: []string{ + "organization:{{.organizationID}}#admin@user:{{.userID}}", + "organization:{{.organizationID}}#admin@user:{{.userID}}", + }, + }) + Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())) + + err = ValidateBundleOperation(&base.Operation{ + AttributesWrite: []string{ + "organization:{{.organizationID}}$public|boolean:true", + "organization:{{.organizationID}}$public|boolean:true", + }, + }) + Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())) + + err = ValidateBundleOperation(&base.Operation{ + AttributesDelete: []string{ + "team:{{.teamID}}$public|boolean:false", + "team:{{.teamID}}$public|boolean:false", + }, + }) + Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_ALREADY_EXIST.String())) + }) + + It("Case 11", func() { + err := ValidateBundleArguments([]string{ + "organizationID", + }, map[string]string{ + "organizationID": "758", + }) + Expect(err).ShouldNot(HaveOccurred()) + + err = ValidateBundleArguments([]string{ + "organizationID", + "teamID", + }, map[string]string{ + "organizationID": "758", + }) + Expect(err.Error()).Should(Equal(base.ErrorCode_ERROR_CODE_MISSING_ARGUMENT.String())) + }) }) })