Added graph and table to document
This commit is contained in:
0
backend/accounts/tests/__init__.py
Normal file
0
backend/accounts/tests/__init__.py
Normal file
56
backend/accounts/tests/test_tasks.py
Normal file
56
backend/accounts/tests/test_tasks.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from unittest.mock import patch
|
||||
from django.test import TestCase
|
||||
from backend.core.models import Organization, Document, Risk, Control, DocumentRiskControl
|
||||
from backend.accounts.tasks import create_document_for_organization
|
||||
|
||||
class CeleryTaskTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.organization = Organization.objects.create(
|
||||
id=1,
|
||||
name="Test Organization",
|
||||
email="test@example.com",
|
||||
employee_headcount="100-500",
|
||||
annual_revenue="$1M-$10M",
|
||||
critical_applications="5-10",
|
||||
compliance_frameworks=["Ab", "Ba"],
|
||||
industry_sector="Technology",
|
||||
it_dependency=8,
|
||||
data_sensitivity="High",
|
||||
network_infrastructure="Cloud-based",
|
||||
remote_workforce_percentage="50%",
|
||||
third_party_vendor_access="10-20",
|
||||
internal_software_development="Moderate",
|
||||
geographic_scope="Global",
|
||||
customer_base="Enterprise",
|
||||
customer_type="B2B",
|
||||
product_portfolio="Diverse",
|
||||
supplier_base="International",
|
||||
it_infrastructure=["Cloud", "On-Premise"],
|
||||
intellectual_property=["Patents", "Trademarks"],
|
||||
sensitive_data=["PII", "Financial Data"],
|
||||
integration_level="Highly Integrated"
|
||||
)
|
||||
self.risk = Risk.objects.create(risk_id="1", risk_name="Test Risk", category="Category1", primary_impact="High")
|
||||
self.control = Control.objects.create(name="Test Control")
|
||||
|
||||
@patch("backend.accounts.tasks.get_top_risk")
|
||||
@patch("backend.accounts.tasks.get_controls_for_risk")
|
||||
@patch("backend.accounts.tasks.send_payment_email")
|
||||
def test_create_document_for_organization(self, mock_send_payment_email, mock_get_controls_for_risk, mock_get_top_risk):
|
||||
mock_get_top_risk.return_value = [self.risk.risk_id]
|
||||
mock_get_controls_for_risk.return_value = [(self.control.id, 5, 7)]
|
||||
create_document_for_organization(self.organization.email)
|
||||
|
||||
document = Document.objects.first()
|
||||
self.assertIsNotNone(document)
|
||||
|
||||
document_risk_control = DocumentRiskControl.objects.first()
|
||||
self.assertIsNotNone(document_risk_control)
|
||||
self.assertEqual(document_risk_control.document, document)
|
||||
self.assertEqual(document_risk_control.risk, self.risk)
|
||||
self.assertEqual(document_risk_control.control, self.control)
|
||||
self.assertEqual(document_risk_control.weight, 5)
|
||||
self.assertEqual(document_risk_control.likelihood, 7)
|
||||
|
||||
mock_send_payment_email.assert_called_once_with(self.organization.email)
|
||||
66
backend/accounts/tests/test_utils.py
Normal file
66
backend/accounts/tests/test_utils.py
Normal file
@@ -0,0 +1,66 @@
|
||||
from django.test import TestCase
|
||||
from django.core import mail
|
||||
from unittest.mock import patch, MagicMock
|
||||
from backend.core.models import Organization, Document
|
||||
from backend.accounts.models import EmailConfirmation
|
||||
from backend.core.utils import generate_first_page_image
|
||||
from backend.accounts.utils import send_confirmation_email, send_payment_email, send_document_email
|
||||
import uuid
|
||||
from django.utils.timezone import now
|
||||
|
||||
class EmailTests(TestCase):
|
||||
def setUp(self):
|
||||
self.email = "test@example.com"
|
||||
self.organization = Organization.objects.create(
|
||||
id=1,
|
||||
name="Test Organization",
|
||||
email="test@example.com",
|
||||
employee_headcount="100-500",
|
||||
annual_revenue="$1M-$10M",
|
||||
critical_applications="5-10",
|
||||
compliance_frameworks=["Ab", "Ba"],
|
||||
industry_sector="Technology",
|
||||
it_dependency=8,
|
||||
data_sensitivity="High",
|
||||
network_infrastructure="Cloud-based",
|
||||
remote_workforce_percentage="50%",
|
||||
third_party_vendor_access="10-20",
|
||||
internal_software_development="Moderate",
|
||||
geographic_scope="Global",
|
||||
customer_base="Enterprise",
|
||||
customer_type="B2B",
|
||||
product_portfolio="Diverse",
|
||||
supplier_base="International",
|
||||
it_infrastructure=["Cloud", "On-Premise"],
|
||||
intellectual_property=["Patents", "Trademarks"],
|
||||
sensitive_data=["PII", "Financial Data"],
|
||||
integration_level="Highly Integrated"
|
||||
)
|
||||
self.document = Document.objects.create(organization=self.organization)
|
||||
|
||||
@patch("backend.accounts.utils.send_mail")
|
||||
def test_send_confirmation_email(self, mock_send_mail):
|
||||
confirmation = EmailConfirmation.objects.create(email=self.email, uuid=uuid.uuid4(), created_at=now())
|
||||
send_confirmation_email(self.email)
|
||||
|
||||
confirmation.refresh_from_db()
|
||||
self.assertIsNotNone(confirmation.uuid)
|
||||
self.assertEqual(mock_send_mail.call_count, 1)
|
||||
|
||||
@patch("backend.accounts.utils.send_mail")
|
||||
def test_send_payment_email(self, mock_send_mail):
|
||||
send_payment_email(self.email)
|
||||
self.assertEqual(mock_send_mail.call_count, 1)
|
||||
|
||||
@patch("backend.accounts.utils.EmailMultiAlternatives.send")
|
||||
@patch("backend.accounts.utils.generate_first_page_image")
|
||||
def test_send_document_email(self, mock_generate_image, mock_send):
|
||||
mock_image_io = MagicMock()
|
||||
mock_image_io.getvalue.return_value = b"fake image data"
|
||||
mock_generate_image.return_value = mock_image_io
|
||||
|
||||
document_link = "https://example.com/document.pdf"
|
||||
send_document_email(self.email, document_link, self.document)
|
||||
|
||||
mock_generate_image.assert_called_once_with(self.document)
|
||||
mock_send.assert_called_once()
|
||||
34
backend/accounts/tests/test_views.py
Normal file
34
backend/accounts/tests/test_views.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import uuid
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from unittest.mock import patch
|
||||
from backend.accounts.models import EmailConfirmation
|
||||
from backend.accounts.utils import send_confirmation_email
|
||||
from django.utils.timezone import now, timedelta
|
||||
|
||||
|
||||
class EmailConfirmationTests(TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.valid_email = "test@example.com"
|
||||
self.confirmation = EmailConfirmation.objects.create(
|
||||
email=self.valid_email,
|
||||
uuid=uuid.uuid4(),
|
||||
created_at=now()
|
||||
)
|
||||
|
||||
@patch("backend.accounts.views.create_document_for_organization.delay")
|
||||
def test_confirm_email_valid(self, mock_task):
|
||||
"""Test valid email confirmation."""
|
||||
response = self.client.get(reverse("confirm_email", args=[self.confirmation.uuid]))
|
||||
self.assertTemplateUsed(response, "accounts/confirmation_success.html")
|
||||
self.assertContains(response, self.valid_email)
|
||||
mock_task.assert_called_once_with(self.valid_email)
|
||||
|
||||
@patch("backend.accounts.views.send_confirmation_email")
|
||||
def test_resend_confirmation(self, mock_send):
|
||||
"""Test resending confirmation email."""
|
||||
response = self.client.post(reverse("resend_confirmation", args=[self.valid_email]))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content.decode(), "Confirmation email resent")
|
||||
mock_send.assert_called_once_with(self.valid_email)
|
||||
Reference in New Issue
Block a user