def test_attachment_parent_realm_raises_exception(self): """TracError is raised when 'attachment' is the resource parent realm. """ path_info = '/attachment/attachment/parent_id/attachment_id' req = MockRequest(self.env, path_info=path_info) module = AttachmentModule(self.env) self.assertTrue(module.match_request(req)) with self.assertRaises(TracError): module.process_request(req)
def test_post_request_without_attachment_raises_exception(self): """TracError is raised for POST request with no file.""" path_info = '/attachment/parent_realm/parent_id' req = MockRequest(self.env, path_info=path_info, method='POST', args={'action': 'new'}) module = AttachmentModule(self.env) self.assertTrue(module.match_request(req)) with self.assertRaises(TracError) as cm: module.process_request(req) self.assertEqual("No file uploaded", unicode(cm.exception))
def test_invalid_post_request_raises_exception(self): path_info = '/attachment/parent_realm/parent_id/attachment_id' attachment = Attachment(self.env, 'parent_realm', 'parent_id') attachment.insert('attachment_id', io.BytesIO(), 0, 1) req = MockRequest(self.env, method='POST', action=None, path_info=path_info) module = AttachmentModule(self.env) self.assertTrue(module.match_request(req)) with self.assertRaises(HTTPBadRequest): module.process_request(req)
def test_post_request_without_attachment_raises_exception(self): """TracError is raised when a POST request is submitted without an attachment. """ path_info = '/attachment/parent_realm/parent_id' req = MockRequest(self.env, path_info=path_info, method='POST', args={'action': 'new'}) module = AttachmentModule(self.env) self.assertTrue(module.match_request(req)) with self.assertRaises(TracError): module.process_request(req)
def test_post_request_with_empty_attachment_raises_exception(self): """TracError is raised for POST request with empty file.""" module = AttachmentModule(self.env) path_info = '/attachment/parent_realm/parent_id' with tempfile.NamedTemporaryFile('rb', dir=self.env.path) as file_: upload = Mock(filename=file_.name, file=file_) req = MockRequest(self.env, path_info=path_info, method='POST', args={'action': 'new', 'attachment': upload}) self.assertTrue(module.match_request(req)) with self.assertRaises(TracError) as cm: module.process_request(req) self.assertEqual("Can't upload empty file", unicode(cm.exception))
def test_post_request_exceeding_max_size_raises_exception(self): """TracError is raised for file exceeding max size""" self.env.config.set('attachment', 'max_size', 10) module = AttachmentModule(self.env) path_info = '/attachment/parent_realm/parent_id' with tempfile.NamedTemporaryFile('w+b', dir=self.env.path) as file_: file_.write(b' ' * (module.max_size + 1)) file_.flush() upload = Mock(filename=file_.name, file=file_) req = MockRequest(self.env, path_info=path_info, method='POST', args={'action': 'new', 'attachment': upload}) self.assertTrue(module.match_request(req)) with self.assertRaises(TracError) as cm: module.process_request(req) self.assertEqual("Maximum attachment size: 10 bytes", unicode(cm.exception))
def _delegate_new_request(self, req): attachments = req.args.get('attachment') if not isinstance(attachments, list): attachments = [attachments] mod = AttachmentModule(self.env) for val in attachments: req.args['attachment'] = val try: mod.process_request(req) except OSError, e: if e.args[0] == errno.ENAMETOOLONG: raise TracError(_("File name too long")) if os.name == 'nt' and e.args[0] == errno.ENOENT: raise TracError(_("File name too long")) raise TracError(os.strerror(e.args[0])) except RedirectListened: pass