conftest.py 4.06 KB
Newer Older
1
import factory
2
import pytest
3
import requests_mock
4
5
import shutil
import tempfile
6

Eliot Berriot's avatar
Eliot Berriot committed
7
from django.contrib.auth.models import AnonymousUser
8
from django.core.cache import cache as django_cache
9
10
from django.test import client

11
from dynamic_preferences.registries import global_preferences_registry
12

13
from rest_framework.test import APIClient
14
from rest_framework.test import APIRequestFactory
15

16
from funkwhale_api.activity import record
17
from funkwhale_api.taskapp import celery
18
19
20
21
22
23
24
25
26
27


@pytest.fixture(scope="session", autouse=True)
def factories_autodiscover():
    from django.apps import apps
    from funkwhale_api import factories
    app_names = [app.name for app in apps.app_configs.values()]
    factories.registry.autodiscover(app_names)


28
29
30
31
32
33
@pytest.fixture(autouse=True)
def cache():
    yield django_cache
    django_cache.clear()


34
35
36
@pytest.fixture
def factories(db):
    from funkwhale_api import factories
37
    for v in factories.registry.values():
38
39
40
41
42
        try:
            v._meta.strategy = factory.CREATE_STRATEGY
        except AttributeError:
            # probably not a class based factory
            pass
43
44
45
46
47
48
49
    yield factories.registry


@pytest.fixture
def nodb_factories():
    from funkwhale_api import factories
    for v in factories.registry.values():
50
51
52
53
54
        try:
            v._meta.strategy = factory.BUILD_STRATEGY
        except AttributeError:
            # probably not a class based factory
            pass
55
56
57
    yield factories.registry


58
@pytest.fixture
59
def preferences(db, cache):
60
61
62
    manager = global_preferences_registry.manager()
    manager.all()
    yield manager
63
64


65
66
67
68
69
70
71
@pytest.fixture
def tmpdir():
    d = tempfile.mkdtemp()
    yield d
    shutil.rmtree(d)


Eliot Berriot's avatar
Eliot Berriot committed
72
73
74
75
76
@pytest.fixture
def tmpfile():
    yield tempfile.NamedTemporaryFile()


77
78
79
80
81
82
83
84
85
@pytest.fixture
def logged_in_client(db, factories, client):
    user = factories['users.User']()
    assert client.login(username=user.username, password='test')
    setattr(client, 'user', user)
    yield client
    delattr(client, 'user')


Eliot Berriot's avatar
Eliot Berriot committed
86
87
88
89
90
@pytest.fixture
def anonymous_user():
    return AnonymousUser()


91
92
93
94
95
@pytest.fixture
def api_client(client):
    return APIClient()


Eliot Berriot's avatar
Eliot Berriot committed
96
97
98
99
100
101
102
103
104
@pytest.fixture
def logged_in_api_client(db, factories, api_client):
    user = factories['users.User']()
    assert api_client.login(username=user.username, password='test')
    setattr(api_client, 'user', user)
    yield api_client
    delattr(api_client, 'user')


105
106
107
108
109
110
111
112
113
@pytest.fixture
def superuser_api_client(db, factories, api_client):
    user = factories['users.SuperUser']()
    assert api_client.login(username=user.username, password='test')
    setattr(api_client, 'user', user)
    yield api_client
    delattr(api_client, 'user')


114
115
116
117
118
119
120
@pytest.fixture
def superuser_client(db, factories, client):
    user = factories['users.SuperUser']()
    assert client.login(username=user.username, password='test')
    setattr(client, 'user', user)
    yield client
    delattr(client, 'user')
121
122


123
124
125
126
127
@pytest.fixture
def api_request():
    return APIRequestFactory()


128
129
130
131
132
@pytest.fixture
def fake_request():
    return client.RequestFactory()


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
@pytest.fixture
def activity_registry():
    r = record.registry
    state = list(record.registry.items())
    yield record.registry
    record.registry.clear()
    for key, value in state:
        record.registry[key] = value


@pytest.fixture
def activity_registry():
    r = record.registry
    state = list(record.registry.items())
    yield record.registry
    record.registry.clear()
    for key, value in state:
        record.registry[key] = value


@pytest.fixture
def activity_muted(activity_registry, mocker):
    yield mocker.patch.object(record, 'send')
Eliot Berriot's avatar
Eliot Berriot committed
156
157
158
159
160
161
162
163


@pytest.fixture(autouse=True)
def media_root(settings):
    tmp_dir = tempfile.mkdtemp()
    settings.MEDIA_ROOT = tmp_dir
    yield settings.MEDIA_ROOT
    shutil.rmtree(tmp_dir)
164
165
166
167
168
169


@pytest.fixture
def r_mock():
    with requests_mock.mock() as m:
        yield m
170
171
172
173
174
175
176
177
178


@pytest.fixture
def authenticated_actor(factories, mocker):
    actor = factories['federation.Actor']()
    mocker.patch(
        'funkwhale_api.federation.authentication.SignatureAuthentication.authenticate_actor',
        return_value=actor)
    yield actor