|
| 1 | +package parallel |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" |
| 9 | + "github.com/codeready-toolchain/toolchain-common/pkg/states" |
| 10 | + testconfig "github.com/codeready-toolchain/toolchain-common/pkg/test/config" |
| 11 | + . "github.com/codeready-toolchain/toolchain-e2e/testsupport" |
| 12 | + "github.com/codeready-toolchain/toolchain-e2e/testsupport/wait" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// Twilio test credentials with magic lookup numbers return deterministic SMS Pumping Risk |
| 18 | +// responses at no cost. The API takes country_code and phone_number separately. |
| 19 | +// See: https://www.twilio.com/docs/lookup/magic-numbers-for-lookup/testing-sms-pumping-risk-with-magic-numbers |
| 20 | +// |
| 21 | +// Magic numbers used: |
| 22 | +// |
| 23 | +// +441234567890 → high risk, not blocked, score 2 |
| 24 | +// +441234567891 → high risk, blocked, score 34 |
| 25 | +// +911234567890 → low risk, not blocked, score 2 |
| 26 | +const ( |
| 27 | + twilioMagicPhoneHighRisk = "1234567890" // +44 prefix → high risk, not blocked |
| 28 | + twilioMagicPhoneHighRiskBlocked = "1234567891" // +44 prefix → high risk, blocked |
| 29 | + usTestPhone = "2025550123" // +1 prefix → fictional NANPA 555-01XX range, safe with test credentials |
| 30 | +) |
| 31 | + |
| 32 | +func TestPhoneLookupMode(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + awaitilities := WaitForDeployments(t) |
| 35 | + hostAwait := awaitilities.Host() |
| 36 | + route := hostAwait.RegistrationServiceURL |
| 37 | + |
| 38 | + t.Run("log mode stores annotations and user is provisioned", func(t *testing.T) { |
| 39 | + // given |
| 40 | + hostAwait.UpdateToolchainConfig(t, testconfig.RegistrationService().Verification().PhoneLookupMode(toolchainv1alpha1.PhoneLookupModeLog)) |
| 41 | + userSignup, token := signup(t, hostAwait) |
| 42 | + |
| 43 | + // when |
| 44 | + NewHTTPRequest(t). |
| 45 | + InvokeEndpoint("PUT", route+"/api/v1/signup/verification", token, |
| 46 | + fmt.Sprintf(`{ "country_code":"+44", "phone_number":"%s" }`, twilioMagicPhoneHighRiskBlocked), http.StatusNoContent) |
| 47 | + |
| 48 | + // then — verification code is set and lookup details are recorded |
| 49 | + userSignup, err := hostAwait.WaitForUserSignup(t, userSignup.Name, |
| 50 | + wait.UntilUserSignupHasAnnotationNotEmpty(toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey)) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + assert.NotEmpty(t, userSignup.Annotations[toolchainv1alpha1.UserSignupPhoneLookupDetailsAnnotationKey]) |
| 54 | + assert.NotEmpty(t, userSignup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]) |
| 55 | + assert.False(t, states.Rejected(userSignup), "UserSignup should NOT be rejected in log mode") |
| 56 | + |
| 57 | + // complete verification and confirm user is provisioned |
| 58 | + NewHTTPRequest(t).InvokeEndpoint("GET", route+fmt.Sprintf("/api/v1/signup/verification/%s", |
| 59 | + userSignup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]), token, "", http.StatusOK) |
| 60 | + |
| 61 | + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). |
| 62 | + Update(userSignup.Name, hostAwait.Namespace, |
| 63 | + func(instance *toolchainv1alpha1.UserSignup) { |
| 64 | + states.SetApprovedManually(instance, true) |
| 65 | + }) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + VerifyResourcesProvisionedForSignup(t, awaitilities, userSignup) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("enabled mode rejects high-risk phone number", func(t *testing.T) { |
| 72 | + // given |
| 73 | + hostAwait.UpdateToolchainConfig(t, testconfig.RegistrationService(). |
| 74 | + Verification().PhoneLookupMode(toolchainv1alpha1.PhoneLookupModeEnabled). |
| 75 | + Verification().PhoneLookupExcludedCountries([]string{"US", "CA"})) |
| 76 | + userSignup, token := signup(t, hostAwait) |
| 77 | + |
| 78 | + // when |
| 79 | + responseMap := NewHTTPRequest(t). |
| 80 | + InvokeEndpoint("PUT", route+"/api/v1/signup/verification", token, |
| 81 | + fmt.Sprintf(`{ "country_code":"+44", "phone_number":"%s" }`, twilioMagicPhoneHighRiskBlocked), http.StatusForbidden). |
| 82 | + UnmarshalMap() |
| 83 | + |
| 84 | + // then |
| 85 | + require.NotEmpty(t, responseMap) |
| 86 | + assert.Equal(t, "Forbidden", responseMap["status"]) |
| 87 | + |
| 88 | + userSignup, err := hostAwait.WaitForUserSignup(t, userSignup.Name) |
| 89 | + require.NoError(t, err) |
| 90 | + assert.True(t, states.Rejected(userSignup), "UserSignup should be rejected in enabled mode with high-risk phone") |
| 91 | + assert.Empty(t, userSignup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]) |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("enabled mode blocks verification for previously rejected signup", func(t *testing.T) { |
| 95 | + // given |
| 96 | + hostAwait.UpdateToolchainConfig(t, testconfig.RegistrationService().Verification().PhoneLookupMode(toolchainv1alpha1.PhoneLookupModeEnabled)) |
| 97 | + userSignup, token := signup(t, hostAwait) |
| 98 | + |
| 99 | + _, err := wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). |
| 100 | + Update(userSignup.Name, hostAwait.Namespace, |
| 101 | + func(us *toolchainv1alpha1.UserSignup) { |
| 102 | + states.SetRejected(us, true) |
| 103 | + }) |
| 104 | + require.NoError(t, err) |
| 105 | + |
| 106 | + // when |
| 107 | + responseMap := NewHTTPRequest(t). |
| 108 | + InvokeEndpoint("PUT", route+"/api/v1/signup/verification", token, |
| 109 | + fmt.Sprintf(`{ "country_code":"+91", "phone_number":"%s" }`, twilioMagicPhoneHighRisk), http.StatusForbidden). |
| 110 | + UnmarshalMap() |
| 111 | + |
| 112 | + // then |
| 113 | + require.NotEmpty(t, responseMap) |
| 114 | + assert.Equal(t, "Forbidden", responseMap["status"]) |
| 115 | + |
| 116 | + userSignup, err = hostAwait.WaitForUserSignup(t, userSignup.Name) |
| 117 | + require.NoError(t, err) |
| 118 | + assert.True(t, states.Rejected(userSignup), "UserSignup should remain rejected") |
| 119 | + assert.Empty(t, userSignup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]) |
| 120 | + }) |
| 121 | + |
| 122 | + t.Run("enabled mode skips lookup for US numbers and user is provisioned", func(t *testing.T) { |
| 123 | + // given |
| 124 | + hostAwait.UpdateToolchainConfig(t, testconfig.RegistrationService(). |
| 125 | + Verification().PhoneLookupMode(toolchainv1alpha1.PhoneLookupModeEnabled). |
| 126 | + Verification().PhoneLookupExcludedCountries([]string{"US", "CA"})) |
| 127 | + userSignup, token := signup(t, hostAwait) |
| 128 | + |
| 129 | + // when — US is excluded so lookup is never called |
| 130 | + NewHTTPRequest(t). |
| 131 | + InvokeEndpoint("PUT", route+"/api/v1/signup/verification", token, |
| 132 | + fmt.Sprintf(`{ "country_code":"+1", "phone_number":"%s" }`, usTestPhone), http.StatusNoContent) |
| 133 | + |
| 134 | + // then |
| 135 | + userSignup, err := hostAwait.WaitForUserSignup(t, userSignup.Name, |
| 136 | + wait.UntilUserSignupHasAnnotationNotEmpty(toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey)) |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + assert.Empty(t, userSignup.Annotations[toolchainv1alpha1.UserSignupPhoneLookupDetailsAnnotationKey]) |
| 140 | + assert.NotEmpty(t, userSignup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]) |
| 141 | + assert.False(t, states.Rejected(userSignup), "UserSignup should NOT be rejected for excluded country") |
| 142 | + |
| 143 | + // complete verification and confirm user is provisioned |
| 144 | + NewHTTPRequest(t).InvokeEndpoint("GET", route+fmt.Sprintf("/api/v1/signup/verification/%s", |
| 145 | + userSignup.Annotations[toolchainv1alpha1.UserSignupVerificationCodeAnnotationKey]), token, "", http.StatusOK) |
| 146 | + |
| 147 | + userSignup, err = wait.For(t, hostAwait.Awaitility, &toolchainv1alpha1.UserSignup{}). |
| 148 | + Update(userSignup.Name, hostAwait.Namespace, |
| 149 | + func(instance *toolchainv1alpha1.UserSignup) { |
| 150 | + states.SetApprovedManually(instance, true) |
| 151 | + }) |
| 152 | + require.NoError(t, err) |
| 153 | + |
| 154 | + VerifyResourcesProvisionedForSignup(t, awaitilities, userSignup) |
| 155 | + }) |
| 156 | +} |
0 commit comments