Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent mutations from secondary side of relation #3124

Merged

Conversation

AndrewSisley
Copy link
Contributor

Relevant issue(s)

Resolves #3102

Description

Prevents mutations from secondary side of relation.

Also validates that values given to relation fields are actually valid docIDs - previously it only validated this on the secondary side, and it was as easy to introduce it to the primary side as it was to correct the tests expecting the failure, so I've added this here.

As IDs set via secondary affected the secondary docID the order of results in some tests have changed. Also changed are tests that accidentally created docs via the secondary side that didn't test the secondary mutation (e.g. a lot of tests testing queries from the secondary side also created the test docs from this direction).

@AndrewSisley AndrewSisley added bug Something isn't working area/query Related to the query component labels Oct 10, 2024
@AndrewSisley AndrewSisley added this to the DefraDB v0.14 milestone Oct 10, 2024
@AndrewSisley AndrewSisley requested a review from a team October 10, 2024 19:54
@AndrewSisley AndrewSisley self-assigned this Oct 10, 2024
Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 82.14286% with 5 lines in your changes missing coverage. Please review.

Project coverage is 80.06%. Comparing base (bc68f57) to head (e4d9363).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
client/document.go 73.68% 4 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #3124      +/-   ##
===========================================
- Coverage    80.09%   80.06%   -0.03%     
===========================================
  Files          353      353              
  Lines        28210    28175      -35     
===========================================
- Hits         22593    22557      -36     
- Misses        4025     4028       +3     
+ Partials      1592     1590       -2     
Flag Coverage Δ
all-tests 80.06% <82.14%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
client/errors.go 58.11% <100.00%> (+1.16%) ⬆️
internal/db/collection.go 73.49% <ø> (-0.78%) ⬇️
internal/db/collection_update.go 78.43% <ø> (+2.59%) ⬆️
internal/request/graphql/schema/generate.go 87.88% <100.00%> (-0.27%) ⬇️
client/document.go 69.84% <73.68%> (-0.49%) ⬇️

... and 13 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bc68f57...e4d9363. Read the comment docs.

@AndrewSisley AndrewSisley force-pushed the 3102-rm-sec-rel-updates branch from 9b5cc3b to 73d016f Compare October 10, 2024 20:24
Comment on lines -97 to -125
func TestMutationCreateOneToMany_AliasedRelationNameInvalidIDManySide_CreatedDoc(t *testing.T) {
test := testUtils.TestCase{
Description: "One to many create mutation, invalid id, from the many side, with alias",
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Painted House",
"author": "ValueDoesntMatter"
}`,
},
testUtils.Request{
Request: `query {
Book {
name
}
}`,
Results: map[string]any{
"Book": []map[string]any{
{
"name": "Painted House",
},
},
},
},
},
}
executeTestCase(t, test)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why not assert the current behavior rather than removing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already tests that test what this test would test if it was modified to match the current behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Cheers thanks!

Comment on lines -76 to -91
func TestMutationCreateOneToOne_UseAliasWithNonExistingRelationSecondarySide_Error(t *testing.T) {
test := testUtils.TestCase{
Description: "One to one create mutation, alias relation, from the secondary side",
Actions: []any{
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Painted House",
"author": "bae-be6d8024-4953-5a92-84b4-f042d25230c6"
}`,
ExpectedError: "document not found or not authorized to access",
},
},
}
executeTestCase(t, test)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Similar question as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer :)

Copy link
Member

@nasdf nasdf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for also fixing the doc ID validation logic.

@@ -566,6 +566,20 @@ func (g *Generator) buildMutationInputTypes(collections []client.CollectionDefin
continue
}

if field.Kind == client.FieldKind_DocID && strings.HasSuffix(field.Name, request.RelatedObjectID) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: the logic here and in client.Document is the same. Would it make sense to have a field.IsSecondaryRelation helper function instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but decided it wasn't worth coupling them together and boost the (public) client package surface area. Especially given that the gql/schema package is begging for a re-write.

Spotted by Keenan, no longer needed.
Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AndrewSisley AndrewSisley merged commit 24a479f into sourcenetwork:develop Oct 11, 2024
42 of 43 checks passed
@AndrewSisley AndrewSisley deleted the 3102-rm-sec-rel-updates branch October 11, 2024 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/query Related to the query component bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Updating a relation from secondary side does not work
3 participants