Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
prv-proton committed Dec 9, 2024
1 parent 73919d1 commit 0eb8a25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
39 changes: 24 additions & 15 deletions backend/lcfs/tests/email/test_email_service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from lcfs.web.api.base import NotificationTypeEnum
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from lcfs.web.api.email.repo import CHESEmailRepository
from lcfs.web.api.email.services import CHESEmailService
import os


@pytest.fixture
def mock_email_repo():
return AsyncMock(spec=CHESEmailRepository)


@pytest.fixture
def mock_environment_vars():
with patch("lcfs.web.api.email.services.settings") as mock_settings:
Expand All @@ -19,14 +22,15 @@ def mock_environment_vars():
mock_settings.ches_sender_name = "Mock Notification System"
yield mock_settings


@pytest.mark.anyio
async def test_send_notification_email_success(mock_email_repo, mock_environment_vars):
# Arrange
notification_type = "INITIATIVE_APPROVED"
notification_type = NotificationTypeEnum.BCEID__COMPLIANCE_REPORT__DIRECTOR_ASSESSMENT
notification_context = {
"subject": "Test Notification",
"user_name": "John Doe",
"message_body": "Test message content"
"message_body": "Test message content",
}
organization_id = 1

Expand All @@ -46,21 +50,24 @@ async def test_send_notification_email_success(mock_email_repo, mock_environment
# Assert
assert result is True
mock_email_repo.get_subscribed_user_emails.assert_called_once_with(
notification_type, organization_id
notification_type.value, organization_id # Ensure value is passed
)
service._render_email_template.assert_called_once_with(
notification_type, notification_context
notification_type.value, notification_context
)
service.send_email.assert_called_once()


@pytest.mark.anyio
async def test_send_notification_email_no_recipients(mock_email_repo, mock_environment_vars):
async def test_send_notification_email_no_recipients(
mock_email_repo, mock_environment_vars
):
# Arrange
notification_type = "INITIATIVE_APPROVED"
notification_type = NotificationTypeEnum.BCEID__TRANSFER__PARTNER_ACTIONS
notification_context = {
"subject": "Test Notification",
"user_name": "John Doe",
"message_body": "Test message content"
"message_body": "Test message content",
}
organization_id = 1

Expand All @@ -76,18 +83,19 @@ async def test_send_notification_email_no_recipients(mock_email_repo, mock_envir
# Assert
assert result is False
mock_email_repo.get_subscribed_user_emails.assert_called_once_with(
notification_type, organization_id
notification_type.value, organization_id # Ensure value is passed
)


@pytest.mark.anyio
async def test_get_ches_token_success(mock_environment_vars):
# Arrange
mock_token = "mock_access_token"
with patch('requests.post') as mock_post:
with patch("requests.post") as mock_post:
mock_response = MagicMock()
mock_response.json.return_value = {
"access_token": mock_token,
"expires_in": 3600
"access_token": mock_token,
"expires_in": 3600,
}
mock_post.return_value = mock_response

Expand All @@ -100,14 +108,15 @@ async def test_get_ches_token_success(mock_environment_vars):
assert token == mock_token
mock_post.assert_called_once()


@pytest.mark.anyio
async def test_get_ches_token_cached(mock_environment_vars):
# Arrange
with patch('requests.post') as mock_post:
with patch("requests.post") as mock_post:
mock_response = MagicMock()
mock_response.json.return_value = {
"access_token": "initial_token",
"expires_in": 3600
"access_token": "initial_token",
"expires_in": 3600,
}
mock_post.return_value = mock_response

Expand All @@ -124,4 +133,4 @@ async def test_get_ches_token_cached(mock_environment_vars):

# Assert
assert first_token == second_token
mock_post.assert_not_called()
mock_post.assert_not_called()
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async def test_update_initiative_agreement(service, mock_repo, mock_request):
mock_repo.get_initiative_agreement_by_id.return_value = mock_agreement
mock_repo.get_initiative_agreement_status_by_name.return_value = mock_status
mock_repo.update_initiative_agreement.return_value = mock_agreement
service.notfn_service = AsyncMock()

update_data = InitiativeAgreementUpdateSchema(
initiative_agreement_id=1,
Expand Down

0 comments on commit 0eb8a25

Please sign in to comment.