|
9 | 9 |
|
10 | 10 | from importlib import import_module |
11 | 11 | from mock import patch |
| 12 | +from pathlib import Path |
12 | 13 |
|
13 | 14 | from django.apps import apps |
14 | 15 | from django.conf import settings |
|
21 | 22 | import debug # pyflakes:ignore |
22 | 23 |
|
23 | 24 | import ietf |
| 25 | +from ietf.doc.utils import get_unicode_document_content |
24 | 26 | from ietf.group.factories import RoleFactory |
25 | 27 | from ietf.meeting.factories import MeetingFactory, SessionFactory |
26 | 28 | from ietf.meeting.test_data import make_meeting_test_data |
@@ -212,6 +214,93 @@ def test_api_add_session_attendees(self): |
212 | 214 | self.assertTrue(session.attended_set.filter(person=recman).exists()) |
213 | 215 | self.assertTrue(session.attended_set.filter(person=otherperson).exists()) |
214 | 216 |
|
| 217 | + def test_api_upload_polls_and_chatlog(self): |
| 218 | + recmanrole = RoleFactory(group__type_id='ietf', name_id='recman') |
| 219 | + recmanrole.person.user.last_login = timezone.now() |
| 220 | + recmanrole.person.user.save() |
| 221 | + |
| 222 | + badrole = RoleFactory(group__type_id='ietf', name_id='ad') |
| 223 | + badrole.person.user.last_login = timezone.now() |
| 224 | + badrole.person.user.save() |
| 225 | + |
| 226 | + meeting = MeetingFactory(type_id='ietf') |
| 227 | + session = SessionFactory(group__type_id='wg', meeting=meeting) |
| 228 | + |
| 229 | + for type_id, content in ( |
| 230 | + ( |
| 231 | + "chatlog", |
| 232 | + """[ |
| 233 | + { |
| 234 | + "author": "Raymond Lutz", |
| 235 | + "text": "<p>Yes I like that comment just made</p>", |
| 236 | + "time": "2022-07-28T19:26:16Z" |
| 237 | + }, |
| 238 | + { |
| 239 | + "author": "Carsten Bormann", |
| 240 | + "text": "<p>But software is not a thing.</p>", |
| 241 | + "time": "2022-07-28T19:26:45Z" |
| 242 | + } |
| 243 | + ]""" |
| 244 | + ), |
| 245 | + ( |
| 246 | + "polls", |
| 247 | + """[ |
| 248 | + { |
| 249 | + "start_time": "2022-07-28T19:19:54Z", |
| 250 | + "end_time": "2022-07-28T19:20:23Z", |
| 251 | + "text": "Are you willing to review the documents?", |
| 252 | + "raise_hand": 57, |
| 253 | + "do_not_raise_hand": 11 |
| 254 | + }, |
| 255 | + { |
| 256 | + "start_time": "2022-07-28T19:20:56Z", |
| 257 | + "end_time": "2022-07-28T19:21:30Z", |
| 258 | + "text": "Would you be willing to edit or coauthor a document?", |
| 259 | + "raise_hand": 31, |
| 260 | + "do_not_raise_hand": 31 |
| 261 | + } |
| 262 | + ]""" |
| 263 | + ), |
| 264 | + ): |
| 265 | + url = urlreverse(f"ietf.meeting.views.api_upload_{type_id}") |
| 266 | + apikey = PersonalApiKey.objects.create(endpoint=url, person=recmanrole.person) |
| 267 | + badapikey = PersonalApiKey.objects.create(endpoint=url, person=badrole.person) |
| 268 | + |
| 269 | + r = self.client.post(url, {}) |
| 270 | + self.assertContains(r, "Missing apikey parameter", status_code=400) |
| 271 | + |
| 272 | + r = self.client.post(url, {'apikey': badapikey.hash()} ) |
| 273 | + self.assertContains(r, "Restricted to role: Recording Manager", status_code=403) |
| 274 | + |
| 275 | + r = self.client.get(url, {'apikey': apikey.hash()} ) |
| 276 | + self.assertContains(r, "Method not allowed", status_code=405) |
| 277 | + |
| 278 | + r = self.client.post(url, {'apikey': apikey.hash()} ) |
| 279 | + self.assertContains(r, "Missing apidata parameter", status_code=400) |
| 280 | + |
| 281 | + for baddict in ( |
| 282 | + '{}', |
| 283 | + '{"bogons;drop table":"bogons;drop table"}', |
| 284 | + '{"session_id":"Not an integer;drop table"}', |
| 285 | + f'{{"session_id":{session.pk},"{type_id}":"not a list;drop table"}}', |
| 286 | + f'{{"session_id":{session.pk},"{type_id}":"not a list;drop table"}}', |
| 287 | + f'{{"session_id":{session.pk},"{type_id}":[{{}}, {{}}, "not an int;drop table", {{}}]}}', |
| 288 | + ): |
| 289 | + r = self.client.post(url, {'apikey': apikey.hash(), 'apidata': baddict}) |
| 290 | + self.assertContains(r, "Malformed post", status_code=400) |
| 291 | + |
| 292 | + bad_session_id = Session.objects.order_by('-pk').first().pk + 1 |
| 293 | + r = self.client.post(url, {'apikey': apikey.hash(), 'apidata': f'{{"session_id":{bad_session_id},"{type_id}":[]}}'}) |
| 294 | + self.assertContains(r, "Invalid session", status_code=400) |
| 295 | + |
| 296 | + # Valid POST |
| 297 | + r = self.client.post(url,{'apikey':apikey.hash(),'apidata': f'{{"session_id":{session.pk}, "{type_id}":{content}}}'}) |
| 298 | + self.assertEqual(r.status_code, 200) |
| 299 | + |
| 300 | + newdoc = session.sessionpresentation_set.get(document__type_id=type_id).document |
| 301 | + newdoccontent = get_unicode_document_content(newdoc.name, Path(session.meeting.get_materials_path()) / type_id / newdoc.uploaded_filename) |
| 302 | + self.assertEqual(json.loads(content), json.loads(newdoccontent)) |
| 303 | + |
215 | 304 | def test_api_upload_bluesheet(self): |
216 | 305 | url = urlreverse('ietf.meeting.views.api_upload_bluesheet') |
217 | 306 | recmanrole = RoleFactory(group__type_id='ietf', name_id='recman') |
|
0 commit comments