def check_packaged_resource(wgt_file, resource_info=None): if resource_info is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_info = template.get_resource_info() if resource_info['type'] == 'widget': code_url = resource_info['contents']['src'] if not code_url.startswith(('http://', 'https://')): try: code = wgt_file.read(code_url) except KeyError: msg = _('Missing contents file: %(file_name)s.') raise InvalidContents(msg % {'file_name': code_url}) try: code.decode(resource_info['contents']['charset']) except UnicodeDecodeError: msg = _( '%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).' ) raise InvalidContents( msg % { 'file_name': code_url, 'charset': resource_info['contents']['charset'] }) check_invalid_doc_content(wgt_file, resource_info, 'longdescription') check_invalid_doc_content(wgt_file, resource_info, 'doc') check_invalid_doc_content(wgt_file, resource_info, 'changelog') check_invalid_embedded_resources(wgt_file, resource_info)
def test_wgt_upload_invalid_contents(self, install_component): c = Client() c.login(username='******', password='******') install_component.side_effect = InvalidContents('test') with open( os.path.join(os.path.dirname(__file__), 'test-data/basic_widget.wgt'), 'rb') as f: response = c.post(self.resource_collection_url, {'file': f}, HTTP_HOST='www.example.com') self.assertEqual(response.status_code, 400)
def check_invalid_embedded_resources(wgt_file, resource_info): if resource_info['type'] != 'mashup': return files = wgt_file.namelist() for embedded_resource in resource_info['embedded']: if embedded_resource['src'] not in files: raise InvalidContents('Missing embedded file: %s' % embedded_resource['src']) try: embedded_wgt = WgtFile( BytesIO(wgt_file.read(embedded_resource['src']))) check_packaged_resource(embedded_wgt) except Exception as e: raise InvalidContents('Invalid embedded file: %s' % embedded_resource['src'], details=e)
def check_invalid_doc_entry(wgt_file, doc_path): try: doc_code = wgt_file.read(doc_path) except: raise InvalidContents('missing file: %s' % doc_path) try: doc_code = doc_code.decode('utf-8') except: raise InvalidContents('file is not encoded using UTF-8: %s' % doc_path) try: markdown.markdown(doc_code, output_format='xhtml5', extensions=['codehilite', 'fenced_code']) except: raise InvalidContents("file cannot be parsed as markdown: %s" % doc_path)
def check_invalid_image(wgt_file, resource_info, key): image_url = resource_info[key] if image_url != '' and not image_url.startswith(('http://', 'https://')): image_path = url2pathname(image_url) try: wgt_file.read(image_path) except KeyError: raise InvalidContents('missing image file: %s' % image_path)