diff --git a/README.md b/README.md index 2fb2d93..bff71e1 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,7 @@ It's possible to configure the test run via some environment variables: ### Master +- Fix adding non-image ressources (). - Cast `markup_language` to an appropriate enum type. - Add changelog. diff --git a/joppy/api.py b/joppy/api.py index 5f7b99d..fa9f524 100644 --- a/joppy/api.py +++ b/joppy/api.py @@ -334,8 +334,14 @@ def add_tag_to_note(self, tag_id: str, note_id: str) -> None: def add_resource_to_note(self, resource_id: str, note_id: str) -> None: """Add a resource to a given note.""" note = self.get_note(id_=note_id, fields="body") - resource = self.get_resource(id_=resource_id, fields="title") - body_with_attachment = f"{note.body}\n![{resource.title}](:/{resource_id})" + resource = self.get_resource(id_=resource_id, fields="title,mime") + # TODO: Use "assertIsNotNone()" when + # https://github.com/python/mypy/issues/5528 is resolved. + assert resource.mime is not None + image_prefix = "!" if resource.mime.startswith("image/") else "" + body_with_attachment = ( + f"{note.body}\n{image_prefix}[{resource.title}](:/{resource_id})" + ) self.modify_note(note_id, body=body_with_attachment) def delete_all_notes(self) -> None: diff --git a/test/test_api.py b/test/test_api.py index eb858f6..9751463 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -410,19 +410,28 @@ def test_add(self, filename): def test_add_to_note(self, filename): """Add a resource to an existing note.""" self.api.add_notebook() - note_id = self.api.add_note() - resource_id = self.api.add_resource(filename=filename) - self.api.add_resource_to_note(resource_id=resource_id, note_id=note_id) - - # Verify the resource is attached to the note. - resources = self.api.get_resources(note_id=note_id).items - self.assertEqual(len(resources), 1) - self.assertEqual(resources[0].id, resource_id) - - # TODO: Seems to be not working. - # notes = self.api.get_notes(resource_id=resource_id)["items"] - # self.assertEqual(len(notes), 1) - # self.assertEqual(notes[0]["id"], note_id) + for file_ in ["test/grant_authorization_button.png", filename]: + with self.subTest(file_=file_): + note_id = self.api.add_note() + resource_id = self.api.add_resource(filename=file_) + self.api.add_resource_to_note(resource_id=resource_id, note_id=note_id) + + # Verify the resource is attached to the note. + resources = self.api.get_resources( + note_id=note_id, fields="id,mime" + ).items + self.assertEqual(len(resources), 1) + self.assertEqual(resources[0].id, resource_id) + + # Verify the markdown is correct (prefix "!" for images). + note = self.api.get_note(id_=note_id, fields="body") + # TODO: Use "assertIsNotNone()" when + # https://github.com/python/mypy/issues/5528 is resolved. + assert resources[0].mime is not None + image_prefix = "!" if resources[0].mime.startswith("image/") else "" + self.assertEqual( + f"\n{image_prefix}[{file_}](:/{resource_id})", note.body + ) @with_resource def test_delete(self, filename):