From 74fec7eeb4e7e2e593ed5e2213020024264681ce Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Tue, 28 Jan 2014 14:30:46 +0000 Subject: Import force_bytes on django >= 1.5 --- rest_framework/tests/test_compat.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 rest_framework/tests/test_compat.py (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_compat.py b/rest_framework/tests/test_compat.py new file mode 100644 index 00000000..4916d19b --- /dev/null +++ b/rest_framework/tests/test_compat.py @@ -0,0 +1,13 @@ +import django +from django.test import TestCase + + +class TestCompat(TestCase): + def test_force_bytes_or_smart_bytes(self): + from rest_framework.compat import force_bytes_or_smart_bytes + if django.VERSION >= (1, 5): + from django.utils.encoding import force_bytes + self.assertEqual(force_bytes_or_smart_bytes, force_bytes) + else: + from django.utils.encoding import smart_str + self.assertEqual(force_bytes_or_smart_bytes, smart_str) -- cgit v1.2.3 From 78e4468f0367cc2a3a5cc6f3570a791ad67c90d9 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Tue, 28 Jan 2014 15:54:50 +0000 Subject: Add file upload test for APIRequestFactory Remove test_compat --- rest_framework/tests/test_compat.py | 13 ------------- rest_framework/tests/test_testing.py | 9 +++++++++ 2 files changed, 9 insertions(+), 13 deletions(-) delete mode 100644 rest_framework/tests/test_compat.py (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_compat.py b/rest_framework/tests/test_compat.py deleted file mode 100644 index 4916d19b..00000000 --- a/rest_framework/tests/test_compat.py +++ /dev/null @@ -1,13 +0,0 @@ -import django -from django.test import TestCase - - -class TestCompat(TestCase): - def test_force_bytes_or_smart_bytes(self): - from rest_framework.compat import force_bytes_or_smart_bytes - if django.VERSION >= (1, 5): - from django.utils.encoding import force_bytes - self.assertEqual(force_bytes_or_smart_bytes, force_bytes) - else: - from django.utils.encoding import smart_str - self.assertEqual(force_bytes_or_smart_bytes, smart_str) diff --git a/rest_framework/tests/test_testing.py b/rest_framework/tests/test_testing.py index 48b8956b..71bd8b55 100644 --- a/rest_framework/tests/test_testing.py +++ b/rest_framework/tests/test_testing.py @@ -1,6 +1,8 @@ # -- coding: utf-8 -- from __future__ import unicode_literals +from io import BytesIO + from django.contrib.auth.models import User from django.test import TestCase from rest_framework.compat import patterns, url @@ -143,3 +145,10 @@ class TestAPIRequestFactory(TestCase): force_authenticate(request, user=user) response = view(request) self.assertEqual(response.data['user'], 'example') + + def test_upload_file(self): + # This is a 1x1 black png + simple_png = BytesIO(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc````\x00\x00\x00\x05\x00\x01\xa5\xf6E@\x00\x00\x00\x00IEND\xaeB`\x82') + simple_png.name = 'test.png' + factory = APIRequestFactory() + factory.post('/', data={'image': simple_png}) -- cgit v1.2.3 From 35f4908e48cc18e94be239f8065c95e87b2fb007 Mon Sep 17 00:00:00 2001 From: Artem Mezhenin Date: Sun, 9 Feb 2014 02:46:25 +0400 Subject: issue #1386 * regex for matching URLs was rewritten * added unittests --- rest_framework/tests/test_templatetags.py | 38 ++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_templatetags.py b/rest_framework/tests/test_templatetags.py index 609a9e08..0c2259b9 100644 --- a/rest_framework/tests/test_templatetags.py +++ b/rest_framework/tests/test_templatetags.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.test import TestCase from rest_framework.test import APIRequestFactory -from rest_framework.templatetags.rest_framework import add_query_param +from rest_framework.templatetags.rest_framework import add_query_param, urlize_quoted_links factory = APIRequestFactory() @@ -17,3 +17,39 @@ class TemplateTagTests(TestCase): json_url = add_query_param(request, "format", "json") self.assertIn("q=%E6%9F%A5%E8%AF%A2", json_url) self.assertIn("format=json", json_url) + + +class Issue1386Tests(TestCase): + """ + Covers #1386 + """ + + def test_issue_1386(self): + """ + Test function urlize_quoted_links with different args + """ + correct_urls = [ + "asdf.com/zxvc", + "asdf.net", + "www.as_df.org", + "as.d8f.ghj8.gov", + "www.a-op.s.d.edu/asdf/dfff_908/", + "cd8fr.com:80/hello", + "cdfr.com:808/hello", + "cdfr.com:8080/hello", + "cdfr.com:44808/hello/asdf/", + ] + for i in correct_urls: + res = urlize_quoted_links(i) + self.assertGreater(len(res), len(i)) + self.assertIn(i, res) + + incorrect_urls = [ + "mailto://asdf@fdf.com", + "asdf://asdf.com", + "asdf.netnet", + "asdf:[/p]zxcv.com" # example from issue #1386 + ] + for i in incorrect_urls: + res = urlize_quoted_links(i) + self.assertEqual(i, res) \ No newline at end of file -- cgit v1.2.3 From 95670933d7954a99e02f0e19f285d8740ab5e449 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Tue, 11 Feb 2014 14:44:56 +0100 Subject: Test and quick fix for #1257 --- rest_framework/tests/test_serializer.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_serializer.py b/rest_framework/tests/test_serializer.py index 75d6e785..6b1e333e 100644 --- a/rest_framework/tests/test_serializer.py +++ b/rest_framework/tests/test_serializer.py @@ -71,6 +71,15 @@ class ActionItemSerializer(serializers.ModelSerializer): class Meta: model = ActionItem +class ActionItemSerializerOptionalFields(serializers.ModelSerializer): + """ + Intended to test that fields with `required=False` are excluded from validation. + """ + title = serializers.CharField(required=False) + + class Meta: + model = ActionItem + fields = ('title',) class ActionItemSerializerCustomRestore(serializers.ModelSerializer): @@ -288,7 +297,13 @@ class BasicTests(TestCase): serializer.save() self.assertIsNotNone(serializer.data.get('id',None), 'Model is saved. `id` should be set.') - + def test_fields_marked_as_not_required_are_excluded_from_validation(self): + """ + Check that fields with `required=False` are included in list of exclusions. + """ + serializer = ActionItemSerializerOptionalFields(self.actionitem) + exclusions = serializer.get_validation_exclusions() + self.assertTrue('title' in exclusions, '`title` field was marked `required=False` and should be excluded') class DictStyleSerializer(serializers.Serializer): @@ -1808,14 +1823,14 @@ class SerializerDefaultTrueBoolean(TestCase): self.assertEqual(serializer.data['cat'], False) self.assertEqual(serializer.data['dog'], False) - + class BoolenFieldTypeTest(TestCase): ''' Ensure the various Boolean based model fields are rendered as the proper field type - + ''' - + def setUp(self): ''' Setup an ActionItemSerializer for BooleanTesting @@ -1831,11 +1846,11 @@ class BoolenFieldTypeTest(TestCase): ''' bfield = self.serializer.get_fields()['done'] self.assertEqual(type(bfield), fields.BooleanField) - + def test_nullbooleanfield_type(self): ''' - Test that BooleanField is infered from models.NullBooleanField - + Test that BooleanField is infered from models.NullBooleanField + https://groups.google.com/forum/#!topic/django-rest-framework/D9mXEftpuQ8 ''' bfield = self.serializer.get_fields()['started'] -- cgit v1.2.3 From f1016441f5b9f6c95530d2ec306f90aa45762831 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Tue, 11 Feb 2014 19:52:32 +0100 Subject: Test and fix for #1210. World's lowest hanging fruit. --- rest_framework/tests/test_fields.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_fields.py b/rest_framework/tests/test_fields.py index 5c96bce9..e127feef 100644 --- a/rest_framework/tests/test_fields.py +++ b/rest_framework/tests/test_fields.py @@ -860,7 +860,9 @@ class SlugFieldTests(TestCase): class URLFieldTests(TestCase): """ - Tests for URLField attribute values + Tests for URLField attribute values. + + (Includes test for #1210, checking that validators can be overridden.) """ class URLFieldModel(RESTFrameworkModel): @@ -902,6 +904,11 @@ class URLFieldTests(TestCase): self.assertEqual(getattr(serializer.fields['url_field'], 'max_length'), 20) + def test_validators_can_be_overridden(self): + url_field = serializers.URLField(validators=[]) + validators = url_field.validators + self.assertEqual([], validators, 'Passing `validators` kwarg should have overridden default validators') + class FieldMetadata(TestCase): def setUp(self): -- cgit v1.2.3 From d00ea3bcac5d622c586b267d18aef4700657f269 Mon Sep 17 00:00:00 2001 From: Artem Mezhenin Date: Thu, 13 Feb 2014 18:59:05 +0400 Subject: change regex back, issue #1386 --- rest_framework/tests/test_templatetags.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_templatetags.py b/rest_framework/tests/test_templatetags.py index 0c2259b9..999c718a 100644 --- a/rest_framework/tests/test_templatetags.py +++ b/rest_framework/tests/test_templatetags.py @@ -29,26 +29,20 @@ class Issue1386Tests(TestCase): Test function urlize_quoted_links with different args """ correct_urls = [ - "asdf.com/zxvc", + "asdf.com", "asdf.net", "www.as_df.org", "as.d8f.ghj8.gov", - "www.a-op.s.d.edu/asdf/dfff_908/", - "cd8fr.com:80/hello", - "cdfr.com:808/hello", - "cdfr.com:8080/hello", - "cdfr.com:44808/hello/asdf/", ] for i in correct_urls: res = urlize_quoted_links(i) - self.assertGreater(len(res), len(i)) + self.assertNotEqual(res, i) self.assertIn(i, res) incorrect_urls = [ "mailto://asdf@fdf.com", - "asdf://asdf.com", "asdf.netnet", - "asdf:[/p]zxcv.com" # example from issue #1386 + "asdf:[/p]zxcv.com", # example from issue #1386 ] for i in incorrect_urls: res = urlize_quoted_links(i) -- cgit v1.2.3 From 08ec23268dbb4a40000b6c4bf877f5563a4ba57b Mon Sep 17 00:00:00 2001 From: Artem Mezhenin Date: Thu, 13 Feb 2014 19:39:53 +0400 Subject: (I hope) tests are fixed, issue #1386 --- rest_framework/tests/test_templatetags.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'rest_framework/tests') diff --git a/rest_framework/tests/test_templatetags.py b/rest_framework/tests/test_templatetags.py index 999c718a..d4da0c23 100644 --- a/rest_framework/tests/test_templatetags.py +++ b/rest_framework/tests/test_templatetags.py @@ -42,8 +42,10 @@ class Issue1386Tests(TestCase): incorrect_urls = [ "mailto://asdf@fdf.com", "asdf.netnet", - "asdf:[/p]zxcv.com", # example from issue #1386 ] for i in incorrect_urls: res = urlize_quoted_links(i) - self.assertEqual(i, res) \ No newline at end of file + self.assertEqual(i, res) + + # example from issue #1386, this shouldn't raise an exception + _ = urlize_quoted_links("asdf:[/p]zxcv.com") -- cgit v1.2.3