factories.py 5.03 KB
Newer Older
1
2
3
import factory
import requests
import requests_http_signature
Eliot Berriot's avatar
Eliot Berriot committed
4
import uuid
5

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

9
10
from funkwhale_api.factories import registry

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


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


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

    @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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@registry.register(name='federation.Link')
class LinkFactory(factory.Factory):
    type = 'Link'
    href = factory.Faker('url')
    mediaType = 'text/html'

    class Meta:
        model = dict

    class Params:
        audio = factory.Trait(
            mediaType=factory.Iterator(['audio/mp3', 'audio/ogg'])
        )


Eliot Berriot's avatar
Eliot Berriot committed
71
72
73
74
75
76
@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
77
78
79
80
    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
81
82
83
84

    class Meta:
        model = models.Actor

Eliot Berriot's avatar
Eliot Berriot committed
85
86
87
88
89
90
    class Params:
        local = factory.Trait(
            domain=factory.LazyAttribute(
                lambda o: settings.FEDERATION_HOSTNAME)
        )

Eliot Berriot's avatar
Eliot Berriot committed
91
92
    @classmethod
    def _generate(cls, create, attrs):
93
94
        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
95
        if not has_public and not has_private:
96
97
98
            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
99
        return super()._generate(create, attrs)
100
101


Eliot Berriot's avatar
Eliot Berriot committed
102
103
104
105
106
107
108
109
@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
110
111
112
113
114
    class Params:
        local = factory.Trait(
            actor=factory.SubFactory(ActorFactory, local=True)
        )

Eliot Berriot's avatar
Eliot Berriot committed
115

116
117
118
119
120
121
122
123
124
@registry.register
class FollowRequestFactory(factory.DjangoModelFactory):
    target = factory.SubFactory(ActorFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = models.FollowRequest


125
126
127
128
129
130
131
132
133
134
135
136
@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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153


@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
Eliot Berriot's avatar
Eliot Berriot committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184


@registry.register(name='federation.AudioMetadata')
class AudioMetadataFactory(factory.Factory):
    recording = factory.LazyAttribute(
        lambda o: 'https://musicbrainz.org/recording/{}'.format(uuid.uuid4())
    )
    artist = factory.LazyAttribute(
        lambda o: 'https://musicbrainz.org/artist/{}'.format(uuid.uuid4())
    )
    release = factory.LazyAttribute(
        lambda o: 'https://musicbrainz.org/release/{}'.format(uuid.uuid4())
    )

    class Meta:
        model = dict


@registry.register(name='federation.Audio')
class AudioFactory(factory.Factory):
    type = 'Audio'
    id = factory.Faker('url')
    published = factory.LazyFunction(
        lambda: timezone.now().isoformat()
    )
    actor = factory.Faker('url')
    url = factory.SubFactory(LinkFactory, audio=True)
    metadata = factory.SubFactory(AudioMetadataFactory)

    class Meta:
        model = dict