2018-03-18 20:31:45 +00:00
|
|
|
import factory
|
2017-12-24 18:15:21 +00:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import pytest
|
2018-03-18 20:31:45 +00:00
|
|
|
|
2018-03-25 15:17:51 +00:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2017-12-26 20:12:37 +00:00
|
|
|
from django.core.cache import cache as django_cache
|
|
|
|
from dynamic_preferences.registries import global_preferences_registry
|
2018-03-18 20:31:45 +00:00
|
|
|
|
2018-02-17 20:21:08 +00:00
|
|
|
from rest_framework.test import APIClient
|
2018-03-07 21:34:16 +00:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2017-12-26 20:12:37 +00:00
|
|
|
|
2018-03-01 19:37:48 +00:00
|
|
|
from funkwhale_api.activity import record
|
2017-12-26 20:12:37 +00:00
|
|
|
from funkwhale_api.taskapp import celery
|
2017-12-24 18:15:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2017-12-26 20:12:37 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def cache():
|
|
|
|
yield django_cache
|
|
|
|
django_cache.clear()
|
|
|
|
|
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def factories(db):
|
|
|
|
from funkwhale_api import factories
|
2018-03-18 20:31:45 +00:00
|
|
|
for v in factories.registry.values():
|
|
|
|
v._meta.strategy = factory.CREATE_STRATEGY
|
|
|
|
yield factories.registry
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def nodb_factories():
|
|
|
|
from funkwhale_api import factories
|
|
|
|
for v in factories.registry.values():
|
|
|
|
v._meta.strategy = factory.BUILD_STRATEGY
|
2017-12-24 18:15:21 +00:00
|
|
|
yield factories.registry
|
|
|
|
|
|
|
|
|
2017-12-26 20:12:37 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def preferences(db):
|
2018-02-17 20:21:08 +00:00
|
|
|
manager = global_preferences_registry.manager()
|
|
|
|
manager.all()
|
|
|
|
yield manager
|
2017-12-26 20:12:37 +00:00
|
|
|
|
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def tmpdir():
|
|
|
|
d = tempfile.mkdtemp()
|
|
|
|
yield d
|
|
|
|
shutil.rmtree(d)
|
|
|
|
|
|
|
|
|
|
|
|
@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')
|
|
|
|
|
|
|
|
|
2018-03-25 15:17:51 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def anonymous_user():
|
|
|
|
return AnonymousUser()
|
|
|
|
|
|
|
|
|
2018-02-17 20:21:08 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def api_client(client):
|
|
|
|
return APIClient()
|
|
|
|
|
|
|
|
|
2018-02-20 22:59:50 +00:00
|
|
|
@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')
|
|
|
|
|
|
|
|
|
2018-02-22 22:33:29 +00:00
|
|
|
@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')
|
|
|
|
|
|
|
|
|
2017-12-24 18:15:21 +00:00
|
|
|
@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')
|
2018-03-01 19:37:48 +00:00
|
|
|
|
|
|
|
|
2018-03-07 21:34:16 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def api_request():
|
|
|
|
return APIRequestFactory()
|
|
|
|
|
|
|
|
|
2018-03-01 19:37:48 +00:00
|
|
|
@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')
|