Skip to content

Commit

Permalink
Set target cluster for social events (#454)
Browse files Browse the repository at this point in the history
* Set target cluster for social events
  • Loading branch information
alexeykazakov authored Aug 16, 2024
1 parent b123fb6 commit 28556e0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.20

require (
github.com/aws/aws-sdk-go v1.44.100
github.com/codeready-toolchain/api v0.0.0-20240708122235-0af5a9a178bb
github.com/codeready-toolchain/toolchain-common v0.0.0-20240716065433-8604fe46b96a
github.com/codeready-toolchain/api v0.0.0-20240815232340-d0c164a83d27
github.com/codeready-toolchain/toolchain-common v0.0.0-20240816011540-2184e6268b4a
github.com/go-logr/logr v1.4.1
github.com/gofrs/uuid v4.2.0+incompatible
github.com/pkg/errors v0.9.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtM
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/codeready-toolchain/api v0.0.0-20240708122235-0af5a9a178bb h1:Wc9CMsv0ODZv9dM5qF3OI0mFDO95YNIXV/8oRvoz8aE=
github.com/codeready-toolchain/api v0.0.0-20240708122235-0af5a9a178bb/go.mod h1:ie9p4LenCCS0LsnbWp6/xwpFDdCWYE0KWzUO6Sk1g0E=
github.com/codeready-toolchain/toolchain-common v0.0.0-20240716065433-8604fe46b96a h1:HcaJtZCLfYkWZCxIa3iTvq3zgn711JGqPLkunBTfGSc=
github.com/codeready-toolchain/toolchain-common v0.0.0-20240716065433-8604fe46b96a/go.mod h1:8M9k7w2VSyRKSK6P08Jo2ddW3uyGgxCcSitnYa3HK9o=
github.com/codeready-toolchain/api v0.0.0-20240815232340-d0c164a83d27 h1:uEH8HAM81QZBccuqQpGKJUoJQe28+DFSYi/mRKZDYrA=
github.com/codeready-toolchain/api v0.0.0-20240815232340-d0c164a83d27/go.mod h1:ie9p4LenCCS0LsnbWp6/xwpFDdCWYE0KWzUO6Sk1g0E=
github.com/codeready-toolchain/toolchain-common v0.0.0-20240816011540-2184e6268b4a h1:o18wLp3eT4HdH8TvDqtLWiC47WY/kaTp9p54exux/MU=
github.com/codeready-toolchain/toolchain-common v0.0.0-20240816011540-2184e6268b4a/go.mod h1:aIbki5CFsykeqZn2/ZwvUb3Krx2f2Tbq58R6MGnk6H8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
5 changes: 5 additions & 0 deletions pkg/verification/service/verification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ func (s *ServiceImpl) VerifyActivationCode(ctx *gin.Context, userID, username, c
annotationValues := map[string]string{}
annotationsToDelete := []string{}
unsetVerificationRequired := false
targetCluster := ""

defer func() {
doUpdate := func() error {
Expand All @@ -418,6 +419,9 @@ func (s *ServiceImpl) VerifyActivationCode(ctx *gin.Context, userID, username, c
signup.Labels = map[string]string{}
}
signup.Labels[toolchainv1alpha1.SocialEventUserSignupLabelKey] = code
if targetCluster != "" {
signup.Spec.TargetCluster = targetCluster
}
_, err = s.Services().SignupService().UpdateUserSignup(signup)
if err != nil {
return err
Expand Down Expand Up @@ -456,6 +460,7 @@ func (s *ServiceImpl) VerifyActivationCode(ctx *gin.Context, userID, username, c
log.Infof(ctx, "the event with code '%s' has not started yet or is already past", code)
return crterrors.NewForbiddenError("invalid code", "the provided code is invalid")
}
targetCluster = event.Spec.TargetCluster
log.Infof(ctx, "approving user signup request with activation code '%s'", code)
// If the activation code is acceptable then set `VerificationRequired` state to false and reset other verification annotations
unsetVerificationRequired = true
Expand Down
30 changes: 20 additions & 10 deletions pkg/verification/service/verification_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,11 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() {
}

func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
s.testVerifyActivationCode("")
s.testVerifyActivationCode("member-1")
}

func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster string) {
// given

cfg := configuration.GetRegistrationServiceConfig()
Expand All @@ -1042,7 +1046,7 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
s.T().Run("verification ok", func(t *testing.T) {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event")
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithTargetCluster(targetCluster))
err := s.setupFakeClients(userSignup, event)
require.NoError(t, err)

Expand All @@ -1054,12 +1058,13 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
userSignup, err = s.FakeUserSignupClient.Get(userSignup.Name)
require.NoError(t, err)
require.False(t, states.VerificationRequired(userSignup))
assert.Equal(t, targetCluster, userSignup.Spec.TargetCluster)
})

s.T().Run("last user to signup", func(t *testing.T) {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithActivationCount(9)) // one seat left
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithActivationCount(9), testsocialevent.WithTargetCluster(targetCluster)) // one seat left
err := s.setupFakeClients(userSignup, event)
require.NoError(t, err)

Expand All @@ -1071,14 +1076,15 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
userSignup, err = s.FakeUserSignupClient.Get(userSignup.Name)
require.NoError(t, err)
require.False(t, states.VerificationRequired(userSignup))
assert.Equal(t, targetCluster, userSignup.Spec.TargetCluster)
})

s.T().Run("when too many attempts made", func(t *testing.T) {
// given
userSignup := testusersignup.NewUserSignup(
testusersignup.VerificationRequired(time.Second), // just signed up
testusersignup.WithVerificationAttempts(cfg.Verification().AttemptsAllowed()))
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event")
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithTargetCluster(targetCluster))
err := s.setupFakeClients(userSignup, event)
require.NoError(t, err)

Expand All @@ -1090,6 +1096,7 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
userSignup, err = s.FakeUserSignupClient.Get(userSignup.Name)
require.NoError(t, err)
require.True(t, states.VerificationRequired(userSignup)) // unchanged
assert.Empty(t, userSignup.Spec.TargetCluster)
})

s.T().Run("when invalid code", func(t *testing.T) {
Expand Down Expand Up @@ -1133,8 +1140,8 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {

s.T().Run("when max attendees reached", func(t *testing.T) {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithActivationCount(10)) // same as default `spec.MaxAttendees`
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithActivationCount(10), testsocialevent.WithTargetCluster(targetCluster)) // same as default `spec.MaxAttendees`
err := s.setupFakeClients(userSignup, event)
require.NoError(t, err)

Expand All @@ -1147,12 +1154,13 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
require.NoError(t, err)
require.True(t, states.VerificationRequired(userSignup))
assert.Equal(t, "1", userSignup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented
assert.Empty(t, userSignup.Spec.TargetCluster)
})

s.T().Run("when event not open yet", func(t *testing.T) {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithStartTime(time.Now().Add(time.Hour))) // starting in 1hr
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithStartTime(time.Now().Add(time.Hour)), testsocialevent.WithTargetCluster(targetCluster)) // starting in 1hr
err := s.setupFakeClients(userSignup, event)
require.NoError(t, err)

Expand All @@ -1165,12 +1173,13 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
require.NoError(t, err)
require.True(t, states.VerificationRequired(userSignup))
assert.Equal(t, "1", userSignup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented
assert.Empty(t, userSignup.Spec.TargetCluster)
})

s.T().Run("when event already closed", func(t *testing.T) {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithEndTime(time.Now().Add(-time.Hour))) // ended 1hr ago
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithEndTime(time.Now().Add(-time.Hour)), testsocialevent.WithTargetCluster(targetCluster)) // ended 1hr ago
err := s.setupFakeClients(userSignup, event)
require.NoError(t, err)

Expand All @@ -1183,6 +1192,7 @@ func (s *TestVerificationServiceSuite) TestVerifyActivationCode() {
require.NoError(t, err)
require.True(t, states.VerificationRequired(userSignup))
assert.Equal(t, "1", userSignup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented
assert.Empty(t, userSignup.Spec.TargetCluster)
})
}

Expand Down

0 comments on commit 28556e0

Please sign in to comment.