factories.py 6.66 KB
Newer Older
Eliot Berriot's avatar
Eliot Berriot committed
1
2
import uuid

3
4
5
import factory
import requests
import requests_http_signature
Eliot Berriot's avatar
Eliot Berriot committed
6
from django.conf import settings
Eliot Berriot's avatar
Eliot Berriot committed
7
from django.utils import timezone
8
from django.utils.http import http_date
9

10
11
from funkwhale_api.factories import registry

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

Eliot Berriot's avatar
Eliot Berriot committed
14
registry.register(keys.get_key_pair, name="federation.KeyPair")
15
16


Eliot Berriot's avatar
Eliot Berriot committed
17
@registry.register(name="federation.SignatureAuth")
18
class SignatureAuthFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
19
    algorithm = "rsa-sha256"
20
    key = factory.LazyFunction(lambda: keys.get_key_pair()[0])
Eliot Berriot's avatar
Eliot Berriot committed
21
    key_id = factory.Faker("url")
22
    use_auth_header = False
Eliot Berriot's avatar
Eliot Berriot committed
23
24
    headers = ["(request-target)", "user-agent", "host", "date", "content-type"]

25
26
27
28
    class Meta:
        model = requests_http_signature.HTTPSignatureAuth


Eliot Berriot's avatar
Eliot Berriot committed
29
@registry.register(name="federation.SignedRequest")
30
class SignedRequestFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
31
32
    url = factory.Faker("url")
    method = "get"
33
34
35
36
    auth = factory.SubFactory(SignatureAuthFactory)

    class Meta:
        model = requests.Request
37
38
39
40

    @factory.post_generation
    def headers(self, create, extracted, **kwargs):
        default_headers = {
Eliot Berriot's avatar
Eliot Berriot committed
41
42
            "User-Agent": "Test",
            "Host": "test.host",
43
            "Date": http_date(timezone.now().timestamp()),
Eliot Berriot's avatar
Eliot Berriot committed
44
            "Content-Type": "application/activity+json",
45
46
47
48
49
50
        }
        if extracted:
            default_headers.update(extracted)
        self.headers.update(default_headers)


Eliot Berriot's avatar
Eliot Berriot committed
51
@registry.register(name="federation.Link")
Eliot Berriot's avatar
Eliot Berriot committed
52
class LinkFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
53
54
55
    type = "Link"
    href = factory.Faker("url")
    mediaType = "text/html"
Eliot Berriot's avatar
Eliot Berriot committed
56
57
58
59
60

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
61
        audio = factory.Trait(mediaType=factory.Iterator(["audio/mp3", "audio/ogg"]))
Eliot Berriot's avatar
Eliot Berriot committed
62
63


Eliot Berriot's avatar
Eliot Berriot committed
64
65
66
67
@registry.register
class ActorFactory(factory.DjangoModelFactory):
    public_key = None
    private_key = None
Eliot Berriot's avatar
Eliot Berriot committed
68
69
70
71
72
73
74
75
76
77
78
79
    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)
    )
Eliot Berriot's avatar
Eliot Berriot committed
80
81
82
83

    class Meta:
        model = models.Actor

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

Eliot Berriot's avatar
Eliot Berriot committed
89
90
    @classmethod
    def _generate(cls, create, attrs):
Eliot Berriot's avatar
Eliot Berriot committed
91
92
        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
93
        if not has_public and not has_private:
94
            private, public = keys.get_key_pair()
Eliot Berriot's avatar
Eliot Berriot committed
95
96
            attrs["private_key"] = private.decode("utf-8")
            attrs["public_key"] = public.decode("utf-8")
Eliot Berriot's avatar
Eliot Berriot committed
97
        return super()._generate(create, attrs)
98
99


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

Eliot Berriot's avatar
Eliot Berriot committed
111

112
113
114
@registry.register
class LibraryFactory(factory.DjangoModelFactory):
    actor = factory.SubFactory(ActorFactory)
Eliot Berriot's avatar
Eliot Berriot committed
115
    url = factory.Faker("url")
116
117
    federation_enabled = True
    download_files = False
118
    autoimport = False
119
120
121
122
123

    class Meta:
        model = models.Library


124
class ArtistMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
125
    name = factory.Faker("name")
126
127
128
129
130

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
131
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
132
133
134


class ReleaseMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
135
    title = factory.Faker("sentence")
136
137
138
139
140

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
141
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
142
143
144


class RecordingMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
145
    title = factory.Faker("sentence")
146
147
148
149
150

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
151
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
152
153


Eliot Berriot's avatar
Eliot Berriot committed
154
@registry.register(name="federation.LibraryTrackMetadata")
155
156
157
158
159
160
161
162
163
164
165
166
class LibraryTrackMetadataFactory(factory.Factory):
    artist = factory.SubFactory(ArtistMetadataFactory)
    recording = factory.SubFactory(RecordingMetadataFactory)
    release = factory.SubFactory(ReleaseMetadataFactory)

    class Meta:
        model = dict


@registry.register
class LibraryTrackFactory(factory.DjangoModelFactory):
    library = factory.SubFactory(LibraryFactory)
Eliot Berriot's avatar
Eliot Berriot committed
167
168
169
170
171
172
    url = factory.Faker("url")
    title = factory.Faker("sentence")
    artist_name = factory.Faker("sentence")
    album_title = factory.Faker("sentence")
    audio_url = factory.Faker("url")
    audio_mimetype = "audio/ogg"
173
    metadata = factory.SubFactory(LibraryTrackMetadataFactory)
174
    published_date = factory.LazyFunction(timezone.now)
175
176
177
178

    class Meta:
        model = models.LibraryTrack

179
    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
180
        with_audio_file = factory.Trait(audio_file=factory.django.FileField())
181

182

Eliot Berriot's avatar
Eliot Berriot committed
183
@registry.register(name="federation.Note")
184
class NoteFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
185
186
187
    type = "Note"
    id = factory.Faker("url")
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
188
    inReplyTo = None
Eliot Berriot's avatar
Eliot Berriot committed
189
    content = factory.Faker("sentence")
190
191
192

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
193
194


Eliot Berriot's avatar
Eliot Berriot committed
195
@registry.register(name="federation.Activity")
Eliot Berriot's avatar
Eliot Berriot committed
196
class ActivityFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
197
198
199
200
    type = "Create"
    id = factory.Faker("url")
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
    actor = factory.Faker("url")
Eliot Berriot's avatar
Eliot Berriot committed
201
202
    object = factory.SubFactory(
        NoteFactory,
Eliot Berriot's avatar
Eliot Berriot committed
203
204
205
        actor=factory.SelfAttribute("..actor"),
        published=factory.SelfAttribute("..published"),
    )
Eliot Berriot's avatar
Eliot Berriot committed
206
207
208

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
209
210


Eliot Berriot's avatar
Eliot Berriot committed
211
@registry.register(name="federation.AudioMetadata")
Eliot Berriot's avatar
Eliot Berriot committed
212
213
class AudioMetadataFactory(factory.Factory):
    recording = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
214
        lambda o: "https://musicbrainz.org/recording/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
215
216
    )
    artist = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
217
        lambda o: "https://musicbrainz.org/artist/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
218
219
    )
    release = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
220
        lambda o: "https://musicbrainz.org/release/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
221
    )
222
223
224
    bitrate = 42
    length = 43
    size = 44
Eliot Berriot's avatar
Eliot Berriot committed
225
226
227
228
229

    class Meta:
        model = dict


Eliot Berriot's avatar
Eliot Berriot committed
230
@registry.register(name="federation.Audio")
Eliot Berriot's avatar
Eliot Berriot committed
231
class AudioFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
232
233
234
235
    type = "Audio"
    id = factory.Faker("url")
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
    actor = factory.Faker("url")
Eliot Berriot's avatar
Eliot Berriot committed
236
    url = factory.SubFactory(LinkFactory, audio=True)
237
    metadata = factory.SubFactory(LibraryTrackMetadataFactory)
Eliot Berriot's avatar
Eliot Berriot committed
238
239
240

    class Meta:
        model = dict