Skip to content

Commit

Permalink
Fixing mocks in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
galvana committed Jan 11, 2025
1 parent e06bb4a commit 7badf89
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/fides/api/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def get_config_proxy(db: Session = Depends(get_db)) -> ConfigProxy:


def get_cache() -> Generator:
"""Return a connection to our redis cache"""
"""Return a connection to our Redis cache"""
if not CONFIG.redis.enabled:
raise FunctionalityNotConfigured(
"Application redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a redis cache."
"Application redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a Redis cache."
)
yield get_redis_connection()

Expand Down
35 changes: 9 additions & 26 deletions src/fides/api/api/v1/endpoints/consent_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
HTTP_403_FORBIDDEN,
HTTP_404_NOT_FOUND,
HTTP_422_UNPROCESSABLE_ENTITY,
HTTP_500_INTERNAL_SERVER_ERROR,
)

from fides.api.api.deps import get_config_proxy, get_db
from fides.api.api.deps import get_config_proxy, get_db, get_messaging_service
from fides.api.common_exceptions import (
FunctionalityNotConfigured,
IdentityVerificationException,
MessageDispatchException,
)
from fides.api.db.seed import DEFAULT_CONSENT_POLICY
from fides.api.models.messaging import get_messaging_method
Expand All @@ -38,7 +36,7 @@
)
from fides.api.models.property import Property
from fides.api.oauth.utils import verify_oauth_client
from fides.api.schemas.messaging.messaging import MessagingActionType, MessagingMethod
from fides.api.schemas.messaging.messaging import MessagingMethod
from fides.api.schemas.privacy_request import BulkPostPrivacyRequests
from fides.api.schemas.privacy_request import Consent as ConsentSchema
from fides.api.schemas.privacy_request import (
Expand All @@ -52,7 +50,6 @@
VerificationCode,
)
from fides.api.schemas.redis_cache import Identity
from fides.api.service.messaging.message_dispatch_service import message_send_enabled
from fides.api.util.api_router import APIRouter
from fides.api.util.consent_util import (
get_or_create_fides_user_device_id_provided_identity,
Expand All @@ -69,10 +66,7 @@
)
from fides.config import CONFIG
from fides.config.config_proxy import ConfigProxy
from fides.services.messaging.messaging_service import (
MessagingService,
send_verification_code_to_user,
)
from fides.services.messaging.messaging_service import MessagingService
from fides.services.privacy_request.privacy_request_service import PrivacyRequestService

router = APIRouter(tags=["Consent"], prefix=V1_URL_PREFIX)
Expand Down Expand Up @@ -184,12 +178,13 @@ def create_consent_request(
*,
db: Session = Depends(get_db),
config_proxy: ConfigProxy = Depends(get_config_proxy),
messaging_service: MessagingService = Depends(get_messaging_service),
data: ConsentRequestCreate,
) -> ConsentRequestResponse:
"""Creates a verification code for the user to verify access to manage consent preferences."""
if not CONFIG.redis.enabled:
raise FunctionalityNotConfigured(
"Application redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a redis cache."
"Application Redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a Redis cache."
)
# TODO: (PROD-2142)- pass in property id here
if data.property_id:
Expand Down Expand Up @@ -229,22 +224,10 @@ def create_consent_request(
consent_request.persist_custom_privacy_request_fields(
db=db, custom_privacy_request_fields=data.custom_privacy_request_fields
)
if message_send_enabled(
db,
data.property_id,
MessagingActionType.SUBJECT_IDENTITY_VERIFICATION,
not config_proxy.execution.disable_consent_identity_verification,
):
try:
send_verification_code_to_user(
db, consent_request, data.identity, data.property_id
)
except MessageDispatchException as exc:
logger.error("Error sending the verification code message: {}", str(exc))
raise HTTPException(
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error sending the verification code message: {str(exc)}",
)

messaging_service.send_verification_code(
consent_request, data.identity, data.property_id
)

return ConsentRequestResponse(
consent_request_id=consent_request.id,
Expand Down
6 changes: 6 additions & 0 deletions src/fides/api/util/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def decode_obj(bs: Optional[str]) -> Optional[Dict[str, Any]]:

def get_cache(should_log: Optional[bool] = False) -> FidesopsRedis:
"""Return a singleton connection to our Redis cache"""

if not CONFIG.redis.enabled:
raise common_exceptions.FunctionalityNotConfigured(
"Application Redis cache required, but it is currently disabled! Please update your application configuration to enable integration with a Redis cache."
)

global _connection # pylint: disable=W0603
if _connection is None:
logger.debug("Creating new Redis connection...")
Expand Down
11 changes: 6 additions & 5 deletions src/fides/services/privacy_request/privacy_request_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sqlalchemy.orm import Session

from fides.api.common_exceptions import (
FunctionalityNotConfigured,
IdentityNotFoundException,
MessageDispatchException,
PolicyNotFoundException,
Expand Down Expand Up @@ -46,7 +47,6 @@
)
from fides.api.tasks import MESSAGING_QUEUE_NAME
from fides.api.util.cache import cache_task_tracking_key
from fides.api.util.logger import Pii
from fides.api.util.logger_context_utils import LoggerContextKeys, log_context
from fides.config.config_proxy import ConfigProxy
from fides.services.messaging.messaging_service import (
Expand Down Expand Up @@ -203,15 +203,16 @@ def create_privacy_request(

return privacy_request

except FunctionalityNotConfigured as exc:
logger.error(f"{exc.__class__.__name__}: {str(exc)}")
raise exc
except MessageDispatchException as exc:
kwargs["privacy_request_id"] = privacy_request.id
raise PrivacyRequestError(
"Verification message could not be sent.", kwargs
) from exc
except Exception as exc:
as_string = Pii(str(exc))
error_cls = str(exc.__class__.__name__)
logger.error(f"Exception {error_cls}: {as_string}")
logger.error(f"{exc.__class__.__name__}: {str(exc)}")
raise PrivacyRequestError("This record could not be added", kwargs) from exc

def create_bulk_privacy_requests(
Expand Down Expand Up @@ -371,7 +372,7 @@ def approve_privacy_requests(
},
)

if suppress_notification:
if not suppress_notification:
self.messaging_service.send_request_approved(privacy_request)
queue_privacy_request(privacy_request.id)

Expand Down
18 changes: 9 additions & 9 deletions tests/ops/api/v1/endpoints/test_consent_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def set_notification_service_type_to_twilio_sms(self, db):
"sovrn_email_connection_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request(self, mock_dispatch_message, api_client, url):
data = {"identity": {"email": "test@example.com"}}
response = api_client.post(url, json=data)
Expand All @@ -258,7 +258,7 @@ def test_consent_request(self, mock_dispatch_message, api_client, url):
"sovrn_email_connection_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_identity_present(
self,
mock_dispatch_message,
Expand All @@ -282,13 +282,13 @@ def test_consent_request_redis_disabled(self, api_client, url):
data = {"identity": {"email": "test@example.com"}}
response = api_client.post(url, json=data)
assert response.status_code == 500
assert "redis cache required" in response.json()["message"]
assert "Redis cache required" in response.json()["message"]

@pytest.mark.usefixtures(
"messaging_config",
"sovrn_email_connection_config",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_subject_verification_disabled_no_email(
self, mock_dispatch_message, api_client, url
):
Expand All @@ -302,7 +302,7 @@ def test_consent_request_subject_verification_disabled_no_email(
"sovrn_email_connection_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_phone_number(self, mock_dispatch_message, api_client, url):
data = {"identity": {"phone_number": "+3368675309"}}
response = api_client.post(url, json=data)
Expand All @@ -314,7 +314,7 @@ def test_consent_request_phone_number(self, mock_dispatch_message, api_client, u
"sovrn_email_connection_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_email_and_phone_use_config(
self,
mock_dispatch_message,
Expand All @@ -341,7 +341,7 @@ def test_consent_request_email_and_phone_use_config(
"sovrn_email_connection_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_email_and_phone_default_to_email(
self,
mock_dispatch_message,
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_consent_request_email_and_phone_default_to_email(
"messaging_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_with_source(
self,
mock_dispatch_message,
Expand All @@ -399,7 +399,7 @@ def test_consent_request_with_source(
"sovrn_email_connection_config",
"subject_identity_verification_required",
)
@patch("fides.api.service._verification.dispatch_message")
@patch("fides.services.messaging.messaging_service.dispatch_message")
def test_consent_request_with_custom_privacy_request_fields(
self,
mock_dispatch_message,
Expand Down
Loading

0 comments on commit 7badf89

Please sign in to comment.