import pytest from apps.users.models import User, UserSocialAccount from apps.users.services import auth from apps.users.services import google_oauth as google pytestmark = [pytest.mark.integration, pytest.mark.django_db] def test_registration_consumes_verified_mobile_and_creates_account(fake_redis, monkeypatch): monkeypatch.setattr(auth, "_redis", lambda: fake_redis) fake_redis.set(auth._verified_otp_key("register", "09190000001"), "1") payload = auth.RegistrationPayload( username="new_member", mobile="09190000001", password="StrongPass123!", code="12345", email=" NEW@EXAMPLE.COM ", ) user = auth.register_user(payload) assert user.email == "new@example.com" assert user.is_mobile_verified assert user.check_password("StrongPass123!") assert fake_redis.get(auth._verified_otp_key("register", "09190000001")) is None def test_registration_rejects_duplicate_username(user_factory, fake_redis, monkeypatch): user_factory(username="taken") monkeypatch.setattr(auth, "_redis", lambda: fake_redis) payload = auth.RegistrationPayload(username="taken", mobile="09190000002", password="StrongPass123!", code="12345") with pytest.raises(auth.AuthServiceError) as error: auth.register_user(payload) assert error.value.field == "username" assert User.objects.count() == 1 def test_google_claim_creates_account_and_link_without_live_gateway(mocker): verify = mocker.patch("apps.users.services.google_oauth.verify_otp_code") profile = google.GoogleProfile("google-1", "person@example.com", True, "Person", "Example", "") flow = google.create_google_flow({ "status": "claim_required", "resolution": "new_account", "mobile": "09190000001", "username": "google_member", "google_profile": google._profile_payload(profile), }) result = google.verify_google_claim(flow, "12345") assert result["status"] == "authenticated" verify.assert_called_once() user = User.objects.get(username="google_member") assert user.is_mobile_verified assert not user.has_usable_password() assert UserSocialAccount.objects.filter(user=user, provider_user_id="google-1").exists()