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

9
10
from funkwhale_api.factories import registry

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

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


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

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


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

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

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


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = models.Actor

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

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


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

Eliot Berriot's avatar
Eliot Berriot committed
110

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

    class Meta:
        model = models.Library


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = dict

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


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

    class Meta:
        model = models.LibraryTrack

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

180

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

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
191
192


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

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
207
208


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

    class Meta:
        model = dict


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

    class Meta:
        model = dict