def test_store_file(monkeypatch, tmp_path):
    def fake_store_file(_ignored, _ignored2):
        pass

    note = Note()
    note.conversion_settings.export_folder = tmp_path
    attachment_id = '1234'
    image_attachment = sn_attachment.FileNSAttachment(note, attachment_id)
    image_attachment._json = {
        'attachment':
            {
                '1234':
                    {'md5': 'abcd', 'name': 'not_my_file.pdf'}
            }
    }

    note.parent_notebook.attachment_md5_file_name_dict = {'0987': "my_file.pdf"}

    monkeypatch.setattr(file_writer, 'store_file', fake_store_file)
    image_attachment.create_file_name()
    image_attachment.generate_relative_path_to_notebook()
    image_attachment.generate_absolute_path()
    assert image_attachment.file_name == Path('my_name.png')
    assert image_attachment._full_path == Path(tmp_path, 'notebook_folder', 'attachments', 'my_name.png')

    image_attachment.store_file()

    assert len(note.parent_notebook.attachment_md5_file_name_dict) == 2
def test_FileNSAttachment_get_content_to_save():
    note = Note()
    attachment_id = '1234'
    file_attachment = sn_attachment.FileNSAttachment(note, attachment_id)
    result = file_attachment.get_content_to_save()

    assert result == 'file name in nsx'
def test_FileNSAttachment_create_file_name(raw_name, expected):
    note = Note()
    attachment_id = '1234'
    file_attachment = sn_attachment.FileNSAttachment(note, attachment_id)

    file_attachment._name = raw_name
    file_attachment.create_file_name()

    assert file_attachment.file_name == Path(expected)
def test_is_duplicate_file_not_a_duplicate():
    note = Note()
    attachment_id = '1234'
    image_attachment = sn_attachment.FileNSAttachment(note, attachment_id)

    image_attachment._name = 'my_file.pdf'
    note.parent_notebook.attachment_md5_file_name_dict = {'abcd': "zxyz"}

    assert image_attachment.is_duplicate_file() is False
def test_test_change_file_name_file_attachment_good_file_name():
    note = Note()
    attachment_id = '1234'
    image_attachment = sn_attachment.FileNSAttachment(note, attachment_id)

    image_attachment._name = 'my_file.pdf'
    image_attachment.create_file_name()

    assert image_attachment.file_name == Path('my_file.pdf')
def test_FileNSAttachment_create_html_link():
    note = Note()
    attachment_id = '1234'
    file_attachment = sn_attachment.FileNSAttachment(note, attachment_id)

    file_attachment._file_name = Path('my_file.png')
    file_attachment._path_relative_to_notebook = Path('attachments/my_file.png')
    file_attachment.create_html_link()

    assert file_attachment.html_link == '<a href="attachments/my_file.png">my_file.png</a>'
def test_test_change_file_name_file_attachment_no_extension_on_file_name_and_not_recognised(mocker):
    note = Note()
    attachment_id = '1234'
    image_attachment = sn_attachment.FileNSAttachment(note, attachment_id)

    image_attachment._name = 'my_file'
    mocker.patch('zip_file_reader.read_binary_file', return_value=b'1234')
    mocker.patch('helper_functions.file_extension_from_bytes', return_value=None)
    image_attachment.create_file_name()

    assert image_attachment.file_name == Path('my_file')
def test_is_duplicate_file_is_a_duplicate():
    note = Note()
    attachment_id = '1234'
    image_attachment = sn_attachment.FileNSAttachment(note, attachment_id)
    image_attachment._json = {
        'attachment':
            {
                '1234':
                    {'md5': '0987', 'name': 'my_file.pdf'}
            }
    }

    note.parent_notebook.attachment_md5_file_name_dict = {'0987': "my_file.pdf"}

    assert image_attachment.is_duplicate_file() is True
def test_store_file_will_not_store_as_duplicate_md5_and_name(monkeypatch):
    def fake_store_file(_ignored, _ignored2):
        pass

    note = Note()
    attachment_id = '1234'
    image_attachment = sn_attachment.FileNSAttachment(note, attachment_id)
    image_attachment._json = {
        'attachment':
            {
                '1234':
                    {'md5': '0987', 'name': 'my_file.pdf'}
            }
    }

    note.parent_notebook.attachment_md5_file_name_dict = {'0987': "my_file.pdf"}
    monkeypatch.setattr(file_writer, 'store_file', fake_store_file)
    image_attachment.store_file()

    assert len(note.parent_notebook.attachment_md5_file_name_dict) == 1
    def create_attachments(self):
        if self._attachments_json is None:
            self.logger.warning(
                f'Note - {self._title} - Has Null set for attachments. '
                f'There may be a sync issues between desktop and web version of Note Station.'
            )
            return 0, 0

        for attachment_id in self._attachments_json:
            if self._attachments_json[attachment_id]['type'].startswith('image') \
                    and \
                    self._attachments_json[attachment_id].get('ref'):
                self._attachments[
                    attachment_id] = sn_attachment.ImageNSAttachment(
                        self, attachment_id)
                self._image_count += 1
            else:
                self._attachments[
                    attachment_id] = sn_attachment.FileNSAttachment(
                        self, attachment_id)
                self._attachment_count += 1

        return self._image_count, self._attachment_count