def test_to_python_invalid(self): from eportfolio.views.validators import ImageUploadConverter from cgi import MiniFieldStorage validator = ImageUploadConverter() image_path = join(dirname(__file__), 'data', 'image.pdf') fd = open(image_path, 'rb') storage = MiniFieldStorage('image', 'image.pdf') storage.file = fd storage.filename = 'image.pdf' self.assertRaises(Invalid, validator._to_python, storage, None)
def test_to_python_png(self): from eportfolio.views.validators import ImageUploadConverter from cgi import MiniFieldStorage validator = ImageUploadConverter() image_path = join(dirname(__file__), 'data', 'image.png') fd = open(image_path, 'rb') storage = MiniFieldStorage('image', 'image.png') storage.file = fd storage.filename = 'image.png' result = validator._to_python(storage, None) image = Image.open(result) # Image has been scaled # FIXME: why do we have 130 x 130 here self.assertEqual((130, 130), image.size) self.assertEqual('JPEG', image.format)
def test_journal_add_view_image(self): from eportfolio.models.app import Application from eportfolio.views.journal import journal_add_view from cgi import MiniFieldStorage root = Application() project = self._add_project() student = self._add_student() project.students.append(student) # 'upload_directory' setting has to be set self.config.add_settings( upload_directory=join(dirname(__file__), 'data')) # Dummy repoze.filesafe data manager from repoze.filesafe.testing import setupDummyDataManager, cleanupDummyDataManager setupDummyDataManager() # Image file to upload image_path = join(dirname(__file__), 'data', 'image.jpg') fd = open(image_path, 'rb') storage = MiniFieldStorage('image', 'image.jpg') storage.file = fd storage.filename = 'image.jpg' # Student is logged in self.config.testing_securitypolicy(userid=student.email) request = testing.DummyRequest(root=root) request.POST['text'] = u'Entry with an image' request.POST['image'] = storage request.POST['form.submitted'] = 1 journal_add_view(project, request) self.assertEquals(1, project.journal_entries.count()) entry = project.journal_entries[0] self.assertEqual('image/jpeg', entry.image.content_type) cleanupDummyDataManager()
def test_journal_add_view_image(self): from eportfolio.models.app import Application from eportfolio.views.journal import journal_add_view from cgi import MiniFieldStorage root = Application() project = self._add_project() student = self._add_student() project.students.append(student) # 'upload_directory' setting has to be set self.config.add_settings(upload_directory=join(dirname(__file__), 'data')) # Dummy repoze.filesafe data manager from repoze.filesafe.testing import setupDummyDataManager, cleanupDummyDataManager setupDummyDataManager() # Image file to upload image_path = join(dirname(__file__), 'data', 'image.jpg') fd = open(image_path, 'rb') storage = MiniFieldStorage('image', 'image.jpg') storage.file = fd storage.filename = 'image.jpg' # Student is logged in self.config.testing_securitypolicy(userid=student.email) request = testing.DummyRequest(root=root) request.POST['text'] = u'Entry with an image' request.POST['image'] = storage request.POST['form.submitted'] = 1 journal_add_view(project, request) self.assertEquals(1, project.journal_entries.count()) entry = project.journal_entries[0] self.assertEqual('image/jpeg', entry.image.content_type) cleanupDummyDataManager()