Skip to content

Commit

Permalink
Re-write automated sales invoice transaction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maniamartial committed Jun 12, 2024
1 parent 08b03e0 commit 6eabfee
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 162 deletions.
18 changes: 10 additions & 8 deletions burundi_compliance/burundi_compliance/api_classes/add_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,25 @@ def _get_headers(self) -> dict:

def post_invoice(self, invoice_data) -> dict:
try:
#make_post_request doesn't seem to work well here
#It doesn't give me the freedom to handle the response
# Make the POST request
response = requests.post(
self.BASE_ADD_INVOICE_API_URL,
json=invoice_data,
headers=self._get_headers()
)

response_data = json.loads(response.text)
success=response_data.get("success")
if success==True:
response_data = response.json()
success = response_data.get("success")

if success:
return self._handle_response(response_data, invoice_data)
else:
self._create_or_update_integration_request(response_data, invoice_data)
self._create_or_update_integration_request(response_data, invoice_data)
return response_data
except Exception as e:
frappe.log_error(f"Error during API request: {str(e)}")
return {"success": False, "error": str(e)}



def update_sales_invoice(self, response):
Expand Down Expand Up @@ -114,7 +116,7 @@ def update_sales_invoice(self, response):
frappe.log_error(f"Error updating Sales Invoice {invoice_number}: {str(e)}")

# Create Integration Request document for failure
self.create_integration_request(response, False, str(e))
self._create_or_update_integration_request(response, {"invoice_number": invoice_number})


def get_doc(self, invoice_data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,46 @@

class TestAuthentication(FrappeTestCase):
def setUp(self):
self.ebims_settings = frappe.get_doc({
"doctype": "eBMS Settings",
"username": "test_username",
"password": "test_password",
"sandbox": 1,
"taxpayers_legal_form": "test_legal_form",
"taxpayers_sector_of_activity": "test_activity_sector",
"system_identification_given_by_obr": "test_system_identification",
"the_taxpayers_commercial_register_number": "test_register_number",
"the_taxpayers_tax_center": "DMC",
"type_of_taxpayer": "pour personne morale",
"subject_to_vat": "pour un non assujetti ou",
"subject_to_consumption_tax": "pour un non assujetti ou",
"subject_to_flatrate_withholding_tax": "pour un non assujetti ou",
"allow_obr_to_track_sales": 1,
"allow_obr_to_track_stock_movement": 1,
"company": "Test Company"
})
self.ebims_settings.insert(ignore_permissions=True)

self.company = frappe.get_list("Company", filters={"company_name": "Test Company_1"})
if not self.company:
self.company = frappe.get_doc({
"doctype": "Company",
"company_name": "Test Company_1",
"abbr": "TC",
"country": "Burundi",
"default_currency": "BIF",
})
self.company.insert(ignore_permissions=True)
frappe.db.commit()

self.ebims_settings=frappe.get_list("eBMS Settings", filters={"company": "Test Company_1"})
if not self.ebims_settings:
self.ebims_settings = frappe.get_doc({
"doctype": "eBMS Settings",
"username": "test_username",
"password": "test_password",

"sandbox": 1,
"taxpayers_legal_form": "test_legal_form",
"taxpayers_sector_of_activity": "test_activity_sector",
"system_identification_given_by_obr": "test_system_identification",
"the_taxpayers_commercial_register_number": "test_register_number",
"the_taxpayers_tax_center": "DMC",
"type_of_taxpayer": "pour personne morale",
"subject_to_vat": "pour un non assujetti ou",
"subject_to_consumption_tax": "pour un non assujetti ou",
"subject_to_flatrate_withholding_tax": "pour un non assujetti ou",
"allow_obr_to_track_sales": 1,
"allow_obr_to_track_stock_movement": 1,
"company": "Test Company_1"
})
self.ebims_settings.insert(ignore_permissions=True)
frappe.db.commit()
else:
self.ebims_settings = frappe.get_doc("eBMS Settings", self.ebims_settings[0].name)
def tearDown(self):
super().tearDown()
frappe.delete_doc("eBMS Settings", "Test Company", force=True)
frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)


def test_authenticate_success(self):
Expand All @@ -40,17 +57,21 @@ def test_authenticate_success(self):
result = OBRAPIBase().authenticate()

self.assertEqual(result, "ValidToken")
frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)


def test_authenticate_failure(self):
with patch.object(requests, 'post') as mock_post:
mock_post.return_value.json.return_value = {"success": False, "msg": "Invalid credentials"}
result=OBRAPIBase().authenticate()
self.assertFalse(result)
frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)

def test_enqueue_retry_task(self):
with patch.object(frappe, 'enqueue') as mock_enqueue:
OBRAPIBase().enqueue_retry_task()
mock_enqueue.assert_called_once_with("burundi_compliance.burundi_compliance.utils.background_jobs.retry_authentication", queue='short', timeout=5, is_async=True, at_front=True)
frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)

def test_get_auth_details(self):
with patch.object(frappe, 'get_doc') as mock_get_doc:
Expand Down Expand Up @@ -79,21 +100,18 @@ def test_get_auth_details(self):
"allow_obr_to_track_stock_movement": 1,
}
self.assertDictEqual(auth_details, expected_auth_details)
frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)

def test_authentication_non_json_response(self):
with patch.object(requests, 'post') as mock_post:
mock_post.return_value.json.side_effect=ValueError("No JSON object could be decoded")
result=OBRAPIBase().authenticate()
self.assertFalse(result)
frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)

def test_authenticate_invalid_url(self):
with patch.object(requests, 'post', side_effect=requests.ConnectionError):
result=OBRAPIBase().authenticate()
self.assertFalse(result)

def test_missing_ebms_settings(self):
frappe.delete_doc("eBMS Settings", "Test Company", force=True)
with self.assertRaises(frappe.DoesNotExistError):
OBRAPIBase().get_auth_details()


frappe.delete_doc("eBMS Settings", "Test Company_1", force=True)

Loading

0 comments on commit 6eabfee

Please sign in to comment.