2020-08-10 17:15:02 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2020-06-11 16:36:47 -04:00
|
|
|
package iam
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-06 17:09:08 -04:00
|
|
|
"context"
|
2020-06-11 16:36:47 -04:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2023-03-10 15:06:39 -05:00
|
|
|
"github.com/hashicorp/boundary/globals"
|
2020-11-05 16:54:03 -05:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/types/scope"
|
2020-06-11 16:36:47 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Test_PublicIds(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
ctx := context.Background()
|
2020-06-15 12:42:08 -04:00
|
|
|
t.Run("role", func(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err := newRoleId(ctx)
|
2020-06-15 12:42:08 -04:00
|
|
|
require.NoError(t, err)
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix(id, globals.RolePrefix+"_"))
|
2020-06-15 12:42:08 -04:00
|
|
|
})
|
2020-06-11 16:36:47 -04:00
|
|
|
t.Run("user", func(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err := newUserId(ctx)
|
2020-06-11 16:36:47 -04:00
|
|
|
require.NoError(t, err)
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix(id, globals.UserPrefix+"_"))
|
2020-06-11 16:36:47 -04:00
|
|
|
})
|
|
|
|
|
t.Run("group", func(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err := newGroupId(ctx)
|
2020-06-11 16:36:47 -04:00
|
|
|
require.NoError(t, err)
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix(id, globals.GroupPrefix+"_"))
|
2020-06-11 16:36:47 -04:00
|
|
|
})
|
2021-06-04 09:44:57 -05:00
|
|
|
t.Run("oidc managed group", func(t *testing.T) {
|
2023-03-10 15:06:39 -05:00
|
|
|
assert.True(t, strings.HasPrefix("mgoidc_1234567890", globals.OidcManagedGroupPrefix+"_"))
|
2021-06-04 09:44:57 -05:00
|
|
|
})
|
2020-06-12 15:22:23 -04:00
|
|
|
t.Run("scopes", func(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err := newScopeId(ctx, scope.Org)
|
2020-06-12 15:22:23 -04:00
|
|
|
require.NoError(t, err)
|
2020-07-11 12:00:49 -04:00
|
|
|
assert.True(t, strings.HasPrefix(id, scope.Org.Prefix()))
|
2020-06-12 15:22:23 -04:00
|
|
|
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err = newScopeId(ctx, scope.Project)
|
2020-06-12 15:22:23 -04:00
|
|
|
require.NoError(t, err)
|
2020-06-26 11:00:14 -04:00
|
|
|
assert.True(t, strings.HasPrefix(id, scope.Project.Prefix()))
|
2020-06-12 15:22:23 -04:00
|
|
|
|
2023-06-06 17:09:08 -04:00
|
|
|
id, err = newScopeId(ctx, scope.Unknown)
|
2020-06-12 15:22:23 -04:00
|
|
|
require.Error(t, err)
|
|
|
|
|
assert.Empty(t, id)
|
2020-12-23 11:00:00 -08:00
|
|
|
assert.True(t, errors.Match(errors.T(errors.InvalidParameter), err))
|
2020-06-12 15:22:23 -04:00
|
|
|
})
|
2020-06-11 16:36:47 -04:00
|
|
|
}
|