-
Notifications
You must be signed in to change notification settings - Fork 3
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
Data refs tests #290
Data refs tests #290
Conversation
cowan-macady
commented
Nov 5, 2024
- add tests for kbac external property
- add values in constants (automation comes soon)
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #290 +/- ##
=======================================
Coverage 72.30% 72.30%
=======================================
Files 39 39
Lines 2011 2011
=======================================
Hits 1454 1454
Misses 493 493
Partials 64 64 ☔ View full report in Codecov by Sentry. |
84f1194
to
1188fbd
Compare
Here's the code health analysis summary for commits Analysis Summary
|
1188fbd
to
71400a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, use DescribeTable+Entry to make the code shorter, it'll be easier to review then...
71400a3
to
d49ff75
Compare
decision := resources[0].Type | ||
resource := resources[0].ExternalId | ||
action := resources[0].Actions[0] | ||
|
||
if numberResources == 2 { | ||
resource1 := resources[1].ExternalId | ||
action1 := resources[1].Actions[0] | ||
Expect(resp).To(PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"DecisionTime": Not(BeNil()), | ||
"Decisions": MatchAllKeys(Keys{ | ||
decision: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Resources": MatchAllKeys(Keys{ | ||
resource: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Actions": MatchAllKeys(Keys{ | ||
action: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Allow": Equal(true), | ||
})), | ||
}), | ||
})), | ||
resource1: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Actions": MatchAllKeys(Keys{ | ||
action1: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Allow": Equal(true), | ||
})), | ||
}), | ||
})), | ||
}), | ||
})), | ||
}), | ||
}))) | ||
} else { | ||
Expect(resp).To(PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"DecisionTime": Not(BeNil()), | ||
"Decisions": MatchAllKeys(Keys{ | ||
decision: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Resources": MatchAllKeys(Keys{ | ||
resource: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Actions": MatchAllKeys(Keys{ | ||
action: PointTo(MatchFields(IgnoreExtras, Fields{ | ||
"Allow": Equal(expectedAllow), | ||
})), | ||
}), | ||
})), | ||
}), | ||
})), | ||
}), | ||
}))) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could rewrite this using some helper function like e.g. (didn't try if it actually works):
func matchDecisions(resources []*authorizationpb.IsAuthorizedRequest_Resource, allow bool) types.GomegaMatcher {
typeToResourceKeys := map[any]Keys
for _, resource := range resources {
resourceKeys, ok := typeToResourceKeys[resource.Type]
if !ok {
resourceKeys = Keys{}
typeToResourceKeys[resource.Type] = resourceKeys
}
actionKeys := Keys{}
for _, action := range resource.Actions {
actionsKeys[action] = PointTo(MatchFields(IgnoreExtras, Fields{
"Allow": Equal(allow),
}))
}
resourceKeys[resource.ExternalId] = PointTo(MatchFields(IgnoreExtras, Fields{
"Actions": MatchAllKeys(actionKeys),
}))
}
decisionKeys := Keys{}
for resourceType, resourceKeys := range typeToResources {
decisionKeys[resourceType] = PointTo(MatchFields(IgnoreExtras, Fields{
"Resources": MatchAllKeys(resourceKeys),
}))
}
return MatchAllKeys(decisionKeys)
}
But I'll let you decide if it is the way to write tests - one could argue it is too complex for being a test code :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And now I see this "clever" solution may be even more code than your solution :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please ignore this suggestion above and rather split these test cases, so there is no need for if numberResources == 2
condition...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM, I'll let you decide whether you use the suggestion I've written above or not.
implement ENG-4307
d49ff75
to
6d1b476
Compare