From 258c17cc724343a8a540fb9bb34f2f97e7daedd6 Mon Sep 17 00:00:00 2001 From: Matous Jobanek Date: Tue, 19 Nov 2024 16:06:42 +0100 Subject: [PATCH] make go linter 1.60.3 happy (#485) --- .github/workflows/ci-golang-sbom.yml | 2 +- .golangci.yml | 1 + .yamllint | 1 + pkg/auth/tokenparser_test.go | 8 +- pkg/proxy/proxy_community_test.go | 6 +- pkg/proxy/proxy_test.go | 56 +++---- pkg/server/server_test.go | 4 +- pkg/signup/service/signup_service_test.go | 90 +++++------ .../service/verification_service_test.go | 150 +++++++++--------- 9 files changed, 160 insertions(+), 158 deletions(-) diff --git a/.github/workflows/ci-golang-sbom.yml b/.github/workflows/ci-golang-sbom.yml index ed795956..ea4b61b0 100644 --- a/.github/workflows/ci-golang-sbom.yml +++ b/.github/workflows/ci-golang-sbom.yml @@ -25,7 +25,7 @@ jobs: - name: Lint uses: golangci/golangci-lint-action@v6 with: - version: v1.56.2 + version: v1.60.3 skip-pkg-cache: true skip-build-cache: true args: --config=./.golangci.yml --verbose diff --git a/.golangci.yml b/.golangci.yml index d69f4890..14444e70 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,6 +14,7 @@ linters: - gofmt - revive - misspell + - gosec presets: # groups of linters. See https://golangci-lint.run/usage/linters/ - bugs - unused diff --git a/.yamllint b/.yamllint index 32a19ef3..89fa3ce3 100644 --- a/.yamllint +++ b/.yamllint @@ -1,5 +1,6 @@ ignore: | /vendor + .git rules: braces: enable diff --git a/pkg/auth/tokenparser_test.go b/pkg/auth/tokenparser_test.go index 98e5c7b4..5522011e 100644 --- a/pkg/auth/tokenparser_test.go +++ b/pkg/auth/tokenparser_test.go @@ -362,17 +362,17 @@ func (s *TestTokenParserSuite) TestTokenParser() { } for k, tc := range tests { - s.T().Run(k, func(t *testing.T) { + s.Run(k, func() { // generate non-serialized token jwt0 := tokengenerator.GenerateToken(*identity0, kid0, authsupport.WithEmailClaim(email0), authsupport.WithAudClaim(tc.aud)) // serialize jwt0string, err := tokengenerator.SignToken(jwt0, kid0) - require.NoError(t, err) + require.NoError(s.T(), err) // validate token parsed, err := tokenParser.FromString(jwt0string) - require.NoError(t, err) - require.Equal(t, jwt.ClaimStrings(tc.aud), parsed.Audience) + require.NoError(s.T(), err) + require.Equal(s.T(), jwt.ClaimStrings(tc.aud), parsed.Audience) }) } }) diff --git a/pkg/proxy/proxy_community_test.go b/pkg/proxy/proxy_community_test.go index 92b9eee7..c2170d7a 100644 --- a/pkg/proxy/proxy_community_test.go +++ b/pkg/proxy/proxy_community_test.go @@ -392,16 +392,16 @@ func (s *TestProxySuite) checkProxyCommunityOK(proxy *Proxy, port string) { // given testServer.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { v := testServerInvoked.Swap(true) - require.False(s.T(), v, "expected handler to be invoked just one time") + assert.False(s.T(), v, "expected handler to be invoked just one time") w.Header().Set("Content-Type", "application/json") // Set the Access-Control-Allow-Origin header to make sure it's overridden by the proxy response modifier w.Header().Set("Access-Control-Allow-Origin", "dummy") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte(httpTestServerResponse)) - require.NoError(s.T(), err) + assert.NoError(s.T(), err) for hk, hv := range tc.ExpectedAPIServerRequestHeaders { - require.Len(s.T(), r.Header.Values(hk), len(hv)) + assert.Len(s.T(), r.Header.Values(hk), len(hv)) for i := range hv { assert.Equal(s.T(), hv[i], r.Header.Values(hk)[i], "header %s", hk) } diff --git a/pkg/proxy/proxy_test.go b/pkg/proxy/proxy_test.go index 3976914a..27c3d111 100644 --- a/pkg/proxy/proxy_test.go +++ b/pkg/proxy/proxy_test.go @@ -425,13 +425,13 @@ func (s *TestProxySuite) checkWebLogin() { switch p := r.URL.Path; p { case "/auth/realms/sandbox-dev/.well-known/openid-configuration": _, err := w.Write([]byte("mock SSO configuration")) - require.NoError(s.T(), err) + assert.NoError(s.T(), err) case "/auth/anything": _, err := w.Write([]byte("mock auth")) - require.NoError(s.T(), err) + assert.NoError(s.T(), err) default: _, err := w.Write([]byte("unknown")) - require.NoError(s.T(), err) + assert.NoError(s.T(), err) } })) defer testServer.Close() @@ -506,7 +506,7 @@ func (s *TestProxySuite) checkProxyOK(proxy *Proxy) { w.Header().Set("Access-Control-Allow-Origin", "dummy") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte("my response")) - require.NoError(s.T(), err) + assert.NoError(s.T(), err) })) defer testServer.Close() @@ -798,9 +798,9 @@ func (s *TestProxySuite) checkProxyOK(proxy *Proxy) { w.Header().Set("Access-Control-Allow-Origin", "dummy") w.WriteHeader(http.StatusOK) _, err := w.Write([]byte("my response")) - require.NoError(s.T(), err) + assert.NoError(s.T(), err) for hk, hv := range tc.ExpectedAPIServerRequestHeaders { - require.Len(s.T(), r.Header.Values(hk), len(hv)) + assert.Len(s.T(), r.Header.Values(hk), len(hv)) for i := range hv { assert.Equal(s.T(), hv[i], r.Header.Values(hk)[i]) } @@ -885,7 +885,7 @@ func (s *TestProxySuite) checkProxyOK(proxy *Proxy) { s.assertResponseBody(resp, "my response") } for hk, hv := range tc.ExpectedProxyResponseHeaders { - require.Len(s.T(), resp.Header.Values(hk), len(hv), fmt.Sprintf("Actual Header %s: %v", hk, resp.Header.Values(hk))) + require.Lenf(s.T(), resp.Header.Values(hk), len(hv), "Actual Header %s: %v", hk, resp.Header.Values(hk)) for i := range hv { assert.Equal(s.T(), hv[i], resp.Header.Values(hk)[i]) } @@ -1091,7 +1091,7 @@ func (s *TestProxySuite) TestGetWorkspaceContext() { } for k, tc := range tests { - s.T().Run(k, func(t *testing.T) { + s.Run(k, func() { req := &http.Request{ URL: &url.URL{ Path: tc.path, @@ -1099,13 +1099,13 @@ func (s *TestProxySuite) TestGetWorkspaceContext() { } proxy, workspace, err := getWorkspaceContext(req) if tc.expectedErr == "" { - require.NoError(t, err, fmt.Sprintf("failed for tc %s", k)) + require.NoErrorf(s.T(), err, "failed for tc %s", k) } else { - require.EqualError(t, err, tc.expectedErr, fmt.Sprintf("failed for tc %s", k)) + require.EqualErrorf(s.T(), err, tc.expectedErr, "failed for tc %s", k) } - assert.Equal(t, tc.expectedWorkspace, workspace, fmt.Sprintf("failed for tc %s", k)) - assert.Equal(t, tc.expectedPath, req.URL.Path, fmt.Sprintf("failed for tc %s", k)) - assert.Equal(t, tc.expectedPlugin, proxy, fmt.Sprintf("failed for tc %s", k)) + assert.Equalf(s.T(), tc.expectedWorkspace, workspace, "failed for tc %s", k) + assert.Equalf(s.T(), tc.expectedPath, req.URL.Path, "failed for tc %s", k) + assert.Equalf(s.T(), tc.expectedPlugin, proxy, "failed for tc %s", k) }) } } @@ -1173,12 +1173,12 @@ func (s *TestProxySuite) TestValidateWorkspaceRequest() { } for k, tc := range tests { - s.T().Run(k, func(t *testing.T) { + s.Run(k, func() { err := validateWorkspaceRequest(tc.requestedWorkspace, tc.workspaces...) if tc.expectedErr == "" { - require.NoError(t, err) + require.NoError(s.T(), err) } else { - require.EqualError(t, err, tc.expectedErr) + require.EqualError(s.T(), err, tc.expectedErr) } }) } @@ -1186,9 +1186,9 @@ func (s *TestProxySuite) TestValidateWorkspaceRequest() { func (s *TestProxySuite) TestGetTransport() { - s.T().Run("when not prod", func(_ *testing.T) { + s.Run("when not prod", func() { for _, envName := range []testconfig.EnvName{testconfig.E2E, testconfig.Dev} { - s.T().Run("env "+string(envName), func(t *testing.T) { + s.Run("env "+string(envName), func() { // given env := s.DefaultConfig().Environment() defer s.SetConfig(testconfig.RegistrationService(). @@ -1204,12 +1204,12 @@ func (s *TestProxySuite) TestGetTransport() { expectedTransport.TLSClientConfig = &tls.Config{ InsecureSkipVerify: true, // nolint:gosec } - assertTransport(t, expectedTransport, transport) + assertTransport(s.T(), expectedTransport, transport) }) } }) - s.T().Run("for prod", func(_ *testing.T) { + s.Run("for prod", func() { // given env := s.DefaultConfig().Environment() defer s.SetConfig(testconfig.RegistrationService(). @@ -1217,7 +1217,7 @@ func (s *TestProxySuite) TestGetTransport() { s.SetConfig(testconfig.RegistrationService(). Environment(string(testconfig.Prod))) - s.T().Run("upgrade header is set to 'SPDY/3.1'", func(t *testing.T) { + s.Run("upgrade header is set to 'SPDY/3.1'", func() { // when transport := getTransport(map[string][]string{ "Connection": {"Upgrade"}, @@ -1229,10 +1229,10 @@ func (s *TestProxySuite) TestGetTransport() { expectedTransport.TLSClientConfig.NextProtos = []string{"http/1.1"} expectedTransport.ForceAttemptHTTP2 = false - assertTransport(t, expectedTransport, transport) + assertTransport(s.T(), expectedTransport, transport) }) - s.T().Run("upgrade header is set to 'websocket'", func(t *testing.T) { + s.Run("upgrade header is set to 'websocket'", func() { // when transport := getTransport(map[string][]string{ "Connection": {"Upgrade"}, @@ -1240,19 +1240,19 @@ func (s *TestProxySuite) TestGetTransport() { }) // then - assertTransport(t, noTimeoutDefaultTransport(), transport) + assertTransport(s.T(), noTimeoutDefaultTransport(), transport) }) - s.T().Run("no upgrade header is set", func(t *testing.T) { + s.Run("no upgrade header is set", func() { // when transport := getTransport(map[string][]string{}) // then - assertTransport(t, noTimeoutDefaultTransport(), transport) + assertTransport(s.T(), noTimeoutDefaultTransport(), transport) }) }) - s.T().Run("default transport should be same except for DailContext", func(t *testing.T) { + s.Run("default transport should be same except for DailContext", func() { // when transport := http.DefaultTransport.(interface { Clone() *http.Transport @@ -1260,7 +1260,7 @@ func (s *TestProxySuite) TestGetTransport() { transport.DialContext = noTimeoutDialerProxy // then - assertTransport(t, noTimeoutDefaultTransport(), transport) + assertTransport(s.T(), noTimeoutDefaultTransport(), transport) }) } diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 084811b5..f1104ad9 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -62,11 +62,11 @@ func (s *TestServerSuite) TestServer() { }, } - s.T().Run("CORS", func(t *testing.T) { + s.Run("CORS", func() { go func(t *testing.T) { err := srv.Engine().Run() assert.NoError(t, err) // require must only be used in the goroutine running the test function (testifylint) - }(t) + }(s.T()) err := wait.PollUntilContextTimeout(context.TODO(), DefaultRetryInterval, DefaultTimeout, false, func(context.Context) (done bool, err error) { req, err := http.NewRequest("GET", "http://localhost:8080/api/v1/health", nil) diff --git a/pkg/signup/service/signup_service_test.go b/pkg/signup/service/signup_service_test.go index ab79cc1b..8ff98002 100644 --- a/pkg/signup/service/signup_service_test.go +++ b/pkg/signup/service/signup_service_test.go @@ -773,7 +773,7 @@ func (s *TestSignupServiceSuite) TestGetSignupStatusNotComplete() { assert.Empty(s.T(), response.StartDate) assert.Empty(s.T(), response.EndDate) - s.T().Run("with no check for UserSignup complete condition", func(t *testing.T) { + s.Run("with no check for UserSignup complete condition", func() { // given states.SetVerificationRequired(userSignupNotComplete, false) mur := s.newProvisionedMUR("bill") @@ -781,7 +781,7 @@ func (s *TestSignupServiceSuite) TestGetSignupStatusNotComplete() { spacebinding := s.newSpaceBinding(mur.Name, space.Name) toolchainStatus := s.newToolchainStatus(".apps.") - fakeClient := commontest.NewFakeClient(t, userSignupNotComplete, mur, space, spacebinding, toolchainStatus) + fakeClient := commontest.NewFakeClient(s.T(), userSignupNotComplete, mur, space, spacebinding, toolchainStatus) svc := service.NewSignupService(namespaced.NewClient(fakeClient, commontest.HostOperatorNs)) // when @@ -789,23 +789,23 @@ func (s *TestSignupServiceSuite) TestGetSignupStatusNotComplete() { response, err := svc.GetSignup(c, userID.String(), userSignupNotComplete.Spec.IdentityClaims.PreferredUsername, false) // then - require.NoError(t, err) - require.NotNil(t, response) - - require.Equal(t, userID.String(), response.Name) - require.Equal(t, "bill", response.Username) - require.Equal(t, "bill", response.CompliantUsername) - require.True(t, response.Status.Ready) - require.Equal(t, "mur_ready_reason", response.Status.Reason) - require.Equal(t, "mur_ready_message", response.Status.Message) - require.False(t, response.Status.VerificationRequired) - require.Equal(t, "https://console.apps.member-123.com", response.ConsoleURL) - require.Equal(t, "http://che-toolchain-che.member-123.com", response.CheDashboardURL) - require.Equal(t, "http://api.devcluster.openshift.com", response.APIEndpoint) - require.Equal(t, "member-123", response.ClusterName) - require.Equal(t, "https://proxy-url.com", response.ProxyURL) - assert.Equal(t, "bill-dev", response.DefaultUserNamespace) - assert.Equal(t, "https://rhods-dashboard-redhat-ods-applications.apps.member-123.com", response.RHODSMemberURL) + require.NoError(s.T(), err) + require.NotNil(s.T(), response) + + require.Equal(s.T(), userID.String(), response.Name) + require.Equal(s.T(), "bill", response.Username) + require.Equal(s.T(), "bill", response.CompliantUsername) + require.True(s.T(), response.Status.Ready) + require.Equal(s.T(), "mur_ready_reason", response.Status.Reason) + require.Equal(s.T(), "mur_ready_message", response.Status.Message) + require.False(s.T(), response.Status.VerificationRequired) + require.Equal(s.T(), "https://console.apps.member-123.com", response.ConsoleURL) + require.Equal(s.T(), "http://che-toolchain-che.member-123.com", response.CheDashboardURL) + require.Equal(s.T(), "http://api.devcluster.openshift.com", response.APIEndpoint) + require.Equal(s.T(), "member-123", response.ClusterName) + require.Equal(s.T(), "https://proxy-url.com", response.ProxyURL) + assert.Equal(s.T(), "bill-dev", response.DefaultUserNamespace) + assert.Equal(s.T(), "https://rhods-dashboard-redhat-ods-applications.apps.member-123.com", response.RHODSMemberURL) }) } @@ -909,7 +909,7 @@ func (s *TestSignupServiceSuite) TestGetSignupStatusOK() { // given for _, appsSubDomain := range []string{".apps.", ".apps-"} { s.SetupTest() - s.T().Run("for apps subdomain: "+appsSubDomain, func(t *testing.T) { + s.Run("for apps subdomain: "+appsSubDomain, func() { s.ServiceConfiguration(true, "", 5) us := s.newUserSignupComplete() @@ -926,26 +926,26 @@ func (s *TestSignupServiceSuite) TestGetSignupStatusOK() { response, err := application.SignupService().GetSignup(c, us.Name, "", true) // then - require.NoError(t, err) - require.NotNil(t, response) - - require.Equal(t, us.Name, response.Name) - require.Equal(t, "jsmith", response.Username) - require.Equal(t, "ted", response.CompliantUsername) - - require.Equal(t, mur.Status.ProvisionedTime.UTC().Format(time.RFC3339), response.StartDate) - require.Equal(t, us.Status.ScheduledDeactivationTimestamp.UTC().Format(time.RFC3339), response.EndDate) - assert.True(t, response.Status.Ready) - assert.Equal(t, "mur_ready_reason", response.Status.Reason) - assert.Equal(t, "mur_ready_message", response.Status.Message) - assert.False(t, response.Status.VerificationRequired) - assert.Equal(t, fmt.Sprintf("https://console%smember-123.com", appsSubDomain), response.ConsoleURL) - assert.Equal(t, "http://che-toolchain-che.member-123.com", response.CheDashboardURL) - assert.Equal(t, "http://api.devcluster.openshift.com", response.APIEndpoint) - assert.Equal(t, "member-123", response.ClusterName) - assert.Equal(t, "https://proxy-url.com", response.ProxyURL) - assert.Equal(t, "ted-dev", response.DefaultUserNamespace) - assert.Equal(t, fmt.Sprintf("https://rhods-dashboard-redhat-ods-applications%smember-123.com", appsSubDomain), response.RHODSMemberURL) + require.NoError(s.T(), err) + require.NotNil(s.T(), response) + + require.Equal(s.T(), us.Name, response.Name) + require.Equal(s.T(), "jsmith", response.Username) + require.Equal(s.T(), "ted", response.CompliantUsername) + + require.Equal(s.T(), mur.Status.ProvisionedTime.UTC().Format(time.RFC3339), response.StartDate) + require.Equal(s.T(), us.Status.ScheduledDeactivationTimestamp.UTC().Format(time.RFC3339), response.EndDate) + assert.True(s.T(), response.Status.Ready) + assert.Equal(s.T(), "mur_ready_reason", response.Status.Reason) + assert.Equal(s.T(), "mur_ready_message", response.Status.Message) + assert.False(s.T(), response.Status.VerificationRequired) + assert.Equal(s.T(), fmt.Sprintf("https://console%smember-123.com", appsSubDomain), response.ConsoleURL) + assert.Equal(s.T(), "http://che-toolchain-che.member-123.com", response.CheDashboardURL) + assert.Equal(s.T(), "http://api.devcluster.openshift.com", response.APIEndpoint) + assert.Equal(s.T(), "member-123", response.ClusterName) + assert.Equal(s.T(), "https://proxy-url.com", response.ProxyURL) + assert.Equal(s.T(), "ted-dev", response.DefaultUserNamespace) + assert.Equal(s.T(), fmt.Sprintf("https://rhods-dashboard-redhat-ods-applications%smember-123.com", appsSubDomain), response.RHODSMemberURL) }) } } @@ -1151,7 +1151,7 @@ func (s *TestSignupServiceSuite) TestGetSignupReadyConditionStatus() { } for tcName, tc := range tests { - s.T().Run(tcName, func(t *testing.T) { + s.Run(tcName, func() { // given mur.Status = toolchainv1alpha1.MasterUserRecordStatus{ @@ -1165,10 +1165,10 @@ func (s *TestSignupServiceSuite) TestGetSignupReadyConditionStatus() { response, err := application.SignupService().GetSignup(c, us.Name, "", true) // then - require.NoError(t, err) - require.Equal(t, tc.expectedConditionReady, response.Status.Ready) - require.Equal(t, tc.condition.Reason, response.Status.Reason) - require.Equal(t, tc.condition.Message, response.Status.Message) + require.NoError(s.T(), err) + require.Equal(s.T(), tc.expectedConditionReady, response.Status.Ready) + require.Equal(s.T(), tc.condition.Reason, response.Status.Reason) + require.Equal(s.T(), tc.condition.Message, response.Status.Message) }) } } diff --git a/pkg/verification/service/verification_service_test.go b/pkg/verification/service/verification_service_test.go index ad4486c2..e082b79a 100644 --- a/pkg/verification/service/verification_service_test.go +++ b/pkg/verification/service/verification_service_test.go @@ -288,7 +288,7 @@ func (s *TestVerificationServiceSuite) TestInitVerificationClientFailure() { states.SetVerificationRequired(userSignup, true) - s.T().Run("when client GET call fails should return error", func(t *testing.T) { + s.Run("when client GET call fails should return error", func() { fakeClient, application := testutil.PrepareInClusterAppWithOption(s.T(), httpClientFactoryOption(), userSignup) // Cause the client GET call to fail @@ -301,10 +301,10 @@ func (s *TestVerificationServiceSuite) TestInitVerificationClientFailure() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().InitVerification(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "+1NUMBER", "1") - require.EqualError(t, err, "get failed: error retrieving usersignup: 123", err.Error()) + require.EqualError(s.T(), err, "get failed: error retrieving usersignup: 123", err.Error()) }) - s.T().Run("when client UPDATE call fails indefinitely should return error", func(t *testing.T) { + s.Run("when client UPDATE call fails indefinitely should return error", func() { fakeClient, application := testutil.PrepareInClusterAppWithOption(s.T(), httpClientFactoryOption(), userSignup) fakeClient.MockUpdate = func(ctx gocontext.Context, obj client.Object, opts ...client.UpdateOption) error { if _, ok := obj.(*toolchainv1alpha1.UserSignup); ok { @@ -315,12 +315,12 @@ func (s *TestVerificationServiceSuite) TestInitVerificationClientFailure() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().InitVerification(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "+1NUMBER", "1") - require.EqualError(t, err, "there was an error while updating your account - please wait a moment before "+ + require.EqualError(s.T(), err, "there was an error while updating your account - please wait a moment before "+ "trying again. If this error persists, please contact the Developer Sandbox team at devsandbox@redhat.com "+ "for assistance: error while verifying phone code") }) - s.T().Run("when client UPDATE call fails twice should return ok", func(t *testing.T) { + s.Run("when client UPDATE call fails twice should return ok", func() { fakeClient, application := testutil.PrepareInClusterAppWithOption(s.T(), httpClientFactoryOption(), userSignup) failCount := 0 @@ -335,26 +335,26 @@ func (s *TestVerificationServiceSuite) TestInitVerificationClientFailure() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().InitVerification(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "+1NUMBER", "1") - require.NoError(t, err) + require.NoError(s.T(), err) signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.NotEmpty(t, signup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]) + require.NotEmpty(s.T(), signup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]) buf := new(bytes.Buffer) _, err = buf.ReadFrom(reqBody) - require.NoError(t, err) + require.NoError(s.T(), err) reqValue := buf.String() params, err := url.ParseQuery(reqValue) - require.NoError(t, err) - require.Equal(t, fmt.Sprintf("Developer Sandbox for Red Hat OpenShift: Your verification code is %s", + require.NoError(s.T(), err) + require.Equal(s.T(), fmt.Sprintf("Developer Sandbox for Red Hat OpenShift: Your verification code is %s", signup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]), params.Get("Body")) - require.Equal(t, "CodeReady", params.Get("From")) - require.Equal(t, "+1NUMBER", params.Get("To")) + require.Equal(s.T(), "CodeReady", params.Get("From")) + require.Equal(s.T(), "+1NUMBER", params.Get("To")) }) } @@ -629,7 +629,7 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { // given now := time.Now() - s.T().Run("verification ok", func(t *testing.T) { + s.Run("verification ok", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -657,16 +657,16 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "123456") - require.NoError(t, err) + require.NoError(s.T(), err) signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.False(t, states.VerificationRequired(signup)) + require.False(s.T(), states.VerificationRequired(signup)) }) - s.T().Run("verification ok for usersignup with username identifier", func(t *testing.T) { + s.Run("verification ok for usersignup with username identifier", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -694,16 +694,16 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, "", "employee085", "654321") - require.NoError(t, err) + require.NoError(s.T(), err) signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.False(t, states.VerificationRequired(signup)) + require.False(s.T(), states.VerificationRequired(signup)) }) - s.T().Run("when verification code is invalid", func(t *testing.T) { + s.Run("when verification code is invalid", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -729,14 +729,14 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "123456") - require.Error(t, err) + require.Error(s.T(), err) e := &crterrors.Error{} - require.ErrorAs(t, err, &e) - require.Equal(t, "invalid code: the provided code is invalid", e.Error()) - require.Equal(t, http.StatusForbidden, int(e.Code)) + require.ErrorAs(s.T(), err, &e) + require.Equal(s.T(), "invalid code: the provided code is invalid", e.Error()) + require.Equal(s.T(), http.StatusForbidden, int(e.Code)) }) - s.T().Run("when verification code has expired", func(t *testing.T) { + s.Run("when verification code has expired", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -763,12 +763,12 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "123456") e := &crterrors.Error{} - require.ErrorAs(t, err, &e) - require.Equal(t, "expired: verification code expired", e.Error()) - require.Equal(t, http.StatusForbidden, int(e.Code)) + require.ErrorAs(s.T(), err, &e) + require.Equal(s.T(), "expired: verification code expired", e.Error()) + require.Equal(s.T(), http.StatusForbidden, int(e.Code)) }) - s.T().Run("when verifications exceeded maximum attempts", func(t *testing.T) { + s.Run("when verifications exceeded maximum attempts", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -794,10 +794,10 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "123456") - require.EqualError(t, err, "too many verification attempts", err.Error()) + require.EqualError(s.T(), err, "too many verification attempts", err.Error()) }) - s.T().Run("when verifications attempts has invalid value", func(t *testing.T) { + s.Run("when verifications attempts has invalid value", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -823,16 +823,16 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "123456") - require.EqualError(t, err, "too many verification attempts", err.Error()) + require.EqualError(s.T(), err, "too many verification attempts", err.Error()) signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.Equal(t, "3", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) + require.Equal(s.T(), "3", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) }) - s.T().Run("when verifications expiry is corrupt", func(t *testing.T) { + s.Run("when verifications expiry is corrupt", func() { userSignup := &toolchainv1alpha1.UserSignup{ ObjectMeta: metav1.ObjectMeta{ @@ -858,10 +858,10 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) err := application.VerificationService().VerifyPhoneCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "123456") - require.EqualError(t, err, "parsing time \"ABC\" as \"2006-01-02T15:04:05.000Z07:00\": cannot parse \"ABC\" as \"2006\": error parsing expiry timestamp", err.Error()) + require.EqualError(s.T(), err, "parsing time \"ABC\" as \"2006-01-02T15:04:05.000Z07:00\": cannot parse \"ABC\" as \"2006\": error parsing expiry timestamp", err.Error()) }) - s.T().Run("captcha configuration ", func(t *testing.T) { + s.Run("captcha configuration ", func() { tests := map[string]struct { activationCounterAnnotationValue string captchaScoreAnnotationValue string @@ -932,7 +932,7 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { }, } for k, tc := range tests { - t.Run(k, func(t *testing.T) { + s.Run(k, func() { // when s.OverrideApplicationDefault( testconfig.RegistrationService().Verification().CaptchaRequiredScore("0.6"), @@ -974,10 +974,10 @@ func (s *TestVerificationServiceSuite) TestVerifyPhoneCode() { signup := &toolchainv1alpha1.UserSignup{} require.NoError(s.T(), fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup)) if tc.expectedErr != "" { - require.EqualError(t, err, tc.expectedErr) + require.EqualError(s.T(), err, tc.expectedErr) } else { - require.NoError(t, err) - require.False(t, states.VerificationRequired(signup)) + require.NoError(s.T(), err) + require.False(s.T(), states.VerificationRequired(signup)) } }) } @@ -995,7 +995,7 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st cfg := configuration.GetRegistrationServiceConfig() ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) - s.T().Run("verification ok", func(t *testing.T) { + s.Run("verification ok", func() { // given userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up event := testsocialevent.NewSocialEvent(commontest.HostOperatorNs, "event", testsocialevent.WithTargetCluster(targetCluster)) @@ -1005,15 +1005,15 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, event.Name) // then - require.NoError(t, err) + require.NoError(s.T(), err) signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.False(t, states.VerificationRequired(signup)) - assert.Equal(t, targetCluster, signup.Spec.TargetCluster) + require.False(s.T(), states.VerificationRequired(signup)) + assert.Equal(s.T(), targetCluster, signup.Spec.TargetCluster) }) - s.T().Run("last user to signup", func(t *testing.T) { + s.Run("last user to signup", func() { // given 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 @@ -1023,15 +1023,15 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, event.Name) // then - require.NoError(t, err) + require.NoError(s.T(), err) signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.False(t, states.VerificationRequired(signup)) - assert.Equal(t, targetCluster, signup.Spec.TargetCluster) + require.False(s.T(), states.VerificationRequired(signup)) + assert.Equal(s.T(), targetCluster, signup.Spec.TargetCluster) }) - s.T().Run("when too many attempts made", func(t *testing.T) { + s.Run("when too many attempts made", func() { // given userSignup := testusersignup.NewUserSignup( testusersignup.VerificationRequired(time.Second), // just signed up @@ -1043,17 +1043,17 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, event.Name) // then - require.EqualError(t, err, "too many verification attempts: 3") + require.EqualError(s.T(), err, "too many verification attempts: 3") signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.True(t, states.VerificationRequired(signup)) // unchanged - assert.Empty(t, signup.Spec.TargetCluster) + require.True(s.T(), states.VerificationRequired(signup)) // unchanged + assert.Empty(s.T(), signup.Spec.TargetCluster) }) - s.T().Run("when invalid code", func(t *testing.T) { + s.Run("when invalid code", func() { - t.Run("first attempt", func(t *testing.T) { + s.Run("first attempt", func() { // given userSignup := testusersignup.NewUserSignup(testusersignup.VerificationRequired(time.Second)) // just signed up fakeClient, application := testutil.PrepareInClusterApp(s.T(), userSignup) @@ -1062,15 +1062,15 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "invalid") // then - require.EqualError(t, err, "invalid code: the provided code is invalid") + require.EqualError(s.T(), err, "invalid code: the provided code is invalid") signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.True(t, states.VerificationRequired(signup)) // unchanged - assert.Equal(t, "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented + require.True(s.T(), states.VerificationRequired(signup)) // unchanged + assert.Equal(s.T(), "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented }) - t.Run("second attempt", func(t *testing.T) { + s.Run("second attempt", func() { // given userSignup := testusersignup.NewUserSignup( testusersignup.VerificationRequired(time.Second), // just signed up @@ -1081,16 +1081,16 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, "invalid") // then - require.EqualError(t, err, "invalid code: the provided code is invalid") + require.EqualError(s.T(), err, "invalid code: the provided code is invalid") signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.True(t, states.VerificationRequired(signup)) // unchanged - assert.Equal(t, "3", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented + require.True(s.T(), states.VerificationRequired(signup)) // unchanged + assert.Equal(s.T(), "3", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented }) }) - s.T().Run("when max attendees reached", func(t *testing.T) { + s.Run("when max attendees reached", func() { // given 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` @@ -1100,16 +1100,16 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, event.Name) // then - require.EqualError(t, err, "invalid code: the event is full") + require.EqualError(s.T(), err, "invalid code: the event is full") signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.True(t, states.VerificationRequired(signup)) - assert.Equal(t, "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented - assert.Empty(t, signup.Spec.TargetCluster) + require.True(s.T(), states.VerificationRequired(signup)) + assert.Equal(s.T(), "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented + assert.Empty(s.T(), signup.Spec.TargetCluster) }) - s.T().Run("when event not open yet", func(t *testing.T) { + s.Run("when event not open yet", func() { // given 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 @@ -1119,16 +1119,16 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, event.Name) // then - require.EqualError(t, err, "invalid code: the provided code is invalid") + require.EqualError(s.T(), err, "invalid code: the provided code is invalid") signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.True(t, states.VerificationRequired(signup)) - assert.Equal(t, "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented - assert.Empty(t, signup.Spec.TargetCluster) + require.True(s.T(), states.VerificationRequired(signup)) + assert.Equal(s.T(), "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented + assert.Empty(s.T(), signup.Spec.TargetCluster) }) - s.T().Run("when event already closed", func(t *testing.T) { + s.Run("when event already closed", func() { // given 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 @@ -1138,12 +1138,12 @@ func (s *TestVerificationServiceSuite) testVerifyActivationCode(targetCluster st err := application.VerificationService().VerifyActivationCode(ctx, userSignup.Name, userSignup.Spec.IdentityClaims.PreferredUsername, event.Name) // then - require.EqualError(t, err, "invalid code: the provided code is invalid") + require.EqualError(s.T(), err, "invalid code: the provided code is invalid") signup := &toolchainv1alpha1.UserSignup{} err = fakeClient.Get(gocontext.TODO(), client.ObjectKeyFromObject(userSignup), signup) require.NoError(s.T(), err) - require.True(t, states.VerificationRequired(signup)) - assert.Equal(t, "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented - assert.Empty(t, signup.Spec.TargetCluster) + require.True(s.T(), states.VerificationRequired(signup)) + assert.Equal(s.T(), "1", signup.Annotations[toolchainv1alpha1.UserVerificationAttemptsAnnotationKey]) // incremented + assert.Empty(s.T(), signup.Spec.TargetCluster) }) }