~sirn/fanboi2

e17ba6e00c95278af3bb8eb9e90eb57f78d0524b — Sirn Thanabulpong 4 months ago 3471636
Fix topic creation with IPv6

Closes #15
2 files changed, 35 insertions(+), 1 deletions(-)

M fanboi2/services/topic.py
M fanboi2/tests/test_services_topic.py
M fanboi2/services/topic.py => fanboi2/services/topic.py +13 -1
@@ 1,4 1,5 @@
import datetime
import ipaddress

from sqlalchemy.orm import contains_eager, joinedload
from sqlalchemy.sql import or_, and_, func, desc


@@ 83,10 84,21 @@ class TopicCreateService(object):
        ident_type = "none"
        if board.settings["use_ident"]:
            ident_type = "ident"
            ident_addr = ip_address

            # Since it's common for IPv6 setup to delegate a /64 from ISP to a home
            # network, it makes more sense here to always generate ident based on
            # /64 network instead of individual address.
            if ipaddress.ip_address(ip_address).version == 6:
                ident_type = "ident_v6"
                ident_addr = str(
                    ipaddress.ip_network("%s/64" % (ip_address,), strict=False)
                )

            ident = self.identity_svc.identity_with_tz_for(
                self.setting_query_svc.value_from_key("app.time_zone"),
                board=topic.board.slug,
                ip_address=ip_address,
                ip_address=ident_addr,
            )

        post = Post(

M fanboi2/tests/test_services_topic.py => fanboi2/tests/test_services_topic.py +22 -0
@@ 52,6 52,28 @@ class TestTopicCreateService(ModelSessionMixin, unittest.TestCase):
        self.assertEqual(topic.posts[0].ident, "foo,127.0.0.1")
        self.assertEqual(topic.posts[0].ident_type, "ident")

    def test_create_ipv6(self):
        from ..models import Board

        board = self._make(
            Board(slug="foo", title="Foo", settings={"name": "Nameless Foobar"})
        )
        self.dbsession.commit()
        topic_create_svc = self._make_one()
        topic = topic_create_svc.create(
            board.slug, "Hello, world!", "Hello Eartians", "fe80:c9cd::1"
        )
        self.assertEqual(topic.board, board)
        self.assertEqual(topic.title, "Hello, world!")
        self.assertEqual(topic.meta.post_count, 1)
        self.assertIsNotNone(topic.meta.bumped_at)
        self.assertEqual(topic.posts[0].number, 1)
        self.assertEqual(topic.posts[0].bumped, True)
        self.assertEqual(topic.posts[0].name, "Nameless Foobar")
        self.assertEqual(topic.posts[0].ip_address, "fe80:c9cd::1")
        self.assertEqual(topic.posts[0].ident, "foo,fe80:c9cd::/64")
        self.assertEqual(topic.posts[0].ident_type, "ident_v6")

    def test_create_without_ident(self):
        from ..models import Board