factories.py 6.55 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


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


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

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


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

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

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


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = models.Actor

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

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


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

Eliot Berriot's avatar
Eliot Berriot committed
112

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

    class Meta:
        model = models.Library


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = dict

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


Eliot Berriot's avatar
Eliot Berriot committed
155
@registry.register(name="federation.LibraryTrackMetadata")
156
157
158
159
160
161
162
163
164
165
166
167
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
168
169
170
171
172
173
    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"
174
175
176
177
178
    metadata = factory.SubFactory(LibraryTrackMetadataFactory)

    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