From ed4ab32599ce0c7d0cb0067c88cf15c380c5c0a7 Mon Sep 17 00:00:00 2001 From: Harold Wanyama Date: Mon, 14 Oct 2024 01:13:33 +0300 Subject: [PATCH] [#4264] Bug/Ecla Acknowledged timestamp - Resolved missing datestamp values for new ecla records Signed-off-by: Harold Wanyama --- cla-backend/cla/models/docusign_models.py | 3 ++ cla-backend/cla/tests/unit/test_ecla.py | 53 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 cla-backend/cla/tests/unit/test_ecla.py diff --git a/cla-backend/cla/models/docusign_models.py b/cla-backend/cla/models/docusign_models.py index 6591827ad..de52401c4 100644 --- a/cla-backend/cla/models/docusign_models.py +++ b/cla-backend/cla/models/docusign_models.py @@ -18,6 +18,7 @@ import xml.etree.ElementTree as ET from typing import Any, Dict, List, Optional from urllib.parse import urlparse +from datetime import datetime import cla import pydocusign # type: ignore @@ -784,6 +785,8 @@ def _save_employee_signature(self,signature): 'signature_approved': {'BOOL': signature.get_signature_approved()}, 'signature_acl': {'SS': list(signature.get_signature_acl())}, 'signature_user_ccla_company_id': {'S': signature.get_signature_user_ccla_company_id()}, + 'date_modified': {'S': datetime.now().isoformat()}, + 'date_created': {'S': datetime.now().isoformat()} } if signature.get_signature_return_url() is not None: diff --git a/cla-backend/cla/tests/unit/test_ecla.py b/cla-backend/cla/tests/unit/test_ecla.py new file mode 100644 index 000000000..42be1c905 --- /dev/null +++ b/cla-backend/cla/tests/unit/test_ecla.py @@ -0,0 +1,53 @@ +import unittest +from unittest.mock import Mock, patch +import datetime + +from cla.models.docusign_models import DocuSign + +def test_save_employee_signature(project, company, user_instance): + """ Test _save_employee_signature """ + # Mock DocuSign method and related class methods + DocuSign.check_and_prepare_employee_signature = Mock(return_value={'success': {'the employee is ready to sign the CCLA'}}) + + # Create an instance of DocuSign and mock its dynamo_client + docusign = DocuSign() + docusign.dynamo_client = Mock() # Mock the dynamo_client on the instance + mock_put_item = docusign.dynamo_client.put_item = Mock() + + # Mock ecla signature object with necessary attributes for the helper method + signature = Mock() + signature.get_signature_id.return_value = "sig_id" + signature.get_signature_project_id.return_value = "proj_id" + signature.get_signature_document_minor_version.return_value = 1 + signature.get_signature_document_major_version.return_value = 2 + signature.get_signature_reference_id.return_value = "ref_id" + signature.get_signature_reference_type.return_value = "user" + signature.get_signature_type.return_value = "cla" + signature.get_signature_signed.return_value = True + signature.get_signature_approved.return_value = True + signature.get_signature_acl.return_value = ['acl1', 'acl2'] + signature.get_signature_user_ccla_company_id.return_value = "company_id" + signature.get_signature_return_url.return_value = None + signature.get_signature_reference_name.return_value = None + + # Call the helper method + docusign._save_employee_signature(signature) + + # Check if dynamo_client.put_item was called + assert mock_put_item.called + + # Extract the 'Item' argument passed to put_item + _, kwargs = mock_put_item.call_args + item = kwargs['Item'] + + # Assert that 'date_modified' and 'date_created' are in the item + assert 'date_modified' in item + assert 'date_created' in item + + + # Optionally, check if they are correctly formatted ISO timestamps + try: + datetime.datetime.fromisoformat(item['date_modified']['S']) + datetime.datetime.fromisoformat(item['date_created']['S']) + except ValueError: + assert False, "date_modified or date_created are not valid ISO format timestamps"