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

Data refs tests #290

Merged
merged 1 commit into from
Nov 8, 2024
Merged

Data refs tests #290

merged 1 commit into from
Nov 8, 2024

Conversation

cowan-macady
Copy link
Contributor

  • add tests for kbac external property
  • add values in constants (automation comes soon)

@cowan-macady cowan-macady requested a review from a team as a code owner November 5, 2024 09:45
@cowan-macady cowan-macady requested a review from maaland November 5, 2024 09:45
Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 72.30%. Comparing base (cb7ea0a) to head (6d1b476).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

Copy link

deepsource-io bot commented Nov 5, 2024

Here's the code health analysis summary for commits cb7ea0a..6d1b476. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Go LogoGo✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

Copy link
Contributor

@jakubtomany jakubtomany left a 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...

authorization/authorization_integration_test.go Outdated Show resolved Hide resolved
Comment on lines 104 to 151
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),
})),
}),
})),
}),
})),
}),
})))
}
Copy link
Contributor

@jakubtomany jakubtomany Nov 8, 2024

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 :)

Copy link
Contributor

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 :)

Copy link
Contributor

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...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor

@jakubtomany jakubtomany left a 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.

@cowan-macady cowan-macady merged commit 65e6758 into master Nov 8, 2024
11 checks passed
@cowan-macady cowan-macady deleted the data-refs-tests branch November 8, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants