Coverage for product_risk_suite / threat_model / tests.py: 100%
39 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-13 23:42 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-13 23:42 +0000
1from django.test import TestCase
2from django.core.files.uploadedfile import SimpleUploadedFile
4from .models import *
6from django.conf import settings
7import tempfile
9settings.MEDIA_ROOT = tempfile.mkdtemp("prs-tm")
11class ThreatModelConnectionNameTest(TestCase):
12 def test_simple(self):
13 td = [
14 {"name": "0", "human_str": None},
15 {"name": "1", "human_str": None},
16 {"name": "DGsSiznoInNCpWqt2XE2-2", "human_str": None},
17 {"name": "DGsSiznoInNCpWqt2XE2-1", "human_str": None},
18 {"name": "DGsSiznoInNCpWqt2XE2-3", "human_str": None},
19 {"name": "DGsSiznoInNCpWqt2XE2-4", "human_str": None},
20 {"name": "DGsSiznoInNCpWqt2XE2-5", "human_str": None},
21 {"name": "admin-connection", "human_str": None},
22 {"name": "DGsSiznoInNCpWqt2XE2-11", "human_str": None},
23 {"name": "rw-connection", "human_str": "Read Write Connection"},
24 {"name": "DGsSiznoInNCpWqt2XE2-10", "human_str": None},
25 {"name": "ro-connection", "human_str": None},
26 {"name": "DGsSiznoInNCpWqt2XE2-9", "human_str": None},
27 ]
29 for tr in td:
30 ThreatModelConnectionName.objects.create(name=tr["name"], human_name=tr["human_str"])
32 all = ThreatModelConnectionName.objects.all()
33 self.assertEqual(len(all), len(td))
35 act_with_human_str = 0
36 for tr in td:
37 tmcn = ThreatModelConnectionName.objects.get(name=tr["name"])
38 if tr["human_str"]:
39 self.assertEqual(str(tmcn), f"{tr['human_str']} ({tr['name']})")
40 act_with_human_str = act_with_human_str + 1
41 else:
42 self.assertEqual(str(tmcn), f"{tr['name']}")
43 self.assertEqual(act_with_human_str, 1)
45class ThreatModelTest(TestCase):
46 def setUp(self):
47 Product.objects.create(id=0, title="My test project", description="A real cool test project")
49 def test_simple(self):
50 tm = ThreatModel.objects.create(
51 id=0,
52 product=Product.objects.get(id=0),
53 title="Test TM",
54 description="Description of TM"
55 )
56 tm.svg = SimpleUploadedFile(
57 name="test-threat-model.svg",
58 content=b"""
59 <svg>
60 <g data-cell-id="0">
61 <g data-cell-id="1">
62 <g data-cell-id="rw connection">
63 </g>
64 <g data-cell-id="usb">
65 </g>
66 </g>
67 </g>
68 </svg>
69 """,
70 content_type="image/svg+xml"
71 )
72 tm.save()
74 actual = ThreatModel.objects.get(id=0)
75 self.assertEqual(actual.product.title, "My test project")
76 self.assertEqual(actual.title, "Test TM")
77 self.assertEqual(actual.description, "Description of TM")
78 self.assertEqual(actual.svg, "uploads/test-threat-model.svg")
79 act_connection_names = [cn.name for cn in actual.connection_names.all()]
80 self.assertEqual(len(act_connection_names), 4)
81 self.assertTrue("0" in act_connection_names)
82 self.assertTrue("1" in act_connection_names)
83 self.assertTrue("rw connection" in act_connection_names)
84 self.assertTrue("usb" in act_connection_names)