-
Notifications
You must be signed in to change notification settings - Fork 73
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 tests for PublicViewer support #931
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package community_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/selection" | ||
waitpoll "k8s.io/apimachinery/pkg/util/wait" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
commonauth "github.com/codeready-toolchain/toolchain-common/pkg/test/auth" | ||
. "github.com/codeready-toolchain/toolchain-e2e/testsupport" | ||
authsupport "github.com/codeready-toolchain/toolchain-e2e/testsupport/auth" | ||
"github.com/codeready-toolchain/toolchain-e2e/testsupport/spacebinding" | ||
"github.com/codeready-toolchain/toolchain-e2e/testsupport/tiers" | ||
"github.com/codeready-toolchain/toolchain-e2e/testsupport/wait" | ||
) | ||
|
||
type proxyUser struct { | ||
expectedMemberCluster *wait.MemberAwaitility | ||
username string | ||
token string | ||
identityID uuid.UUID | ||
signup *toolchainv1alpha1.UserSignup | ||
compliantUsername string | ||
} | ||
|
||
// tests access to community-shared spaces | ||
func TestPublicViewerProxy(t *testing.T) { | ||
// given | ||
|
||
// make sure everything is ready before running the actual tests | ||
awaitilities := WaitForDeployments(t) | ||
hostAwait := awaitilities.Host() | ||
memberAwait := awaitilities.Member1() | ||
// we create a space to share , a new MUR and a SpaceBindingRequest | ||
space, _, _ := NewSpaceBindingRequest(t, awaitilities, memberAwait, hostAwait, "admin") | ||
|
||
communityUser := &proxyUser{ | ||
expectedMemberCluster: memberAwait, | ||
username: "community-user", | ||
identityID: uuid.Must(uuid.NewV4()), | ||
} | ||
createAppStudioUser(t, awaitilities, communityUser) | ||
|
||
communityUserProxyClient, err := hostAwait.CreateAPIProxyClient(t, communityUser.token, hostAwait.APIProxyURL) | ||
require.NoError(t, err) | ||
|
||
t.Run("space is flagged as community", func(t *testing.T) { | ||
// when | ||
sb := CreateCommunitySpaceBinding(t, hostAwait, space.Name, space.Namespace) | ||
require.NoError(t, err) | ||
t.Logf("created space binding for public-viewer:\n%+v", sb) | ||
|
||
// Wait until space is flagged as community | ||
require.NoError(t, | ||
waitpoll.Poll(hostAwait.RetryInterval, hostAwait.Timeout, func() (bool, error) { | ||
mr, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingMasterUserRecordLabelKey, selection.In, []string{"public-viewer"}) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
sr, err := labels.NewRequirement(toolchainv1alpha1.SpaceBindingSpaceLabelKey, selection.Equals, []string{space.Name}) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
opts := &client.ListOptions{ | ||
Namespace: space.Namespace, | ||
LabelSelector: labels.NewSelector().Add(*sr, *mr), | ||
} | ||
sbs := &toolchainv1alpha1.SpaceBindingList{} | ||
if err := hostAwait.Client.List(context.TODO(), sbs, opts); err != nil { | ||
return false, err | ||
} | ||
|
||
return len(sbs.Items) == 1, nil | ||
})) | ||
|
||
sp := toolchainv1alpha1.Space{} | ||
err = hostAwait.Client.Get(context.TODO(), client.ObjectKeyFromObject(space), &sp) | ||
require.NoError(t, err) | ||
|
||
/* | ||
Given Space exists for user A | ||
Given User community-user exists | ||
When A flags their space visibility to "community" | ||
Then community-user can view A's Space | ||
And community-user cannot create resources in A's Space | ||
*/ | ||
t.Run("community user access to community space", func(t *testing.T) { | ||
require.NotEmpty(t, sp.Status.ProvisionedNamespaces) | ||
|
||
t.Run("community user can list config maps from community space", func(t *testing.T) { | ||
// then | ||
cms := corev1.ConfigMapList{} | ||
|
||
communityUserProxyClient, err := hostAwait.CreateAPIProxyClient(t, communityUser.token, hostAwait.ProxyURLWithWorkspaceContext(sp.Name)) | ||
require.NoError(t, err) | ||
|
||
err = communityUserProxyClient.List(context.TODO(), &cms, client.InNamespace(sp.Status.ProvisionedNamespaces[0].Name)) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("community user cannot create config maps into space", func(t *testing.T) { | ||
cm := corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-cm", | ||
Namespace: sp.Status.ProvisionedNamespaces[0].Name, | ||
}, | ||
} | ||
err := communityUserProxyClient.Create(context.TODO(), &cm) | ||
require.Error(t, err) | ||
}) | ||
}) | ||
|
||
/* | ||
Given Space exists for user A | ||
Given SSO user joe exists | ||
When A flags their space visibility to "community" | ||
Then joe can view A's Space | ||
And joe cannot create resources in A's Space | ||
*/ | ||
t.Run("as sso user", func(t *testing.T) { | ||
// Given | ||
userIdentity := &commonauth.Identity{ | ||
ID: uuid.Must(uuid.NewV4()), | ||
Username: "joe", | ||
} | ||
claims := []commonauth.ExtraClaim{commonauth.WithEmailClaim("[email protected]")} | ||
token, err := authsupport.NewTokenFromIdentity(userIdentity, claims...) | ||
require.NoError(t, err) | ||
|
||
joeCli, err := hostAwait.CreateAPIProxyClient(t, token, hostAwait.ProxyURLWithWorkspaceContext(sp.Name)) | ||
require.NoError(t, err) | ||
|
||
t.Run("sso user can list config maps from space", func(t *testing.T) { | ||
// then | ||
cms := corev1.ConfigMapList{} | ||
err := joeCli.List(context.TODO(), &cms, client.InNamespace(sp.Status.ProvisionedNamespaces[0].Name)) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("sso user cannot create config maps into space", func(t *testing.T) { | ||
// then | ||
cm := corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-cm", | ||
Namespace: sp.Status.ProvisionedNamespaces[0].Name, | ||
}, | ||
} | ||
err := joeCli.Create(context.TODO(), &cm) | ||
require.Error(t, err) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func createAppStudioUser(t *testing.T, awaitilities wait.Awaitilities, user *proxyUser) { | ||
// Create and approve signup | ||
req := NewSignupRequest(awaitilities). | ||
Username(user.username). | ||
IdentityID(user.identityID). | ||
ManuallyApprove(). | ||
TargetCluster(user.expectedMemberCluster). | ||
EnsureMUR(). | ||
RequireConditions(wait.ConditionSet(wait.Default(), wait.ApprovedByAdmin())...). | ||
Execute(t) | ||
user.signup, _ = req.Resources() | ||
user.token = req.GetToken() | ||
tiers.MoveSpaceToTier(t, awaitilities.Host(), user.signup.Status.CompliantUsername, "appstudio") | ||
VerifyResourcesProvisionedForSignup(t, awaitilities, user.signup, "deactivate30", "appstudio") | ||
user.compliantUsername = user.signup.Status.CompliantUsername | ||
_, err := awaitilities.Host().WaitForMasterUserRecord(t, user.compliantUsername, wait.UntilMasterUserRecordHasCondition(wait.Provisioned())) | ||
require.NoError(t, err) | ||
} | ||
|
||
func CreateCommunitySpaceBinding( | ||
t *testing.T, | ||
hostAwait *wait.HostAwaitility, | ||
spaceName, spaceNamespace string, | ||
) *toolchainv1alpha1.SpaceBinding { | ||
return spacebinding.CreateSpaceBindingStr(t, hostAwait, "public-viewer", spaceName, spaceNamespace, "contributor") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package community_test | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
|
||
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
|
||
testspace "github.com/codeready-toolchain/toolchain-common/pkg/test/space" | ||
spacebindingrequesttestcommon "github.com/codeready-toolchain/toolchain-common/pkg/test/spacebindingrequest" | ||
. "github.com/codeready-toolchain/toolchain-e2e/testsupport" | ||
testsupportspace "github.com/codeready-toolchain/toolchain-e2e/testsupport/space" | ||
"github.com/codeready-toolchain/toolchain-e2e/testsupport/spacebinding" | ||
"github.com/codeready-toolchain/toolchain-e2e/testsupport/wait" | ||
) | ||
|
||
func NewSpaceBindingRequest( | ||
t *testing.T, | ||
awaitilities wait.Awaitilities, | ||
memberAwait *wait.MemberAwaitility, | ||
hostAwait *wait.HostAwaitility, | ||
spaceRole string, | ||
) ( | ||
*toolchainv1alpha1.Space, | ||
*toolchainv1alpha1.SpaceBindingRequest, | ||
*toolchainv1alpha1.SpaceBinding, | ||
) { | ||
space, firstUserSignup, _ := testsupportspace.CreateSpace(t, awaitilities, testspace.WithTierName("appstudio"), testspace.WithSpecTargetCluster(memberAwait.ClusterName)) | ||
// wait for the namespace to be provisioned since we will be creating the SpaceBindingRequest into it. | ||
space, err := hostAwait.WaitForSpace(t, space.Name, wait.UntilSpaceHasAnyProvisionedNamespaces()) | ||
require.NoError(t, err) | ||
// let's create a new MUR that will have access to the space | ||
username := uuid.Must(uuid.NewV4()).String() | ||
_, secondUserMUR := NewSignupRequest(awaitilities). | ||
Username(username). | ||
Email(username + "@acme.com"). | ||
ManuallyApprove(). | ||
TargetCluster(memberAwait). | ||
RequireConditions(wait.ConditionSet(wait.Default(), wait.ApprovedByAdmin())...). | ||
NoSpace(). | ||
WaitForMUR().Execute(t).Resources() | ||
// create the spacebinding request | ||
spaceBindingRequest := spacebinding.CreateSpaceBindingRequest(t, awaitilities, memberAwait.ClusterName, | ||
spacebinding.WithSpecSpaceRole(spaceRole), | ||
|
||
spacebinding.WithSpecMasterUserRecord(secondUserMUR.GetName()), | ||
spacebinding.WithNamespace(testsupportspace.GetDefaultNamespace(space.Status.ProvisionedNamespaces)), | ||
) | ||
|
||
// then | ||
// check for the spaceBinding creation | ||
spaceBinding, err := hostAwait.WaitForSpaceBinding(t, spaceBindingRequest.Spec.MasterUserRecord, space.Name, | ||
wait.UntilSpaceBindingHasMurName(spaceBindingRequest.Spec.MasterUserRecord), | ||
wait.UntilSpaceBindingHasSpaceName(space.Name), | ||
wait.UntilSpaceBindingHasSpaceRole(spaceBindingRequest.Spec.SpaceRole), | ||
wait.UntilSpaceBindingHasLabel(toolchainv1alpha1.SpaceBindingRequestLabelKey, spaceBindingRequest.GetName()), | ||
wait.UntilSpaceBindingHasLabel(toolchainv1alpha1.SpaceBindingRequestNamespaceLabelKey, spaceBindingRequest.GetNamespace()), | ||
) | ||
require.NoError(t, err) | ||
// wait for spacebinding request status | ||
spaceBindingRequest, err = memberAwait.WaitForSpaceBindingRequest(t, types.NamespacedName{Namespace: spaceBindingRequest.GetNamespace(), Name: spaceBindingRequest.GetName()}, | ||
wait.UntilSpaceBindingRequestHasConditions(spacebindingrequesttestcommon.Ready()), | ||
) | ||
require.NoError(t, err) | ||
tier, err := awaitilities.Host().WaitForNSTemplateTier(t, space.Spec.TierName) | ||
require.NoError(t, err) | ||
if spaceRole == "admin" { | ||
usernamesSorted := []string{firstUserSignup.Status.CompliantUsername, secondUserMUR.Name} | ||
sort.Strings(usernamesSorted) | ||
_, err = memberAwait.WaitForNSTmplSet(t, space.Name, | ||
wait.UntilNSTemplateSetHasSpaceRoles( | ||
wait.SpaceRole(tier.Spec.SpaceRoles[spaceRole].TemplateRef, usernamesSorted[0], usernamesSorted[1]))) | ||
require.NoError(t, err) | ||
} else { | ||
_, err = memberAwait.WaitForNSTmplSet(t, space.Name, | ||
wait.UntilNSTemplateSetHasSpaceRoles( | ||
wait.SpaceRole(tier.Spec.SpaceRoles["admin"].TemplateRef, firstUserSignup.Status.CompliantUsername), | ||
wait.SpaceRole(tier.Spec.SpaceRoles[spaceRole].TemplateRef, secondUserMUR.Name))) | ||
require.NoError(t, err) | ||
} | ||
testsupportspace.VerifyResourcesProvisionedForSpace(t, awaitilities, space.Name) | ||
return space, spaceBindingRequest, spaceBinding | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nit: What's the difference between
oc delete pods
(plural) andoc delete pod
(singular)? I'd prefer a convention that we stick to.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.
a typo 😄 good catch!