forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_utils.py
More file actions
430 lines (368 loc) · 19.9 KB
/
tests_utils.py
File metadata and controls
430 lines (368 loc) · 19.9 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Copyright The IETF Trust 2020, All Rights Reserved
import datetime
import debug # pyflakes:ignore
from unittest.mock import patch
from django.db import IntegrityError
from ietf.group.factories import GroupFactory, RoleFactory
from ietf.name.models import DocTagName
from ietf.person.factories import PersonFactory
from ietf.utils.test_utils import TestCase, name_of_file_containing
from ietf.person.models import Person
from ietf.doc.factories import DocumentFactory, WgRfcFactory, WgDraftFactory
from ietf.doc.models import State, DocumentActionHolder, DocumentAuthor, Document
from ietf.doc.utils import (update_action_holders, add_state_change_event, update_documentauthors,
fuzzy_find_documents, rebuild_reference_relations)
from ietf.utils.draft import Draft, PlaintextDraft
from ietf.utils.xmldraft import XMLDraft
class ActionHoldersTests(TestCase):
def setUp(self):
"""Set up helper for the update_action_holders tests"""
super().setUp()
self.authors = PersonFactory.create_batch(3)
self.ad = Person.objects.get(user__username='ad')
self.group = GroupFactory()
RoleFactory(name_id='ad', group=self.group, person=self.ad)
def doc_in_iesg_state(self, slug):
return DocumentFactory(authors=self.authors, group=self.group, ad=self.ad, states=[('draft-iesg', slug)])
def update_doc_state(self, doc, new_state, add_tags=None, remove_tags=None):
"""Update document state/tags, create change event, and save"""
prev_tags = list(doc.tags.all()) # list to make sure we retrieve now
# prev_action_holders = list(doc.action_holders.all())
prev_state = doc.get_state(new_state.type_id)
if new_state != prev_state:
doc.set_state(new_state)
if add_tags:
doc.tags.add(*DocTagName.objects.filter(slug__in=add_tags))
if remove_tags:
doc.tags.remove(*DocTagName.objects.filter(slug__in=remove_tags))
new_tags = list(doc.tags.all())
events = []
e = add_state_change_event(
doc,
Person.objects.get(name='(System)'),
prev_state, new_state,
prev_tags, new_tags)
self.assertIsNotNone(e, 'Test logic error')
events.append(e)
e = update_action_holders(doc, prev_state, new_state, prev_tags, new_tags)
if e:
events.append(e)
doc.save_with_history(events)
def test_update_action_holders_by_state(self):
"""Doc action holders should auto-update correctly on state change"""
# Test the transition from every state to each of its 'next_states'
for initial_state in State.objects.filter(type__slug='draft-iesg'):
for next_state in initial_state.next_states.all():
# Test with no action holders initially
doc = DocumentFactory(
authors=self.authors,
group=self.group,
ad=self.ad,
states=[('draft-iesg', initial_state.slug)],
)
docevents_before = set(doc.docevent_set.all())
self.update_doc_state(doc, next_state)
new_docevents = set(doc.docevent_set.all()).difference(docevents_before)
self.assertIn(doc.latest_event(type='changed_state'), new_docevents)
if next_state.slug in DocumentActionHolder.CLEAR_ACTION_HOLDERS_STATES:
self.assertCountEqual(doc.action_holders.all(), [])
self.assertEqual(len(new_docevents), 1)
else:
self.assertCountEqual(
doc.action_holders.all(), [doc.ad],
'AD should be only action holder after transition to %s' % next_state.slug)
self.assertEqual(len(new_docevents), 2)
change_event = doc.latest_event(type='changed_action_holders')
self.assertIn(change_event, new_docevents)
self.assertIn('Changed action holders', change_event.desc)
self.assertIn(doc.ad.name, change_event.desc)
doc.delete() # clean up for next iteration
# Test with action holders initially
doc = DocumentFactory(
authors=self.authors,
group=self.group,
ad=self.ad,
states=[('draft-iesg', initial_state.slug)],
)
doc.action_holders.add(*self.authors) # adds all authors
docevents_before = set(doc.docevent_set.all())
self.update_doc_state(doc, next_state)
new_docevents = set(doc.docevent_set.all()).difference(docevents_before)
self.assertEqual(len(new_docevents), 2)
self.assertIn(doc.latest_event(type='changed_state'), new_docevents)
change_event = doc.latest_event(type='changed_action_holders')
self.assertIn(change_event, new_docevents)
if next_state.slug in DocumentActionHolder.CLEAR_ACTION_HOLDERS_STATES:
self.assertCountEqual(doc.action_holders.all(), [])
self.assertIn('Removed all action holders', change_event.desc)
else:
self.assertCountEqual(
doc.action_holders.all(), [doc.ad],
'AD should be only action holder after transition to %s' % next_state.slug)
self.assertIn('Changed action holders', change_event.desc)
self.assertIn(doc.ad.name, change_event.desc)
doc.delete() # clean up for next iteration
def test_update_action_holders_with_no_ad(self):
"""A document with no AD should be handled gracefully"""
doc = self.doc_in_iesg_state('idexists')
doc.ad = None
doc.save()
docevents_before = set(doc.docevent_set.all())
self.update_doc_state(doc, State.objects.get(slug='pub-req'))
new_docevents = set(doc.docevent_set.all()).difference(docevents_before)
self.assertEqual(len(new_docevents), 1)
self.assertIn(doc.latest_event(type='changed_state'), new_docevents)
self.assertCountEqual(doc.action_holders.all(), [])
def test_update_action_holders_resets_age(self):
"""Action holder age should reset when document state changes"""
doc = self.doc_in_iesg_state('pub-req')
doc.action_holders.set([self.ad])
dah = doc.documentactionholder_set.get(person=self.ad)
dah.time_added = datetime.datetime(2020, 1, 1) # arbitrary date in the past
dah.save()
self.assertNotEqual(doc.documentactionholder_set.get(person=self.ad).time_added.date(), datetime.date.today())
self.update_doc_state(doc, State.objects.get(slug='ad-eval'))
self.assertEqual(doc.documentactionholder_set.get(person=self.ad).time_added.date(), datetime.date.today())
def test_update_action_holders_add_tag_need_rev(self):
"""Adding need-rev tag adds authors as action holders"""
doc = self.doc_in_iesg_state('pub-req')
first_author = self.authors[0]
doc.action_holders.add(first_author)
self.assertCountEqual(doc.action_holders.all(), [first_author])
self.update_doc_state(doc,
doc.get_state('draft-iesg'),
add_tags=['need-rev'],
remove_tags=None)
self.assertCountEqual(doc.action_holders.all(), self.authors)
def test_update_action_holders_add_tag_need_rev_no_dups(self):
"""Adding need-rev tag does not duplicate existing action holders"""
doc = self.doc_in_iesg_state('pub-req')
self.assertCountEqual(doc.action_holders.all(), [])
self.update_doc_state(doc,
doc.get_state('draft-iesg'),
add_tags=['need-rev'],
remove_tags=None)
self.assertCountEqual(doc.action_holders.all(), self.authors)
def test_update_action_holders_remove_tag_need_rev(self):
"""Removing need-rev tag drops authors as action holders"""
doc = self.doc_in_iesg_state('pub-req')
doc.tags.add(DocTagName.objects.get(slug='need-rev'))
self.assertEqual(doc.action_holders.count(), 0)
self.update_doc_state(doc,
doc.get_state('draft-iesg'),
add_tags=None,
remove_tags=['need-rev'])
self.assertEqual(doc.action_holders.count(), 0)
def test_update_action_holders_add_tag_need_rev_ignores_non_authors(self):
"""Adding need-rev tag does not affect existing action holders"""
doc = self.doc_in_iesg_state('pub-req')
doc.action_holders.add(self.ad)
self.assertCountEqual(doc.action_holders.all(),[self.ad])
self.update_doc_state(doc,
doc.get_state('draft-iesg'),
add_tags=['need-rev'],
remove_tags=None)
self.assertCountEqual(doc.action_holders.all(), [self.ad] + self.authors)
def test_update_action_holders_remove_tag_need_rev_ignores_non_authors(self):
"""Removing need-rev tag does not affect non-author action holders"""
doc = self.doc_in_iesg_state('pub-req')
doc.tags.add(DocTagName.objects.get(slug='need-rev'))
doc.action_holders.add(self.ad)
self.assertCountEqual(doc.action_holders.all(), [self.ad])
self.update_doc_state(doc,
doc.get_state('draft-iesg'),
add_tags=None,
remove_tags=['need-rev'])
self.assertCountEqual(doc.action_holders.all(), [self.ad])
def test_doc_action_holders_enabled(self):
"""Action holders should only be enabled in certain states"""
doc = self.doc_in_iesg_state('idexists')
self.assertFalse(doc.action_holders_enabled())
for state in State.objects.filter(type='draft-iesg').exclude(slug='idexists'):
doc.set_state(state)
self.assertTrue(doc.action_holders_enabled())
class MiscTests(TestCase):
def test_update_documentauthors_with_nulls(self):
"""A 'None' value in the affiliation/country should be handled correctly"""
author_person = PersonFactory()
doc = DocumentFactory(authors=[author_person])
doc.documentauthor_set.update(
affiliation='Some Affiliation', country='USA'
)
try:
events = update_documentauthors(
doc,
[
DocumentAuthor(
person=author_person,
email=author_person.email(),
affiliation=None,
country=None,
)
],
)
except IntegrityError as err:
self.fail('IntegrityError was raised: {}'.format(err))
self.assertEqual(len(events), 1)
self.assertEqual(events[0].type, 'edited_authors')
self.assertIn('cleared affiliation (was "Some Affiliation")', events[0].desc)
self.assertIn('cleared country (was "USA")', events[0].desc)
docauth = doc.documentauthor_set.first()
self.assertEqual(docauth.affiliation, '')
self.assertEqual(docauth.country, '')
def do_fuzzy_find_documents_rfc_test(self, name):
rfc = WgRfcFactory(name=name, create_revisions=(0, 1, 2))
rfc = Document.objects.get(pk=rfc.pk) # clear out any cached values
# by canonical name
found = fuzzy_find_documents(rfc.canonical_name(), None)
self.assertCountEqual(found.documents, [rfc])
self.assertEqual(found.matched_rev, None)
self.assertEqual(found.matched_name, rfc.canonical_name())
# by draft name, no rev
found = fuzzy_find_documents(rfc.name, None)
self.assertCountEqual(found.documents, [rfc])
self.assertEqual(found.matched_rev, None)
self.assertEqual(found.matched_name, rfc.name)
# by draft name, latest rev
found = fuzzy_find_documents(rfc.name, '02')
self.assertCountEqual(found.documents, [rfc])
self.assertEqual(found.matched_rev, '02')
self.assertEqual(found.matched_name, rfc.name)
# by draft name, earlier rev
found = fuzzy_find_documents(rfc.name, '01')
self.assertCountEqual(found.documents, [rfc])
self.assertEqual(found.matched_rev, '01')
self.assertEqual(found.matched_name, rfc.name)
# wrong name or revision
found = fuzzy_find_documents(rfc.name + '-incorrect')
self.assertCountEqual(found.documents, [], 'Should not find document that does not match')
found = fuzzy_find_documents(rfc.name + '-incorrect', '02')
self.assertCountEqual(found.documents, [], 'Still should not find document, even with a version')
found = fuzzy_find_documents(rfc.name, '22')
self.assertCountEqual(found.documents, [rfc],
'Should find document even if rev does not exist')
def test_fuzzy_find_documents(self):
# Should add additional tests/test cases for other document types/name formats
self.do_fuzzy_find_documents_rfc_test('draft-normal-name')
self.do_fuzzy_find_documents_rfc_test('draft-name-with-number-01')
self.do_fuzzy_find_documents_rfc_test('draft-name-that-has-two-02-04')
self.do_fuzzy_find_documents_rfc_test('draft-wild-01-numbers-0312')
class RebuildReferenceRelationsTests(TestCase):
def setUp(self):
super().setUp()
self.doc = WgDraftFactory() # document under test
# Other documents that should be found by rebuild_reference_relations
self.normative, self.informative, self.unknown = WgRfcFactory.create_batch(3)
for relationship in ['refnorm', 'refinfo', 'refunk', 'refold']:
self.doc.relateddocument_set.create(
target=WgRfcFactory().docalias.first(),
relationship_id=relationship,
)
self.updated = WgRfcFactory() # related document that should be left alone
self.doc.relateddocument_set.create(target=self.updated.docalias.first(), relationship_id='updates')
self.assertCountEqual(self.doc.relateddocument_set.values_list('relationship__slug', flat=True),
['refnorm', 'refinfo', 'refold', 'refunk', 'updates'],
'Test conditions set up incorrectly: wrong prior document relationships')
for other_doc in [self.normative, self.informative, self.unknown]:
self.assertEqual(
self.doc.relateddocument_set.filter(target__name=other_doc.canonical_name()).count(),
0,
'Test conditions set up incorrectly: new documents already related',
)
def _get_refs_return_value(self):
return {
self.normative.canonical_name(): Draft.REF_TYPE_NORMATIVE,
self.informative.canonical_name(): Draft.REF_TYPE_INFORMATIVE,
self.unknown.canonical_name(): Draft.REF_TYPE_UNKNOWN,
'draft-not-found': Draft.REF_TYPE_NORMATIVE,
}
def test_requires_txt_or_xml(self):
result = rebuild_reference_relations(self.doc, {})
self.assertCountEqual(result.keys(), ['errors'])
self.assertEqual(len(result['errors']), 1)
self.assertIn('No draft text available', result['errors'][0],
'Error should be reported if no draft file is given')
result = rebuild_reference_relations(self.doc, {'md': 'cant-do-this.md'})
self.assertCountEqual(result.keys(), ['errors'])
self.assertEqual(len(result['errors']), 1)
self.assertIn('No draft text available', result['errors'][0],
'Error should be reported if no XML or plaintext file is given')
@patch.object(XMLDraft, 'get_refs')
@patch.object(XMLDraft, '__init__', return_value=None)
def test_xml(self, mock_init, mock_get_refs):
"""Should build reference relations with only XML"""
mock_get_refs.return_value = self._get_refs_return_value()
result = rebuild_reference_relations(self.doc, {'xml': 'file.xml'})
# if the method of calling the XMLDraft() constructor changes, this will need to be updated
xmldraft_init_args, _ = mock_init.call_args
self.assertEqual(xmldraft_init_args, ('file.xml',), 'XMLDraft initialized with unexpected arguments')
self.assertEqual(
result,
{
'warnings': ['There were 1 references with no matching DocAlias'],
'unfound': ['draft-not-found'],
}
)
self.assertCountEqual(
self.doc.relateddocument_set.values_list('target__name', 'relationship__slug'),
[
(self.normative.canonical_name(), 'refnorm'),
(self.informative.canonical_name(), 'refinfo'),
(self.unknown.canonical_name(), 'refunk'),
(self.updated.docalias.first().name, 'updates'),
]
)
@patch.object(PlaintextDraft, 'get_refs')
@patch.object(PlaintextDraft, '__init__', return_value=None)
def test_plaintext(self, mock_init, mock_get_refs):
"""Should build reference relations with only plaintext"""
mock_get_refs.return_value = self._get_refs_return_value()
with name_of_file_containing('contents') as temp_file_name:
result = rebuild_reference_relations(self.doc, {'txt': temp_file_name})
# if the method of calling the PlaintextDraft() constructor changes, this test will need to be updated
_, mock_init_kwargs = mock_init.call_args
self.assertEqual(mock_init_kwargs, {'text': 'contents', 'source': temp_file_name},
'PlaintextDraft initialized with unexpected arguments')
self.assertEqual(
result,
{
'warnings': ['There were 1 references with no matching DocAlias'],
'unfound': ['draft-not-found'],
}
)
self.assertCountEqual(
self.doc.relateddocument_set.values_list('target__name', 'relationship__slug'),
[
(self.normative.canonical_name(), 'refnorm'),
(self.informative.canonical_name(), 'refinfo'),
(self.unknown.canonical_name(), 'refunk'),
(self.updated.docalias.first().name, 'updates'),
]
)
@patch.object(PlaintextDraft, '__init__')
@patch.object(XMLDraft, 'get_refs')
@patch.object(XMLDraft, '__init__', return_value=None)
def test_xml_and_plaintext(self, mock_init, mock_get_refs, mock_plaintext_init):
"""Should build reference relations with XML when plaintext also available"""
mock_get_refs.return_value = self._get_refs_return_value()
result = rebuild_reference_relations(self.doc, {'txt': 'file.txt', 'xml': 'file.xml'})
self.assertFalse(mock_plaintext_init.called, 'PlaintextDraft should not be used when XML is available')
# if the method of calling the XMLDraft() constructor changes, this will need to be updated
xmldraft_init_args, _ = mock_init.call_args
self.assertEqual(xmldraft_init_args, ('file.xml',), 'XMLDraft initialized with unexpected arguments')
self.assertEqual(
result,
{
'warnings': ['There were 1 references with no matching DocAlias'],
'unfound': ['draft-not-found'],
}
)
self.assertCountEqual(
self.doc.relateddocument_set.values_list('target__name', 'relationship__slug'),
[
(self.normative.canonical_name(), 'refnorm'),
(self.informative.canonical_name(), 'refinfo'),
(self.unknown.canonical_name(), 'refunk'),
(self.updated.docalias.first().name, 'updates'),
]
)