factories.py 9.08 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
from funkwhale_api.factories import registry, NoUpdateOnCreate
11
from funkwhale_api.users import factories as user_factories
12

Eliot Berriot's avatar
Eliot Berriot committed
13
from . import keys, models
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
    headers = ["(request-target)", "user-agent", "host", "date", "accept"]
Eliot Berriot's avatar
Eliot Berriot committed
24

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


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()),
44
            "Accept": "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


64
65
66
67
def create_user(actor):
    return user_factories.UserFactory(actor=actor)


68
@registry.register
69
class DomainFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
70
    name = factory.Faker("domain_name")
Eliot Berriot's avatar
Eliot Berriot committed
71
    nodeinfo_fetch_date = factory.LazyFunction(lambda: timezone.now())
72
    allowed = None
73
74
75
76
77

    class Meta:
        model = "federation.Domain"
        django_get_or_create = ("name",)

Eliot Berriot's avatar
Eliot Berriot committed
78
79
80
81
82
83
84
85
86
    @factory.post_generation
    def with_service_actor(self, create, extracted, **kwargs):
        if not create or not extracted:
            return

        self.service_actor = ActorFactory(domain=self)
        self.save(update_fields=["service_actor"])
        return self.service_actor

87

88
89
90
91
92
93
94
95
96
97
98
_CACHE = {}


def get_cached_key_pair():
    try:
        return _CACHE["keys"]
    except KeyError:
        _CACHE["keys"] = keys.get_key_pair()
    return _CACHE["keys"]


Eliot Berriot's avatar
Eliot Berriot committed
99
@registry.register
100
class ActorFactory(NoUpdateOnCreate, factory.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
101
102
    public_key = None
    private_key = None
Eliot Berriot's avatar
Eliot Berriot committed
103
104
    preferred_username = factory.Faker("user_name")
    summary = factory.Faker("paragraph")
105
    domain = factory.SubFactory(DomainFactory)
106
    fid = factory.LazyAttribute(
107
        lambda o: "https://{}/users/{}".format(o.domain.name, o.preferred_username)
Eliot Berriot's avatar
Eliot Berriot committed
108
    )
Eliot Berriot's avatar
Eliot Berriot committed
109
    followers_url = factory.LazyAttribute(
110
111
112
        lambda o: "https://{}/users/{}followers".format(
            o.domain.name, o.preferred_username
        )
Eliot Berriot's avatar
Eliot Berriot committed
113
    )
Eliot Berriot's avatar
Eliot Berriot committed
114
    inbox_url = factory.LazyAttribute(
115
116
117
        lambda o: "https://{}/users/{}/inbox".format(
            o.domain.name, o.preferred_username
        )
Eliot Berriot's avatar
Eliot Berriot committed
118
119
    )
    outbox_url = factory.LazyAttribute(
120
121
122
        lambda o: "https://{}/users/{}/outbox".format(
            o.domain.name, o.preferred_username
        )
Eliot Berriot's avatar
Eliot Berriot committed
123
    )
124
    keys = factory.LazyFunction(get_cached_key_pair)
Eliot Berriot's avatar
Eliot Berriot committed
125
126
127
128

    class Meta:
        model = models.Actor

129
130
131
    class Params:
        with_real_keys = factory.Trait(keys=factory.LazyFunction(keys.get_key_pair),)

132
133
134
135
136
137
    @factory.post_generation
    def local(self, create, extracted, **kwargs):
        if not extracted and not kwargs:
            return
        from funkwhale_api.users.factories import UserFactory

138
139
140
        self.domain = models.Domain.objects.get_or_create(
            name=settings.FEDERATION_HOSTNAME
        )[0]
Eliot Berriot's avatar
Eliot Berriot committed
141
142
        self.fid = "https://{}/actors/{}".format(self.domain, self.preferred_username)
        self.save(update_fields=["domain", "fid"])
143
144
145
146
147
148
149
150
151
152
153
        if not create:
            if extracted and hasattr(extracted, "pk"):
                extracted.actor = self
            else:
                UserFactory.build(actor=self, **kwargs)
        if extracted and hasattr(extracted, "pk"):
            extracted.actor = self
            extracted.save(update_fields=["user"])
        else:
            self.user = UserFactory(actor=self, **kwargs)

154

Eliot Berriot's avatar
Eliot Berriot committed
155
@registry.register
156
class FollowFactory(NoUpdateOnCreate, factory.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
157
158
159
160
161
162
    target = factory.SubFactory(ActorFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = models.Follow

Eliot Berriot's avatar
Eliot Berriot committed
163
    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
164
        local = factory.Trait(actor=factory.SubFactory(ActorFactory, local=True))
Eliot Berriot's avatar
Eliot Berriot committed
165

Eliot Berriot's avatar
Eliot Berriot committed
166

167
@registry.register
168
class MusicLibraryFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
169
170
171
172
    actor = factory.SubFactory(ActorFactory)
    privacy_level = "me"
    name = factory.Faker("sentence")
    description = factory.Faker("sentence")
Eliot Berriot's avatar
Eliot Berriot committed
173
174
    uploads_count = 0
    fid = factory.Faker("federation_url")
175
176
177
    followers_url = factory.LazyAttribute(
        lambda o: o.fid + "/followers" if o.fid else None
    )
178
179
180
181

    class Meta:
        model = "music.Library"

182
    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
183
184
185
        local = factory.Trait(
            fid=None, actor=factory.SubFactory(ActorFactory, local=True)
        )
186

187
188

@registry.register
189
class LibraryScanFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
190
191
    library = factory.SubFactory(MusicLibraryFactory)
    actor = factory.SubFactory(ActorFactory)
Eliot Berriot's avatar
Eliot Berriot committed
192
    total_files = factory.LazyAttribute(lambda o: o.library.uploads_count)
193
194
195
196
197

    class Meta:
        model = "music.LibraryScan"


198
199
200
201
202
203
204
205
@registry.register
class FetchFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = "federation.Fetch"


206
@registry.register
207
class ActivityFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
208
    actor = factory.SubFactory(ActorFactory)
Eliot Berriot's avatar
Eliot Berriot committed
209
    url = factory.Faker("federation_url")
210
    payload = factory.LazyFunction(lambda: {"type": "Create"})
211
212

    class Meta:
213
214
215
216
        model = "federation.Activity"


@registry.register
217
class InboxItemFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
218
    actor = factory.SubFactory(ActorFactory, local=True)
219
220
221
222
223
224
225
    activity = factory.SubFactory(ActivityFactory)
    type = "to"

    class Meta:
        model = "federation.InboxItem"


Eliot Berriot's avatar
Eliot Berriot committed
226
@registry.register
227
class DeliveryFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
228
229
230
231
232
233
234
    activity = factory.SubFactory(ActivityFactory)
    inbox_url = factory.Faker("url")

    class Meta:
        model = "federation.Delivery"


235
@registry.register
236
class LibraryFollowFactory(NoUpdateOnCreate, factory.DjangoModelFactory):
237
238
239
240
241
    target = factory.SubFactory(MusicLibraryFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = "federation.LibraryFollow"
242
243


244
class ArtistMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
245
    name = factory.Faker("name")
246
247
248
249
250

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
251
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
252
253
254


class ReleaseMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
255
    title = factory.Faker("sentence")
256
257
258
259
260

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
261
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
262
263
264


class RecordingMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
265
    title = factory.Faker("sentence")
266
267
268
269
270

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
271
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
272
273


Eliot Berriot's avatar
Eliot Berriot committed
274
@registry.register(name="federation.LibraryTrackMetadata")
275
276
277
278
279
280
281
282
283
class LibraryTrackMetadataFactory(factory.Factory):
    artist = factory.SubFactory(ArtistMetadataFactory)
    recording = factory.SubFactory(RecordingMetadataFactory)
    release = factory.SubFactory(ReleaseMetadataFactory)

    class Meta:
        model = dict


Eliot Berriot's avatar
Eliot Berriot committed
284
@registry.register(name="federation.Note")
285
class NoteFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
286
287
288
    type = "Note"
    id = factory.Faker("url")
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
289
    inReplyTo = None
Eliot Berriot's avatar
Eliot Berriot committed
290
    content = factory.Faker("sentence")
291
292
293

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
294
295


Eliot Berriot's avatar
Eliot Berriot committed
296
@registry.register(name="federation.AudioMetadata")
Eliot Berriot's avatar
Eliot Berriot committed
297
298
class AudioMetadataFactory(factory.Factory):
    recording = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
299
        lambda o: "https://musicbrainz.org/recording/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
300
301
    )
    artist = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
302
        lambda o: "https://musicbrainz.org/artist/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
303
304
    )
    release = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
305
        lambda o: "https://musicbrainz.org/release/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
306
    )
307
308
309
    bitrate = 42
    length = 43
    size = 44
Eliot Berriot's avatar
Eliot Berriot committed
310
311
312
313
314

    class Meta:
        model = dict


Eliot Berriot's avatar
Eliot Berriot committed
315
@registry.register(name="federation.Audio")
Eliot Berriot's avatar
Eliot Berriot committed
316
class AudioFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
317
    type = "Audio"
Eliot Berriot's avatar
Eliot Berriot committed
318
    id = factory.Faker("federation_url")
Eliot Berriot's avatar
Eliot Berriot committed
319
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
Eliot Berriot's avatar
Eliot Berriot committed
320
    actor = factory.Faker("federation_url")
Eliot Berriot's avatar
Eliot Berriot committed
321
    url = factory.SubFactory(LinkFactory, audio=True)
322
    metadata = factory.SubFactory(LibraryTrackMetadataFactory)
Eliot Berriot's avatar
Eliot Berriot committed
323
324
325

    class Meta:
        model = dict