Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Avoid Keycloak full scan in Resend sync (#14560)

Graham Neubig8fdf41d33115
diff --git a/enterprise/storage/resend_synced_user_store.py b/enterprise/storage/resend_synced_user_store.py
index f6cf689120..b4b7c1ba29 100644
--- a/enterprise/storage/resend_synced_user_store.py
+++ b/enterprise/storage/resend_synced_user_store.py
@@ -54,7 +54,7 @@ class ResendSyncedUserStore:
self,
email: str,
audience_id: str,
- keycloak_user_id: Optional[str] = None,
+ user_id: Optional[str] = None,
) -> ResendSyncedUser:
"""Mark a user as synced to a specific audience.
@@ -64,7 +64,7 @@ class ResendSyncedUserStore:
Args:
email: The email address of the user.
audience_id: The Resend audience ID.
- keycloak_user_id: Optional Keycloak user ID.
+ user_id: Optional OpenHands user ID. Stored in the legacy keycloak_user_id column.
Returns:
The ResendSyncedUser record.
@@ -78,7 +78,7 @@ class ResendSyncedUserStore:
.values(
email=email.lower(),
audience_id=audience_id,
- keycloak_user_id=keycloak_user_id,
+ keycloak_user_id=user_id,
synced_at=datetime.now(UTC),
)
.on_conflict_do_nothing(constraint='uq_resend_synced_email_audience')
diff --git a/enterprise/sync/README.md b/enterprise/sync/README.md
index f023a36158..5c1c020c60 100644
--- a/enterprise/sync/README.md
+++ b/enterprise/sync/README.md
@@ -1,10 +1,10 @@
# Resend Sync Service
-This service syncs users from Keycloak to a Resend.com audience. It runs as a Kubernetes CronJob that periodically queries the Keycloak database and adds any new users to the specified Resend audience.
+This service syncs users from the OpenHands database to a Resend.com audience. It runs as a Kubernetes CronJob that periodically queries the OpenHands database and adds any new users to the specified Resend audience.
## Features
-- Syncs users from Keycloak to Resend.com audience
+- Syncs OpenHands users with email addresses to a Resend.com audience
- Handles rate limiting and retries with exponential backoff
- Runs as a Kubernetes CronJob
- Configurable batch size and sync frequency
@@ -17,14 +17,22 @@ The service is configured using environment variables:
|----------|-------------|---------|
| `RESEND_API_KEY` | Resend API key | (required) |
| `RESEND_AUDIENCE_ID` | Resend audience ID | (required) |
-| `KEYCLOAK_REALM` | Keycloak realm | `all-hands` |
-| `BATCH_SIZE` | Number of users to process in each batch | `100` |
+| `BATCH_SIZE` | Number of users to process in each batch | `2000` |
| `MAX_RETRIES` | Maximum number of retries for API calls | `3` |
| `INITIAL_BACKOFF_SECONDS` | Initial backoff time for retries | `1` |
| `MAX_BACKOFF_SECONDS` | Maximum backoff time for retries | `60` |
| `BACKOFF_FACTOR` | Backoff factor for retries | `2` |
| `RATE_LIMIT` | Rate limit for API calls (requests per second) | `2` |
+
+### Migration from Keycloak-backed sync
+
+This sync no longer reads or counts users through Keycloak. Deployments should remove the old `KEYCLOAK_SERVER_URL`, `KEYCLOAK_REALM_NAME`, and `KEYCLOAK_ADMIN_PASSWORD` wiring for this CronJob and ensure it has the same OpenHands database configuration as the application server. If the database session cannot be created, the job fails before attempting to sync contacts.
+
+Display-name personalization now comes from the local `user.git_user_name` field when present; users without a stored display name continue to receive the generic `Hi there,` greeting.
+
+**Failure modes:** If the OpenHands database is unavailable, the sync job exits with status code 1 and logs the failure. No contacts are synced until the database connection is restored. Monitor the CronJob logs for database connection errors.
+
## Deployment
The service is deployed as part of the openhands Helm chart. To enable it, set the following in your values.yaml:
diff --git a/enterprise/sync/resend_keycloak.py b/enterprise/sync/resend_keycloak.py
index d2c09d5cbd..b7ef30531d 100644
--- a/enterprise/sync/resend_keycloak.py
+++ b/enterprise/sync/resend_keycloak.py
@@ -1,23 +1,20 @@
#!/usr/bin/env python3
-"""Sync script to add Keycloak users to Resend.com audience.
+"""Sync OpenHands users to a Resend.com audience.
-This script uses the Keycloak admin client to fetch users and adds them to a
-Resend.com audience. It handles rate limiting and retries with exponential
-backoff for adding contacts. When a user is newly added to the mailing list, a welcome email is sent.
+This script reads users from the OpenHands application database and adds them to
+a Resend.com audience. It handles rate limiting and retries with exponential
+backoff for adding contacts. When a user is newly added to the mailing list, a
+welcome email is sent.
Required environment variables:
-- KEYCLOAK_SERVER_URL: URL of the Keycloak server
-- KEYCLOAK_REALM_NAME: Keycloak realm name
-- KEYCLOAK_ADMIN_PASSWORD: Password for the Keycloak admin user
- RESEND_API_KEY: API key for Resend.com
- RESEND_AUDIENCE_ID: ID of the Resend audience to add users to
Optional environment variables:
-- KEYCLOAK_CLIENT_ID: Client ID for Keycloak
-- KEYCLOAK_CLIENT_SECRET: Client secret for Keycloak
-- RESEND_FROM_EMAIL: Email address to use as the sender (default: "OpenHands Team <[email protected]>")
+- RESEND_FROM_EMAIL: Email address to use as the sender
+ (default: "OpenHands Team <[email protected]>")
- RESEND_REPLY_TO_EMAIL: Email address for replies (default: "[email protected]")
-- BATCH_SIZE: Number of users to process in each batch (default: 100)
+- BATCH_SIZE: Number of users to process in each batch (default: 2000)
- MAX_RETRIES: Maximum number of retries for API calls (default: 3)
- INITIAL_BACKOFF_SECONDS: Initial backoff time for retries (default: 1)
- MAX_BACKOFF_SECONDS: Maximum backoff time for retries (default: 60)
@@ -29,13 +26,14 @@ import os
import re
import sys
import time
-from typing import Any, Dict, List, Optional
+from dataclasses import dataclass
+from typing import Any, Dict, Optional
import resend
-from keycloak.exceptions import KeycloakError
from resend.exceptions import ResendError
-from server.auth.token_manager import get_keycloak_admin
+from sqlalchemy import and_, func, select
from storage.resend_synced_user_store import ResendSyncedUserStore
+from storage.user import User
from tenacity import (
retry,
retry_if_exception_type,
@@ -45,21 +43,15 @@ from tenacity import (
from openhands.app_server.utils.logger import openhands_logger as logger
-# Get Keycloak configuration from environment variables
-KEYCLOAK_SERVER_URL = os.environ.get('KEYCLOAK_SERVER_URL', '')
-KEYCLOAK_REALM_NAME = os.environ.get('KEYCLOAK_REALM_NAME', '')
-KEYCLOAK_CLIENT_ID = os.environ.get('KEYCLOAK_CLIENT_ID', '')
-KEYCLOAK_CLIENT_SECRET = os.environ.get('KEYCLOAK_CLIENT_SECRET', '')
-KEYCLOAK_ADMIN_PASSWORD = os.environ.get('KEYCLOAK_ADMIN_PASSWORD', '')
-
-# Logger is imported from openhands.app_server.utils.logger
-
# Get configuration from environment variables
RESEND_API_KEY = os.environ.get('RESEND_API_KEY')
RESEND_AUDIENCE_ID = os.environ.get('RESEND_AUDIENCE_ID', '')
# Sync configuration
-BATCH_SIZE = int(os.environ.get('BATCH_SIZE', '100'))
+# BATCH_SIZE controls only local DB pagination. Resend API calls remain
+# individually rate-limited by RATE_LIMIT, so increasing this avoids frequent
+# DB count/page queries without creating a 2000-contact API burst.
+BATCH_SIZE = int(os.environ.get('BATCH_SIZE', '2000'))
MAX_RETRIES = int(os.environ.get('MAX_RETRIES', '3'))
INITIAL_BACKOFF_SECONDS = float(os.environ.get('INITIAL_BACKOFF_SECONDS', '1'))
MAX_BACKOFF_SECONDS = float(os.environ.get('MAX_BACKOFF_SECONDS', '60'))
@@ -70,14 +62,16 @@ RATE_LIMIT = float(os.environ.get('RATE_LIMIT', '2')) # Requests per second
resend.api_key = RESEND_API_KEY
-class ResendSyncError(Exception):
- """Base exception for Resend sync errors."""
-
- pass
+@dataclass(frozen=True)
+class ResendUser:
+ id: str
+ email: str
+ first_name: str | None = None
+ last_name: str | None = None
-class KeycloakClientError(ResendSyncError):
- """Exception for Keycloak client errors."""
+class ResendSyncError(Exception):
+ """Base exception for Resend sync errors."""
pass
@@ -95,6 +89,67 @@ class ResendAPIError(ResendSyncError):
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
+def _get_session_maker():
+ """Get the application database session maker."""
+ from openhands.app_server.config import get_global_config
+
+ config = get_global_config()
+ return config.db_session.get_session_maker()
+
+
+def _valid_user_email_filter():
+ return and_(User.email.isnot(None), User.email != '')
+
+
+def _split_display_name(display_name: str | None) -> tuple[str | None, str | None]:
+ """Split a stored display name into Resend first/last name fields."""
+ if not display_name or not display_name.strip():
+ return None, None
+
+ parts = display_name.strip().split(maxsplit=1)
+ first_name = parts[0]
+ last_name = parts[1] if len(parts) > 1 else None
+ return first_name, last_name
+
+
+def get_local_users(offset: int = 0, limit: int = BATCH_SIZE) -> list[ResendUser]:
+ """Get users with email addresses from the OpenHands database."""
+ session_maker = _get_session_maker()
+ with session_maker() as session:
+ rows = session.execute(
+ select(User.id, User.email, User.git_user_name)
+ .where(_valid_user_email_filter())
+ .order_by(User.id)
+ .offset(offset)
+ .limit(limit)
+ ).all()
+
+ users = []
+ for row in rows:
+ first_name, last_name = _split_display_name(row.git_user_name)
+ users.append(
+ ResendUser(
+ id=str(row.id),
+ email=row.email,
+ first_name=first_name,
+ last_name=last_name,
+ )
+ )
+ return users
+
+
+def get_total_local_users() -> int:
+ """Get the total number of OpenHands users with email addresses."""
+ session_maker = _get_session_maker()
+ with session_maker() as session:
+ return (
+ session.execute(
+ select(func.count()).select_from(User).where(_valid_user_email_filter())
+ ).scalar()
+ or 0
+ )
+
+
def is_valid_email(email: Optional[str]) -> bool:
"""Validate an email address format.
@@ -113,77 +168,6 @@ def is_valid_email(email: Optional[str]) -> bool:
return bool(EMAIL_REGEX.match(email))
-def get_keycloak_users(offset: int = 0, limit: int = 100) -> List[Dict[str, Any]]:
- """Get users from Keycloak using the admin client.
-
- Args:
- offset: The offset to start from.
- limit: The maximum number of users to return.
-
- Returns:
- A list of users.
-
- Raises:
- KeycloakClientError: If the API call fails.
- """
- try:
- keycloak_admin = get_keycloak_admin()
-
- # Get users with pagination
- # The Keycloak API uses 'first' for offset and 'max' for limit
- params: Dict[str, Any] = {
- 'first': offset,
- 'max': limit,
- 'briefRepresentation': False, # Get full user details
- }
-
- users_data = keycloak_admin.get_users(params)
- logger.info(f'Fetched {len(users_data)} users from Keycloak')
-
- # Transform the response to match our expected format
- users = []
- for user in users_data:
- if user.get('email'): # Ensure user has an email
- users.append(
- {
- 'id': user.get('id'),
- 'email': user.get('email'),
- 'first_name': user.get('firstName'),
- 'last_name': user.get('lastName'),
- 'username': user.get('username'),
- }
- )
-
- return users
- except KeycloakError:
- logger.exception('Failed to get users from Keycloak')
- raise
- except Exception:
- logger.exception('Unexpected error getting users from Keycloak')
- raise
-
-
-def get_total_keycloak_users() -> int:
- """Get the total number of users in Keycloak.
-
- Returns:
- The total number of users.
-
- Raises:
- KeycloakClientError: If the API call fails.
- """
- try:
- keycloak_admin = get_keycloak_admin()
- count = keycloak_admin.users_count()
- return count
- except KeycloakError:
- logger.exception('Failed to get total users from Keycloak')
- raise
- except Exception:
- logger.exception('Unexpected error getting total users from Keycloak')
- raise
-
-
def get_resend_contacts(audience_id: str) -> Dict[str, Dict[str, Any]]:
"""Get contacts from Resend.
@@ -213,7 +197,7 @@ def get_resend_contacts(audience_id: str) -> Dict[str, Dict[str, Any]]:
max=MAX_BACKOFF_SECONDS,
exp_base=BACKOFF_FACTOR,
),
- retry=retry_if_exception_type((ResendError, KeycloakClientError)),
+ retry=retry_if_exception_type(ResendError),
)
def add_contact_to_resend(
audience_id: str,
@@ -334,11 +318,7 @@ def _get_resend_synced_user_store() -> ResendSyncedUserStore:
This is separated into a function to allow for easier testing/mocking.
"""
- from openhands.app_server.config import get_global_config
-
- config = get_global_config()
- db_session_injector = config.db_session
- return ResendSyncedUserStore(session_maker=db_session_injector.get_session_maker())
+ return ResendSyncedUserStore(session_maker=_get_session_maker())
def _backfill_existing_resend_contacts(
@@ -376,7 +356,7 @@ def _backfill_existing_resend_contacts(
synced_user_store.mark_user_synced(
email=email,
audience_id=audience_id,
- keycloak_user_id=None, # We don't have this info during backfill
+ user_id=None, # Backfilled Resend contacts have no local user ID.
)
backfilled_count += 1
logger.debug(f'Backfilled existing Resend contact: {email}')
@@ -393,10 +373,10 @@ def _backfill_existing_resend_contacts(
def sync_users_to_resend():
- """Sync users from Keycloak to Resend.
+ """Sync OpenHands users to Resend.
- This function syncs users from Keycloak to a Resend audience. It tracks
- which users have been synced in the database to ensure that:
+ This function syncs users from the OpenHands database to a Resend audience.
+ It tracks which users have been synced in the database to ensure that:
1. Users are only added once (even across multiple sync runs)
2. Users who are manually deleted from Resend are not re-added
@@ -407,13 +387,9 @@ def sync_users_to_resend():
the tracking table with existing Resend contacts to avoid sending
duplicate welcome emails.
"""
- # Check required environment variables
required_vars = {
'RESEND_API_KEY': RESEND_API_KEY,
'RESEND_AUDIENCE_ID': RESEND_AUDIENCE_ID,
- 'KEYCLOAK_SERVER_URL': KEYCLOAK_SERVER_URL,
- 'KEYCLOAK_REALM_NAME': KEYCLOAK_REALM_NAME,
- 'KEYCLOAK_ADMIN_PASSWORD': KEYCLOAK_ADMIN_PASSWORD,
}
missing_vars = [var for var, value in required_vars.items() if not value]
@@ -423,31 +399,22 @@ def sync_users_to_resend():
logger.error(f'{var} environment variable is not set')
sys.exit(1)
- # Log configuration (without sensitive info)
- logger.info(f'Using Keycloak server: {KEYCLOAK_SERVER_URL}')
- logger.info(f'Using Keycloak realm: {KEYCLOAK_REALM_NAME}')
-
logger.info(
- f'Starting sync of Keycloak users to Resend audience {RESEND_AUDIENCE_ID}'
+ f'Starting sync of OpenHands users to Resend audience {RESEND_AUDIENCE_ID}'
)
try:
- # Get the store for tracking synced users
synced_user_store = _get_resend_synced_user_store()
- # Backfill existing Resend contacts into our tracking table
- # This ensures users already in Resend don't get duplicate welcome emails
+ # Backfill existing Resend contacts into our tracking table.
+ # This ensures users already in Resend don't get duplicate welcome emails.
backfilled_count = _backfill_existing_resend_contacts(
synced_user_store, RESEND_AUDIENCE_ID
)
- # Get the total number of users
- total_users = get_total_keycloak_users()
- logger.info(
- f'Found {total_users} users in Keycloak realm {KEYCLOAK_REALM_NAME}'
- )
+ total_users = get_total_local_users()
+ logger.info(f'Found {total_users} users with emails in OpenHands database')
- # Stats
stats = {
'total_users': total_users,
'backfilled_contacts': backfilled_count,
@@ -462,18 +429,13 @@ def sync_users_to_resend():
)
logger.info(f'Found {len(synced_emails)} already synced emails in database')
- # Process users in batches
offset = 0
while offset < total_users:
- users = get_keycloak_users(offset, BATCH_SIZE)
+ users = get_local_users(offset, BATCH_SIZE)
logger.info(f'Processing batch of {len(users)} users (offset {offset})')
for user in users:
- email = user.get('email')
- if not email:
- continue
-
- email = email.lower()
+ email = user.email.lower()
if email in synced_emails:
logger.debug(
@@ -482,23 +444,16 @@ def sync_users_to_resend():
stats['already_synced'] += 1
continue
- # Validate email format before attempting to add to Resend
if not is_valid_email(email):
logger.warning(f'Skipping user with invalid email format: {email}')
stats['skipped_invalid_emails'] += 1
continue
- first_name = user.get('first_name')
- last_name = user.get('last_name')
- keycloak_user_id = user.get('id')
-
- # Mark as synced first (optimistic) to ensure consistency.
- # If Resend API fails, we remove the record.
try:
synced_user_store.mark_user_synced(
email=email,
audience_id=RESEND_AUDIENCE_ID,
- keycloak_user_id=keycloak_user_id,
+ user_id=user.id,
)
except Exception:
logger.exception(f'Failed to mark user {email} as synced')
@@ -507,7 +462,7 @@ def sync_users_to_resend():
try:
add_contact_to_resend(
- RESEND_AUDIENCE_ID, email, first_name, last_name
+ RESEND_AUDIENCE_ID, email, user.first_name, user.last_name
)
logger.info(f'Added user {email} to Resend')
except Exception:
@@ -519,27 +474,22 @@ def sync_users_to_resend():
synced_emails.add(email)
stats['added_contacts'] += 1
- # Sleep to respect rate limit after first API call
time.sleep(1 / RATE_LIMIT)
- # Send a welcome email to the newly added contact
try:
- send_welcome_email(email, first_name, last_name)
+ send_welcome_email(email, user.first_name, user.last_name)
logger.info(f'Sent welcome email to {email}')
except Exception:
logger.exception(
- f'Failed to send welcome email to {email}, but contact was added to audience'
+ f'Failed to send welcome email to {email}, '
+ 'but contact was added to audience'
)
- # Sleep to respect rate limit after second API call
time.sleep(1 / RATE_LIMIT)
offset += BATCH_SIZE
logger.info(f'Sync completed: {stats}')
- except KeycloakClientError:
- logger.exception('Keycloak client error')
- sys.exit(1)
except ResendAPIError:
logger.exception('Resend API error')
sys.exit(1)
diff --git a/enterprise/tests/unit/sync/test_resend_keycloak.py b/enterprise/tests/unit/sync/test_resend_keycloak.py
index 866726ddbc..714113f035 100644
--- a/enterprise/tests/unit/sync/test_resend_keycloak.py
+++ b/enterprise/tests/unit/sync/test_resend_keycloak.py
@@ -1,24 +1,30 @@
"""Tests for Resend Keycloak sync functionality."""
import os
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, call, patch
+from uuid import UUID
import pytest
from resend.exceptions import ResendError
+from sqlalchemy import String, create_engine
+from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, sessionmaker
from tenacity import RetryError
# Set required environment variables before importing the module
# that reads them at import time
os.environ['RESEND_API_KEY'] = 'test_api_key'
os.environ['RESEND_AUDIENCE_ID'] = 'test_audience_id'
-os.environ['KEYCLOAK_SERVER_URL'] = 'http://localhost:8080'
-os.environ['KEYCLOAK_REALM_NAME'] = 'test_realm'
-os.environ['KEYCLOAK_ADMIN_PASSWORD'] = 'test_password'
from enterprise.sync.resend_keycloak import ( # noqa: E402
+ BATCH_SIZE,
+ ResendUser,
+ _split_display_name,
add_contact_to_resend,
+ get_local_users,
+ get_total_local_users,
is_valid_email,
send_welcome_email,
+ sync_users_to_resend,
)
@@ -136,6 +142,100 @@ class TestIsValidEmail:
assert is_valid_email('[email protected]') is True
+class TestDisplayNameParsing:
+ def test_split_display_name_handles_full_single_and_blank_names(self) -> None:
+ assert _split_display_name('Ada Lovelace') == ('Ada', 'Lovelace')
+ assert _split_display_name('Prince') == ('Prince', None)
+ assert _split_display_name(' Grace Brewster Hopper ') == (
+ 'Grace',
+ 'Brewster Hopper',
+ )
+ assert _split_display_name('') == (None, None)
+ assert _split_display_name(None) == (None, None)
+
+
+class _LocalUserBase(DeclarativeBase):
+ pass
+
+
+class _LocalUser(_LocalUserBase):
+ __tablename__ = 'user'
+
+ id: Mapped[UUID] = mapped_column(primary_key=True)
+ email: Mapped[str | None] = mapped_column(String, nullable=True)
+ git_user_name: Mapped[str | None] = mapped_column(String, nullable=True)
+
+
+def _local_user_session_maker():
+ engine = create_engine('sqlite:///:memory:')
+ _LocalUserBase.metadata.create_all(engine)
+ session_factory = sessionmaker(bind=engine)
+
+ with session_factory() as session:
+ session.add_all(
+ [
+ _LocalUser(
+ id=UUID('00000000-0000-0000-0000-000000000001'),
+ git_user_name='Ada Lovelace',
+ ),
+ _LocalUser(
+ id=UUID('00000000-0000-0000-0000-000000000002'),
+ email=None,
+ git_user_name='No Email',
+ ),
+ _LocalUser(
+ id=UUID('00000000-0000-0000-0000-000000000003'),
+ email='',
+ git_user_name='Blank Email',
+ ),
+ _LocalUser(
+ id=UUID('00000000-0000-0000-0000-000000000004'),
+ git_user_name='Prince',
+ ),
+ ]
+ )
+ session.commit()
+
+ return session_factory
+
+
+class TestLocalUserQueries:
+ @patch('enterprise.sync.resend_keycloak.User', _LocalUser)
+ @patch('enterprise.sync.resend_keycloak._get_session_maker')
+ def test_get_local_users_reads_real_database_with_names(
+ self, mock_get_session_maker: MagicMock
+ ) -> None:
+ mock_get_session_maker.return_value = _local_user_session_maker()
+
+ users = get_local_users(offset=0, limit=10)
+
+ assert users == [
+ ResendUser(
+ id='00000000-0000-0000-0000-000000000001',
+ first_name='Ada',
+ last_name='Lovelace',
+ ),
+ ResendUser(
+ id='00000000-0000-0000-0000-000000000004',
+ first_name='Prince',
+ last_name=None,
+ ),
+ ]
+
+ @patch('enterprise.sync.resend_keycloak.User', _LocalUser)
+ @patch('enterprise.sync.resend_keycloak._get_session_maker')
+ def test_get_total_local_users_counts_real_database_emails(
+ self, mock_get_session_maker: MagicMock
+ ) -> None:
+ mock_get_session_maker.return_value = _local_user_session_maker()
+
+ assert get_total_local_users() == 2
+
+
class TestSendWelcomeEmail:
"""Tests for send_welcome_email function."""
@@ -265,3 +365,98 @@ class TestAddContactToResend:
assert result == {'id': 'contact_123'}
assert mock_create.call_count == 2
+
+
+class TestSyncUsersToResend:
+ @patch('enterprise.sync.resend_keycloak.time.sleep')
+ @patch('enterprise.sync.resend_keycloak.send_welcome_email')
+ @patch('enterprise.sync.resend_keycloak.add_contact_to_resend')
+ @patch('enterprise.sync.resend_keycloak.get_local_users')
+ @patch('enterprise.sync.resend_keycloak.get_total_local_users')
+ @patch('enterprise.sync.resend_keycloak._backfill_existing_resend_contacts')
+ @patch('enterprise.sync.resend_keycloak._get_resend_synced_user_store')
+ def test_sync_reads_local_users_and_skips_synced_or_invalid_emails(
+ self,
+ mock_get_store: MagicMock,
+ mock_backfill: MagicMock,
+ mock_get_total: MagicMock,
+ mock_get_local_users: MagicMock,
+ mock_add_contact: MagicMock,
+ mock_send_welcome: MagicMock,
+ mock_sleep: MagicMock,
+ ) -> None:
+ store = MagicMock()
+ store.get_synced_emails_for_audience.return_value = {'[email protected]'}
+ mock_get_store.return_value = store
+ mock_backfill.return_value = 1
+ mock_get_total.return_value = 3
+ mock_get_local_users.return_value = [
+ ResendUser(id='user-1', email='[email protected]'),
+ ResendUser(id='user-2', email='[email protected]'),
+ ResendUser(
+ id='user-3',
+ first_name='Ada',
+ last_name='Lovelace',
+ ),
+ ]
+
+ sync_users_to_resend()
+
+ mock_get_total.assert_called_once_with()
+ mock_get_local_users.assert_called_once_with(0, BATCH_SIZE)
+ store.mark_user_synced.assert_called_once_with(
+ audience_id='test_audience_id',
+ user_id='user-3',
+ )
+ mock_add_contact.assert_called_once_with(
+ 'test_audience_id', '[email protected]', 'Ada', 'Lovelace'
+ )
+ mock_send_welcome.assert_called_once_with('[email protected]', 'Ada', 'Lovelace')
+ assert mock_sleep.call_count == 2
+
+ @patch('enterprise.sync.resend_keycloak.time.sleep')
+ @patch('enterprise.sync.resend_keycloak.send_welcome_email')
+ @patch('enterprise.sync.resend_keycloak.add_contact_to_resend')
+ @patch('enterprise.sync.resend_keycloak.get_local_users')
+ @patch('enterprise.sync.resend_keycloak.get_total_local_users')
+ @patch('enterprise.sync.resend_keycloak._backfill_existing_resend_contacts')
+ @patch('enterprise.sync.resend_keycloak._get_resend_synced_user_store')
+ def test_sync_reads_multiple_local_user_batches(
+ self,
+ mock_get_store: MagicMock,
+ mock_backfill: MagicMock,
+ mock_get_total: MagicMock,
+ mock_get_local_users: MagicMock,
+ mock_add_contact: MagicMock,
+ mock_send_welcome: MagicMock,
+ mock_sleep: MagicMock,
+ ) -> None:
+ store = MagicMock()
+ store.get_synced_emails_for_audience.return_value = set()
+ mock_get_store.return_value = store
+ mock_backfill.return_value = 0
+ mock_get_total.return_value = BATCH_SIZE + 1
+ mock_get_local_users.side_effect = [
+ [ResendUser(id='user-1', email='[email protected]')],
+ [],
+ ]
+
+ sync_users_to_resend()
+
+ mock_get_total.assert_called_once_with()
+ mock_get_local_users.assert_has_calls(
+ [call(0, BATCH_SIZE), call(BATCH_SIZE, BATCH_SIZE)]
+ )
+ assert mock_get_local_users.call_count == 2
+ store.mark_user_synced.assert_called_once_with(
+ audience_id='test_audience_id',
+ user_id='user-1',
+ )
+ mock_add_contact.assert_called_once_with(
+ 'test_audience_id', '[email protected]', None, None
+ )
+ mock_send_welcome.assert_called_once_with('[email protected]', None, None)
+ assert mock_sleep.call_count == 2