forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_bofreq.py
More file actions
387 lines (358 loc) · 18.9 KB
/
tests_bofreq.py
File metadata and controls
387 lines (358 loc) · 18.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
# Copyright The IETF Trust 2021 All Rights Reserved
import datetime
import debug # pyflakes:ignore
import os
from pathlib import Path
from pyquery import PyQuery
from random import randint
from tempfile import NamedTemporaryFile
from html import unescape
from django.conf import settings
from django.urls import reverse as urlreverse
from django.template.loader import render_to_string
from ietf.group.factories import RoleFactory
from ietf.doc.factories import BofreqFactory, NewRevisionDocEventFactory
from ietf.doc.models import State, Document, DocAlias, NewRevisionDocEvent
from ietf.doc.utils_bofreq import bofreq_editors, bofreq_responsible
from ietf.person.factories import PersonFactory
from ietf.utils.mail import outbox, empty_outbox
from ietf.utils.test_utils import TestCase, reload_db_objects, unicontent, login_testing_unauthorized
from ietf.utils.text import xslugify
class BofreqTests(TestCase):
settings_temp_path_overrides = TestCase.settings_temp_path_overrides + ['BOFREQ_PATH']
def write_bofreq_file(self, bofreq):
fname = Path(settings.BOFREQ_PATH) / ("%s-%s.md" % (bofreq.canonical_name(), bofreq.rev))
with fname.open("w") as f:
f.write(f"""# This is a test bofreq.
Version: {bofreq.rev}
## A section
This test section has some text.
""")
def test_show_bof_requests(self):
url = urlreverse('ietf.doc.views_bofreq.bof_requests')
r = self.client.get(url)
self.assertContains(r, 'There are currently no BOF Requests', status_code=200)
states = State.objects.filter(type_id='bofreq')
self.assertTrue(states.count()>0)
for i in range(3*len(states)):
BofreqFactory(states=[('bofreq',states[i%len(states)].slug)],newrevisiondocevent__time=datetime.datetime.today()-datetime.timedelta(days=randint(0,20)))
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
for state in states:
self.assertEqual(len(q(f'#bofreqs-{state.slug}')), 1)
self.assertEqual(len(q(f'#bofreqs-{state.slug} tbody tr')), 3)
self.assertFalse(q('#start_button'))
PersonFactory(user__username='nobody')
self.client.login(username='nobody', password='nobody+password')
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
self.assertTrue(q('#start_button'))
def test_bofreq_main_page(self):
doc = BofreqFactory()
doc.save_with_history(doc.docevent_set.all())
self.write_bofreq_file(doc)
nr_event = NewRevisionDocEventFactory(doc=doc,rev='01')
doc.rev='01'
doc.save_with_history([nr_event])
self.write_bofreq_file(doc)
editors = bofreq_editors(doc)
responsible = bofreq_responsible(doc)
url = urlreverse('ietf.doc.views_doc.document_main', kwargs=dict(name=doc.name))
r = self.client.get(url)
self.assertContains(r,'Version: 01',status_code=200)
q = PyQuery(r.content)
self.assertEqual(0, len(q('td.edit>a.btn')))
self.assertEqual([],q('#change-request'))
editor_row = q('#editors').html()
for editor in editors:
self.assertInHTML(editor.name, editor_row)
responsible_row = q('#responsible').html()
for leader in responsible:
self.assertInHTML(leader.name,responsible_row)
for user in ('secretary','ad','iab-member'):
self.client.login(username=user,password=user+"+password")
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
self.assertEqual(6, len(q('td.edit>a.btn')))
self.client.logout()
self.assertNotEqual([],q('#change-request'))
editor = editors.first().user.username
self.client.login(username=editor, password=editor+"+password")
r = self.client.get(url)
self.assertEqual(r.status_code,200)
q = PyQuery(r.content)
self.assertEqual(3, len(q('td.edit>a.btn')))
self.assertNotEqual([],q('#change-request'))
self.client.logout()
url = urlreverse('ietf.doc.views_doc.document_main', kwargs=dict(name=doc,rev='00'))
r = self.client.get(url)
self.assertContains(r,'Version: 00',status_code=200)
self.assertContains(r,'is for an older version')
def test_edit_title(self):
doc = BofreqFactory()
editor = bofreq_editors(doc).first()
url = urlreverse('ietf.doc.views_bofreq.edit_title', kwargs=dict(name=doc.name))
title = doc.title
r = self.client.post(url,dict(title='New title'))
self.assertEqual(r.status_code, 302)
doc = reload_db_objects(doc)
self.assertEqual(title, doc.title)
nobody = PersonFactory()
self.client.login(username=nobody.user.username,password=nobody.user.username+'+password')
r = self.client.post(url,dict(title='New title'))
self.assertEqual(r.status_code, 403)
doc = reload_db_objects(doc)
self.assertEqual(title, doc.title)
self.client.logout()
for username in ('secretary', 'ad', 'iab-member', editor.user.username):
self.client.login(username=username, password=username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code,200)
docevent_count = doc.docevent_set.count()
empty_outbox()
r = self.client.post(url,dict(title=username))
self.assertEqual(r.status_code,302)
doc = reload_db_objects(doc)
self.assertEqual(doc.title, username)
self.assertEqual(docevent_count+1, doc.docevent_set.count())
self.assertEqual(1, len(outbox))
self.client.logout()
def state_pk_as_str(self, type_id, slug):
return str(State.objects.get(type_id=type_id, slug=slug).pk)
def test_edit_state(self):
doc = BofreqFactory()
editor = bofreq_editors(doc).first()
url = urlreverse('ietf.doc.views_bofreq.change_state', kwargs=dict(name=doc.name))
state = doc.get_state('bofreq')
r = self.client.post(url, dict(new_state=self.state_pk_as_str('bofreq','approved')))
self.assertEqual(r.status_code, 302)
doc = reload_db_objects(doc)
self.assertEqual(state, doc.get_state('bofreq'))
self.client.login(username=editor.user.username,password=editor.user.username+'+password')
r = self.client.post(url, dict(new_state=self.state_pk_as_str('bofreq','approved')))
self.assertEqual(r.status_code, 403)
doc = reload_db_objects(doc)
self.assertEqual(state,doc.get_state('bofreq'))
self.client.logout()
for username in ('secretary', 'ad', 'iab-member'):
doc.set_state(state)
self.client.login(username=username,password=username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code,200)
docevent_count = doc.docevent_set.count()
r = self.client.post(url,dict(new_state=self.state_pk_as_str('bofreq','approved' if username=='secretary' else 'declined'),comment=f'{username}-2309hnf'))
self.assertEqual(r.status_code,302)
doc = reload_db_objects(doc)
self.assertEqual('approved' if username=='secretary' else 'declined',doc.get_state_slug('bofreq'))
self.assertEqual(docevent_count+2, doc.docevent_set.count())
self.assertIn(f'{username}-2309hnf',doc.latest_event(type='added_comment').desc)
self.client.logout()
def test_change_editors(self):
doc = BofreqFactory()
previous_editors = list(bofreq_editors(doc))
acting_editor = previous_editors[0]
new_editors = set(previous_editors)
new_editors.discard(acting_editor)
new_editors.add(PersonFactory())
url = urlreverse('ietf.doc.views_bofreq.change_editors', kwargs=dict(name=doc.name))
postdict = dict(editors=[str(p.pk) for p in new_editors])
r = self.client.post(url, postdict)
self.assertEqual(r.status_code,302)
editors = bofreq_editors(doc)
self.assertEqual(set(previous_editors),set(editors))
nobody = PersonFactory()
self.client.login(username=nobody.user.username,password=nobody.user.username+'+password')
r = self.client.post(url, postdict)
self.assertEqual(r.status_code,403)
editors = bofreq_editors(doc)
self.assertEqual(set(previous_editors),set(editors))
self.client.logout()
for username in (previous_editors[0].user.username, 'secretary', 'ad', 'iab-member'):
empty_outbox()
self.client.login(username=username,password=username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code,200)
# Yes, unescape is needed twice, for names like "O'Connor"
unescaped = unescape(unescape(unicontent(r).encode('utf-8').decode('unicode-escape')))
for editor in previous_editors:
self.assertIn(editor.name,unescaped)
new_editors = set(previous_editors)
new_editors.discard(acting_editor)
new_editors.add(PersonFactory())
postdict = dict(editors=[str(p.pk) for p in new_editors])
r = self.client.post(url,postdict)
self.assertEqual(r.status_code, 302)
updated_editors = bofreq_editors(doc)
self.assertEqual(new_editors,set(updated_editors))
previous_editors = new_editors
self.client.logout()
self.assertEqual(len(outbox),1)
self.assertIn('BOF Request editors changed',outbox[0]['Subject'])
def test_change_responsible(self):
doc = BofreqFactory()
previous_responsible = list(bofreq_responsible(doc))
new_responsible = set(previous_responsible[1:])
new_responsible.add(RoleFactory(group__type_id='area',name_id='ad').person)
url = urlreverse('ietf.doc.views_bofreq.change_responsible', kwargs=dict(name=doc.name))
postdict = dict(responsible=[str(p.pk) for p in new_responsible])
r = self.client.post(url, postdict)
self.assertEqual(r.status_code,302)
responsible = bofreq_responsible(doc)
self.assertEqual(set(previous_responsible), set(responsible))
PersonFactory(user__username='nobody')
self.client.login(username='nobody',password='nobody+password')
r = self.client.post(url, postdict)
self.assertEqual(r.status_code,403)
responsible = bofreq_responsible(doc)
self.assertEqual(set(previous_responsible), set(responsible))
self.client.logout()
for username in ('secretary', 'ad', 'iab-member'):
empty_outbox()
self.client.login(username=username,password=username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code,200)
# Yes, unescape is needed twice, for names like "O'Connor"
unescaped = unescape(unescape(unicontent(r).encode('utf-8').decode('unicode-escape')))
for responsible in previous_responsible:
self.assertIn(responsible.name, unescaped)
new_responsible = set(previous_responsible)
new_responsible.add(RoleFactory(group__type_id='area',name_id='ad').person)
postdict = dict(responsible=[str(p.pk) for p in new_responsible])
r = self.client.post(url,postdict)
self.assertEqual(r.status_code, 302)
updated_responsible = bofreq_responsible(doc)
self.assertEqual(new_responsible,set(updated_responsible))
previous_responsible = new_responsible
self.client.logout()
self.assertEqual(len(outbox),1)
self.assertIn('BOF Request responsible leadership changed',outbox[0]['Subject'])
def test_change_responsible_validation(self):
doc = BofreqFactory()
url = urlreverse('ietf.doc.views_bofreq.change_responsible', kwargs=dict(name=doc.name))
login_testing_unauthorized(self,'secretary',url)
bad_batch = PersonFactory.create_batch(3)
good_batch = list()
good_batch.append(RoleFactory(group__type_id='area', name_id='ad').person)
good_batch.append(RoleFactory(group__acronym='iab', name_id='member').person)
pks = set()
pks.update([p.pk for p in good_batch])
pks.update([p.pk for p in bad_batch])
postdict = dict(responsible=[str(pk) for pk in pks])
r = self.client.post(url,postdict)
self.assertEqual(r.status_code, 200)
q = PyQuery(r.content)
error_text = q('.invalid-feedback').text()
for p in good_batch:
self.assertNotIn(p.plain_name(), error_text)
for p in bad_batch:
self.assertIn(p.plain_name(), error_text)
def test_submit(self):
doc = BofreqFactory()
url = urlreverse('ietf.doc.views_bofreq.submit', kwargs=dict(name=doc.name))
rev = doc.rev
r = self.client.post(url,{'bofreq_submission':'enter','bofreq_content':'# oiwefrase'})
self.assertEqual(r.status_code, 302)
doc = reload_db_objects(doc)
self.assertEqual(rev, doc.rev)
nobody = PersonFactory()
self.client.login(username=nobody.user.username, password=nobody.user.username+'+password')
r = self.client.post(url,{'bofreq_submission':'enter','bofreq_content':'# oiwefrase'})
self.assertEqual(r.status_code, 403)
doc = reload_db_objects(doc)
self.assertEqual(rev, doc.rev)
self.client.logout()
editor = bofreq_editors(doc).first()
for username in ('secretary', 'ad', 'iab-member', editor.user.username):
self.client.login(username=username, password=username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
file = NamedTemporaryFile(delete=False,mode="w+",encoding='utf-8')
file.write(f'# {username}')
file.close()
for postdict in [
{'bofreq_submission':'enter','bofreq_content':f'# {username}'},
{'bofreq_submission':'upload','bofreq_file':open(file.name,'rb')},
]:
docevent_count = doc.docevent_set.count()
empty_outbox()
r = self.client.post(url, postdict)
self.assertEqual(r.status_code, 302)
doc = reload_db_objects(doc)
self.assertEqual('%02d'%(int(rev)+1) ,doc.rev)
self.assertEqual(f'# {username}', doc.text())
self.assertEqual(docevent_count+1, doc.docevent_set.count())
self.assertEqual(1, len(outbox))
rev = doc.rev
self.client.logout()
os.unlink(file.name)
def test_start_new_bofreq(self):
url = urlreverse('ietf.doc.views_bofreq.new_bof_request')
nobody = PersonFactory()
login_testing_unauthorized(self,nobody.user.username,url)
r = self.client.get(url)
self.assertContains(r,'Fill in the details below. Keep items in the order they appear here.',status_code=200)
r = self.client.post(url, dict(title='default',
bofreq_submission='enter',
bofreq_content=render_to_string('doc/bofreq/bofreq_template.md',{})))
self.assertContains(r, 'The example content may not be saved.', status_code=200)
file = NamedTemporaryFile(delete=False,mode="w+",encoding='utf-8')
file.write('some stuff')
file.close()
for postdict in [
dict(title='title one', bofreq_submission='enter', bofreq_content='some stuff'),
dict(title='title two', bofreq_submission='upload', bofreq_file=open(file.name,'rb')),
]:
empty_outbox()
r = self.client.post(url, postdict)
self.assertEqual(r.status_code,302)
name = f"bofreq-{xslugify(nobody.last_name())[:64]}-{postdict['title']}".replace(' ','-')
bofreq = Document.objects.filter(name=name,type_id='bofreq').first()
self.assertIsNotNone(bofreq)
self.assertIsNotNone(DocAlias.objects.filter(name=name).first())
self.assertEqual(bofreq.title, postdict['title'])
self.assertEqual(bofreq.rev, '00')
self.assertEqual(bofreq.get_state_slug(), 'proposed')
self.assertEqual(list(bofreq_editors(bofreq)), [nobody])
self.assertEqual(bofreq.latest_event(NewRevisionDocEvent).rev, '00')
self.assertEqual(bofreq.text_or_error(), 'some stuff')
self.assertEqual(len(outbox),1)
os.unlink(file.name)
existing_bofreq = BofreqFactory(requester_lastname=nobody.last_name())
for postdict in [
dict(title='', bofreq_submission='enter', bofreq_content='some stuff'),
dict(title='a title', bofreq_submission='enter', bofreq_content=''),
dict(title=existing_bofreq.title, bofreq_submission='enter', bofreq_content='some stuff'),
dict(title='森川', bofreq_submission='enter', bofreq_content='some stuff'),
dict(title='a title', bofreq_submission='', bofreq_content='some stuff'),
]:
r = self.client.post(url,postdict)
self.assertEqual(r.status_code, 200, f'Wrong status_code for {postdict}')
q = PyQuery(r.content)
self.assertTrue(q('form div.is-invalid'), f'Expected an error for {postdict}')
def test_post_proposed_restrictions(self):
states = State.objects.filter(type_id='bofreq').exclude(slug='proposed')
bofreq = BofreqFactory()
editor = bofreq_editors(bofreq).first()
for view in ('submit', 'change_editors', 'edit_title'):
url = urlreverse(f'ietf.doc.views_bofreq.{view}', kwargs=dict(name=bofreq.name))
for state in states:
bofreq.set_state(state)
for username in ('secretary', 'ad', 'iab-member'):
self.client.login(username=username, password=username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code,200)
self.client.logout()
self.client.login(username=editor.user.username, password=editor.user.username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code, 403, f'editor should not be able to use {view} in state {state.slug}')
self.client.logout()
url = urlreverse('ietf.doc.views_doc.document_main', kwargs=dict(name=bofreq.name))
self.client.login(username=editor.user.username, password=editor.user.username+'+password')
r = self.client.get(url)
self.assertEqual(r.status_code,200)
q = PyQuery(r.content)
self.assertEqual(0, len(q('td.edit>a.btn')))
self.assertEqual([],q('#change-request'))