Dodat import/export Kontrola
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import os
|
||||
import csv
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
from backend.core.models import Risk, Control
|
||||
|
||||
class ExportControlsCommandTest(TestCase):
|
||||
def setUp(self):
|
||||
self.risk1 = Risk.objects.create(risk_id=1, risk_name="Test Risk 1")
|
||||
self.risk2 = Risk.objects.create(risk_id=2, risk_name="Test Risk 2")
|
||||
|
||||
self.control1 = Control.objects.create(
|
||||
risk=self.risk1,
|
||||
safeguard="Test Safeguard 1",
|
||||
description="Description 1",
|
||||
weight=5
|
||||
)
|
||||
self.control2 = Control.objects.create(
|
||||
risk=self.risk2,
|
||||
safeguard="Test Safeguard 2",
|
||||
description="Description 2",
|
||||
weight=10
|
||||
)
|
||||
|
||||
self.csv_file_path = 'test_export_controls.csv'
|
||||
|
||||
def tearDown(self):
|
||||
if os.path.exists(self.csv_file_path):
|
||||
os.remove(self.csv_file_path)
|
||||
|
||||
def test_export_controls_success(self):
|
||||
call_command('export_controls', self.csv_file_path)
|
||||
|
||||
self.assertTrue(os.path.exists(self.csv_file_path))
|
||||
|
||||
with open(self.csv_file_path, mode='r', encoding='utf-8') as csv_file:
|
||||
reader = csv.DictReader(csv_file)
|
||||
rows = list(reader)
|
||||
|
||||
self.assertEqual(len(rows), 2)
|
||||
|
||||
self.assertEqual(rows[0]["Risk #"], str(self.risk1.risk_id))
|
||||
self.assertEqual(rows[0]["Risk Description"], self.risk1.risk_name)
|
||||
self.assertEqual(rows[0]["CIS v8.1 Safeguards (Sub-Controls)"], self.control1.safeguard)
|
||||
self.assertEqual(rows[0]["Weight (0-10)"], str(self.control1.weight))
|
||||
|
||||
self.assertEqual(rows[1]["Risk #"], str(self.risk2.risk_id))
|
||||
self.assertEqual(rows[1]["Risk Description"], self.risk2.risk_name)
|
||||
self.assertEqual(rows[1]["CIS v8.1 Safeguards (Sub-Controls)"], self.control2.safeguard)
|
||||
self.assertEqual(rows[1]["Weight (0-10)"], str(self.control2.weight))
|
||||
|
||||
def test_export_controls_empty(self):
|
||||
Control.objects.all().delete()
|
||||
|
||||
call_command('export_controls', self.csv_file_path)
|
||||
|
||||
self.assertTrue(os.path.exists(self.csv_file_path))
|
||||
|
||||
with open(self.csv_file_path, mode='r', encoding='utf-8') as csv_file:
|
||||
reader = csv.DictReader(csv_file)
|
||||
rows = list(reader)
|
||||
|
||||
self.assertEqual(len(rows), 0)
|
||||
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
import csv
|
||||
import tempfile
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
from backend.core.models import Risk, Control
|
||||
|
||||
class ImportControlsCommandTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Create a temporary CSV file with test control data"""
|
||||
self.temp_csv = tempfile.NamedTemporaryFile(mode="w", delete=False, newline='', encoding='utf-8')
|
||||
fieldnames = [
|
||||
"Risk #", "Risk Description", "CIS v8.1 Safeguards (Sub-Controls)", "Weight (0-10)"
|
||||
]
|
||||
|
||||
writer = csv.DictWriter(self.temp_csv, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
writer.writerow({
|
||||
"Risk #": "1",
|
||||
"Risk Description": "Test Risk 1",
|
||||
"CIS v8.1 Safeguards (Sub-Controls)": "Test Safeguard 1",
|
||||
"Weight (0-10)": "5"
|
||||
})
|
||||
writer.writerow({
|
||||
"Risk #": "2",
|
||||
"Risk Description": "Test Risk 2",
|
||||
"CIS v8.1 Safeguards (Sub-Controls)": "Test Safeguard 2",
|
||||
"Weight (0-10)": "10"
|
||||
})
|
||||
self.temp_csv.close()
|
||||
|
||||
def test_import_controls_command(self):
|
||||
"""Test importing controls from a CSV file"""
|
||||
self.assertEqual(Control.objects.count(), 0)
|
||||
|
||||
self.assertEqual(Risk.objects.count(), 0)
|
||||
|
||||
Risk.objects.create(risk_id=1, risk_name="Test Risk 1")
|
||||
Risk.objects.create(risk_id=2, risk_name="Test Risk 2")
|
||||
|
||||
call_command('import_controls', self.temp_csv.name)
|
||||
|
||||
self.assertEqual(Control.objects.count(), 2)
|
||||
|
||||
control1 = Control.objects.get(risk__risk_id=1)
|
||||
self.assertEqual(control1.safeguard, "Test Safeguard 1")
|
||||
self.assertEqual(control1.weight, 5)
|
||||
|
||||
control2 = Control.objects.get(risk__risk_id=2)
|
||||
self.assertEqual(control2.safeguard, "Test Safeguard 2")
|
||||
self.assertEqual(control2.weight, 10)
|
||||
|
||||
def tearDown(self):
|
||||
"""Remove temporary CSV file after test"""
|
||||
os.remove(self.temp_csv.name)
|
||||
Reference in New Issue
Block a user