-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathauditlog_test.py
122 lines (107 loc) · 4.87 KB
/
auditlog_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from nav.tests.cases import DjangoTransactionTestCase
from nav.models.arnold import Justification
from nav.auditlog import find_modelname
from nav.auditlog.models import LogEntry
from nav.auditlog.utils import get_auditlog_entries
class AuditlogModelTestCase(DjangoTransactionTestCase):
def setUp(self):
# This specific model is used because it is very simple
self.justification = Justification.objects.create(name='testarossa')
def test_str(self):
LogEntry.add_log_entry(self.justification, u'str test', 'foo')
log_entry = LogEntry.objects.filter(verb='str test').get()
self.assertEqual(str(log_entry), 'foo')
log_entry.delete()
def test_add_log_entry_bad_template(self):
with self.assertLogs(level='ERROR') as log:
LogEntry.add_log_entry(
self.justification, u'bad template test', u'this is a {bad} template'
)
log_entry = LogEntry.objects.filter(verb='bad template test').get()
self.assertEqual(
log_entry.summary, u'Error creating summary - see error log'
)
log_entry.delete()
self.assertEqual(len(log.output), 1)
self.assertEqual(len(log.records), 1)
self.assertIn('KeyError when creating summary:', log.output[0])
def test_add_log_entry_actor_only(self):
LogEntry.add_log_entry(
self.justification, u'actor test', u'actor "{actor}" only is tested'
)
log_entry = LogEntry.objects.filter(verb='actor test').get()
self.assertEqual(log_entry.summary, u'actor "testarossa" only is tested')
log_entry.delete()
def test_add_create_entry(self):
LogEntry.add_create_entry(self.justification, self.justification)
log_entry = LogEntry.objects.filter(verb=u'create-justification').get()
self.assertEqual(log_entry.summary, u'testarossa created testarossa')
log_entry.delete()
def test_add_delete_entry(self):
LogEntry.add_delete_entry(self.justification, self.justification)
log_entry = LogEntry.objects.filter(verb=u'delete-justification').get()
self.assertEqual(log_entry.summary, u'testarossa deleted testarossa')
log_entry.delete()
def test_compare_objects(self):
justification_1 = Justification.objects.create(
name='ferrari', description='Psst!'
)
justification_2 = Justification.objects.create(name='lambo', description='Hush')
LogEntry.compare_objects(
self.justification,
justification_1,
justification_2,
('name', 'description'),
('description',),
)
log_entry = LogEntry.objects.filter(verb=u'edit-justification-name').get()
self.assertEqual(
log_entry.summary,
u'testarossa edited lambo: name changed' u" from 'ferrari' to 'lambo'",
)
log_entry.delete()
log_entry = LogEntry.objects.filter(
verb=u'edit-justification-description'
).get()
self.assertEqual(
log_entry.summary, u'testarossa edited lambo: description changed'
)
log_entry.delete()
def test_addLog_entry_before(self):
LogEntry.add_log_entry(self.justification, u'actor test', u'blbl', before=1)
log_entry = LogEntry.objects.filter(verb='actor test').get()
self.assertEqual(log_entry.before, u'1')
log_entry.delete()
def test_find_name(self):
name = find_modelname(self.justification)
self.assertEqual(name, 'blocked_reason')
class AuditlogUtilsTestCase(DjangoTransactionTestCase):
def setUp(self):
# This specific model is used because it is very simple
self.justification = Justification.objects.create(name='testarossa')
def test_get_auditlog_entries(self):
modelname = 'blocked_reason' # Justification's db_table
justification_1 = Justification.objects.create(name='j1')
justification_2 = Justification.objects.create(name='j2')
LogEntry.add_create_entry(self.justification, justification_1)
LogEntry.add_log_entry(
self.justification,
u'greet',
u'{actor} greets {object}',
object=justification_2,
subsystem="hello",
)
LogEntry.add_log_entry(
self.justification,
u'deliver',
u'{actor} delivers {object} to {target}',
object=justification_1,
target=justification_2,
subsystem='delivery',
)
entries = get_auditlog_entries(modelname=modelname)
self.assertEqual(entries.count(), 3)
entries = get_auditlog_entries(modelname=modelname, subsystem='hello')
self.assertEqual(entries.count(), 1)
entries = get_auditlog_entries(modelname=modelname, pks=[justification_1.pk])
self.assertEqual(entries.count(), 2)