takahe/tests/conftest.py

253 lines
7.6 KiB
Python
Raw Normal View History

2022-11-27 10:09:46 -08:00
import time
import pytest
2023-02-13 18:40:10 -08:00
from django.test import Client
2022-12-10 23:25:48 -08:00
from api.models import Application, Token
from core.models import Config
2022-11-27 10:09:46 -08:00
from stator.runner import StatorModel, StatorRunner
2022-11-21 19:25:00 -08:00
from users.models import Domain, Identity, User
2022-11-21 19:10:01 -08:00
@pytest.fixture
def keypair():
"""
Testing-only keypair
"""
return {
"private_key": """-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCzNJa9JIxQpOtQ
z8UQKXDPREF9DyBliGu3uPWo6DMnkOm7hoh2+nOryrWDqWOFaVK//n7kltHXUEbm
U3exh0/0iWfzx2AbNrI04csAvW/hRvHbHBnVTotSxzqTd3ESkpcSW4xVuz9aCcFR
kW3unSCO3fF0Lh8Jsy9N/CT6oTnwG+ZpeGvHVbh9xfR5Ww6zA7z8A6B17hbzdMd/
3qUPijyIb5se4cWVtGg/ZJ0X1syn9u9kpwUjhHlyWH/esMRHxPuW49BPZPhhKs1+
t//4xgZcRX515qFqPS2EtYgZAfh7M3TRv8uCSzL4TT+8ka9IUwKdV6TFaqH27bAG
KyJQfGaTAgMBAAECggEALZY5qFjlRtiFMfQApdlc5KTw4d7Yt2tqN3zaJUMYTD7d
boJNMbMJfNCetyT+d6Aw2D1ly0GglNzLhGkEQElzKfpQUt/Lj3CtCa3Mpd4K2Wxi
NwJhgfUulPqwaHYQchCPVLCsNNziw0VLA7Rymionb6B+/TaEV8PYy0ZSo90ir3UD
CL5t+IWgIPiy6pk1wGOmeB+tU4+V7/hFel+vPFNahafqVhLE311dfx2aOfweAEfN
e4JoPeJP1/fB+BVZMyVSAraKz6wheymBBNKKn/vpFsdd6it2AP4UZeFp6ma9wT9t
nk65IpHg1MBxazQd7621GrPH+ZnhMg62H/FEj6rIDQKBgQC1w1fEbk+zjI54DXU8
FAe5cJbZS89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gGzayNOHLAL1v4Ubx2xwZ1U6LUsc6k3dxEpKXEluMVbs/WgnBUZFt7p0g
jt3xdC4fCbMvTfwk+qE58BvmaXhrx1W4fcX0eVsOswO8/AOgde4W83THf96lD4o8
iG+bHuHFlbRoP2SdF9bMp/bvZKcFI4R5clh/3rDER8T7luPQT2T4YSrNfrf/+MYG
XEV+deahaj0thLWIGQH4ezN00b/Lgksy+E0/vJGvSFMCnVekxWqh9u2wBisiUHxm
kwIDAQAB
2022-11-21 19:10:01 -08:00
-----END PUBLIC KEY-----""",
"public_key_id": "https://example.com/test-actor#test-key",
}
2022-12-15 09:09:35 -08:00
@pytest.fixture(autouse=True)
def _test_settings(settings):
# We use `StaticFilesStorage` instead of `ManifestStaticFilesStorage` in tests
# since want stable filenames (`css/styles.css`) instead of hashed (`css/styles.55e7cbb9ba48.css`)
settings.STORAGES = {
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
},
}
settings.SETUP.MAIN_DOMAIN = "example.com"
settings.MAIN_DOMAIN = "example.com"
2022-12-15 09:09:35 -08:00
@pytest.fixture
2022-11-21 19:10:01 -08:00
def config_system(keypair):
Config.system = Config.SystemOptions(
2022-11-21 19:10:01 -08:00
system_actor_private_key=keypair["private_key"],
system_actor_public_key=keypair["public_key"],
)
Config.__forced__ = True
yield Config.system
Config.__forced__ = False
del Config.system
2022-11-21 19:25:00 -08:00
@pytest.fixture
def client_with_user(client, user):
"""
Provides a logged-in test client
"""
client.force_login(user)
return client
2022-11-21 19:25:00 -08:00
@pytest.fixture
@pytest.mark.django_db
def user() -> User:
return User.objects.create(email="test@example.com")
@pytest.fixture
@pytest.mark.django_db
2022-11-26 09:09:31 -08:00
def domain() -> Domain:
return Domain.objects.create(
domain="example.com", local=True, public=True, state="updated"
)
2022-11-26 09:09:31 -08:00
2022-12-05 20:44:26 -08:00
@pytest.fixture
@pytest.mark.django_db
def domain2() -> Domain:
return Domain.objects.create(
domain="example2.com", local=True, public=True, state="updated"
)
2022-12-05 20:44:26 -08:00
2022-11-26 09:09:31 -08:00
@pytest.fixture
@pytest.mark.django_db
def identity(user, domain, keypair) -> Identity:
2022-11-21 19:25:00 -08:00
"""
Creates a basic test identity with a user and domain.
"""
identity = Identity.objects.create(
actor_uri="https://example.com/@test@example.com/",
inbox_uri="https://example.com/@test@example.com/inbox/",
private_key=keypair["private_key"],
public_key=keypair["public_key"],
2022-11-21 19:25:00 -08:00
username="test",
domain=domain,
name="Test User",
local=True,
)
identity.users.set([user])
return identity
2022-11-21 19:25:00 -08:00
2022-12-05 20:44:26 -08:00
@pytest.fixture
@pytest.mark.django_db
def identity2(user, domain2) -> Identity:
"""
Creates a basic test identity with a user and domain.
"""
identity = Identity.objects.create(
actor_uri="https://example2.com/@test@example2.com/",
username="test",
domain=domain2,
name="Test User Domain2",
local=True,
)
identity.users.set([user])
return identity
2022-11-26 09:09:31 -08:00
@pytest.fixture
def other_identity(user, domain) -> Identity:
"""
Creates a different basic test identity with a user and domain.
"""
identity = Identity.objects.create(
actor_uri="https://example.com/@other@example.com/",
inbox_uri="https://example.com/@other@example.com/inbox/",
2022-11-26 09:09:31 -08:00
username="other",
domain=domain,
name="Other User",
local=True,
)
identity.users.set([user])
return identity
2022-11-21 19:25:00 -08:00
@pytest.fixture
@pytest.mark.django_db
2022-11-26 09:09:31 -08:00
def remote_identity() -> Identity:
2022-11-21 19:25:00 -08:00
"""
Creates a basic remote test identity with a domain.
"""
domain = Domain.objects.create(domain="remote.test", local=False, state="updated")
2022-11-21 19:25:00 -08:00
return Identity.objects.create(
actor_uri="https://remote.test/test-actor/",
inbox_uri="https://remote.test/@test/inbox/",
profile_uri="https://remote.test/@test/",
2023-05-13 09:01:27 -07:00
featured_collection_uri="https://remote.test/test-actor/collections/featured/",
2022-11-21 19:25:00 -08:00
username="test",
domain=domain,
name="Test Remote User",
local=False,
state="updated",
2022-11-21 19:25:00 -08:00
)
2022-11-27 10:09:46 -08:00
2022-12-05 20:44:26 -08:00
@pytest.fixture
@pytest.mark.django_db
def remote_identity2() -> Identity:
"""
Creates a basic remote test identity with a domain.
"""
domain = Domain.objects.create(domain="remote2.test", local=False)
return Identity.objects.create(
actor_uri="https://remote2.test/test-actor/",
profile_uri="https://remote2.test/@test/",
username="test",
domain=domain,
name="Test2 Remote User",
local=False,
)
2022-12-10 23:25:48 -08:00
@pytest.fixture
@pytest.mark.django_db
def api_token(identity) -> Token:
"""
Creates an API application, an identity, and a token for that identity
"""
application = Application.objects.create(
name="Test App",
client_id="tk-test",
client_secret="mytestappsecret",
)
return Token.objects.create(
application=application,
user=identity.users.first(),
identity=identity,
token="mytestapitoken",
scopes=["read", "write", "follow", "push"],
)
2023-02-13 18:40:10 -08:00
@pytest.fixture
def api_client(api_token):
return Client(
2023-05-13 10:20:47 -07:00
headers={
"authorization": f"Bearer {api_token.token}",
"accept": "application/json",
}
2023-02-13 18:40:10 -08:00
)
2022-11-27 10:09:46 -08:00
@pytest.fixture
def stator(config_system) -> StatorRunner:
2022-11-27 10:09:46 -08:00
"""
Return an initialized StatorRunner for tests that need state transitioning
to happen.
"""
runner = StatorRunner(
StatorModel.subclasses,
concurrency=100,
schedule_interval=30,
)
2022-12-15 12:48:24 -08:00
runner.handled = {}
2022-11-27 10:09:46 -08:00
runner.started = time.monotonic()
runner.last_clean = time.monotonic() - runner.schedule_interval
runner.tasks = []
return runner