diff --git a/testing/internal/e2e/tests/base/auth_method_password_test.go b/testing/internal/e2e/tests/base/auth_method_password_test.go index 080ec5c43..47f49ac90 100644 --- a/testing/internal/e2e/tests/base/auth_method_password_test.go +++ b/testing/internal/e2e/tests/base/auth_method_password_test.go @@ -6,13 +6,17 @@ package base_test import ( "context" "encoding/json" + "slices" "testing" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/boundary/api/authmethods" "github.com/hashicorp/boundary/api/scopes" "github.com/hashicorp/boundary/api/users" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -136,3 +140,172 @@ func TestCliAuthMethodPassword(t *testing.T) { ) require.NoError(t, output.Err, string(output.Stderr)) } + +// TestCliPaginateAuthMethods asserts that the CLI automatically paginates to retrieve +// all auth methods in a single invocation. +func TestCliPaginateAuthMethods(t *testing.T) { + e2e.MaybeSkipTest(t) + c, err := loadTestConfig() + require.NoError(t, err) + + ctx := context.Background() + boundary.AuthenticateAdminCli(t, ctx) + client, err := boundary.NewApiClient() + require.NoError(t, err) + newOrgId := boundary.CreateNewOrgCli(t, ctx) + t.Cleanup(func() { + ctx := context.Background() + boundary.AuthenticateAdminCli(t, ctx) + output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", newOrgId)) + require.NoError(t, output.Err, string(output.Stderr)) + }) + + // Create enough auth methods to overflow a single page. + var authMethodIds []string + for i := 0; i < c.MaxPageSize+1; i++ { + authMethodId := boundary.CreateNewAuthMethodApi(t, ctx, client, newOrgId) + authMethodIds = append(authMethodIds, authMethodId) + } + + // List auth methods + output := e2e.RunCommand(ctx, "boundary", + e2e.WithArgs( + "auth-methods", "list", + "-scope-id", newOrgId, + "-format=json", + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) + var initialAuthMethods authmethods.AuthMethodListResult + err = json.Unmarshal(output.Stdout, &initialAuthMethods) + require.NoError(t, err) + + var returnedIds []string + for _, authMethod := range initialAuthMethods.Items { + returnedIds = append(returnedIds, authMethod.Id) + } + require.Len(t, initialAuthMethods.Items, c.MaxPageSize+1) + assert.Empty(t, cmp.Diff(returnedIds, authMethodIds, cmpopts.SortSlices(func(i, j string) bool { return i < j }))) + assert.Empty(t, initialAuthMethods.ResponseType) + assert.Empty(t, initialAuthMethods.RemovedIds) + assert.Empty(t, initialAuthMethods.ListToken) + + // Create a new auth method and destroy one of the other auth methods + newAuthMethodId := boundary.CreateNewAuthMethodApi(t, ctx, client, newOrgId) + output = e2e.RunCommand(ctx, "boundary", + e2e.WithArgs( + "auth-methods", "delete", + "-id", initialAuthMethods.Items[0].Id, + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) + + // List auth methods again, should have new one but not old one + output = e2e.RunCommand(ctx, "boundary", + e2e.WithArgs( + "auth-methods", "list", + "-scope-id", newOrgId, + "-format=json", + ), + ) + require.NoError(t, output.Err, string(output.Stderr)) + + var newAuthMethods authmethods.AuthMethodListResult + err = json.Unmarshal(output.Stdout, &newAuthMethods) + require.NoError(t, err) + + require.Len(t, newAuthMethods.Items, c.MaxPageSize+1) + // The first item should be the most recently created, + // which should be the new auth method + firstItem := newAuthMethods.Items[0] + assert.Equal(t, newAuthMethodId, firstItem.Id) + assert.Empty(t, initialAuthMethods.ResponseType) + assert.Empty(t, initialAuthMethods.RemovedIds) + assert.Empty(t, initialAuthMethods.ListToken) + // Ensure the deleted auth method isn't returned + for _, authMethod := range newAuthMethods.Items { + assert.NotEqual(t, initialAuthMethods.Items[0].Id, authMethod.Id) + } +} + +// TestApiPaginateAuthMethods asserts that the API automatically paginates to retrieve +// all auth methods in a single invocation. +func TestApiPaginateAuthMethods(t *testing.T) { + e2e.MaybeSkipTest(t) + c, err := loadTestConfig() + require.NoError(t, err) + + client, err := boundary.NewApiClient() + require.NoError(t, err) + adminToken := client.Token() + ctx := context.Background() + sClient := scopes.NewClient(client) + amClient := authmethods.NewClient(client) + newOrgId := boundary.CreateNewOrgApi(t, ctx, client) + t.Cleanup(func() { + ctx := context.Background() + client.SetToken(adminToken) + _, err = sClient.Delete(ctx, newOrgId) + require.NoError(t, err) + }) + + // Create enough auth methods to overflow a single page. + var authMethodIds []string + for i := 0; i < c.MaxPageSize+1; i++ { + authMethodIds = append(authMethodIds, boundary.CreateNewAuthMethodApi(t, ctx, client, newOrgId)) + } + + // List auth methods + initialAuthMethods, err := amClient.List(ctx, newOrgId) + require.NoError(t, err) + + var returnedIds []string + for _, authMethod := range initialAuthMethods.Items { + returnedIds = append(returnedIds, authMethod.Id) + } + + require.Len(t, initialAuthMethods.Items, c.MaxPageSize+1) + assert.Empty(t, cmp.Diff(returnedIds, authMethodIds, cmpopts.SortSlices(func(i, j string) bool { return i < j }))) + assert.Equal(t, "complete", initialAuthMethods.ResponseType) + assert.Empty(t, initialAuthMethods.RemovedIds) + assert.NotEmpty(t, initialAuthMethods.ListToken) + mapItems, ok := initialAuthMethods.GetResponse().Map["items"] + require.True(t, ok) + mapSliceItems, ok := mapItems.([]any) + require.True(t, ok) + assert.Len(t, mapSliceItems, c.MaxPageSize+1) + + // Create a new auth method and destroy one of the other auth methods + newAuthMethodId := boundary.CreateNewAuthMethodApi(t, ctx, client, newOrgId) + _, err = amClient.Delete(ctx, initialAuthMethods.Items[0].Id) + require.NoError(t, err) + + // List auth methods again, should have new one but not old one + newAuthMethods, err := amClient.List(ctx, newOrgId, authmethods.WithListToken(initialAuthMethods.ListToken)) + require.NoError(t, err) + + // Note that this will likely contain all the auth methods, + // since they were created very shortly before the listing, + // and we add a 30 second buffer to the lower bound of update + // times when listing. + require.GreaterOrEqual(t, len(newAuthMethods.Items), 1) + // The first item should be the most recently created, which + // should be our new auth method + firstItem := newAuthMethods.Items[0] + assert.Equal(t, newAuthMethodId, firstItem.Id) + assert.Equal(t, "complete", newAuthMethods.ResponseType) + // Note that the removed IDs may contain entries from other tests, + // so just check that there is at least 1 entry and that our entry + // is somewhere in the list. + require.GreaterOrEqual(t, len(newAuthMethods.RemovedIds), 1) + assert.True(t, slices.ContainsFunc(newAuthMethods.RemovedIds, func(authMethodId string) bool { + return authMethodId == initialAuthMethods.Items[0].Id + })) + assert.NotEmpty(t, newAuthMethods.ListToken) + // Check that the response map contains all entries + mapItems, ok = newAuthMethods.GetResponse().Map["items"] + require.True(t, ok) + mapSliceItems, ok = mapItems.([]any) + require.True(t, ok) + assert.GreaterOrEqual(t, len(mapSliceItems), 1) +}