|
11 | 11 | from imio.esign.utils import add_files_to_session |
12 | 12 | from imio.esign.utils import create_external_session |
13 | 13 | from imio.esign.utils import get_file_download_url |
| 14 | +from imio.esign.utils import get_file_info |
14 | 15 | from imio.esign.utils import get_filesize |
15 | 16 | from imio.esign.utils import get_max_download_date |
16 | 17 | from imio.esign.utils import get_session_annotation |
|
30 | 31 | from plone.namedfile.file import NamedBlobFile |
31 | 32 | from plone.namedfile.file import NamedBlobImage |
32 | 33 | from zope.annotation import IAnnotations |
| 34 | +from zope.event import notify |
| 35 | +from zope.lifecycleevent import ObjectModifiedEvent |
33 | 36 |
|
34 | 37 | import collective.iconifiedcategory |
35 | 38 | import json |
@@ -452,6 +455,77 @@ def test_add_files_with_duplicate_filenames(self): |
452 | 455 | self.assertIn("same_filename-1.pdf", filenames) |
453 | 456 | self.assertIn("same_filename-2.pdf", filenames) |
454 | 457 |
|
| 458 | + def test_add_files_already_exist_is_updated(self): |
| 459 | + """When adding a file to the same session, it is updated.""" |
| 460 | + annot = get_session_annotation() |
| 461 | + self.assertEqual(len(annot["sessions"]), 0) |
| 462 | + |
| 463 | + signers = [ |
| 464 | + ("user1", "user1@sign.com", "User 1", "Position 1"), |
| 465 | + ] |
| 466 | + |
| 467 | + annex0_uid = self.uids[0] |
| 468 | + annex0 = api.content.get(UID=annex0_uid) |
| 469 | + |
| 470 | + sid, session = add_files_to_session(signers, (annex0_uid,)) |
| 471 | + self.assertEqual(sid, 0) |
| 472 | + self.assertEqual(len(session["files"]), 1) |
| 473 | + self.assertEqual(session["files"][0]["filename"], "annex0.pdf") |
| 474 | + self.assertEqual(session["files"][0]["title"], "Annex 0") |
| 475 | + self.assertEqual(session["size"], 6968) |
| 476 | + # edit annex and add again, still one annex in session and data are updated |
| 477 | + annex0.file.filename = u"new_annex0.pdf" |
| 478 | + annex0.setTitle('New Annex 0') |
| 479 | + sid, session = add_files_to_session(signers, (annex0_uid,)) |
| 480 | + # same session_id |
| 481 | + self.assertEqual(sid, 0) |
| 482 | + self.assertEqual(len(session["files"]), 1) |
| 483 | + self.assertEqual(session["files"][0]["filename"], "new_annex0.pdf") |
| 484 | + self.assertEqual(session["files"][0]["title"], "New Annex 0") |
| 485 | + self.assertEqual(session["size"], 6968) |
| 486 | + # add again exact same file |
| 487 | + sid, session = add_files_to_session(signers, (annex0_uid,)) |
| 488 | + self.assertEqual(sid, 0) |
| 489 | + self.assertEqual(len(session["files"]), 1) |
| 490 | + self.assertEqual(session["files"][0]["filename"], "new_annex0.pdf") |
| 491 | + self.assertEqual(session["files"][0]["title"], "New Annex 0") |
| 492 | + self.assertEqual(session["size"], 6968) |
| 493 | + # add second file 2 times |
| 494 | + annex1_uid = self.uids[1] |
| 495 | + annex1 = api.content.get(UID=annex1_uid) |
| 496 | + sid, session = add_files_to_session(signers, (annex1_uid,)) |
| 497 | + self.assertEqual(sid, 0) |
| 498 | + self.assertEqual(len(session["files"]), 2) |
| 499 | + self.assertEqual(session["files"][1]["filename"], "annex1.pdf") |
| 500 | + self.assertEqual(session["files"][1]["title"], "Annex 1") |
| 501 | + self.assertEqual(session["size"], 13982) |
| 502 | + # edit and add again |
| 503 | + annex1.setTitle('New Annex 1') |
| 504 | + # edit file, filename and content so size changed |
| 505 | + with open(os.path.join(os.path.dirname(__file__), "annex1.pdf"), "rb") as f: |
| 506 | + annex1.file = NamedBlobFile(data=f.read(), filename=u"new_annex1.pdf", contentType="application/pdf") |
| 507 | + notify(ObjectModifiedEvent(annex1)) |
| 508 | + # data were already updated |
| 509 | + self.assertEqual(len(session["files"]), 2) |
| 510 | + self.assertEqual(session["files"][1]["filename"], "new_annex1.pdf") |
| 511 | + self.assertEqual(session["files"][1]["title"], "New Annex 1") |
| 512 | + self.assertEqual(session["files"][1]["scan_id"], "012345600000001") |
| 513 | + self.assertEqual(session["size"], 13936) |
| 514 | + # change file but add a filename already used, change scan_id as well |
| 515 | + with open(os.path.join(os.path.dirname(__file__), "annex1.pdf"), "rb") as f: |
| 516 | + annex1.file = NamedBlobFile(data=f.read(), filename=u"new_annex0.pdf", contentType="application/pdf") |
| 517 | + annex1.scan_id = "012345600000002" |
| 518 | + notify(ObjectModifiedEvent(annex1)) |
| 519 | + self.assertEqual(session["files"][1]["filename"], "new_annex0-1.pdf") |
| 520 | + self.assertEqual(session["files"][1]["scan_id"], "012345600000002") |
| 521 | + # edit annex out of any session |
| 522 | + annex2_uid = self.uids[2] |
| 523 | + annex2 = api.content.get(UID=annex2_uid) |
| 524 | + notify(ObjectModifiedEvent(annex2)) |
| 525 | + # just to check, remove annex0 |
| 526 | + remove_files_from_session((annex0_uid,)) |
| 527 | + self.assertEqual(session["size"], 6968) |
| 528 | + |
455 | 529 | def test_remove_context_from_session(self): |
456 | 530 | """Test removing a context from a session.""" |
457 | 531 | annot = get_session_annotation() |
@@ -528,6 +602,30 @@ def test_get_sessions_for(self): |
528 | 602 | sessions[0]['watchers'] = ["watcher@sign.com"] |
529 | 603 | self.assertEqual(get_session_info(0)['watchers'], ["watcher@sign.com"]) |
530 | 604 |
|
| 605 | + def test_get_file_info(self): |
| 606 | + """Test getting infos for a given file.""" |
| 607 | + annex0_uid = self.uids[0] |
| 608 | + annex1_uid = self.uids[1] |
| 609 | + # no session |
| 610 | + self.assertIsNone(get_file_info(0, annex0_uid)) |
| 611 | + # create session |
| 612 | + signers = [("user1", "user1@sign.com", "User 1", "Position 1")] |
| 613 | + sid, session = add_files_to_session(signers, (annex0_uid,)) |
| 614 | + self.assertEqual(get_file_info(0, annex0_uid)['uid'], annex0_uid) |
| 615 | + self.assertIsNone(get_file_info(0, annex1_uid)) |
| 616 | + # readonly=True by default |
| 617 | + file_info = get_file_info(0, annex0_uid) |
| 618 | + file_info['title'] = u'New title annex 0' |
| 619 | + # not changed in the annotation |
| 620 | + self.assertEqual( |
| 621 | + get_session_annotation()['sessions'][0]['files'][0]['title'], u'Annex 0') |
| 622 | + # readonly=False |
| 623 | + file_info = get_file_info(0, annex0_uid, readonly=False) |
| 624 | + file_info['title'] = u'New title annex 0' |
| 625 | + # changed in the annotation |
| 626 | + self.assertEqual( |
| 627 | + get_session_annotation()['sessions'][0]['files'][0]['title'], u'New title annex 0') |
| 628 | + |
531 | 629 | def test_get_file_download_url(self): |
532 | 630 | """Test generating file download URL from UID.""" |
533 | 631 | uid = "f40682caafc045b4b81973bd82ea9ab6" |
|
0 commit comments