def test_download( approved_workbasket, client, valid_user, hmrc_storage, s3_resource, s3_object_names, settings, ): client.force_login(valid_user) bucket = "hmrc" settings.HMRC_STORAGE_BUCKET_NAME = bucket s3_resource.create_bucket(Bucket="hmrc") with patch( "exporter.storages.HMRCStorage.save", wraps=MagicMock(side_effect=hmrc_storage.save), ): upload_workbaskets.apply() url = reverse("workbaskets:workbasket-download") response = client.get(url) # the url signature will always be unique, so we can only compare the first part of the url expected_url, _ = s3_resource.meta.client.generate_presigned_url( ClientMethod="get_object", ExpiresIn=3600, Params={ "Bucket": settings.HMRC_STORAGE_BUCKET_NAME, "Key": s3_object_names("hmrc")[0], }, ).split("?", 1) assert response.status_code == 302 assert expected_url in response.url
def test_notify_hmrc_retries(mock_post, settings, hmrc_storage, responses): """Verify if HMRCStorage.save raises a boto.ConnectionError the task upload_workflow task retries based on settings.EXPORTER_UPLOAD_MAX_RETRIES.""" responses.add( responses.POST, url="https://test-api.service.hmrc.gov.uk/oauth/token", json={ "access_token": "access_token", "token_type": "bearer", "expires_in": 3600, "refresh_token": "refresh_token", "scope": "write:transfer-complete write:transfer-ready", }, ) settings.EXPORTER_DISABLE_NOTIFICATION = False # Notifications are disabled, as they are not being tested here. settings.EXPORTER_UPLOAD_MAX_RETRIES = 1 with ApprovedTransactionFactory.create(): RegulationFactory.create(), FootnoteTypeFactory.create() # On the first run, the test makes .save trigger APIRequestError which # causes the task to retry, raising SentinelError. with pytest.raises(SentinelError): upload_workbaskets.apply().get() assert mock_post.call_count == 2
def test_upload_task_uploads_approved_workbasket_to_s3( approved_transaction, hmrc_storage, s3, s3_object_exists, settings, ): """Exercise HMRCStorage and verify content is saved to bucket.""" expected_bucket = "test-hmrc" expected_key = "tohmrc/staging/DIT200001.xml" settings.HMRC_STORAGE_BUCKET_NAME = expected_bucket RegulationFactory.create(transaction=approved_transaction) FootnoteTypeFactory.create(transaction=approved_transaction) with mock.patch( "exporter.storages.HMRCStorage.save", wraps=mock.MagicMock(side_effect=hmrc_storage.save), ) as mock_save: upload_workbaskets.apply() mock_save.assert_called_once() assert s3_object_exists( expected_bucket, expected_key, ), "File was not uploaded with expected name." envelope = s3.get_object(Bucket=expected_bucket, Key=expected_key)["Body"].read() xml = etree.XML(envelope) validate_taric_xml_record_order(xml) # tuples of (record_code, subrecord_code). expected_codes = [ ("100", "00"), # FootnoteType ("100", "05"), # FootnoteType description ("150", "00"), # Group ("150", "05"), # Group description ("285", "00"), # Regulation ] codes = taric_xml_record_codes(xml) assert codes == expected_codes
def test_upload_workbaskets_retries(mock_save, settings): """Verify if HMRCStorage.save raises a boto.ConnectionError the task upload_workflow task retries based on settings.EXPORTER_UPLOAD_MAX_RETRIES.""" settings.EXPORTER_DISABLE_NOTIFICATION = True # Notifications are disabled, as they are not being tested here. settings.EXPORTER_UPLOAD_MAX_RETRIES = 1 with ApprovedTransactionFactory.create(): RegulationFactory.create(), FootnoteTypeFactory.create() # On the first run, the test makes .save trigger ConnectionError which # should not be propagated to here, but should make the task retry. # On first retry, the SentinelError defined above is triggered, # and caught here. with pytest.raises(SentinelError): upload_workbaskets.apply().get() assert mock_save.call_count == 2
def test_upload_workbaskets_uploads_approved_workbasket_to_s3( approved_transaction, hmrc_storage, s3, s3_bucket_names, s3_object_names, settings, ): """Exercise HMRCStorage and verify content is saved to bucket.""" assert WorkBasket.objects.filter( status=WorkflowStatus.SENT).exists() == False now = datetime.now() expected_bucket = "hmrc" expected_key = f"tohmrc/staging/DIT{now:%y}0001.xml" settings.HMRC_STORAGE_BUCKET_NAME = expected_bucket RegulationFactory.create( transaction=approved_transaction, regulation_group__transaction=approved_transaction, ) FootnoteTypeFactory.create(transaction=approved_transaction) with mock.patch( "exporter.storages.HMRCStorage.save", wraps=mock.MagicMock(side_effect=hmrc_storage.save), ) as mock_save: upload_workbaskets.apply() mock_save.assert_called_once() assert expected_bucket in s3_bucket_names() assert expected_key in s3_object_names(expected_bucket) s3_object = s3.get_object(Bucket=expected_bucket, Key=expected_key) filename = os.path.basename(expected_key) assert s3_object.get( "ContentDisposition") == f"attachment; filename={filename}" envelope = s3_object["Body"].read() xml = etree.XML(envelope) validate_taric_xml_record_order(xml) # tuples of (record_code, subrecord_code). expected_codes = [ ("100", "00"), # FootnoteType ("100", "05"), # FootnoteType description ("150", "00"), # Group ("150", "05"), # Group description ("285", "00"), # Regulation ] codes = taric_xml_record_codes(xml) assert codes == expected_codes assert WorkBasket.objects.filter( status=WorkflowStatus.SENT).exists() == True