def test_change_thumbnail(self): file = (io.BytesIO(b"abcdef"), 'test.jpg') api_utils.post(url_for_security('login'), data={ 'email': '*****@*****.**', 'password': '******' }, headers={'Authentication-Token': self.token}) bookmark = BookMark(url='dummy.url', img='dummy.png') past_img = bookmark.img self.current_user.bookmarks.append(bookmark) db.session.add(self.current_user) db.session.commit() data = {'img': file, 'id': BookMark.query.first().id} res = self.client.patch(url_for('library.change_thumbnail'), data=data, headers={ 'Authentication-Token': self.token, 'Content-Type': 'multipart/form-data' }) self.assertNotEqual(past_img, bookmark.img) path = Path(app.config['STORAGE_PATH'] + '/' + bookmark.img) self.assertTrue(path.is_file()) self.assertStatus(res, 204)
def add_ele(): code = const.NO_CONTENT try: bookmark = BookMark( url=get_http_format_url(request.json.get('url', ''))) tag_inputs = Tag.conv_tag_str_to_list(request.json.get('tags', '')) tags = [Tag.find_or_make(tag) for idx, tag in enumerate(tag_inputs)] bookmark.makeup() contract.register_bookmark_and_tag(current_user, bookmark, *tags) except InvalidURLException: code = const.SERVER_ERROR finally: return json.dumps({}), code, {'ContentType': 'application/json'}
def test_search_thumbnail_by_tag_not_exists(self): from bookMarkLibrary.database import db user = User.query.first() b1 = BookMark(url='http://localhost', name='test_thumbnail') b2 = BookMark(url='http://localhost2', name='test_thumbnail2') tag = Tag(tag='test_tag') b1.tags.append(tag) b2.tags.append(tag) user.bookmarks.extend([b1, b2]) db.session.commit() result = api_utils.get(url_for('library.urls', tag='null'), headers={'Authentication-Token': self.token}) content = api_utils.loads(result.data.decode('utf-8')) self.assertEqual(0, len(content['bookmarks'])) self.assertEqual(1, len(content['tags']))
def test_register_bookmark_and_tag(self): u1 = User() b1 = BookMark(url='google.com') t1 = Tag(tag='tag1') t2 = Tag(tag='tag2') contract.register_bookmark_and_tag(u1, b1, t1, t2) self.assertEqual(b1, u1.bookmarks[0]) self.assertEqual(2, len(u1.bookmarks[0].tags))
def test_tag_bookmark_relation(self): u1 = User() b1 = BookMark(url='google.com') b2 = BookMark(url='python.org') u1.create_bookmarks(b1, b2) t1 = Tag(tag='tag1') t2 = Tag(tag='tag2') b1.tags.extend([t1, t2]) t2.bookmarks.extend([b1, b2]) db.session.add_all([u1, b1, b2, t1, t2]) db.session.commit() self.assertEqual(2, len(u1.bookmarks.all())) self.assertEqual(u1, b1.holder) self.assertEqual(u1, b1.holder) self.assertEqual(2, len(b1.tags)) self.assertEqual(2, len(t2.bookmarks)) self.assertEqual(1, len(b2.tags)) self.assertEqual(1, len(t1.bookmarks))
def test_thumbnail_return_img_path_when_exist(self): with patch('library.models.os') as mock_os: mock_os.path.exists.return_value = True bookmark = BookMark() bookmark.url = 'https://google.com' bookmark.makeup() bookmark.img = 'test' self.assertEqual('/storage/test', bookmark.thumbnail)
def test_change_thumbnail(self, mock_resize_img, file): with patch('library.contract.db') as mock_db: b = BookMark() file.filename = 'test.png' file.save.return_value = True mock_resize_img.return_value = True # mock_db.session.add.return_value = True # mock_db.session.commit.return_value = True contract.change_thumbnail(b, file) file.save.assert_called_once() mock_resize_img.assert_called_once() mock_db.session.add.assert_called_once_with(b) assert 'test.png' in b.img
def change_thumbnail(bookmark: BookMark, file): if file and __allowed_file(file.filename): ts = time.time() img_name = str(int(ts)) + file.filename path = os.path.join(app.config['STORAGE_PATH'], img_name) file.save(path) resize_img(path) try: bookmark.img = img_name db.session.add(bookmark) db.session.commit() except Exception: os.remove(path) db.session.rollback()
def test_register_bookmark_and_tag_rollback_on_error(self): with patch('library.contract.db', wraps=db) as mock_db: # mock_db.session.rollback.return_value = None mock_db.session.commit.side_effect = IntegrityError u1 = User() b1 = BookMark(url='google.com') t = Tag(tag='tag2') t2 = Tag() contract.register_bookmark_and_tag(u1, b1, t, t2) mock_db.session.rollback.assert_called_once() self.assertIsNone(u1.bookmarks[0].id, u1.bookmarks[0].id) self.assertEqual(0, Tag.query.count()) self.assertEqual(0, BookMark.query.count())
def test_change_thumbnail_rollback_when_error_occured( self, mock_os, mock_resize_img, file): with patch('library.contract.db') as mock_db: b = BookMark() file.filename = 'test.png' file.save.return_value = True mock_resize_img.return_value = True mock_db.session.add.return_value = True mock_db.session.rollback.return_value = True mock_db.session.commit.side_effect = IntegrityError mock_os.remove.return_value = True contract.change_thumbnail(b, file) file.save.assert_called_once() mock_resize_img.assert_called_once() mock_db.session.add.assert_called_once_with(b) mock_db.session.rollback.assert_called_once() mock_os.remove.assert_called_once()
def test_show_thumbnails(self): from bookMarkLibrary.database import db user = User.query.first() b1 = BookMark(url='http://localhost', name='test_thumbnail') b2 = BookMark(url='http://localhost2', name='test_thumbnail2') user.bookmarks.extend([b1, b2]) db.session.add_all([user]) db.session.commit() result = api_utils.get(url_for('library.urls'), headers={'Authentication-Token': self.token}) content = api_utils.loads(result.data.decode('utf-8')) assert 'bookmarks' in content assert 'tags' in content self.assertEqual([b1.as_dict(), b2.as_dict()], content['bookmarks'])
def test_thumbnail_return_img_value_on_default(self): bookmark = BookMark() bookmark.url = 'https://google.com' bookmark.makeup() bookmark.img = 'test' self.assertEqual('test', bookmark.thumbnail)
def test_blank_page_thumbnail(self): bookmark = BookMark() bookmark.url = 'https://google.com' bookmark.makeup() bookmark.img = None self.assertEqual('/static/img/blank.png', bookmark.thumbnail)