factories.py 3.31 KB
Newer Older
1
2
3
4
import factory
import requests
import requests_http_signature

5
6
from django.utils import timezone

7
8
from funkwhale_api.factories import registry

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


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


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

    @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
54
55
56
57
58
59
@registry.register
class ActorFactory(factory.DjangoModelFactory):
    public_key = None
    private_key = None
    preferred_username = factory.Faker('user_name')
    summary = factory.Faker('paragraph')
Eliot Berriot's avatar
Eliot Berriot committed
60
61
62
63
    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
64
65
66
67
68
69

    class Meta:
        model = models.Actor

    @classmethod
    def _generate(cls, create, attrs):
70
71
        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
72
        if not has_public and not has_private:
73
74
75
            private, public = keys.get_key_pair()
            attrs['private_key'] = private.decode('utf-8')
            attrs['public_key'] = public.decode('utf-8')
Eliot Berriot's avatar
Eliot Berriot committed
76
        return super()._generate(create, attrs)
77
78


Eliot Berriot's avatar
Eliot Berriot committed
79
80
81
82
83
84
85
86
87
@registry.register
class FollowFactory(factory.DjangoModelFactory):
    target = factory.SubFactory(ActorFactory)
    actor = factory.SubFactory(ActorFactory)

    class Meta:
        model = models.Follow


88
89
90
91
92
93
94
95
96
97
98
99
@registry.register(name='federation.Note')
class NoteFactory(factory.Factory):
    type = 'Note'
    id = factory.Faker('url')
    published = factory.LazyFunction(
        lambda: timezone.now().isoformat()
    )
    inReplyTo = None
    content = factory.Faker('sentence')

    class Meta:
        model = dict
Eliot Berriot's avatar
Eliot Berriot committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116


@registry.register(name='federation.Activity')
class ActivityFactory(factory.Factory):
    type = 'Create'
    id = factory.Faker('url')
    published = factory.LazyFunction(
        lambda: timezone.now().isoformat()
    )
    actor = factory.Faker('url')
    object = factory.SubFactory(
        NoteFactory,
        actor=factory.SelfAttribute('..actor'),
        published=factory.SelfAttribute('..published'))

    class Meta:
        model = dict