2020-05-13 10:18:48 +00:00
|
|
|
from urllib.parse import urlsplit
|
|
|
|
|
|
|
|
from django.test import TestCase, Client
|
|
|
|
from django.utils.http import http_date
|
|
|
|
|
|
|
|
from fedireads.models import User
|
|
|
|
from fedireads.broadcast import make_signature
|
|
|
|
from fedireads.activitypub import get_follow_request
|
|
|
|
from fedireads.settings import DOMAIN
|
|
|
|
|
|
|
|
class Signature(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.mouse = User.objects.create_user('mouse', 'mouse@example.com', '')
|
|
|
|
self.rat = User.objects.create_user('rat', 'rat@example.com', '')
|
|
|
|
self.cat = User.objects.create_user('cat', 'cat@example.com', '')
|
|
|
|
|
2020-05-13 10:40:57 +00:00
|
|
|
def send_follow(self, signature, now):
|
2020-05-13 10:18:48 +00:00
|
|
|
c = Client()
|
2020-05-13 10:40:57 +00:00
|
|
|
return c.post(
|
2020-05-13 10:18:48 +00:00
|
|
|
urlsplit(self.rat.inbox).path,
|
2020-05-13 10:40:57 +00:00
|
|
|
data=get_follow_request(
|
|
|
|
self.mouse,
|
|
|
|
self.rat,
|
|
|
|
),
|
2020-05-13 10:18:48 +00:00
|
|
|
content_type='application/json',
|
|
|
|
**{
|
|
|
|
'HTTP_DATE': now,
|
|
|
|
'HTTP_SIGNATURE': signature,
|
|
|
|
'HTTP_CONTENT_TYPE': 'application/activity+json; charset=utf-8',
|
|
|
|
'HTTP_HOST': DOMAIN,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-05-13 10:40:57 +00:00
|
|
|
def test_correct_signature(self):
|
|
|
|
now = http_date()
|
|
|
|
signature = make_signature(self.mouse, self.rat.inbox, now)
|
|
|
|
return self.send_follow(signature, now).status_code == 200
|
|
|
|
|
|
|
|
def test_wrong_signature(self):
|
|
|
|
''' Messages must be signed by the right actor.
|
|
|
|
(cat cannot sign messages on behalf of mouse)
|
|
|
|
'''
|
|
|
|
now = http_date()
|
|
|
|
signature = make_signature(self.cat, self.rat.inbox, now)
|
|
|
|
assert self.send_follow(signature, now).status_code == 401
|