test(users): cover google signup otp gating
This commit is contained in:
@@ -733,13 +733,11 @@ class GoogleOAuthApiTests(APITestCase):
|
|||||||
self.assertEqual(response.data["code"], "google_email_mobile_conflict")
|
self.assertEqual(response.data["code"], "google_email_mobile_conflict")
|
||||||
self.assertEqual(response.data["mobile_hint"], "09*****0002")
|
self.assertEqual(response.data["mobile_hint"], "09*****0002")
|
||||||
|
|
||||||
@patch("apps.users.services.google_oauth.requests.get")
|
@patch("apps.users.services.google_oauth.generate_and_send_otp")
|
||||||
def test_google_complete_new_mobile_creates_user_and_link(self, requests_get):
|
def test_google_complete_new_mobile_moves_flow_to_claim_required_without_creating_user(
|
||||||
avatar_response = Mock()
|
self,
|
||||||
avatar_response.content = b"avatar-bytes"
|
generate_and_send_otp,
|
||||||
avatar_response.headers = {"Content-Type": "image/png"}
|
):
|
||||||
avatar_response.raise_for_status.return_value = None
|
|
||||||
requests_get.return_value = avatar_response
|
|
||||||
cache.set(
|
cache.set(
|
||||||
"google_oauth_flow:new-flow",
|
"google_oauth_flow:new-flow",
|
||||||
{
|
{
|
||||||
@@ -767,20 +765,10 @@ class GoogleOAuthApiTests(APITestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.data["status"], "authenticated")
|
self.assertEqual(response.data["status"], "claim_required")
|
||||||
created_user = User.objects.get(mobile="09125550009")
|
self.assertEqual(response.data["resolution"], "new_account")
|
||||||
self.assertFalse(created_user.has_usable_password())
|
self.assertFalse(User.objects.filter(mobile="09125550009").exists())
|
||||||
self.assertEqual(created_user.email, "created@example.com")
|
generate_and_send_otp.assert_called_once_with("09125550009", "register")
|
||||||
self.assertEqual(created_user.first_name, "Created")
|
|
||||||
self.assertEqual(created_user.last_name, "User")
|
|
||||||
self.assertTrue(bool(created_user.profile_picture))
|
|
||||||
self.assertTrue(
|
|
||||||
UserSocialAccount.objects.filter(
|
|
||||||
user=created_user,
|
|
||||||
provider=UserSocialAccount.ProviderType.GOOGLE,
|
|
||||||
provider_user_id="google-sub-4",
|
|
||||||
).exists()
|
|
||||||
)
|
|
||||||
|
|
||||||
@patch("apps.users.services.google_oauth.generate_and_send_otp")
|
@patch("apps.users.services.google_oauth.generate_and_send_otp")
|
||||||
def test_google_complete_existing_blank_email_mobile_moves_flow_to_claim_required(
|
def test_google_complete_existing_blank_email_mobile_moves_flow_to_claim_required(
|
||||||
@@ -937,6 +925,68 @@ class GoogleOAuthApiTests(APITestCase):
|
|||||||
).exists()
|
).exists()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@patch("apps.users.services.google_oauth.requests.get")
|
||||||
|
@patch("apps.users.services.google_oauth.get_tokens_for_user")
|
||||||
|
def test_google_claim_verify_creates_new_user_only_after_otp_confirmation(
|
||||||
|
self,
|
||||||
|
get_tokens_for_user,
|
||||||
|
requests_get,
|
||||||
|
):
|
||||||
|
get_tokens_for_user.return_value = {"access": "a", "refresh": "r"}
|
||||||
|
avatar_response = Mock()
|
||||||
|
avatar_response.content = b"avatar-bytes"
|
||||||
|
avatar_response.headers = {"Content-Type": "image/png"}
|
||||||
|
avatar_response.raise_for_status.return_value = None
|
||||||
|
requests_get.return_value = avatar_response
|
||||||
|
cache.set(
|
||||||
|
"google_oauth_flow:new-claim-verify-flow",
|
||||||
|
{
|
||||||
|
"status": "claim_required",
|
||||||
|
"google_profile": {
|
||||||
|
"provider_user_id": "google-sub-new-verify",
|
||||||
|
"email": "new-verified@example.com",
|
||||||
|
"email_verified": True,
|
||||||
|
"first_name": "Verified",
|
||||||
|
"last_name": "Signup",
|
||||||
|
"avatar_url": "https://example.com/new-verify.png",
|
||||||
|
},
|
||||||
|
"mobile": "09125550010",
|
||||||
|
"user_id": None,
|
||||||
|
"resolution": "new_account",
|
||||||
|
"email": "new-verified@example.com",
|
||||||
|
"mobile_hint": None,
|
||||||
|
"detail": "claim",
|
||||||
|
},
|
||||||
|
900,
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("django_redis.get_redis_connection") as get_redis_connection:
|
||||||
|
redis_mock = get_redis_connection.return_value
|
||||||
|
redis_mock.get.return_value = b"12345"
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
"/api/users/oauth/google/claim/verify/",
|
||||||
|
{"flow": "new-claim-verify-flow", "code": "12345"},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.data["status"], "authenticated")
|
||||||
|
created_user = User.objects.get(mobile="09125550010")
|
||||||
|
self.assertTrue(created_user.is_verified)
|
||||||
|
self.assertFalse(created_user.has_usable_password())
|
||||||
|
self.assertEqual(created_user.email, "new-verified@example.com")
|
||||||
|
self.assertEqual(created_user.first_name, "Verified")
|
||||||
|
self.assertEqual(created_user.last_name, "Signup")
|
||||||
|
self.assertTrue(bool(created_user.profile_picture))
|
||||||
|
self.assertTrue(
|
||||||
|
UserSocialAccount.objects.filter(
|
||||||
|
user=created_user,
|
||||||
|
provider=UserSocialAccount.ProviderType.GOOGLE,
|
||||||
|
provider_user_id="google-sub-new-verify",
|
||||||
|
).exists()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class GoogleOAuthAuditCommandTests(APITestCase):
|
class GoogleOAuthAuditCommandTests(APITestCase):
|
||||||
def test_audit_google_social_links_reports_suspicious_links(self):
|
def test_audit_google_social_links_reports_suspicious_links(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user