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 1 commit
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
56 changes: 40 additions & 16 deletions pkg/controller/signup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
testconfig "github.com/codeready-toolchain/toolchain-common/pkg/test/config"
testsocialevent "github.com/codeready-toolchain/toolchain-common/pkg/test/socialevent"
testusersignup "github.com/codeready-toolchain/toolchain-common/pkg/test/usersignup"
"github.com/codeready-toolchain/toolchain-common/pkg/usersignup"

Check failure on line 30 in pkg/controller/signup_test.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

ST1019: package "github.com/codeready-toolchain/toolchain-common/pkg/usersignup" is being imported more than once (staticcheck)
signupcommon "github.com/codeready-toolchain/toolchain-common/pkg/usersignup"

Check failure on line 31 in pkg/controller/signup_test.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

ST1019(related information): other import of "github.com/codeready-toolchain/toolchain-common/pkg/usersignup" (staticcheck)
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -602,24 +603,47 @@
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 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: signupcommon.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
20 changes: 17 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,20 @@
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")
}
if _, exists := signup.Annotations[toolchainv1alpha1.UserSignupActivationCounterAnnotationKey]; !exists {
log.Infof(ctx, "UserSignup created: %s", signup.Name)
} else {
log.Infof(ctx, "UserSignup reactivated: %s", signup.Name)
}
} else {
return crterrors.NewInternalError(err, fmt.Sprintf("error retrieving usersignup with username '%s'", username))

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L393 - L405 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 Down
Loading