Skip to content

Commit

Permalink
Wrap pubsub publish in try catch. Mock out pubsub call with autofixtu…
Browse files Browse the repository at this point in the history
…re. Unit test fixes.
  • Loading branch information
seeker25 committed Apr 30, 2024
1 parent af86eb7 commit b73749b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion auth-api/src/auth_api/services/affiliation_invitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def send_affiliation_invitation(affiliation_invitation: AffiliationInvitationMod
'emailAddresses': email_addresses,
'orgName': from_org_name
}
notification_type = 'affiliationInvitation'
notification_type = QueueMessageTypes.AFFILIATION_INVITATION.value

if affiliation_invitation_type == AffiliationInvitationType.EMAIL.value:
# if MAGIC LINK type, add data for magic link
Expand Down
5 changes: 4 additions & 1 deletion auth-api/src/auth_api/utils/account_mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ def publish_to_mailer(notification_type, data=None, source='sbc-auth-auth-api'):
type=notification_type,
data=data
)
queue.publish(current_app.config.get('ACCOUNT_MAILER_TOPIC'), GcpQueue.to_queue_message(cloud_event))
try:
queue.publish(current_app.config.get('ACCOUNT_MAILER_TOPIC'), GcpQueue.to_queue_message(cloud_event))
except Exception as e: # NOQA # pylint: disable=broad-except
current_app.logger.error(f'Failed to publish to mailer: {str(e)}')
10 changes: 10 additions & 0 deletions auth-api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,13 @@ def mock_auth(): # pylint: disable=unused-argument; mocks of library methods

monkeypatch.setattr('auth_api.utils.user_context._get_token', mock_auth)
monkeypatch.setattr('auth_api.utils.user_context._get_token_info', token_info)


@pytest.fixture(autouse=True)
def mock_pub_sub_call(monkeypatch):
"""Mock pub sub call."""

def publish(topic, message):
return True

monkeypatch.setattr('auth_api.services.gcp_queue.queue.publish', publish)
18 changes: 8 additions & 10 deletions auth-api/tests/unit/services/test_affiliation_invitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from auth_api.services import Org as OrgService
from auth_api.services import User
from auth_api.utils import roles
from auth_api.utils.enums import InvitationStatus
from auth_api.utils.enums import InvitationStatus, QueueMessageTypes
from tests.utilities.factory_scenarios import TestContactInfo, TestEntityInfo, TestJwtClaims, TestOrgInfo, TestUserInfo
from tests.utilities.factory_utils import (
factory_affiliation_invitation, factory_entity_model, factory_membership_model, factory_user_model,
Expand Down Expand Up @@ -507,8 +507,7 @@ def test_send_affiliation_invitation_magic_link(publish_to_mailer_mock,
'contextUrl': 'None/RnJvbSB0aGUgbW9vbiBpbmMu/affiliationInvitation/acceptToken/ABCD'
}

publish_to_mailer_mock.assert_called_with(notification_type='affiliationInvitation',
org_id=affiliation_invitation.from_org.id,
publish_to_mailer_mock.assert_called_with(notification_type=QueueMessageTypes.AFFILIATION_INVITATION.value,
data=expected_data)


Expand Down Expand Up @@ -538,9 +537,8 @@ def test_send_affiliation_invitation_request_sent(publish_to_mailer_mock,
'toOrgBranchName': affiliation_invitation.to_org.branch_name,
'additionalMessage': additional_message
}

publish_to_mailer_mock.assert_called_with(notification_type='affiliationInvitationRequest',
org_id=affiliation_invitation.from_org.id,
notification_type = QueueMessageTypes.AFFILIATION_INVITATION_REQUEST.value
publish_to_mailer_mock.assert_called_with(notification_type=notification_type,
data=expected_data)


Expand Down Expand Up @@ -581,8 +579,8 @@ def test_send_affiliation_invitation_request_authorized(publish_to_mailer_mock,
'isAuthorized': True
}

publish_to_mailer_mock.assert_called_with(notification_type='affiliationInvitationRequestAuthorization',
org_id=affiliation_invitation.from_org.id,
notification_type = QueueMessageTypes.AFFILIATION_INVITATION_REQUEST_AUTHORIZATION.value
publish_to_mailer_mock.assert_called_with(notification_type=notification_type,
data=expected_data)


Expand Down Expand Up @@ -625,8 +623,8 @@ def test_send_affiliation_invitation_request_refused(publish_to_mailer_mock,
'isAuthorized': False
}

publish_to_mailer_mock.assert_called_with(notification_type='affiliationInvitationRequestAuthorization',
org_id=affiliation_invitation.from_org.id,
notification_type = QueueMessageTypes.AFFILIATION_INVITATION_REQUEST_AUTHORIZATION.value
publish_to_mailer_mock.assert_called_with(notification_type=notification_type,
data=expected_data)


Expand Down

0 comments on commit b73749b

Please sign in to comment.