def update(self, request, *args, **kwargs): note_id = request.data['id'] # drop the id to pass validator del (request.data['id']) serializer = NotesSerializer(data=request.data, partial=True) if serializer.is_valid(): data = {'is_todo': request.data.get('is_todo', 0), 'tags': request.data.get('tag', '')} joplin = JoplinApi(api_type=settings.JOPLIN_API_TYPE, token=settings.JOPLIN_TOKEN) res = joplin.update_note(note_id=note_id, parent_id=request.data['parent_id'], title=request.data['title'], body=request.data['body'], **data) if res.status_code == 200: return Response({'status': 'note updated'}) return Response({'status': 'note not updated {}'.format(res.status_code)}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class TestJoplinApi(unittest.TestCase): def setUp(self): token = '5fa5a79fdd9feb428297b32b8ebfb92160b95678d6a05b7823df19212046106e89c71e3674df7b8c60ee47c53469cfe3cb4c8b9a174cde5960fabc9186503ae4' self.joplin = JoplinApi(token=token) def test_create_folder(self): folder = 'TEST FOLDER1' self.assertIs(type(folder), str) res = self.joplin.create_folder(folder=folder) self.assertTrue(res.status_code == 200) def test_get_folders(self): res = self.joplin.get_folders() self.assertTrue(res.status_code == 200) self.assertIsInstance(res.json(), list) def test_get_folder(self): res = self.joplin.create_folder(folder='MY FOLDER2') data = res.json() parent_id = data['id'] res = self.joplin.get_folder(parent_id) self.assertTrue(res.status_code == 200) self.assertIsInstance(res.json(), dict) def test_create_note(self): res = self.joplin.create_folder(folder='MY FOLDER3') data = res.json() parent_id = data['id'] self.assertIs(type(parent_id), str) body = '# title 1\n ## subtitle \n ```python\npython --version\n```' self.assertIs(type(body), str) kwargs = {'tags': 'tag1, tag2'} res = self.joplin.create_note(title="NOTE TEST", body=body, parent_id=parent_id, **kwargs) self.assertTrue(res.status_code == 200) note_id = res.json()['id'] body = '# title 1\n ## subtitle \n ```python\npython --version\n```' self.assertIs(type(body), str) kwargs = {'tags': 'tag1, tag2, tag11'} res = self.joplin.update_note(note_id=note_id, body=body, title="NOTE TEST", parent_id=parent_id, **kwargs) self.assertTrue(res.status_code == 200) def test_get_notes(self): res = self.joplin.get_notes() self.assertTrue(res.status_code == 200) self.assertIsInstance(res.json(), list) def test_get_note(self): res = self.joplin.create_folder(folder='MY FOLDER4') data = res.json() parent_id = data['id'] body = '# title 1\n ## subtitle \n ```python\npython --version\n```' res = self.joplin.create_note(title="NOTE TEST2", body=body, parent_id=parent_id) data = res.json() note_id = data['id'] self.assertIs(type(note_id), str) res = self.joplin.get_note(note_id) self.assertTrue(res.status_code == 200) self.assertIsInstance(res.json(), dict) def test_get_tags(self): res = self.joplin.get_tags() self.assertTrue(res.status_code == 200) self.assertIsInstance(res.json(), list)