factories.py 3.1 KB
Newer Older
1
2
3
4
import factory
import requests
import requests_http_signature

5
6
from django.utils import timezone

7
8
from funkwhale_api.factories import registry

9
from . import keys
Eliot Berriot's avatar
Eliot Berriot committed
10
from . import models
11
12


13
registry.register(keys.get_key_pair, name='federation.KeyPair')
14
15
16
17
18


@registry.register(name='federation.SignatureAuth')
class SignatureAuthFactory(factory.Factory):
    algorithm = 'rsa-sha256'
19
    key = factory.LazyFunction(lambda: keys.get_key_pair()[0])
20
    key_id = factory.Faker('url')
21
    use_auth_header = False
22
23
24
25
26
27
    headers = [
        '(request-target)',
        'user-agent',
        'host',
        'date',
        'content-type',]
28
29
30
31
32
33
34
35
36
37
38
39
    class Meta:
        model = requests_http_signature.HTTPSignatureAuth


@registry.register(name='federation.SignedRequest')
class SignedRequestFactory(factory.Factory):
    url = factory.Faker('url')
    method = 'get'
    auth = factory.SubFactory(SignatureAuthFactory)

    class Meta:
        model = requests.Request
40
41
42
43
44
45
46
47
48
49
50
51
52
53

    @factory.post_generation
    def headers(self, create, extracted, **kwargs):
        default_headers = {
            'User-Agent': 'Test',
            'Host': 'test.host',
            'Date': 'Right now',
            'Content-Type': 'application/activity+json'
        }
        if extracted:
            default_headers.update(extracted)
        self.headers.update(default_headers)


Eliot Berriot's avatar
Eliot Berriot committed
54
55
@registry.register
class ActorFactory(factory.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
56

Eliot Berriot's avatar
Eliot Berriot committed
57
58
59
60
    public_key = None
    private_key = None
    preferred_username = factory.Faker('user_name')
    summary = factory.Faker('paragraph')
Eliot Berriot's avatar
Eliot Berriot committed
61
62
63
64
    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))
Eliot Berriot's avatar
Eliot Berriot committed
65
66
67
68
69
70

    class Meta:
        model = models.Actor

    @classmethod
    def _generate(cls, create, attrs):
71
72
        has_public = attrs.get('public_key') is not None
        has_private = attrs.get('private_key') is not None
Eliot Berriot's avatar
Eliot Berriot committed
73
        if not has_public and not has_private:
74
75
76
            private, public = keys.get_key_pair()
            attrs['private_key'] = private.decode('utf-8')
            attrs['public_key'] = public.decode('utf-8')
Eliot Berriot's avatar
Eliot Berriot committed
77
        return super()._generate(create, attrs)
78
79
80
81
82
83
84
85
86
87
88
89
90
91


@registry.register(name='federation.Note')
class NoteFactory(factory.Factory):
    type = 'Note'
    id = factory.Faker('url')
    published = factory.LazyFunction(
        lambda: timezone.now().isoformat()
    )
    inReplyTo = None
    content = factory.Faker('sentence')

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108


@registry.register(name='federation.Activity')
class ActivityFactory(factory.Factory):
    type = 'Create'
    id = factory.Faker('url')
    published = factory.LazyFunction(
        lambda: timezone.now().isoformat()
    )
    actor = factory.Faker('url')
    object = factory.SubFactory(
        NoteFactory,
        actor=factory.SelfAttribute('..actor'),
        published=factory.SelfAttribute('..published'))

    class Meta:
        model = dict