From 77c6bd583985eb61e5b7ba3e37e85a28a73a8d2d Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Mon, 2 Apr 2018 19:15:27 +0200
Subject: [PATCH] Fixed failing test

---
 api/funkwhale_api/federation/actors.py    | 21 +++------------
 api/funkwhale_api/federation/factories.py |  8 +++---
 api/funkwhale_api/federation/models.py    | 15 +++++++++++
 api/funkwhale_api/federation/webfinger.py |  2 +-
 api/tests/federation/test_actors.py       | 32 +++++++++++++++++++----
 5 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/api/funkwhale_api/federation/actors.py b/api/funkwhale_api/federation/actors.py
index 7a9b47d1..a6220ed1 100644
--- a/api/funkwhale_api/federation/actors.py
+++ b/api/funkwhale_api/federation/actors.py
@@ -175,29 +175,14 @@ class TestActor(SystemActor):
         reply_url = 'https://{}/activities/note/{}'.format(
             settings.FEDERATION_HOSTNAME, now.timestamp()
         )
-        mention = '@{}@{}'.format(
-            sender.preferred_username,
-            sender.domain
-        )
         reply_content = '{} Pong!'.format(
-            mention
+            sender.mention_username
         )
         reply_activity = {
             "@context": [
         		"https://www.w3.org/ns/activitystreams",
         		"https://w3id.org/security/v1",
-        		{
-        			"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
-        			"sensitive": "as:sensitive",
-        			"movedTo": "as:movedTo",
-        			"Hashtag": "as:Hashtag",
-        			"ostatus": "http://ostatus.org#",
-        			"atomUri": "ostatus:atomUri",
-        			"inReplyToAtomUri": "ostatus:inReplyToAtomUri",
-        			"conversation": "ostatus:conversation",
-        			"toot": "http://joinmastodon.org/ns#",
-        			"Emoji": "toot:Emoji"
-        		}
+        		{}
         	],
             'type': 'Create',
             'actor': test_actor.url,
@@ -221,7 +206,7 @@ class TestActor(SystemActor):
                     {
                         "type": "Mention",
                         "href": ac['actor'],
-                        "name": mention
+                        "name": sender.mention_username
                     }
                 ]
             )
diff --git a/api/funkwhale_api/federation/factories.py b/api/funkwhale_api/federation/factories.py
index ebd6b2fd..88c86f79 100644
--- a/api/funkwhale_api/federation/factories.py
+++ b/api/funkwhale_api/federation/factories.py
@@ -53,13 +53,15 @@ class SignedRequestFactory(factory.Factory):
 
 @registry.register
 class ActorFactory(factory.DjangoModelFactory):
-    url = factory.Faker('url')
-    inbox_url = factory.Faker('url')
-    outbox_url = factory.Faker('url')
+
     public_key = None
     private_key = None
     preferred_username = factory.Faker('user_name')
     summary = factory.Faker('paragraph')
+    domain = factory.Faker('domain_name')
+    url = factory.LazyAttribute(lambda o: 'https://{}/users/{}'.format(o.domain, o.preferred_username))
+    inbox_url = factory.LazyAttribute(lambda o: 'https://{}/users/{}/inbox'.format(o.domain, o.preferred_username))
+    outbox_url = factory.LazyAttribute(lambda o: 'https://{}/users/{}/outbox'.format(o.domain, o.preferred_username))
 
     class Meta:
         model = models.Actor
diff --git a/api/funkwhale_api/federation/models.py b/api/funkwhale_api/federation/models.py
index fa38678e..d76ad173 100644
--- a/api/funkwhale_api/federation/models.py
+++ b/api/funkwhale_api/federation/models.py
@@ -42,3 +42,18 @@ class Actor(models.Model):
     @property
     def private_key_id(self):
         return '{}#main-key'.format(self.url)
+
+    @property
+    def mention_username(self):
+        return '@{}@{}'.format(self.preferred_username, self.domain)
+
+    def save(self, **kwargs):
+        lowercase_fields = [
+            'domain',
+        ]
+        for field in lowercase_fields:
+            v = getattr(self, field, None)
+            if v:
+                setattr(self, field, v.lower())
+
+        super().save(**kwargs)
diff --git a/api/funkwhale_api/federation/webfinger.py b/api/funkwhale_api/federation/webfinger.py
index 95a51e1c..4e975338 100644
--- a/api/funkwhale_api/federation/webfinger.py
+++ b/api/funkwhale_api/federation/webfinger.py
@@ -29,7 +29,7 @@ def clean_acct(acct_string):
     except ValueError:
         raise forms.ValidationError('Invalid format')
 
-    if hostname != settings.FEDERATION_HOSTNAME:
+    if hostname.lower() != settings.FEDERATION_HOSTNAME:
         raise forms.ValidationError(
             'Invalid hostname {}'.format(hostname))
 
diff --git a/api/tests/federation/test_actors.py b/api/tests/federation/test_actors.py
index 88a94e56..b3b0f8df 100644
--- a/api/tests/federation/test_actors.py
+++ b/api/tests/federation/test_actors.py
@@ -126,7 +126,8 @@ def test_test_post_outbox_validates_actor(nodb_factories):
         assert msg in exc_info.value
 
 
-def test_test_post_outbox_handles_create_note(mocker, factories):
+def test_test_post_outbox_handles_create_note(
+        settings, mocker, factories):
     deliver = mocker.patch(
         'funkwhale_api.federation.activity.deliver')
     actor = factories['federation.Actor']()
@@ -142,6 +143,7 @@ def test_test_post_outbox_handles_create_note(mocker, factories):
             'content': '<p><a>@mention</a> /ping</p>'
         }
     }
+    test_actor = actors.SYSTEM_ACTORS['test'].get_actor_instance()
     expected_note = factories['federation.Note'](
         id='https://test.federation/activities/note/{}'.format(
             now.timestamp()
@@ -149,16 +151,36 @@ def test_test_post_outbox_handles_create_note(mocker, factories):
         content='Pong!',
         published=now.isoformat(),
         inReplyTo=data['object']['id'],
+        cc=[],
+        summary=None,
+        sensitive=False,
+        attributedTo=test_actor.url,
+        attachment=[],
+        to=[actor.url],
+        url='https://{}/activities/note/{}'.format(
+            settings.FEDERATION_HOSTNAME, now.timestamp()
+        ),
+        tag=[{
+            'href': actor.url,
+            'name': actor.mention_username,
+            'type': 'Mention',
+        }]
     )
-    test_actor = actors.SYSTEM_ACTORS['test'].get_actor_instance()
     expected_activity = {
+        '@context': [
+            'https://www.w3.org/ns/activitystreams',
+            'https://w3id.org/security/v1',
+            {}
+        ],
         'actor': test_actor.url,
-        'id': 'https://test.federation/activities/note/{}/activity'.format(
-            now.timestamp()
+        'id': 'https://{}/activities/note/{}/activity'.format(
+            settings.FEDERATION_HOSTNAME, now.timestamp()
         ),
+        'to': actor.url,
         'type': 'Create',
         'published': now.isoformat(),
-        'object': expected_note
+        'object': expected_note,
+        'cc': [],
     }
     actors.SYSTEM_ACTORS['test'].post_inbox(data, actor=actor)
     deliver.assert_called_once_with(
-- 
GitLab