diff options
author | Andrew Godwin | 2022-11-21 20:10:01 -0700 |
---|---|---|
committer | Andrew Godwin | 2022-11-21 20:10:01 -0700 |
commit | 0d1e09fbcdb1a1db93d9561c9323c7ef105e71ca (patch) | |
tree | 3799cb9523757c98b62e6a552cdf13ca3d98bb1b /users/tests | |
parent | e38e17678432613111ea220260c5d76677a84d3e (diff) | |
download | takahe-0d1e09fbcdb1a1db93d9561c9323c7ef105e71ca.tar.gz takahe-0d1e09fbcdb1a1db93d9561c9323c7ef105e71ca.tar.bz2 takahe-0d1e09fbcdb1a1db93d9561c9323c7ef105e71ca.zip |
Refactor almost all tests into /tests/
Diffstat (limited to 'users/tests')
-rw-r--r-- | users/tests/__init__.py | 0 | ||||
-rw-r--r-- | users/tests/conftest.py | 51 | ||||
-rw-r--r-- | users/tests/models/__init__.py | 0 | ||||
-rw-r--r-- | users/tests/models/test_identity.py | 178 | ||||
-rw-r--r-- | users/tests/test_activitypub.py | 31 | ||||
-rw-r--r-- | users/tests/views/__init__.py | 0 | ||||
-rw-r--r-- | users/tests/views/test_auth.py | 59 |
7 files changed, 0 insertions, 319 deletions
diff --git a/users/tests/__init__.py b/users/tests/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/users/tests/__init__.py +++ /dev/null diff --git a/users/tests/conftest.py b/users/tests/conftest.py deleted file mode 100644 index 0b12793..0000000 --- a/users/tests/conftest.py +++ /dev/null @@ -1,51 +0,0 @@ -import pytest - -from core.models import Config - -# Our testing-only keypair -private_key = """-----BEGIN PRIVATE KEY----- -MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCzNJa9JIxQpOtQ -z8UQKXDPREF9DyBliGu3uPWo6DMnkOm7hoh2+nOryrWDqWOFaVK//n7kltHXUEbm -U3exh0/0iWfzx2AbNrI04csAvW/hRvHbHBnVTotSxzqTd3ESkpcSW4xVuz9aCcFR -kW3unSCO3fF0Lh8Jsy9N/CT6oTnwG+ZpeGvHVbh9xfR5Ww6zA7z8A6B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Jgo/1GBRNvfy02NuMiDpbzt4tRa4BWgzqVD8gYRp -wa0EYmFcA7OR3lQbenSyOMgre0oHFgGA0eMNs7CRctqA2dR4vyZ7IDS4nwgHnqDK -pxxwUvuKdWsceVWhgAjZQj5iRtvDK8Fi0XDCFekCgYALTU1v5iMIpaRAe+eyA2B1 -42qm4B/uhXznvOu2YXU6iJFmMgHGYgpa+Dq8uUjKtpn/LIFeX1KN0hH8z/0LW3gB -e7tN7taW0oLK3RQcEMfkZ7diE9x3LGqo/xMxsZMtxAr88p5eMEU/nxxznOqq+W9b -qxRbXYzEtHz+cW9+FZkyVw== ------END PRIVATE KEY-----""" - -public_key = """-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAszSWvSSMUKTrUM/FEClw -z0RBfQ8gZYhrt7j1qOgzJ5Dpu4aIdvpzq8q1g6ljhWlSv/5+5JbR11BG5lN3sYdP -9Iln88dgGzayNOHLAL1v4Ubx2xwZ1U6LUsc6k3dxEpKXEluMVbs/WgnBUZFt7p0g -jt3xdC4fCbMvTfwk+qE58BvmaXhrx1W4fcX0eVsOswO8/AOgde4W83THf96lD4o8 -iG+bHuHFlbRoP2SdF9bMp/bvZKcFI4R5clh/3rDER8T7luPQT2T4YSrNfrf/+MYG -XEV+deahaj0thLWIGQH4ezN00b/Lgksy+E0/vJGvSFMCnVekxWqh9u2wBisiUHxm -kwIDAQAB ------END PUBLIC KEY-----""" - - -@pytest.fixture -def config_system(): - Config.system = Config.SystemOptions( - system_actor_private_key=private_key, system_actor_public_key=public_key - ) - yield Config.system diff --git a/users/tests/models/__init__.py b/users/tests/models/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/users/tests/models/__init__.py +++ /dev/null diff --git a/users/tests/models/test_identity.py b/users/tests/models/test_identity.py deleted file mode 100644 index 738abe3..0000000 --- a/users/tests/models/test_identity.py +++ /dev/null @@ -1,178 +0,0 @@ -import pytest -from asgiref.sync import async_to_sync - -from core.models import Config -from users.models import Domain, Identity, User -from users.views.identity import CreateIdentity - - -@pytest.mark.django_db -def test_create_identity_form(client): - """ """ - # Make a user - user = User.objects.create(email="test@example.com") - admin = User.objects.create(email="admin@example.com", admin=True) - # Make a domain - domain = Domain.objects.create(domain="example.com", local=True) - domain.users.add(user) - domain.users.add(admin) - - # Test identity_min_length - data = { - "username": "a", - "domain": domain.domain, - "name": "The User", - } - - form = CreateIdentity.form_class(user=user, data=data) - assert not form.is_valid() - assert "username" in form.errors - assert "value has at least" in form.errors["username"][0] - - form = CreateIdentity.form_class(user=admin, data=data) - assert form.errors == {} - - # Test restricted_usernames - data = { - "username": "@root", - "domain": domain.domain, - "name": "The User", - } - - form = CreateIdentity.form_class(user=user, data=data) - assert not form.is_valid() - assert "username" in form.errors - assert "restricted to administrators" in form.errors["username"][0] - - form = CreateIdentity.form_class(user=admin, data=data) - assert form.errors == {} - - # Test valid chars - data = { - "username": "@someval!!!!", - "domain": domain.domain, - "name": "The User", - } - - for u in (user, admin): - form = CreateIdentity.form_class(user=u, data=data) - assert not form.is_valid() - assert "username" in form.errors - assert form.errors["username"][0].startswith("Only the letters") - - -@pytest.mark.django_db -def test_identity_max_per_user(client): - """ - Ensures that the identity limit is functioning - """ - # Make a user - user = User.objects.create(email="test@example.com") - # Make a domain - domain = Domain.objects.create(domain="example.com", local=True) - domain.users.add(user) - # Make an identity for them - for i in range(Config.system.identity_max_per_user): - identity = Identity.objects.create( - actor_uri=f"https://example.com/@test{i}@example.com/actor/", - username=f"test{i}", - domain=domain, - name=f"Test User{i}", - local=True, - ) - identity.users.add(user) - - data = { - "username": "toomany", - "domain": domain.domain, - "name": "Too Many", - } - form = CreateIdentity.form_class(user=user, data=data) - assert form.errors["__all__"][0].startswith("You are not allowed more than") - - user.admin = True - form = CreateIdentity.form_class(user=user, data=data) - assert form.is_valid() - - -@pytest.mark.django_db -def test_fetch_actor(httpx_mock, config_system): - """ - Ensures that making identities via actor fetching works - """ - # Make a shell remote identity - identity = Identity.objects.create( - actor_uri="https://example.com/test-actor/", - local=False, - ) - - # Trigger actor fetch - httpx_mock.add_response( - url="https://example.com/.well-known/webfinger?resource=acct:test@example.com", - json={ - "subject": "acct:test@example.com", - "aliases": [ - "https://example.com/test-actor/", - ], - "links": [ - { - "rel": "http://webfinger.net/rel/profile-page", - "type": "text/html", - "href": "https://example.com/test-actor/", - }, - { - "rel": "self", - "type": "application/activity+json", - "href": "https://example.com/test-actor/", - }, - ], - }, - ) - httpx_mock.add_response( - url="https://example.com/test-actor/", - json={ - "@context": [ - "https://www.w3.org/ns/activitystreams", - "https://w3id.org/security/v1", - ], - "id": "https://example.com/test-actor/", - "type": "Person", - "inbox": "https://example.com/test-actor/inbox/", - "publicKey": { - "id": "https://example.com/test-actor/#main-key", - "owner": "https://example.com/test-actor/", - "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nits-a-faaaake\n-----END PUBLIC KEY-----\n", - }, - "followers": "https://example.com/test-actor/followers/", - "following": "https://example.com/test-actor/following/", - "icon": { - "type": "Image", - "mediaType": "image/jpeg", - "url": "https://example.com/icon.jpg", - }, - "image": { - "type": "Image", - "mediaType": "image/jpeg", - "url": "https://example.com/image.jpg", - }, - "as:manuallyApprovesFollowers": False, - "name": "Test User", - "preferredUsername": "test", - "published": "2022-11-02T00:00:00Z", - "summary": "<p>A test user</p>", - "url": "https://example.com/test-actor/view/", - }, - ) - async_to_sync(identity.fetch_actor)() - - # Verify the data arrived - identity = Identity.objects.get(pk=identity.pk) - assert identity.name == "Test User" - assert identity.username == "test" - assert identity.domain_id == "example.com" - assert identity.profile_uri == "https://example.com/test-actor/view/" - assert identity.inbox_uri == "https://example.com/test-actor/inbox/" - assert identity.icon_uri == "https://example.com/icon.jpg" - assert identity.image_uri == "https://example.com/image.jpg" - assert identity.summary == "<p>A test user</p>" - assert "ts-a-faaaake" in identity.public_key diff --git a/users/tests/test_activitypub.py b/users/tests/test_activitypub.py deleted file mode 100644 index 72ab8c3..0000000 --- a/users/tests/test_activitypub.py +++ /dev/null @@ -1,31 +0,0 @@ -import pytest - -from users.models import Domain, Identity, User - - -@pytest.mark.django_db -def test_webfinger_actor(client): - """ - Ensures the webfinger and actor URLs are working properly - """ - # Make a user - user = User.objects.create(email="test@example.com") - # Make a domain - domain = Domain.objects.create(domain="example.com", local=True) - domain.users.add(user) - # Make an identity for them - identity = Identity.objects.create( - actor_uri="https://example.com/@test@example.com/", - username="test", - domain=domain, - name="Test User", - local=True, - ) - identity.generate_keypair() - # Fetch their webfinger - data = client.get("/.well-known/webfinger?resource=acct:test@example.com").json() - assert data["subject"] == "acct:test@example.com" - assert data["aliases"][0] == "https://example.com/@test/" - # Fetch their actor - data = client.get("/@test@example.com/", HTTP_ACCEPT="application/ld+json").json() - assert data["id"] == "https://example.com/@test@example.com/" diff --git a/users/tests/views/__init__.py b/users/tests/views/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/users/tests/views/__init__.py +++ /dev/null diff --git a/users/tests/views/test_auth.py b/users/tests/views/test_auth.py deleted file mode 100644 index 22e1fb6..0000000 --- a/users/tests/views/test_auth.py +++ /dev/null @@ -1,59 +0,0 @@ -import mock -import pytest - -from core.models import Config -from users.models import User - - -@pytest.fixture -def config_system(): - # TODO: Good enough for now, but a better Config mocking system is needed - result = Config.load_system() - with mock.patch("core.models.Config.load_system", return_value=result): - yield result - - -@pytest.mark.django_db -def test_signup_disabled(client, config_system): - # Signup disabled and no signup text - config_system.signup_allowed = False - resp = client.get("/auth/signup/") - assert resp.status_code == 200 - content = str(resp.content) - assert "Not accepting new users at this time" in content - assert "<button>Create</button>" not in content - - # Signup disabled with signup text configured - config_system.signup_text = "Go away!!!!!!" - resp = client.get("/auth/signup/") - assert resp.status_code == 200 - content = str(resp.content) - assert "Go away!!!!!!" in content - - # Ensure direct POST doesn't side step guard - resp = client.post( - "/auth/signup/", data={"email": "test_signup_disabled@example.org"} - ) - assert resp.status_code == 200 - assert not User.objects.filter(email="test_signup_disabled@example.org").exists() - - # Signup enabled - config_system.signup_allowed = True - resp = client.get("/auth/signup/") - assert resp.status_code == 200 - content = str(resp.content) - assert "Not accepting new users at this time" not in content - assert "<button>Create</button>" in content - - -@pytest.mark.django_db -def test_signup_invite_only(client, config_system): - config_system.signup_allowed = True - config_system.signup_invite_only = True - - resp = client.get("/auth/signup/") - assert resp.status_code == 200 - content = str(resp.content) - assert 'name="invite_code"' in content - - # TODO: Actually test this |