Skip to content

Commit

Permalink
feat(update): verify etag updated
Browse files Browse the repository at this point in the history
From [AIP-154](https://google.aip.dev/154) documentation:
```
The etag field must be provided by the server on output,
and values should conform to RFC 7232
```

Note: no validation is done that Etag conforms to `RFC 7232`.
  • Loading branch information
thall committed Jul 4, 2024
1 parent 4731e46 commit 6c35c61
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Sample skips:
| persisted | The updated resource should be persisted and reachable with Get. | Generated only if all are true: <ul><li>has Update method</li><li>Update method does not return long-running operation</li><li>has Get method</li></ul> |
| preserve create_time | The field create_time should be preserved when a '\*'-update mask is used. | Generated only if all are true: <ul><li>has Update method</li><li>Update method does not return long-running operation</li><li>has field 'create_time'</li><li>resource has any required fields</li></ul> |
| etag mismatch | Method should fail with Aborted if the supplied etag doesnt match the current etag value. | Generated only if all are true: <ul><li>has Update method</li><li>request has etag field</li><li>has field 'etag'</li></ul> |
| etag updated | Field etag should have a new value when the resource is successfully updated. | Generated only if all are true: <ul><li>has Update method</li><li>request has etag field</li><li>has field 'etag'</li></ul> |
| not found | Method should fail with NotFound if the resource does not exist. | Generated only if all are true: <ul><li>has Update method</li></ul> |
| invalid update mask | The method should fail with InvalidArgument if the update_mask is invalid. | Generated only if all are true: <ul><li>has Update method</li><li>Update method has update_mask</li></ul> |
| required fields | Method should fail with InvalidArgument if any required field is missing when called with '\*' update_mask. | Generated only if all are true: <ul><li>has Update method</li><li>resource has any required fields</li></ul> |
Expand Down
32 changes: 32 additions & 0 deletions internal/aiptest/update/etag.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ var etagMismatch = suite.Test{
return nil
},
}

//nolint:gochecknoglobals
var etagUpdated = suite.Test{
Name: "etag updated",
Doc: []string{
"Field etag should have a new value when the resource is successfully updated.",
},
OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeUpdate),
onlyif.HasRequestEtag(aipreflect.MethodTypeUpdate),
onlyif.HasField("etag"),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
if util.HasParent(scope.Resource) {
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
f.P("created := fx.create(t, parent)")
} else {
f.P("created := fx.create(t)")
}
updateMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeUpdate)
util.MethodUpdate{
Resource: scope.Resource,
Method: updateMethod,
Parent: "parent",
Name: "created.Name",
Etag: "created.Etag",
}.Generate(f, "updated", "err", ":=")
f.P(ident.AssertNilError, "(t, err)")
f.P(ident.AssertCheck, "(t, updated.Etag != created.Etag)")
return nil
},
}
2 changes: 1 addition & 1 deletion internal/aiptest/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Suite = suite.Suite{
persisted,
preserveCreateTime,
etagMismatch,
etagUpdated,
},
TestGroups: []suite.TestGroup{
withResourceGroup,
Expand All @@ -41,6 +42,5 @@ var withResourceGroup = suite.TestGroup{
invalidUpdateMask,
requiredFields,
// TODO: add test for supplying wildcard as name
// TODO: add test for etags
},
}
29 changes: 29 additions & 0 deletions proto/gen/einride/example/freight/v1/freight_service_aiptest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c35c61

Please sign in to comment.