factories.py 8.72 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
    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
            "User-Agent": "Test",
            "Host": "test.host",
44
            "Date": http_date(timezone.now().timestamp()),
Eliot Berriot's avatar
Eliot Berriot committed
45
            "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


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


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

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

Eliot Berriot's avatar
Eliot Berriot committed
79
80
81
82
83
84
85
86
87
    @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

88

Eliot Berriot's avatar
Eliot Berriot committed
89
@registry.register
90
class ActorFactory(NoUpdateOnCreate, factory.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
91
92
    public_key = None
    private_key = None
Eliot Berriot's avatar
Eliot Berriot committed
93
94
    preferred_username = factory.Faker("user_name")
    summary = factory.Faker("paragraph")
95
    domain = factory.SubFactory(DomainFactory)
96
    fid = factory.LazyAttribute(
97
        lambda o: "https://{}/users/{}".format(o.domain.name, o.preferred_username)
Eliot Berriot's avatar
Eliot Berriot committed
98
    )
Eliot Berriot's avatar
Eliot Berriot committed
99
    followers_url = factory.LazyAttribute(
100
101
102
        lambda o: "https://{}/users/{}followers".format(
            o.domain.name, o.preferred_username
        )
Eliot Berriot's avatar
Eliot Berriot committed
103
    )
Eliot Berriot's avatar
Eliot Berriot committed
104
    inbox_url = factory.LazyAttribute(
105
106
107
        lambda o: "https://{}/users/{}/inbox".format(
            o.domain.name, o.preferred_username
        )
Eliot Berriot's avatar
Eliot Berriot committed
108
109
    )
    outbox_url = factory.LazyAttribute(
110
111
112
        lambda o: "https://{}/users/{}/outbox".format(
            o.domain.name, o.preferred_username
        )
Eliot Berriot's avatar
Eliot Berriot committed
113
    )
114
    keys = factory.LazyFunction(keys.get_key_pair)
Eliot Berriot's avatar
Eliot Berriot committed
115
116
117
118

    class Meta:
        model = models.Actor

119
120
121
122
123
124
    @factory.post_generation
    def local(self, create, extracted, **kwargs):
        if not extracted and not kwargs:
            return
        from funkwhale_api.users.factories import UserFactory

125
126
127
        self.domain = models.Domain.objects.get_or_create(
            name=settings.FEDERATION_HOSTNAME
        )[0]
128
129
130
131
132
133
134
135
136
137
138
139
        self.save(update_fields=["domain"])
        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)

140

Eliot Berriot's avatar
Eliot Berriot committed
141
@registry.register
142
class FollowFactory(NoUpdateOnCreate, factory.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
143
144
145
146
147
148
    target = factory.SubFactory(ActorFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = models.Follow

Eliot Berriot's avatar
Eliot Berriot committed
149
    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
150
        local = factory.Trait(actor=factory.SubFactory(ActorFactory, local=True))
Eliot Berriot's avatar
Eliot Berriot committed
151

Eliot Berriot's avatar
Eliot Berriot committed
152

153
@registry.register
154
class MusicLibraryFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
155
156
157
158
    actor = factory.SubFactory(ActorFactory)
    privacy_level = "me"
    name = factory.Faker("sentence")
    description = factory.Faker("sentence")
Eliot Berriot's avatar
Eliot Berriot committed
159
160
    uploads_count = 0
    fid = factory.Faker("federation_url")
161
162
163
    followers_url = factory.LazyAttribute(
        lambda o: o.fid + "/followers" if o.fid else None
    )
164
165
166
167

    class Meta:
        model = "music.Library"

168
169
170
    class Params:
        local = factory.Trait(actor=factory.SubFactory(ActorFactory, local=True))

171
172

@registry.register
173
class LibraryScanFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
174
175
    library = factory.SubFactory(MusicLibraryFactory)
    actor = factory.SubFactory(ActorFactory)
Eliot Berriot's avatar
Eliot Berriot committed
176
    total_files = factory.LazyAttribute(lambda o: o.library.uploads_count)
177
178
179
180
181

    class Meta:
        model = "music.LibraryScan"


182
183
184
185
186
187
188
189
@registry.register
class FetchFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = "federation.Fetch"


190
@registry.register
191
class ActivityFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
192
    actor = factory.SubFactory(ActorFactory)
Eliot Berriot's avatar
Eliot Berriot committed
193
    url = factory.Faker("federation_url")
194
    payload = factory.LazyFunction(lambda: {"type": "Create"})
195
196

    class Meta:
197
198
199
200
        model = "federation.Activity"


@registry.register
201
class InboxItemFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
202
    actor = factory.SubFactory(ActorFactory, local=True)
203
204
205
206
207
208
209
    activity = factory.SubFactory(ActivityFactory)
    type = "to"

    class Meta:
        model = "federation.InboxItem"


Eliot Berriot's avatar
Eliot Berriot committed
210
@registry.register
211
class DeliveryFactory(NoUpdateOnCreate, factory.django.DjangoModelFactory):
Eliot Berriot's avatar
Eliot Berriot committed
212
213
214
215
216
217
218
    activity = factory.SubFactory(ActivityFactory)
    inbox_url = factory.Faker("url")

    class Meta:
        model = "federation.Delivery"


219
@registry.register
220
class LibraryFollowFactory(NoUpdateOnCreate, factory.DjangoModelFactory):
221
222
223
224
225
    target = factory.SubFactory(MusicLibraryFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = "federation.LibraryFollow"
226
227


228
class ArtistMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
229
    name = factory.Faker("name")
230
231
232
233
234

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
235
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
236
237
238


class ReleaseMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
239
    title = factory.Faker("sentence")
240
241
242
243
244

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
245
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
246
247
248


class RecordingMetadataFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
249
    title = factory.Faker("sentence")
250
251
252
253
254

    class Meta:
        model = dict

    class Params:
Eliot Berriot's avatar
Eliot Berriot committed
255
        musicbrainz = factory.Trait(musicbrainz_id=factory.Faker("uuid4"))
256
257


Eliot Berriot's avatar
Eliot Berriot committed
258
@registry.register(name="federation.LibraryTrackMetadata")
259
260
261
262
263
264
265
266
267
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
268
@registry.register(name="federation.Note")
269
class NoteFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
270
271
272
    type = "Note"
    id = factory.Faker("url")
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
273
    inReplyTo = None
Eliot Berriot's avatar
Eliot Berriot committed
274
    content = factory.Faker("sentence")
275
276
277

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
278
279


Eliot Berriot's avatar
Eliot Berriot committed
280
@registry.register(name="federation.AudioMetadata")
Eliot Berriot's avatar
Eliot Berriot committed
281
282
class AudioMetadataFactory(factory.Factory):
    recording = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
283
        lambda o: "https://musicbrainz.org/recording/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
284
285
    )
    artist = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
286
        lambda o: "https://musicbrainz.org/artist/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
287
288
    )
    release = factory.LazyAttribute(
Eliot Berriot's avatar
Eliot Berriot committed
289
        lambda o: "https://musicbrainz.org/release/{}".format(uuid.uuid4())
Eliot Berriot's avatar
Eliot Berriot committed
290
    )
291
292
293
    bitrate = 42
    length = 43
    size = 44
Eliot Berriot's avatar
Eliot Berriot committed
294
295
296
297
298

    class Meta:
        model = dict


Eliot Berriot's avatar
Eliot Berriot committed
299
@registry.register(name="federation.Audio")
Eliot Berriot's avatar
Eliot Berriot committed
300
class AudioFactory(factory.Factory):
Eliot Berriot's avatar
Eliot Berriot committed
301
    type = "Audio"
Eliot Berriot's avatar
Eliot Berriot committed
302
    id = factory.Faker("federation_url")
Eliot Berriot's avatar
Eliot Berriot committed
303
    published = factory.LazyFunction(lambda: timezone.now().isoformat())
Eliot Berriot's avatar
Eliot Berriot committed
304
    actor = factory.Faker("federation_url")
Eliot Berriot's avatar
Eliot Berriot committed
305
    url = factory.SubFactory(LinkFactory, audio=True)
306
    metadata = factory.SubFactory(LibraryTrackMetadataFactory)
Eliot Berriot's avatar
Eliot Berriot committed
307
308
309

    class Meta:
        model = dict