def add_reamaze_script(context): import hmac import hashlib try: anonymous_mode = getattr(settings, 'REAMAZE_OK_FOR_ANONYMOUS', False) prefix_for_user_id = getattr(settings, 'REAMAZE_PREFIX_USER_ID', None) request = context['request'] reamaze_context = {'display_reamaze': False, 'reamaze_auth_key': None} user_id = reamaze_context["user_id"] = request.user.id if prefix_for_user_id: user_id = prefix_for_user_id + str(request.user.id) reamaze_context["user_id"] = user_id if user_is_authenticated(request.user) or anonymous_mode: reamaze_context.update({'display_reamaze': True, 'reamaze_js_url': getattr(settings, 'REAMAZE_JS_URL', ""), 'reamaze_account': getattr(settings, 'REAMAZE_ACCOUNT', ""), 'user_agent': request.META['HTTP_USER_AGENT'], 'http_referer': request.META.get('HTTP_REFERER', ""), 'reamaze_channel': getattr(settings, 'REAMAZE_CHANNEL', None)}) if user_is_authenticated(request.user): reamaze_secret_key = getattr(settings, 'REAMAZE_SECRET_KEY', b"") reamaze_auth_key = hmac.new(reamaze_secret_key, six.b(str(user_id)) + b":" + six.b(request.user.email), hashlib.sha256).hexdigest() reamaze_context['reamaze_auth_key'] = reamaze_auth_key context.update(reamaze_context) return context except KeyError: return context
def edit_document(self, **params): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Create a document without tags to edit document = models.Document.objects.create(title="Test document", file=fake_file) # Build another fake file another_fake_file = ContentFile(b("A boring example document")) another_fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document changed!", 'file': another_fake_file, } post_data.update(params) response = self.client.post(reverse('wagtaildocs:edit', args=(document.id,)), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be changed doc = models.Document.objects.filter(title=post_data['title']) self.assertTrue(doc.exists()) return doc.first()
def __getitem__(self, item): if item in ('PositiveIntegerField', 'PositiveSmallIntegerField'): # The check name must be unique for the database. Add a random # component so the regresion tests don't complain about duplicate names fldtype = {'PositiveIntegerField': 'int', 'PositiveSmallIntegerField': 'smallint'}[item] rnd_hash = hashlib.md5(b(str(random.random()))).hexdigest() unique = base64.b64encode(b(rnd_hash), b('__'))[:6] return '%(fldtype)s CONSTRAINT [CK_%(fldtype)s_pos_%(unique)s_%%(column)s] CHECK ([%%(column)s] >= 0)' % locals() return super(DataTypesWrapper, self).__getitem__(item)
def get_authors(exclude_primary_author=True): git_log = subprocess.Popen( ["git", "log", "--format=%aN <%aE>", "--numstat"], stdout=subprocess.PIPE) output = git_log.communicate()[0] authors = {} author = None for line in output.splitlines(): match = re_line.match(line) if not match: if line: author = line continue authors[author] = authors.get(author, 0) + max([ int(num) for num in match.groups()]) # Combine duplicate authors (by email). emails = {} for author, changes in list(authors.items()): match = re_author.match(author) if not match: continue author_emails = match.group(1) for email in author_emails.split(six.b(',')): if six.b('@') not in email: continue if email in emails: remove_author = emails[email] if remove_author not in authors: continue if changes < authors[remove_author]: author, remove_author = remove_author, author authors[author] = authors[author] + authors[remove_author] del authors[remove_author] else: emails[email] = author # Sort authors list. list_authors = sorted(authors.items(), key=itemgetter(1), reverse=True) total = float(sum(authors.values())) if exclude_primary_author: top_author = list_authors.pop(0) total -= top_author[1] return [ (author.decode(), changes, changes / total * 100) for author, changes in list_authors]
def test_listdir(self): """File storage returns a tuple containing directories and files.""" self.assertFalse(self.storage.exists('storage_test_1')) self.assertFalse(self.storage.exists('storage_test_2')) self.assertFalse(self.storage.exists('storage_dir_1')) self.storage.save('storage_test_1', ContentFile(six.b('custom content'))) self.storage.save('storage_test_2', ContentFile(six.b('custom content'))) self.storage.client.file_create_folder(self.location + '/storage_dir_1') dirs, files = self.storage.listdir(self.location) self.assertEqual(set(dirs), set([u'storage_dir_1'])) self.assertEqual(set(files), set([u'storage_test_1', u'storage_test_2']))
def __init__(self, data=None, *args, **kwargs): try: form_name = self.form_name except AttributeError: # if form_name is unset, then generate a pseudo unique name, based upon the class name form_name = b64encode(six.b(self.__class__.__name__)).rstrip(six.b('=')) self.form_name = kwargs.pop('form_name', form_name) error_class = kwargs.pop('error_class', TupleErrorList) kwargs.setdefault('error_class', error_class) self.convert_widgets() if isinstance(data, QueryDict): data = self.rectify_multipart_form_data(data.copy()) elif isinstance(data, dict): data = self.rectify_ajax_form_data(data.copy()) super(NgFormBaseMixin, self).__init__(data, *args, **kwargs)
def test_download_pypi_release_when_isolated_is_on(requests_mock, rf, repository, settings): file_data = six.b('Hello from PyPI') md5_digest = md5(file_data).hexdigest() settings.LOCALSHOP_ISOLATED = True release_file = ReleaseFileFactory( release__package__repository=repository, distribution=None, md5_digest=md5_digest) url_kwargs = { 'repo': repository.slug, 'name': release_file.release.package.name, 'pk': release_file.pk, 'filename': release_file.filename } requests_mock.return_value = Mock(**{ 'headers': { 'content-length': len(file_data), 'content-type': 'application/octet-stream', }, 'content': file_data, }) request = rf.get(reverse('packages:download', kwargs=url_kwargs)) response = views.DownloadReleaseFile.as_view()(request, **url_kwargs) assert response.status_code == 200 assert response.content == file_data requests_mock.assert_called_with( u'http://www.example.org/download/test-1.0.0-sdist.zip', proxies=None, stream=True)
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created, and be placed in the root collection document = models.Document.objects.get(title="Test document") root_collection = Collection.get_first_root_node() self.assertEqual( document.collection, root_collection ) # Check that the file_size field was set self.assertTrue(document.file_size)
def dumps(self, obj): if not isinstance(obj, Q): raise SerializationError string = json.dumps(self.serialize(obj), default=dt2ts) if self.b64_enabled: return base64.b64encode(six.b(string)) return string
def assertUpload(self, filename, content): # First request a slot self.assertEquals(Upload.objects.count(), 0) response = slot(jid=user_jid, name=filename, size=len(content)) self.assertEquals(response.status_code, 200) self.assertEquals(Upload.objects.count(), 1) put_url, get_url = response.content.decode('utf-8').split() # Upload the file put_path = urlsplit(put_url).path response = put(put_path, content) self.assertEquals(response.status_code, 201) # Get the object, verify that the same URLs are generated upload = Upload.objects.all()[0] # we verified there is exactly one above try: self.assertEqual((put_url, get_url), upload.get_urls(response.wsgi_request)) # open the file, verify contents self.assertEqual(six.b(content), upload.file.read()) # try to download it self.assertEqual(upload.file.url, urlsplit(get_url).path) finally: # remove file upload.file.delete(save=True)
def setUp(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' self.root_collection = Collection.get_first_root_node() self.evil_plans_collection = self.root_collection.add_child(name="Evil plans") self.nice_plans_collection = self.root_collection.add_child(name="Nice plans") # Create a document to edit self.document = models.Document.objects.create( title="Test document", file=fake_file, collection=self.nice_plans_collection ) # Create a user with change_document permission but not add_document user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******' ) change_permission = Permission.objects.get( content_type__app_label='wagtaildocs', codename='change_document' ) admin_permission = Permission.objects.get( content_type__app_label='wagtailadmin', codename='access_admin' ) self.changers_group = Group.objects.create(name='Document changers') GroupCollectionPermission.objects.create( group=self.changers_group, collection=self.root_collection, permission=change_permission ) user.groups.add(self.changers_group) user.user_permissions.add(admin_permission) self.assertTrue(self.client.login(username='******', password='******'))
def test_post_with_collections(self): root_collection = Collection.get_first_root_node() evil_plans_collection = root_collection.add_child(name="Evil plans") # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, 'collection': evil_plans_collection.id, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created, and be placed in the Evil Plans collection self.assertTrue(models.Document.objects.filter(title="Test document").exists()) root_collection = Collection.get_first_root_node() self.assertEqual( models.Document.objects.get(title="Test document").collection, evil_plans_collection )
def test_not_image(self): """ Non-images raise an exception. """ self.assertRaises( IOError, source_generators.pil_image, BytesIO(six.b('not an image')))
def get(self, request, *args, **kwargs): temp_file = ContentFile(b(""), name=self.tarfile_name) with tarfile.TarFile(fileobj=temp_file, mode='w', debug=3) as tar_file: files = self.get_files() for file_ in files: file_name = file_.name try: data = file_.read() except UnicodeDecodeError: pass file_.seek(0, os.SEEK_SET) size = len(data) try: if isinstance(data, bytes): lol = BytesIO(data) else: lol = BytesIO(data.encode()) except UnicodeDecodeError: pass try: info = tar_file.gettarinfo(fileobj=file_) except UnsupportedOperation: info = tarfile.TarInfo(name=file_name) info.size = size tar_file.addfile(tarinfo=info, fileobj=lol) file_size = temp_file.tell() temp_file.seek(0) response = HttpResponse(temp_file, content_type='application/x-tar') response['Content-Disposition'] = 'attachment; filename=%s' % self.tarfile_name response['Content-Length'] = file_size return response
def test_pdf_template_response(self, show_content=False): """Test PDFTemplateResponse.""" context = {'title': 'Heading'} request = RequestFactory().get('/') response = PDFTemplateResponse(request=request, template=self.template, context=context, show_content_in_browser=show_content) self.assertEqual(response._request, request) self.assertEqual(response.template_name, self.template) self.assertEqual(response.context_data, context) self.assertEqual(response.filename, None) self.assertEqual(response.header_template, None) self.assertEqual(response.footer_template, None) self.assertEqual(response.cmd_options, {}) self.assertFalse(response.has_header('Content-Disposition')) # Render to temporary file tempfile = response.render_to_temporary_file(self.template) tempfile.seek(0) html_content = smart_str(tempfile.read()) self.assertTrue(html_content.startswith('<html>')) self.assertTrue('<h1>{title}</h1>'.format(**context) in html_content) pdf_content = response.rendered_content self.assertTrue(pdf_content.startswith(b'%PDF-')) self.assertTrue(pdf_content.endswith(b'%%EOF\n')) # Footer cmd_options = {'title': 'Test PDF'} response = PDFTemplateResponse(request=request, template=self.template, context=context, filename=self.pdf_filename, show_content_in_browser=show_content, footer_template=self.footer_template, cmd_options=cmd_options) self.assertEqual(response.filename, self.pdf_filename) self.assertEqual(response.header_template, None) self.assertEqual(response.footer_template, self.footer_template) self.assertEqual(response.cmd_options, cmd_options) self.assertTrue(response.has_header('Content-Disposition')) tempfile = response.render_to_temporary_file(self.footer_template) tempfile.seek(0) footer_content = smart_str(tempfile.read()) footer_content = make_absolute_paths(footer_content) media_url = 'file://{0}/'.format(settings.MEDIA_ROOT) self.assertTrue(media_url in footer_content, True) static_url = 'file://{0}/'.format(settings.STATIC_ROOT) self.assertTrue(static_url in footer_content, True) pdf_content = response.rendered_content title = '\0'.join(cmd_options['title']) self.assertIn(six.b(title), pdf_content)
def test_save_contents(self): release_file = factories.ReleaseFileFactory() dummy_fh = six.BytesIO(six.b("release-file-contents")) release_file.save_filecontent('dummy.txt', dummy_fh) assert release_file.distribution.name == 'default/2.7/t/test-package/dummy.txt' assert os.path.exists(release_file.distribution.path)
def test_save_tempfile_ok(self): filename = self.session_id + '_save_new_file.txt' content = six.b('test content') fileobj = self._make_tempfile(filename, content) self.storage.save(filename, fileobj) with self.storage.open(filename) as f: self.assertEqual(f.read(), content)
def setUp(self): self.login() # Create a document for running tests on self.doc = Document.objects.create( title="Test document", file=ContentFile(b("Simple text document")), )
def setUp(self): self.fake_file = ContentFile(b("A boring example document")) self.fake_file.name = 'test.txt' self.password_collection = Collection.objects.get(name='Password protected') self.login_collection = Collection.objects.get(name='Login protected') self.group_collection = Collection.objects.get(name='Group protected') self.view_restriction = CollectionViewRestriction.objects.get(collection=self.password_collection) self.event_editors_group = Group.objects.get(name='Event editors')
def test_django_csrf(self): request = self.factory.get('/browserid/csrf/') rotate_token(request) token = get_token(request) response = self.view.get(request) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, six.b(token))
def setUp(self): self.login() # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Create a document to edit self.document = models.Document.objects.create(title="Test document", file=fake_file)
def hash(self, value): """ Generate a hash of the given tuple. This is for use in a cache key. """ if isinstance(value, tuple): value = tuple(to_bytestring(v) for v in value) return hashlib.md5(six.b(':').join(value)).hexdigest()
def test_validation_with_no_data(self): """ Validation should still function when no data dictionary is provided. """ uploaded_file = BytesIO(six.b('stuff')) uploaded_file.name = 'stuff.txt' uploaded_file.size = len(uploaded_file.getvalue()) serializer = UploadedFileSerializer(files={'file': uploaded_file}) self.assertFalse(serializer.is_valid())
def pad(text, block_size, zero=False): """ Given a text string and a block size, pads the text with bytes of the same value as the number of padding bytes. This is the recommended method, and the one used by pgcrypto. See http://www.di-mgt.com.au/cryptopad.html for more information. """ num = block_size - (len(text) % block_size) ch = b'\0' if zero else six.b(chr(num)) return text + (ch * num)
def test_standard_behaviour_determines_non_form_content_PUT(self): """ Ensure request.DATA returns content for PUT request with non-form content. """ content = six.b('qwerty') content_type = 'text/plain' request = Request(factory.put('/', content, content_type=content_type)) request.parsers = (PlainTextParser(), ) self.assertEqual(request.DATA, content)
def test_activation_nonexistent_key(self): """ Attempting to activate with a non-existent key (i.e., one not associated with any account) fails. """ # Due to the way activation keys are constructed during # registration, this will never be a valid key. invalid_key = hashlib.sha1(six.b('foo')).hexdigest() self.failIf(RegistrationProfile.objects.activate_user(invalid_key))
def test_save_memoryfile_seeked_ok(self): filename = self.session_id + '_save_new_file.txt' content = six.b('test content one') fileobj = self._make_memfile(filename, content) fileobj.seek(3) self.storage.save(filename, fileobj) with self.storage.open(filename) as f: self.assertEqual(f.read(), content)
def test_delete_file(self): release_file = factories.ReleaseFileFactory() dummy_fh = six.BytesIO(six.b("release-file-contents")) release_file.save_filecontent('dummy.txt', dummy_fh) assert os.path.exists(release_file.distribution.path) utils.delete_files(models.ReleaseFile, instance=release_file) assert not os.path.exists(release_file.distribution.path)
def test_request_DATA_with_text_content(self): """ Ensure request.data returns content for POST request with non-form content. """ content = six.b("qwerty") content_type = "text/plain" request = Request(factory.post("/", content, content_type=content_type)) request.parsers = (PlainTextParser(),) self.assertEqual(request.data, content)
def test_request_DATA_with_text_content(self): """ Ensure request.DATA returns content for POST request with non-form content. """ content = six.b('qwerty') content_type = 'text/plain' request = Request(factory.post('/', content, content_type=content_type)) request.parsers = (PlainTextParser(),) self.assertEqual(request.DATA, content)
def test_request_DATA_with_text_content(self): """ Ensure request.data returns content for POST request with non-form content. """ content = six.b('qwerty') content_type = 'text/plain' request = Request(factory.post('/', content, content_type=content_type)) request.parsers = (PlainTextParser(), ) self.assertEqual(request.data, content)
def test_delete_instance_view(self): """ DELETE requests to RetrieveUpdateDestroyAPIView should delete an object. """ request = factory.delete('/1') with self.assertNumQueries(2): response = self.view(request, pk=1).render() assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == six.b('') ids = [obj.id for obj in self.objects.all()] assert ids == [2, 3]
def test_create(self): now = datetime.datetime.now() file = BytesIO(six.b('stuff')) file.name = 'stuff.txt' file.size = len(file.getvalue()) serializer = UploadedFileSerializer(data={'created': now}, files={'file': file}) uploaded_file = UploadedFile(file=file, created=now) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.object.created, uploaded_file.created) self.assertEqual(serializer.object.file, uploaded_file.file) self.assertFalse(serializer.object is uploaded_file)
def test_filepreviews_doesnt_generate_if_settings_not_enabled(self): setup_mock() self.settings.api_key = '' self.settings.api_secret = '' self.settings.save() PreviewableDocument.objects.create(title='Test document', file=ContentFile( b('Hello world'), 'test1.txt')) self.assertEqual(len(responses.calls), 0)
def test_calendar_view_home(self): calendar_view_url = reverse('calendar_home', kwargs={'calendar_slug': 'example'}) response = self.client.get(calendar_view_url) self.assertEquals(response.status_code, 200) expected = '<a href="/feed/calendar/upcoming/1/">Feed</a>' try: self.assertTrue(expected in response.content) except TypeError: self.assertTrue( six.b('<a href="/feed/calendar/upcoming/1/">Feed</a>') in response.content)
def downloadFolderAsTar(path): temp_file = ContentFile(b(""), name=os.path.basename(path) + '.tar') with tarfile.TarFile(fileobj=temp_file, mode='w', debug=3) as tar_file: tar_file.add(path, arcname=os.path.basename(path)) file_size = temp_file.tell() temp_file.seek(0) response = HttpResponse(temp_file, content_type='application/x-tar') response[ 'Content-Disposition'] = 'attachment; filename=\"' + os.path.basename( path) + '.tar\"' response['Content-Length'] = file_size return response
def render(self, context): try: email = self.email.resolve(context) except template.VariableDoesNotExist: return '' default = "blank" size = int(self.size) * 2 # requested at retina size by default and scaled down at point of use with css gravatar_url = "//www.gravatar.com/avatar/" + hashlib.md5(b(email.lower())).hexdigest() + "?" gravatar_url += urlencode({'s': str(size), 'd': default}) return gravatar_url
def test_match_md5(monkeypatch, random_bytes, tmpdir): md5 = hashlib.md5() md5.update(random_bytes) md5sum = md5.hexdigest() urlopen_mock = mock.Mock(return_value=mock.Mock( read=lambda: six.b(md5sum))) monkeypatch.setattr("django.utils.six.moves.urllib.request.urlopen", urlopen_mock) filename = os.path.join(str(tmpdir), "md5") with open(filename, "wb") as f: f.write(random_bytes) assert geoipdb_loader._match_md5(filename, "someurl") urlopen_mock.assert_called_once_with("someurl")
def test_with_missing_source_file(self): # Build a fake file fake_file = ContentFile(b("An ephemeral document")) fake_file.name = 'to-be-deleted.txt' # Create a new document to delete the source for document = models.Document.objects.create(title="Test missing source document", file=fake_file) document.file.delete(False) response = self.client.get(reverse('wagtaildocs:edit', args=(document.id,)), {}) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'wagtaildocs/documents/edit.html') self.assertContains(response, 'File not found')
def setUp(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Create a document to edit self.document = models.Document.objects.create(title="Test document", file=fake_file) # Create a user with change_document permission but not add_document user = get_user_model().objects.create_user(username='******', email='*****@*****.**', password='******') change_permission = Permission.objects.get(content_type__app_label='wagtaildocs', codename='change_document') admin_permission = Permission.objects.get(content_type__app_label='wagtailadmin', codename='access_admin') user.user_permissions.add(change_permission, admin_permission) self.client.login(username='******', password='******')
def test_package_upload(django_app, admin_user, repository, upload_post_data, separator): """Test package upload with \n and \r\n as separator. \r\n is actually the correct separator but there was a bug in older python versions which used \n. See http://bugs.python.org/issue10510 """ post_data = upload_post_data.to_string().replace(b'\r\n', separator) headers = { 'Content-type': upload_post_data.content_type, 'Authorization': basic_auth_header('admin', 'password'), } response = django_app.post( '/repo/%s/' % repository.slug, params=post_data, headers=headers, user=admin_user) assert response.status_code == 200 package = Package.objects.filter(name='localshop').first() assert package is not None assert package.is_local is True assert package.releases.count() == 1 release = package.releases.first() assert release.author == 'Michael van Tellingen' assert release.author_email == '*****@*****.**' assert release.description == '' assert release.download_url == '' assert release.home_page == 'http://github.com/mvantellingen/localshop' assert release.license == 'BSD' assert release.metadata_version == '1.0' assert release.summary == 'A private pypi server including auto-mirroring of pypi.' assert release.version == '0.1' assert release.files.count() == 1 release_file = release.files.first() assert release_file is not None assert release_file.python_version == 'source' assert release_file.filetype == 'sdist' assert release_file.md5_digest == '06ffe94789d7bd9efba1109f40e935cf' assert release_file.distribution.read() == six.b('binary-test-data-here')
def test_delete_file_twice_referenced(self): release_file = factories.ReleaseFileFactory() dummy_fh = six.BytesIO(six.b("release-file-contents")) release_file.save_filecontent('dummy.txt', dummy_fh) release_file = factories.ReleaseFileFactory( release=release_file.release, filetype='bdist_egg') release_file.save_filecontent('dummy.txt', dummy_fh) self.assertTrue(os.path.exists(release_file.distribution.path)) utils.delete_files(models.ReleaseFile, instance=release_file) # File should still exist self.assertTrue(os.path.exists(release_file.distribution.path))
def test_file_access_options(self): """ Standard file access options are available, and work as expected. """ self.assertFalse(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'w') f.write('storage contents') f.close() self.assertTrue(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'r') self.assertEqual(f.read(), six.b('storage contents')) f.close() self.storage.delete('storage_test') self.assertFalse(self.storage.exists('storage_test'))
def test_run_doctest(self, uuid4_mock): uuid4_mock.side_effect = iter( [char * 32 for char in string.hexdigits[:16]]) class DummyStream: content = "" encoding = 'utf8' def write(self, text): self.content += text def flush(self): pass dummy_stream = DummyStream() before = sys.stdout sys.stdout = dummy_stream with open(os.path.join(os.path.dirname(__file__), 'doctests.txt')) as f: with tempfile.NamedTemporaryFile() as temp: text = f.read() if PY3: # unicode literals in the doctests screw up doctest on py3. # this is pretty icky, but I can't find any other # workarounds :( text = re.sub(r"""\bu(["\'])""", r"\1", text) temp.write(b(text)) else: temp.write(text) temp.flush() import doctest doctest.testfile( temp.name, module_relative=False, optionflags=doctest.IGNORE_EXCEPTION_DETAIL | doctest.ELLIPSIS, encoding='utf-8', ) sys.stdout = before content = dummy_stream.content if content: before.write(content + '\n') self.fail()
class ResultSetTestCase(TestCase): example_multipart = b( dedent("""\ MIME-Version: 1.0\r Content-Type: multipart/mixed; boundary=frontier\r \r This is a message with multiple parts in MIME format.\r --frontier Content-Type: text/plain\r Content-Disposition: attachmet; filename="message.msg"\r Content-Id: message-part\r \r This is the body of the message.\r --frontier Content-Type: application/octet-stream\r Content-Transfer-Encoding: base64\r \r PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUgYm9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\r --frontier-- """)) def test_result_set_from_raw(self): result_set = result_set_from_raw_data(self.example_multipart) self.assertTrue(len(result_set) == 2) first = result_set[0] second = result_set[1] if isinstance(first.data, str): first_data = b(first.data) else: first_data = first.data if isinstance(second.data, str): second_data = b(second.data) else: second_data = second.data self.assertEqual(first_data, b"This is the body of the message.") self.assertEqual(first.content_type, b"text/plain") self.assertEqual(first.filename, b"message.msg") self.assertEqual(first.identifier, b"message-part") self.assertEqual( second_data, b"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUgYm9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==" )
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, } response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created self.assertTrue(models.Document.objects.filter(title="Test document").exists())
def test_download_file_incorrect_md5_sum(requests_mock): file_data = six.b('My cool package') release_file = ReleaseFileFactory(distribution=None, md5_digest='arcoiro') requests_mock.return_value = mock.Mock(**{ 'headers': { 'content-length': len(file_data), 'content-type': 'application/octet-stream', }, 'content': file_data, }) tasks.download_file.run(release_file.pk) release_file = models.ReleaseFile.objects.get(pk=release_file.pk) assert not release_file.distribution
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit title change post_data = { 'title': "Test document changed!", 'file': fake_file, } response = self.client.post(reverse('wagtaildocs:edit', args=(self.document.id,)), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document title should be changed self.assertEqual(models.Document.objects.get(id=self.document.id).title, "Test document changed!")
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, } response = self.client.post(reverse('wagtaildocs:chooser_upload'), post_data) # Check that the response is a javascript file saying the document was chosen self.assertTemplateUsed(response, 'wagtaildocs/chooser/document_chosen.js') self.assertContains(response, "modal.respond('documentChosen'") # Document should be created self.assertTrue(models.Document.objects.filter(title="Test document").exists())
def test_post(self): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, } response = self.client.post(reverse('wagtaildocs:chooser_upload'), post_data) # Check that the response is the 'document_chosen' step response_json = json.loads(response.content.decode()) self.assertEqual(response_json['step'], 'document_chosen') # Document should be created self.assertTrue(models.Document.objects.filter(title="Test document").exists())
def get(self, request, *args, **kwargs): temp_file = ContentFile(b(""), name=self.zipfile_name) with zipfile.ZipFile(temp_file, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file: files = self.get_files() for file_ in files: path = file_.name zip_file.writestr(path, file_.read()) file_size = temp_file.tell() temp_file.seek(0) response = HttpResponse(temp_file, content_type='application/zip') response[ 'Content-Disposition'] = 'attachment; filename=%s' % self.zipfile_name response['Content-Length'] = file_size return response
def test_run_doctest(self): class DummyStream: content = "" encoding = 'utf8' def write(self, text): self.content += text def flush(self): pass dummy_stream = DummyStream() before = sys.stdout #sys.stdout = dummy_stream with open(os.path.join(os.path.dirname(__file__), 'doctests.txt')) as f: with tempfile.NamedTemporaryFile() as temp: text = f.read() if PY3: # unicode literals in the doctests screw up doctest on py3. # this is pretty icky, but I can't find any other # workarounds :( text = re.sub(r"""\bu(["\'])""", r"\1", text) temp.write(b(text)) temp.flush() import doctest doctest.testfile( temp.name, module_relative=False, optionflags=doctest.IGNORE_EXCEPTION_DETAIL, encoding='utf-8', raise_on_error=True, ) sys.stdout = before content = dummy_stream.content if content: sys.stderr.write(content + '\n') self.fail()
class MultipartTest(TestCase): """ Test class for multipart parsing/splitting """ example_multipart = b( dedent("""\ MIME-Version: 1.0\r Content-Type: multipart/mixed; boundary=frontier\r \r This is a message with multiple parts in MIME format.\r --frontier Content-Type: text/plain\r \r This is the body of the message.\r --frontier Content-Type: application/octet-stream\r Content-Transfer-Encoding: base64\r \r PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUgYm9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\r --frontier-- """)) def test_multipart_iteration(self): parsed = [(i[0], i[1].tobytes() if isinstance(i[1], memoryview) else i[1]) for i in mp.iterate(self.example_multipart)] self.assertEqual([ ({ b"MIME-Version": b"1.0", b"Content-Type": b"multipart/mixed; boundary=frontier" }, b""), ({ b"Content-Type": b"text/plain" }, b"This is the body of the message."), ({ b"Content-Type": b"application/octet-stream", b"Content-Transfer-Encoding": b"base64" }, b"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUgYm9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==" ) ], parsed)
def add_document(self, **params): # Build a fake file fake_file = ContentFile(b("A boring example document")) fake_file.name = 'test.txt' # Submit post_data = { 'title': "Test document", 'file': fake_file, } post_data.update(params) response = self.client.post(reverse('wagtaildocs:add'), post_data) # User should be redirected back to the index self.assertRedirects(response, reverse('wagtaildocs:index')) # Document should be created doc = models.Document.objects.filter(title=post_data['title']) self.assertTrue(doc.exists()) return doc.first()
def test_basic(self): out, err = self.init_ca() self.assertEqual(out, '') self.assertEqual(err, '') ca = CertificateAuthority.objects.first() self.assertEqual(ca.x509.get_signature_algorithm(), six.b('sha512WithRSAEncryption')) self.assertSubject( ca.x509, { 'C': 'AT', 'ST': 'Vienna', 'L': 'Vienna', 'O': 'Org', 'OU': 'OrgUnit', 'CN': 'Test CA' }) self.assertIssuer(ca, ca) self.assertAuthorityKeyIdentifier(ca, ca)
def get_headers(result_item): """ Yields content headers, if they are set in the result item. """ if hasattr(result_item, 'headers'): for header in result_item.headers: yield header yield b"Content-Type", result_item.content_type or b"application/octet-stream" if result_item.identifier: yield b"Content-Id", result_item.identifier.encode('utf-8') if isinstance(result_item.filename, str): result_item.filename = b(result_item.filename) if result_item.filename: yield ( b"Content-Disposition", b'attachment; filename="%s"' % result_item.filename ) try: yield b"Content-Length", b"%d" % len(result_item) except (AttributeError, NotImplementedError): pass
def test_download_local_release(rf, isolated, repository, settings): settings.LOCALSHOP_ISOLATED = isolated release_file = ReleaseFileFactory(release__package__repository=repository) url_kwargs = { 'repo': repository.slug, 'name': release_file.release.package.name, 'pk': release_file.pk, 'filename': release_file.filename } request = rf.get(reverse('packages:download', kwargs=url_kwargs)) response = views.DownloadReleaseFile.as_view()(request, **url_kwargs) # Localshop must return the release file assert response.status_code == 200 assert response.content == six.b('the file data') assert response.get('Content-Length') == '13' assert response.get( 'Content-Disposition') == 'attachment; filename=test-1.0.0-sdist.zip'
def render_component(path_to_source, serialized_props=None, to_static_markup=None): if not path_to_source or not os.path.exists(path_to_source): raise SourceFileNotFound(path_to_source) if to_static_markup is None: render_to = 'string' else: render_to = 'static' if serialized_props: with tempfile.NamedTemporaryFile() as serialized_props_file: serialized_props_file.write(six.b(serialized_props)) serialized_props_file.flush() path_to_serialized_props = serialized_props_file.name return renderer.render(path_to_source, render_to, path_to_serialized_props) return renderer.render(path_to_source, render_to)
def test_download_file_missing_content_length(requests_mock, settings, tmpdir): settings.MEDIA_ROOT = tmpdir file_data = six.b('My cool package') release_file = ReleaseFileFactory(distribution=None, md5_digest=md5(file_data).hexdigest()) requests_mock.return_value = mock.Mock(**{ 'headers': { 'content-type': 'application/octet-stream', }, 'content': file_data, }) tasks.download_file.run(release_file.pk) release_file = models.ReleaseFile.objects.get(pk=release_file.pk) assert release_file.distribution.read() == file_data assert release_file.distribution.size == len(file_data) assert release_file.distribution.name == ( 'default/2.7/t/test-package/test-1.0.0-sdist.zip')