def test_write(self):
        file_contents = 'XML'
        real_application = Application(SCHEDULE)
        application = mock.MagicMock(document_type=SCHEDULE,
                                     OUTPUT_DIR='/tmp',
                                     MODEL_DIR=real_application.MODEL_DIR)
        db_chapter = ChapterFactory(id=1, description='Test')

        chapter = ScheduleChapter(application, db_chapter)
        actual_remote_file_name = chapter.write(file_contents)
        db_chapter.refresh_from_db()

        assert db_chapter.schedule_document.name == actual_remote_file_name
        with zipfile.ZipFile(db_chapter.schedule_document) as fh:
            actual_files = [f.filename for f in fh.filelist]
            assert set(actual_files) == {
                'word/fontTable.xml', 'word/numbering.xml', 'word/header1.xml',
                'docProps/core.xml', 'word/_rels/document.xml.rels',
                'customXml/_rels/item1.xml.rels', 'customXml/itemProps1.xml',
                'word/theme/theme1.xml', 'word/header2.xml',
                'customXml/item1.xml', 'docProps/app.xml', 'word/endnotes.xml',
                'word/footer1.xml', 'word/webSettings.xml', 'word/styles.xml',
                '_rels/.rels', 'word/settings.xml', 'word/footnotes.xml',
                'word/document.xml', '[Content_Types].xml'
            }
            actual_document_xml = fh.read('word/document.xml')
            assert actual_document_xml == bytes(file_contents, 'utf-8')
    def test_write(self, mock_get_duties, mock_get_section_details,
                   mock_prepend_introduction):
        mock_get_duties.return_value = None
        mock_get_section_details.return_value = None
        mock_prepend_introduction.return_value = None

        file_contents = 'XML'
        application = Application(CLASSIFICATION)
        db_chapter = ChapterFactory(id=1, description='Test')
        chapter = ClassificationChapter(application, db_chapter)
        actual_remote_file_name = chapter.write(file_contents)
        assert mock_prepend_introduction.called is True
        db_chapter.refresh_from_db()

        assert db_chapter.classification_document.name == actual_remote_file_name
        with zipfile.ZipFile(db_chapter.classification_document) as fh:
            actual_files = [f.filename for f in fh.filelist]
            assert set(actual_files) == {
                'word/fontTable.xml', 'word/numbering.xml', 'word/header1.xml',
                'docProps/core.xml', 'word/_rels/document.xml.rels',
                'customXml/_rels/item1.xml.rels', 'customXml/itemProps1.xml',
                'word/theme/theme1.xml', 'word/header2.xml',
                'customXml/item1.xml', 'docProps/app.xml', 'word/endnotes.xml',
                'word/footer1.xml', 'word/webSettings.xml', 'word/styles.xml',
                '_rels/.rels', 'word/settings.xml', 'word/footnotes.xml',
                'word/document.xml', '[Content_Types].xml'
            }
            actual_document_xml = fh.read('word/document.xml')
            assert actual_document_xml == bytes(file_contents, 'utf-8')
Example #3
0
def test_upload_generic_document_to_s3():
    chapter = ChapterFactory()
    fake_file_content = b'information'
    with tempfile.NamedTemporaryFile() as f:
        f.write(fake_file_content)
        f.seek(0)
        actual_remote_file_name = upload_generic_document_to_s3(
            chapter, 'schedule_document', f.name, '1.docx')
        chapter.refresh_from_db()
        assert chapter.schedule_document.read() == fake_file_content
        assert actual_remote_file_name.endswith('docx')
        assert chapter.schedule_document_created_at == timezone.now()