factories.py 1.65 KB
Newer Older
1
2
3
4
5
6
import factory
import requests
import requests_http_signature

from funkwhale_api.factories import registry

7
from . import keys
8
9


10
registry.register(keys.get_key_pair, name='federation.KeyPair')
11
12
13
14
15


@registry.register(name='federation.SignatureAuth')
class SignatureAuthFactory(factory.Factory):
    algorithm = 'rsa-sha256'
16
    key = factory.LazyFunction(lambda: keys.get_key_pair()[0])
17
    key_id = factory.Faker('url')
18
    use_auth_header = False
19
20
21
22
23
24
    headers = [
        '(request-target)',
        'user-agent',
        'host',
        'date',
        'content-type',]
25
26
27
28
29
30
31
32
33
34
35
36
    class Meta:
        model = requests_http_signature.HTTPSignatureAuth


@registry.register(name='federation.SignedRequest')
class SignedRequestFactory(factory.Factory):
    url = factory.Faker('url')
    method = 'get'
    auth = factory.SubFactory(SignatureAuthFactory)

    class Meta:
        model = requests.Request
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    @factory.post_generation
    def headers(self, create, extracted, **kwargs):
        default_headers = {
            'User-Agent': 'Test',
            'Host': 'test.host',
            'Date': 'Right now',
            'Content-Type': 'application/activity+json'
        }
        if extracted:
            default_headers.update(extracted)
        self.headers.update(default_headers)


# @registry.register
# class ActorFactory(factory.DjangoModelFactory):
#     url = factory.Faker('url')
#     inbox_url = factory.Faker('url')
#     outbox_url = factory.Faker('url')
#     public_key = factory.LazyFunction(lambda: keys.get_key_pair()[1])
#     preferred_username = factory.Faker('username')
#     summary = factory.Faker('paragraph')
#
#     class Meta:
#         model = models.Actor