Skip to content

Update verification-code entrypoint #534

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

Merged
merged 4 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions pkg/controller/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,24 +602,73 @@ func initPhoneVerification(t *testing.T, handler gin.HandlerFunc, params gin.Par
func (s *TestSignupSuite) TestVerifyActivationCodeHandler() {

s.Run("verification successful", func() {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequiredAgo(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event")
fakeClient, application := testutil.PrepareInClusterApp(s.T(), userSignup, event)
ctrl := controller.NewSignup(application)
handler := gin.HandlerFunc(ctrl.VerifyActivationCodeHandler)

// when
rr := initActivationCodeVerification(s.T(), handler, userSignup.Name, event.Name)
s.Run("usersignup already exists", func() {
// given
userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequiredAgo(time.Second)) // just signed up
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event")
fakeClient, application := testutil.PrepareInClusterApp(s.T(), userSignup, event)
ctrl := controller.NewSignup(application)
handler := gin.HandlerFunc(ctrl.VerifyActivationCodeHandler)

// then
require.Equal(s.T(), http.StatusOK, rr.Code)
updatedUserSignup := &crtapi.UserSignup{}
err := fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), updatedUserSignup)
require.NoError(s.T(), err)
require.False(s.T(), states.VerificationRequired(updatedUserSignup))
require.Empty(s.T(), updatedUserSignup.Annotations[crtapi.UserVerificationAttemptsAnnotationKey])
require.Equal(s.T(), event.Name, updatedUserSignup.Labels[crtapi.SocialEventUserSignupLabelKey])
// when
rr := initActivationCodeVerification(s.T(), handler, userSignup.Name, event.Name)

// then
require.Equal(s.T(), http.StatusOK, rr.Code)
updatedUserSignup := &crtapi.UserSignup{}
err := fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), updatedUserSignup)
require.NoError(s.T(), err)
require.False(s.T(), states.VerificationRequired(updatedUserSignup))
require.Empty(s.T(), updatedUserSignup.Annotations[crtapi.UserVerificationAttemptsAnnotationKey])
require.Equal(s.T(), event.Name, updatedUserSignup.Labels[crtapi.SocialEventUserSignupLabelKey])
})

s.Run("usersignup already exists but it's deactivated", func() {
// given
// the user is deactivated
deactivatedUS := testusersignup.NewUserSignup(testusersignup.VerificationRequiredAgo(time.Second)) // just signed up
states.SetDeactivated(deactivatedUS, true)
deactivatedUS.Status.Conditions = fake.Deactivated()
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event")
fakeClient, application := testutil.PrepareInClusterApp(s.T(), deactivatedUS, event)
ctrl := controller.NewSignup(application)
handler := gin.HandlerFunc(ctrl.VerifyActivationCodeHandler)

// when
rr := initActivationCodeVerification(s.T(), handler, deactivatedUS.Name, event.Name)

// then
require.Equal(s.T(), http.StatusOK, rr.Code)
updatedUserSignup := &crtapi.UserSignup{}
err := fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(deactivatedUS), updatedUserSignup)
require.NoError(s.T(), err)
require.False(s.T(), states.VerificationRequired(updatedUserSignup))
require.Empty(s.T(), updatedUserSignup.Annotations[crtapi.UserVerificationAttemptsAnnotationKey])
require.Equal(s.T(), event.Name, updatedUserSignup.Labels[crtapi.SocialEventUserSignupLabelKey])
require.False(s.T(), states.VerificationRequired(updatedUserSignup)) // user is activated
require.False(s.T(), states.Deactivated(updatedUserSignup)) // user is activated
})

s.Run("usersignup doesn't exist it should be created", func() {
// given
event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event")
fakeClient, application := testutil.PrepareInClusterApp(s.T(), event)
ctrl := controller.NewSignup(application)
handler := gin.HandlerFunc(ctrl.VerifyActivationCodeHandler)

// when
rr := initActivationCodeVerification(s.T(), handler, "Jane", event.Name)

// then
require.Equal(s.T(), http.StatusOK, rr.Code)
createdUserSignup := &crtapi.UserSignup{}
err := fakeClient.Get(gocontext.TODO(), client.ObjectKey{Namespace: commontest.HostOperatorNs, Name: usersignup.EncodeUserIdentifier("Jane")}, createdUserSignup)
require.NoError(s.T(), err)
require.False(s.T(), states.VerificationRequired(createdUserSignup))
require.Empty(s.T(), createdUserSignup.Annotations[crtapi.UserVerificationAttemptsAnnotationKey])
require.Equal(s.T(), event.Name, createdUserSignup.Labels[crtapi.SocialEventUserSignupLabelKey])
})
})

s.Run("verification failed", func() {
Expand Down
23 changes: 4 additions & 19 deletions pkg/signup/service/signup_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (s *TestSignupServiceSuite) TestSignup() {
deactivatedUS.Annotations[toolchainv1alpha1.UserSignupActivationCounterAnnotationKey] = "2" // assume the user was activated 2 times already
deactivatedUS.Annotations[toolchainv1alpha1.UserSignupLastTargetClusterAnnotationKey] = "member-3" // assume the user was targeted to member-3
states.SetDeactivated(deactivatedUS, true)
deactivatedUS.Status.Conditions = deactivated()
deactivatedUS.Status.Conditions = fake.Deactivated()
fakeClient, application := testutil.PrepareInClusterApp(s.T(), deactivatedUS)

// when
Expand All @@ -144,7 +144,7 @@ func (s *TestSignupServiceSuite) TestSignup() {
// given
deactivatedUS := existing.DeepCopy()
states.SetDeactivated(deactivatedUS, true)
deactivatedUS.Status.Conditions = deactivated()
deactivatedUS.Status.Conditions = fake.Deactivated()
// also, alter the activation counter annotation
delete(deactivatedUS.Annotations, toolchainv1alpha1.UserSignupActivationCounterAnnotationKey)
delete(deactivatedUS.Annotations, toolchainv1alpha1.UserSignupLastTargetClusterAnnotationKey)
Expand All @@ -164,7 +164,7 @@ func (s *TestSignupServiceSuite) TestSignup() {
// given
deactivatedUS := existing.DeepCopy()
states.SetDeactivated(deactivatedUS, true)
deactivatedUS.Status.Conditions = deactivated()
deactivatedUS.Status.Conditions = fake.Deactivated()
fakeClient, application := testutil.PrepareInClusterApp(s.T(), deactivatedUS)
fakeClient.MockUpdate = func(ctx gocontext.Context, obj client.Object, opts ...client.UpdateOption) error {
if _, ok := obj.(*toolchainv1alpha1.UserSignup); ok && obj.GetName() == signupcommon.EncodeUserIdentifier("jsmith@kubesaw") {
Expand Down Expand Up @@ -698,7 +698,7 @@ func (s *TestSignupServiceSuite) TestGetSignupDeactivated() {
s.ServiceConfiguration(true, "", 5)

username, us := s.newUserSignupComplete()
us.Status.Conditions = deactivated()
us.Status.Conditions = fake.Deactivated()

_, application := testutil.PrepareInClusterApp(s.T(), us)

Expand Down Expand Up @@ -1346,21 +1346,6 @@ func (s *TestSignupServiceSuite) newSpaceBinding(murName, spaceName string) *too
return fake.NewSpaceBinding(name.String(), murName, spaceName, "admin")
}

func deactivated() []toolchainv1alpha1.Condition {
return []toolchainv1alpha1.Condition{
{
Type: toolchainv1alpha1.UserSignupComplete,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatedReason,
},
{
Type: toolchainv1alpha1.UserSignupApproved,
Status: apiv1.ConditionFalse,
Reason: toolchainv1alpha1.UserSignupUserDeactivatedReason,
},
}
}

type FakeCaptchaChecker struct {
score float32
result error
Expand Down
17 changes: 14 additions & 3 deletions pkg/verification/service/verification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

"github.com/codeready-toolchain/registration-service/pkg/namespaced"
signuppkg "github.com/codeready-toolchain/registration-service/pkg/signup"
signupsvc "github.com/codeready-toolchain/registration-service/pkg/signup/service"
"github.com/codeready-toolchain/registration-service/pkg/verification/sender"
signupcommon "github.com/codeready-toolchain/toolchain-common/pkg/usersignup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
Expand All @@ -26,7 +28,6 @@

"github.com/gin-gonic/gin"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand All @@ -41,6 +42,7 @@
namespaced.Client
HTTPClient *http.Client
NotificationService sender.NotificationSender
SignupService service.SignupService
}

type VerificationServiceOption func(svc *ServiceImpl)
Expand All @@ -54,6 +56,7 @@
return &ServiceImpl{
Client: client,
NotificationService: sender.CreateNotificationSender(httpClient),
SignupService: signupsvc.NewSignupService(client),
}
}

Expand Down Expand Up @@ -387,9 +390,16 @@
signup := &toolchainv1alpha1.UserSignup{}
if err := s.Get(gocontext.TODO(), s.NamespacedName(signupcommon.EncodeUserIdentifier(username)), signup); err != nil {
if apierrors.IsNotFound(err) {
return crterrors.NewNotFoundError(err, "user not found")
// signup user
signup, err = s.SignupService.Signup(ctx)
if err != nil {
log.Error(ctx, err, "error creating UserSignup resource")
return crterrors.NewInternalError(err, "error creating UserSignup resource")
}
log.Infof(ctx, "UserSignup created: %s", signup.Name)
} else {
return crterrors.NewInternalError(err, fmt.Sprintf("error retrieving usersignup with username '%s'", username))

Check warning on line 401 in pkg/verification/service/verification_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/verification/service/verification_service.go#L393-L401

Added lines #L393 - L401 were not covered by tests
}
return crterrors.NewInternalError(err, fmt.Sprintf("error retrieving usersignup with username '%s'", username))
}
annotationValues := map[string]string{}
annotationsToDelete := []string{}
Expand All @@ -404,6 +414,7 @@
}
if unsetVerificationRequired {
states.SetVerificationRequired(signup, false)
states.SetDeactivated(signup, false)
}
if signup.Annotations == nil {
signup.Annotations = map[string]string{}
Expand Down
21 changes: 21 additions & 0 deletions test/fake/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fake

import (
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
apiv1 "k8s.io/api/core/v1"
)

func Deactivated() []toolchainv1alpha1.Condition {
return []toolchainv1alpha1.Condition{
{
Type: toolchainv1alpha1.UserSignupComplete,
Status: apiv1.ConditionTrue,
Reason: toolchainv1alpha1.UserSignupUserDeactivatedReason,
},
{
Type: toolchainv1alpha1.UserSignupApproved,
Status: apiv1.ConditionFalse,
Reason: toolchainv1alpha1.UserSignupUserDeactivatedReason,
},
}
}
Loading