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

add grants tests for groups API #5403

Open
wants to merge 21 commits into
base: llb-normalized-grants
Choose a base branch
from

Conversation

bosorawis
Copy link
Collaborator

Add tests that validate covering grants in preparation for grants system rework

@bosorawis bosorawis requested a review from a team as a code owner January 3, 2025 17:47
@bosorawis bosorawis force-pushed the bosorawis-prototype-grant-test branch from ffbb1c6 to e18100e Compare January 3, 2025 17:48
@bosorawis bosorawis marked this pull request as draft January 3, 2025 17:49
Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

Very cool, thanks for this!

internal/daemon/controller/handlers/groups/grants_test.go Outdated Show resolved Hide resolved
@bosorawis bosorawis changed the base branch from main to llb-normalized-grants January 3, 2025 19:19
@bosorawis bosorawis force-pushed the bosorawis-prototype-grant-test branch from fa1bac8 to 8a31f36 Compare January 3, 2025 19:19
@johanbrandhorst
Copy link
Collaborator

Looks like you need a "go mod tidy"

// genAuthTokenCtx creates an auth.VerifierContext which contains a valid auth token
// for a user which is associated with roles in the roles parameter
// this function creates an authMethod, account, user at global scope
func genAuthTokenCtx(t *testing.T,
Copy link
Member

Choose a reason for hiding this comment

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

We should add a prefix of Test to the function since it's only used for testing. So nobody accidentally calls it. I think we have a pattern of this in Boundary

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since the function takes *testing.T as its first parameter so I don't think this can be accidentally called outside of test but I'll add test... prefix to its name 👍

Copy link
Member

Choose a reason for hiding this comment

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

I would also note that this function is defined in an _test.go file, which means it does not get compiled into the final binary. As such it can only be called via the tests. The pattern of using a Test prefix is usually for things defined in testing.go.

wantIDs: []string{globalGroup.PublicId, org1Group.PublicId, org2Group.PublicId, proj1Group.PublicId, proj2Group.PublicId, proj3Group.PublicId},
},
{
name: "org role grant children IDs only org children",
Copy link
Member

Choose a reason for hiding this comment

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

So descendants will also include items in the current scope which is org2Group. I expected only proj2Group and proj3Group to be returned just like GetGroup works with descendants

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, this is a bad test name. The role is in global with descendant access - calling list at org2 will return org2, proj2, proj3. AFAIK, there's no way to make a list exclude the "listing scope". You always get all the groups in ScopeId of the list call.

proj2Group := iam.TestGroup(t, conn, proj2.GetPublicId(), iam.WithDescription("proj2"), iam.WithName("proj2"))
proj3Group := iam.TestGroup(t, conn, proj3.GetPublicId(), iam.WithDescription("proj3"), iam.WithName("proj3"))

t.Run("List", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

We could add test coverage for:

  • List with grant string with specific scope id
  • Test permission error with type that is not a group

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added tests to cover both cases

},
},
{
name: "global_role_grant_all_specific_permissions",
Copy link
Member

Choose a reason for hiding this comment

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

This test and the previous are the same or? Unless I am missing the difference

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

another test I forgot to rename 🤦

},
},
{
name: "global_role_grant_all_specific_permissions",
Copy link
Member

Choose a reason for hiding this comment

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

The test name here is also the same as the last 2 tests. Not distinct enough

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch! my bad, will push a fix soon

t.Run("update", func(t *testing.T) {
testcases := []struct {
name string
setupScopesResourcesRoles func(t *testing.T, conn *db.DB, iamRepo *iam.Repository) (*iam.Group, []roleRequest)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why we can't pass only roleRequest and allow the for loop to create a test group to follow the pattern of other tests? is it because we want to create groups in different scopes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because in some cases we need ID of the group for testing resource-specific grant string

@bosorawis bosorawis marked this pull request as ready for review January 6, 2025 23:34
@bosorawis bosorawis force-pushed the bosorawis-prototype-grant-test branch from e44604f to 8e8eceb Compare January 7, 2025 01:21
Comment on lines 104 to 112
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
wrap := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrap)
repoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
s, err := groups.NewService(ctx, repoFn, 1000)
require.NoError(t, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We're performing this test setup in a few places -- we could reduce code by moving this into a shared function

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤔 We're using most of these setup objects throughout the test though. I'm not sure if we could abstract this away while keeping those available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants