Skip to content

Commit

Permalink
fix: Feedback changes plus add heartbeat name to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
julianajlk committed Nov 9, 2023
1 parent 18b8f12 commit 7f9caa7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import logging
import tempfile

import requests
import opsgenie_sdk
import requests
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import transaction
Expand All @@ -32,16 +32,16 @@ def add_arguments(self, parser):
help='File size MB threshold, under which we will not import it. Use default if argument not specified'
)

def hit_opsgenie_heartbeat(self):
def _hit_opsgenie_heartbeat(self):
"""
Hit OpsGenie heartbeat to determine whether the fallback job is running.
Hit OpsGenie heartbeat to indicate that the fallback job has run successfully recently.
"""
og_sdk_config = opsgenie_sdk.configuration.Configuration()
og_sdk_config.api_key['Authorization'] = settings.OPSGENIE_API_KEY
og_api_client = opsgenie_sdk.api_client.ApiClient(configuration=og_sdk_config)
og_heartbeat_api = opsgenie_sdk.HeartbeatApi(api_client=og_api_client)

heartbeat_name = 'sanctions-sdn-fallback-job'
heartbeat_name = settings.OPSGENIE_HEARTBEAT_NAME

logger.info(f'Calling opsgenie heartbeat for {heartbeat_name}')
response = og_heartbeat_api.ping(heartbeat_name)
Expand All @@ -65,7 +65,7 @@ def handle(self, *args, **options):
"Timeout threshold (in seconds): %s", timeout)
raise
except Exception as e:
logger.warning("Sanctions SDNFallback: DOWNLOAD FAILURE: Exception occurred: [%s]", e)
logger.exception("Sanctions SDNFallback: DOWNLOAD FAILURE: Exception occurred: [%s]", e)
raise

if download.status_code != 200:
Expand Down Expand Up @@ -93,7 +93,7 @@ def handle(self, *args, **options):
" and SDNFallbackData models."
)
)
self.hit_opsgenie_heartbeat()
self._hit_opsgenie_heartbeat()
else:
logger.warning(
"Sanctions SDNFallback: DOWNLOAD FAILURE: file too small! "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
Tests for Django management command to download CSV for SDN Fallback.
"""
from unittest import mock

import requests
import responses
from django.core.management import call_command
from django.test import TestCase

from mock import patch
from testfixtures import LogCapture, StringComparison

Expand All @@ -22,42 +23,60 @@ class TestResponse:
def __init__(self, **kwargs):
self.__dict__ = kwargs

# mock response for CSV download: just one row of the CSV
# mock response for CSV download: just one row of the CSV
self.test_response = TestResponse(**{
'content': bytes('_id,source,entity_number,type,programs,name,title,addresses,federal_register_notice,start_date,end_date,standard_order,license_requirement,license_policy,call_sign,vessel_type,gross_tonnage,gross_registered_tonnage,vessel_flag,vessel_owner,remarks,source_list_url,alt_names,citizenships,dates_of_birth,nationalities,places_of_birth,source_information_url,ids\ne5a9eff64cec4a74ed5e9e93c2d851dc2d9132d2,Denied Persons List (DPL) - Bureau of Industry and Security,,,, MICKEY MOUSE,,"123 S. TEST DRIVE, SCOTTSDALE, AZ, 85251",82 F.R. 48792 10/01/2017,2017-10-18,2020-10-15,Y,,,,,,,,,FR NOTICE ADDED,http://bit.ly/1Qi5heF,,,,,,http://bit.ly/1iwxiF0', 'utf-8'), # pylint: disable=line-too-long
'content': bytes(
'_id,source,entity_number,type,programs,name,title,addresses,federal_register_notice,start_date,'
'end_date,standard_order,license_requirement,license_policy,call_sign,vessel_type,gross_tonnage,'
'gross_registered_tonnage,vessel_flag,vessel_owner,remarks,source_list_url,alt_names,citizenships,'
'dates_of_birth,nationalities,places_of_birth,source_information_url,'
'ids\ne5a9eff64cec4a74ed5e9e93c2d851dc2d9132d2,Denied Persons List (DPL) - Bureau of Industry and '
'Security,,,, MICKEY MOUSE,,"123 S. TEST DRIVE, SCOTTSDALE, AZ, 85251",'
'82 F.R. 48792 10/01/2017,2017-10-18,2020-10-15,Y,,,,,,,,,FR NOTICE ADDED,'
'http://bit.ly/1Qi5heF,,,,,,http://bit.ly/1iwxiF0', 'utf-8'
),
'status_code': 200,
})

self.test_response_500 = TestResponse(**{
'status_code': 500,
})

@patch('requests.Session.get')
def test_handle_pass(self, mock_response):
""" Test using mock response from setup, using threshold it will clear"""

mock_response.return_value = self.test_response

with LogCapture(self.LOGGER_NAME) as log:
call_command('populate_sdn_fallback_data_and_metadata', '--threshold=0.0001')

log.check(
(
self.LOGGER_NAME,
'INFO',
StringComparison(
r'(?s)Sanctions SDNFallback: IMPORT SUCCESS: Imported SDN CSV\. Metadata id.*')
),
(
self.LOGGER_NAME,
'INFO',
"Sanctions SDNFallback: DOWNLOAD SUCCESS: Successfully downloaded the SDN CSV."
"""
Test using mock response from setup, using threshold it will clear.
"""
with mock.patch(
'sanctions.apps.sanctions.management.commands.'
'populate_sdn_fallback_data_and_metadata.Command._hit_opsgenie_heartbeat'
) as mock_og_heartbeat:
mock_response.return_value = self.test_response

with LogCapture(self.LOGGER_NAME) as log:
call_command('populate_sdn_fallback_data_and_metadata', '--threshold=0.0001')

log.check(
(
self.LOGGER_NAME,
'INFO',
StringComparison(
r'(?s)Sanctions SDNFallback: IMPORT SUCCESS: Imported SDN CSV\. Metadata id.*')
),
(
self.LOGGER_NAME,
'INFO',
"Sanctions SDNFallback: DOWNLOAD SUCCESS: Successfully downloaded the SDN CSV."
)
)
)

assert mock_og_heartbeat.is_called()

@patch('requests.Session.get')
def test_handle_fail_size(self, mock_response):
""" Test using mock response from setup, using threshold it will NOT clear"""

"""
Test using mock response from setup, using threshold it will NOT clear.
"""
mock_response.return_value = self.test_response

with LogCapture(self.LOGGER_NAME) as log:
Expand All @@ -72,11 +91,11 @@ def test_handle_fail_size(self, mock_response):
"(0.000642 MB vs threshold of 1.0 MB)"
)
)
self.assertEqual('CSV file download did not meet threshold given', str(e.exception))
assert 'CSV file download did not meet threshold given' == str(e.exception)

@patch('requests.Session.get')
def test_handle_500_response(self, mock_response):
""" Test using url for 500 error"""
""" Test using url for 500 error. """
mock_response.return_value = self.test_response_500
with LogCapture(self.LOGGER_NAME) as log:
with self.assertRaises(Exception) as e:
Expand All @@ -89,7 +108,7 @@ def test_handle_500_response(self, mock_response):
"Sanctions SDNFallback: DOWNLOAD FAILURE: Status code was: [500]"
)
)
self.assertEqual("('CSV download url got an unsuccessful response code: ', 500)", str(e.exception))
assert "('CSV download url got an unsuccessful response code: ', 500)" == str(e.exception)


class TestDownloadSDNFallbackCommandExceptions(TestCase):
Expand All @@ -112,12 +131,12 @@ def test_general_exception(self):
log.check(
(
self.LOGGER_NAME,
'WARNING',
'ERROR',
"Sanctions SDNFallback: DOWNLOAD FAILURE: Exception occurred: [%s]" % self.ERROR_MESSAGE
)
)

self.assertEqual(self.ERROR_MESSAGE, str(e.exception))
assert self.ERROR_MESSAGE == str(e.exception)

@responses.activate
def test_timeout_exception(self):
Expand All @@ -136,4 +155,4 @@ def test_timeout_exception(self):
)
)

self.assertEqual(self.ERROR_MESSAGE, str(e.exception))
assert self.ERROR_MESSAGE == str(e.exception)
17 changes: 9 additions & 8 deletions sanctions/apps/sanctions/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@
Factoryboy factories for Sanctions app.
"""
import logging
from datetime import datetime, timedelta

import factory
from django.utils import timezone
from faker import Faker

from sanctions.apps.sanctions.models import SDNFallbackData, SDNFallbackMetadata

# Silence faker locale warnings
logging.getLogger("faker").setLevel(logging.ERROR)


class SDNFallbackMetadataFactory(factory.django.DjangoModelFactory):
"""
Test factory for the `SDNFallbackMetadata` model.
"""
class Meta:
model = SDNFallbackMetadata

file_checksum = factory.Sequence(lambda n: Faker().md5())
import_state = 'New'
download_timestamp = datetime.now() - timedelta(days=10)
download_timestamp = timezone.now() - timezone.timedelta(days=10)

class Meta:
model = SDNFallbackMetadata


class SDNFallbackDataFactory(factory.django.DjangoModelFactory):
"""
Test factory for the `SDNFallbackData` model.
"""
class Meta:
model = SDNFallbackData

sdn_fallback_metadata = factory.SubFactory(SDNFallbackMetadataFactory)
source = "Specially Designated Nationals (SDN) - Treasury Department"
sdn_type = "Individual"
names = factory.Faker('name')
addresses = factory.Faker('address')
countries = factory.Faker('country_code')

class Meta:
model = SDNFallbackData
12 changes: 2 additions & 10 deletions sanctions/apps/sanctions/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@
from django.test import TestCase
from testfixtures import LogCapture

# from ecommerce.extensions.payment.exceptions import SDNFallbackDataEmptyError
from sanctions.apps.sanctions.models import (
SanctionsCheckFailure,
SDNFallbackData,
SDNFallbackMetadata
)
from sanctions.apps.sanctions.tests.factories import (
SDNFallbackMetadataFactory,
SDNFallbackDataFactory
)
from sanctions.apps.sanctions.models import SanctionsCheckFailure, SDNFallbackData, SDNFallbackMetadata
from sanctions.apps.sanctions.tests.factories import SDNFallbackDataFactory, SDNFallbackMetadataFactory


class SanctionsCheckFailureTests(TestCase):
Expand Down
1 change: 1 addition & 0 deletions sanctions/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,4 @@ def root(*path_fragments):
LOGGING = get_logger_config(debug=DEBUG)

OPSGENIE_API_KEY = ''
OPSGENIE_HEARTBEAT_NAME = ''

0 comments on commit 7f9caa7

Please sign in to comment.