factories.py 1.95 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
Eliot Berriot's avatar
Eliot Berriot committed
8
from . import models
9
10


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


@registry.register(name='federation.SignatureAuth')
class SignatureAuthFactory(factory.Factory):
    algorithm = 'rsa-sha256'
17
    key = factory.LazyFunction(lambda: keys.get_key_pair()[0])
18
    key_id = factory.Faker('url')
19
    use_auth_header = False
20
21
22
23
24
25
    headers = [
        '(request-target)',
        'user-agent',
        'host',
        'date',
        'content-type',]
26
27
28
29
30
31
32
33
34
35
36
37
    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
38
39
40
41
42
43
44
45
46
47
48
49
50
51

    @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)


Eliot Berriot's avatar
Eliot Berriot committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@registry.register
class ActorFactory(factory.DjangoModelFactory):
    url = factory.Faker('url')
    inbox_url = factory.Faker('url')
    outbox_url = factory.Faker('url')
    public_key = None
    private_key = None
    preferred_username = factory.Faker('user_name')
    summary = factory.Faker('paragraph')

    class Meta:
        model = models.Actor

    @classmethod
    def _generate(cls, create, attrs):
        has_public = attrs.get('public_key') is None
        has_private = attrs.get('private_key') is None
        if not has_public and not has_private:
            attrs['private_key'], attrs['public'] = keys.get_key_pair()
        return super()._generate(create, attrs)