Ticket #16302: notes-update-comments-ip-address-with-tests.patch

File notes-update-comments-ip-address-with-tests.patch, 9.2 KB (added by Dan McGee, 12 years ago)

added tests with ipv6 address persistence

  • docs/releases/1.4.txt

     
    341341a :class:`~django.db.models.fields.GenericIPAddressField` model field,
    342342a :class:`~django.forms.fields.GenericIPAddressField` form field and
    343343the validators :data:`~django.core.validators.validate_ipv46_address` and
    344 :data:`~django.core.validators.validate_ipv6_address`
     344:data:`~django.core.validators.validate_ipv6_address`.
    345345
     346The :class:`~django.contrib.comments.models.Comment` model formerly used a
     347:class:`~django.db.models.fields.IPAddressField` to store the ip_address of
     348comment submitters. This has been updated to use the new IPv4 and IPv6 capable
     349``GenericIPAddressField``. Unfortunately, the old type would silently truncate
     350IPv6 addresses longer than 15 characters for users of databases not having a
     351native IP address type (this does not apply to PostgreSQL). It is recommended
     352that users of the comments application resize the
     353``django_comments.ip_address`` column in affected databases to 39 characters.
     354
    346355Updated default project layout and ``manage.py``
    347356~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    348357
  • tests/modeltests/validation/models.py

     
    8888    generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True)
    8989    v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4")
    9090    v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6")
     91    ip_verbose_name = models.GenericIPAddressField("IP Address Verbose",
     92                        blank=True, null=True)
    9193
    9294class GenericIPAddrUnpackUniqueTest(models.Model):
    9395    generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)
     
    102104        auto2 = models.AutoField(primary_key=True)
    103105except AssertionError, assertion_error:
    104106    pass # Fail silently
    105 assert str(assertion_error) == u"A model can't have more than one AutoField."
    106  No newline at end of file
     107assert str(assertion_error) == u"A model can't have more than one AutoField."
  • django/db/models/fields/__init__.py

     
    10101010    description = _("IP address")
    10111011    default_error_messages = {}
    10121012
    1013     def __init__(self, protocol='both', unpack_ipv4=False, *args, **kwargs):
     1013    def __init__(self, verbose_name=None, name=None, protocol='both',
     1014                 unpack_ipv4=False, **kwargs):
    10141015        self.unpack_ipv4 = unpack_ipv4
    10151016        self.default_validators, invalid_error_message = \
    10161017            validators.ip_address_validators(protocol, unpack_ipv4)
    10171018        self.default_error_messages['invalid'] = invalid_error_message
    10181019        kwargs['max_length'] = 39
    1019         Field.__init__(self, *args, **kwargs)
     1020        Field.__init__(self, verbose_name, name, **kwargs)
    10201021
    10211022    def get_internal_type(self):
    10221023        return "GenericIPAddressField"
  • django/contrib/comments/models.py

     
    5757
    5858    # Metadata about the comment
    5959    submit_date = models.DateTimeField(_('date/time submitted'), default=None)
    60     ip_address  = models.IPAddressField(_('IP address'), blank=True, null=True)
     60    ip_address  = models.GenericIPAddressField(_('IP address'),
     61                    unpack_ipv4=True, blank=True, null=True)
    6162    is_public   = models.BooleanField(_('is public'), default=True,
    6263                    help_text=_('Uncheck this box to make the comment effectively ' \
    6364                                'disappear from the site.'))
  • tests/modeltests/validation/models.py

     
    8888    generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True)
    8989    v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4")
    9090    v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6")
     91    ip_verbose_name = models.GenericIPAddressField("IP Address Verbose",
     92                        blank=True, null=True)
    9193
    9294class GenericIPAddrUnpackUniqueTest(models.Model):
    9395    generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)
     
    102104        auto2 = models.AutoField(primary_key=True)
    103105except AssertionError, assertion_error:
    104106    pass # Fail silently
    105 assert str(assertion_error) == u"A model can't have more than one AutoField."
    106  No newline at end of file
     107assert str(assertion_error) == u"A model can't have more than one AutoField."
  • tests/regressiontests/comment_tests/tests/comment_view_tests.py

     
    101101        settings.DEBUG = olddebug
    102102
    103103    def testCreateValidComment(self):
     104        address = "1.2.3.4"
    104105        a = Article.objects.get(pk=1)
    105106        data = self.getValidData(a)
    106         self.response = self.client.post("/post/", data, REMOTE_ADDR="1.2.3.4")
     107        self.response = self.client.post("/post/", data, REMOTE_ADDR=address)
    107108        self.assertEqual(self.response.status_code, 302)
    108109        self.assertEqual(Comment.objects.count(), 1)
    109110        c = Comment.objects.all()[0]
    110         self.assertEqual(c.ip_address, "1.2.3.4")
     111        self.assertEqual(c.ip_address, address)
    111112        self.assertEqual(c.comment, "This is my comment")
    112113
     114    def testCreateValidCommentIPv6(self):
     115        address = "2a02::223:6cff:fe8a:2e8a"
     116        a = Article.objects.get(pk=1)
     117        data = self.getValidData(a)
     118        self.response = self.client.post("/post/", data, REMOTE_ADDR=address)
     119        self.assertEqual(self.response.status_code, 302)
     120        self.assertEqual(Comment.objects.count(), 1)
     121        c = Comment.objects.all()[0]
     122        self.assertEqual(c.ip_address, address)
     123        self.assertEqual(c.comment, "This is my comment")
     124
     125    def testCreateValidCommentIPv6Unpack(self):
     126        address = "::ffff:18.52.18.52"
     127        a = Article.objects.get(pk=1)
     128        data = self.getValidData(a)
     129        self.response = self.client.post("/post/", data, REMOTE_ADDR=address)
     130        self.assertEqual(self.response.status_code, 302)
     131        self.assertEqual(Comment.objects.count(), 1)
     132        c = Comment.objects.all()[0]
     133        # We trim the '::ffff:' bit off because it is an IPv4 addr
     134        self.assertEqual(c.ip_address, address[7:])
     135        self.assertEqual(c.comment, "This is my comment")
     136
    113137    def testPostAsAuthenticatedUser(self):
    114138        a = Article.objects.get(pk=1)
    115139        data = self.getValidData(a)
  • tests/regressiontests/model_fields/tests.py

     
    1111from django.utils import unittest
    1212
    1313from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post,
    14     NullBooleanModel, BooleanModel, Document, RenamedField)
     14    NullBooleanModel, BooleanModel, Document, RenamedField, VerboseNameField)
    1515
    1616# If PIL available, do these tests.
    1717if Image:
     
    6565        self.assertTrue(hasattr(instance, 'get_fieldname_display'))
    6666        self.assertFalse(hasattr(instance, 'get_modelname_display'))
    6767
     68    def test_field_verbose_name(self):
     69        m = VerboseNameField
     70        for i in range(1, 9):
     71            self.assertEqual(m._meta.get_field('field%d' % i).verbose_name,
     72                    'verbose field%d' % i)
     73
    6874class DecimalFieldTests(test.TestCase):
    6975    def test_to_python(self):
    7076        f = models.DecimalField(max_digits=4, decimal_places=2)
  • tests/regressiontests/model_fields/models.py

     
    6969class RenamedField(models.Model):
    7070    modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))
    7171
     72class VerboseNameField(models.Model):
     73    field1 = models.BooleanField("verbose field1")
     74    field2 = models.DateField("verbose field2")
     75    field3 = models.DateTimeField("verbose field3")
     76    field4 = models.TimeField("verbose field4")
     77    field5 = models.DecimalField("verbose field5", max_digits=6, decimal_places=1)
     78    field6 = models.FilePathField("verbose field6")
     79    field7 = models.GenericIPAddressField("verbose field7", protocol="ipv4")
     80    field8 = models.URLField("verbose field8")
     81
    7282# This model isn't used in any test, just here to ensure it validates successfully.
    7383# See ticket #16570.
    7484class DecimalLessThanOne(models.Model):
Back to Top