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

5
from django.utils import timezone
Eliot Berriot's avatar
Eliot Berriot committed
6
from django.conf import settings
7

8
9
from funkwhale_api.factories import registry

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


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


@registry.register(name='federation.SignatureAuth')
class SignatureAuthFactory(factory.Factory):
    algorithm = 'rsa-sha256'
20
    key = factory.LazyFunction(lambda: keys.get_key_pair()[0])
21
    key_id = factory.Faker('url')
22
    use_auth_header = False
23
24
25
26
27
28
    headers = [
        '(request-target)',
        'user-agent',
        'host',
        'date',
        'content-type',]
29
30
31
32
33
34
35
36
37
38
39
40
    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
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    @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
55
56
57
58
59
60
@registry.register
class ActorFactory(factory.DjangoModelFactory):
    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

    class Meta:
        model = models.Actor

Eliot Berriot's avatar
Eliot Berriot committed
69
70
71
72
73
74
    class Params:
        local = factory.Trait(
            domain=factory.LazyAttribute(
                lambda o: settings.FEDERATION_HOSTNAME)
        )

Eliot Berriot's avatar
Eliot Berriot committed
75
76
    @classmethod
    def _generate(cls, create, attrs):
77
78
        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
79
        if not has_public and not has_private:
80
81
82
            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
83
        return super()._generate(create, attrs)
84
85


Eliot Berriot's avatar
Eliot Berriot committed
86
87
88
89
90
91
92
93
@registry.register
class FollowFactory(factory.DjangoModelFactory):
    target = factory.SubFactory(ActorFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = models.Follow

Eliot Berriot's avatar
Eliot Berriot committed
94
95
96
97
98
    class Params:
        local = factory.Trait(
            actor=factory.SubFactory(ActorFactory, local=True)
        )

Eliot Berriot's avatar
Eliot Berriot committed
99

100
101
102
103
104
105
106
107
108
@registry.register
class FollowRequestFactory(factory.DjangoModelFactory):
    target = factory.SubFactory(ActorFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = models.FollowRequest


109
110
111
112
113
114
115
116
117
118
119
120
@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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137


@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