From 0d1e09fbcdb1a1db93d9561c9323c7ef105e71ca Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Mon, 21 Nov 2022 20:10:01 -0700 Subject: [PATCH] Refactor almost all tests into /tests/ --- activities/tests/__init__.py | 0 activities/tests/models/__init__.py | 0 activities/tests/templatetags/__init__.py | 0 core/tests/__init__.py | 0 core/tests/conftest.py | 9 --- .../activities}/models/test_post.py | 0 .../templatetags/test_activity_tags.py | 0 {users/tests => tests}/conftest.py | 24 ++++--- {core/tests => tests/core}/test_signatures.py | 68 ++++--------------- .../users}/models/test_identity.py | 4 +- .../users/views}/test_activitypub.py | 0 .../tests => tests/users}/views/test_auth.py | 0 users/tests/__init__.py | 0 users/tests/models/__init__.py | 0 users/tests/views/__init__.py | 0 15 files changed, 31 insertions(+), 74 deletions(-) delete mode 100644 activities/tests/__init__.py delete mode 100644 activities/tests/models/__init__.py delete mode 100644 activities/tests/templatetags/__init__.py delete mode 100644 core/tests/__init__.py delete mode 100644 core/tests/conftest.py rename {activities/tests => tests/activities}/models/test_post.py (100%) rename {activities/tests => tests/activities}/templatetags/test_activity_tags.py (100%) rename {users/tests => tests}/conftest.py (82%) rename {core/tests => tests/core}/test_signatures.py (60%) rename {users/tests => tests/users}/models/test_identity.py (98%) rename {users/tests => tests/users/views}/test_activitypub.py (100%) rename {users/tests => tests/users}/views/test_auth.py (100%) delete mode 100644 users/tests/__init__.py delete mode 100644 users/tests/models/__init__.py delete mode 100644 users/tests/views/__init__.py diff --git a/activities/tests/__init__.py b/activities/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/activities/tests/models/__init__.py b/activities/tests/models/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/activities/tests/templatetags/__init__.py b/activities/tests/templatetags/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/tests/__init__.py b/core/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/tests/conftest.py b/core/tests/conftest.py deleted file mode 100644 index ab8d6ea..0000000 --- a/core/tests/conftest.py +++ /dev/null @@ -1,9 +0,0 @@ -import pytest -from pyld import jsonld - -from core.ld import builtin_document_loader - - -@pytest.fixture(scope="session", autouse=True) -def ldloader(): - jsonld.set_document_loader(builtin_document_loader) diff --git a/activities/tests/models/test_post.py b/tests/activities/models/test_post.py similarity index 100% rename from activities/tests/models/test_post.py rename to tests/activities/models/test_post.py diff --git a/activities/tests/templatetags/test_activity_tags.py b/tests/activities/templatetags/test_activity_tags.py similarity index 100% rename from activities/tests/templatetags/test_activity_tags.py rename to tests/activities/templatetags/test_activity_tags.py diff --git a/users/tests/conftest.py b/tests/conftest.py similarity index 82% rename from users/tests/conftest.py rename to tests/conftest.py index 0b12793..79bdf60 100644 --- a/users/tests/conftest.py +++ b/tests/conftest.py @@ -2,8 +2,14 @@ import pytest from core.models import Config -# Our testing-only keypair -private_key = """-----BEGIN PRIVATE KEY----- + +@pytest.fixture +def keypair(): + """ + Testing-only keypair + """ + return { + "private_key": """-----BEGIN PRIVATE KEY----- MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCzNJa9JIxQpOtQ z8UQKXDPREF9DyBliGu3uPWo6DMnkOm7hoh2+nOryrWDqWOFaVK//n7kltHXUEbm U3exh0/0iWfzx2AbNrI04csAvW/hRvHbHBnVTotSxzqTd3ESkpcSW4xVuz9aCcFR @@ -30,9 +36,8 @@ pxxwUvuKdWsceVWhgAjZQj5iRtvDK8Fi0XDCFekCgYALTU1v5iMIpaRAe+eyA2B1 42qm4B/uhXznvOu2YXU6iJFmMgHGYgpa+Dq8uUjKtpn/LIFeX1KN0hH8z/0LW3gB e7tN7taW0oLK3RQcEMfkZ7diE9x3LGqo/xMxsZMtxAr88p5eMEU/nxxznOqq+W9b qxRbXYzEtHz+cW9+FZkyVw== ------END PRIVATE KEY-----""" - -public_key = """-----BEGIN PUBLIC KEY----- +-----END PRIVATE KEY-----""", + "public_key": """-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAszSWvSSMUKTrUM/FEClw z0RBfQ8gZYhrt7j1qOgzJ5Dpu4aIdvpzq8q1g6ljhWlSv/5+5JbR11BG5lN3sYdP 9Iln88dgGzayNOHLAL1v4Ubx2xwZ1U6LUsc6k3dxEpKXEluMVbs/WgnBUZFt7p0g @@ -40,12 +45,15 @@ jt3xdC4fCbMvTfwk+qE58BvmaXhrx1W4fcX0eVsOswO8/AOgde4W83THf96lD4o8 iG+bHuHFlbRoP2SdF9bMp/bvZKcFI4R5clh/3rDER8T7luPQT2T4YSrNfrf/+MYG XEV+deahaj0thLWIGQH4ezN00b/Lgksy+E0/vJGvSFMCnVekxWqh9u2wBisiUHxm kwIDAQAB ------END PUBLIC KEY-----""" +-----END PUBLIC KEY-----""", + "public_key_id": "https://example.com/test-actor#test-key", + } @pytest.fixture -def config_system(): +def config_system(keypair): Config.system = Config.SystemOptions( - system_actor_private_key=private_key, system_actor_public_key=public_key + system_actor_private_key=keypair["private_key"], + system_actor_public_key=keypair["public_key"], ) yield Config.system diff --git a/core/tests/test_signatures.py b/tests/core/test_signatures.py similarity index 60% rename from core/tests/test_signatures.py rename to tests/core/test_signatures.py index 2d480b7..f15e090 100644 --- a/core/tests/test_signatures.py +++ b/tests/core/test_signatures.py @@ -5,50 +5,8 @@ from pytest_httpx import HTTPXMock from core.signatures import HttpSignature, LDSignature, VerificationError -# Our testing-only keypair -private_key = """-----BEGIN PRIVATE KEY----- -MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCzNJa9JIxQpOtQ -z8UQKXDPREF9DyBliGu3uPWo6DMnkOm7hoh2+nOryrWDqWOFaVK//n7kltHXUEbm -U3exh0/0iWfzx2AbNrI04csAvW/hRvHbHBnVTotSxzqTd3ESkpcSW4xVuz9aCcFR -kW3unSCO3fF0Lh8Jsy9N/CT6oTnwG+ZpeGvHVbh9xfR5Ww6zA7z8A6B17hbzdMd/ -3qUPijyIb5se4c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4tRa4BWgzqVD8gYRp -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-----""" - -public_key_id = "https://example.com/test-actor#test-key" - - -def test_sign_ld(): +def test_sign_ld(keypair): """ Tests signing JSON-LD documents by round-tripping them through the verifier. @@ -65,18 +23,18 @@ def test_sign_ld(): } signature_section = LDSignature.create_signature( document, - private_key, - public_key_id, + keypair["private_key"], + keypair["public_key_id"], ) # Check it and assign it to the document assert "signatureValue" in signature_section assert signature_section["type"] == "RsaSignature2017" document["signature"] = signature_section # Now verify it ourselves - LDSignature.verify_signature(document, public_key) + LDSignature.verify_signature(document, keypair["public_key"]) -def test_verifying_ld(): +def test_verifying_ld(keypair): """ Tests verifying JSON-LD signatures from a known-good document """ @@ -94,14 +52,14 @@ def test_verifying_ld(): }, } # Ensure it verifies with correct data - LDSignature.verify_signature(document, public_key) + LDSignature.verify_signature(document, keypair["public_key"]) # Mutate it slightly and ensure it does not verify with pytest.raises(VerificationError): document["actor"] = "https://example.com/evil-actor" - LDSignature.verify_signature(document, public_key) + LDSignature.verify_signature(document, keypair["public_key"]) -def test_sign_http(httpx_mock: HTTPXMock): +def test_sign_http(httpx_mock: HTTPXMock, keypair): """ Tests signing HTTP requests by round-tripping them through our verifier """ @@ -120,8 +78,8 @@ def test_sign_http(httpx_mock: HTTPXMock): async_to_sync(HttpSignature.signed_request)( uri="https://example.com/test-actor", body=document, - private_key=private_key, - key_id=public_key_id, + private_key=keypair["private_key"], + key_id=keypair["public_key_id"], ) # Retrieve it and construct a fake request object outbound_request = httpx_mock.get_request() @@ -135,10 +93,10 @@ def test_sign_http(httpx_mock: HTTPXMock): HTTP_DIGEST=outbound_request.headers["digest"], ) # Verify that - HttpSignature.verify_request(fake_request, public_key) + HttpSignature.verify_request(fake_request, keypair["public_key"]) -def test_verify_http(): +def test_verify_http(keypair): """ Tests verifying HTTP requests against a known good example """ @@ -153,4 +111,4 @@ def test_verify_http(): HTTP_DIGEST="SHA-256=07sIbQ3GlOHWMbFMNajtPNtmUQXXu20UuvrIYLlI3kc=", ) # Verify that - HttpSignature.verify_request(fake_request, public_key, skip_date=True) + HttpSignature.verify_request(fake_request, keypair["public_key"], skip_date=True) diff --git a/users/tests/models/test_identity.py b/tests/users/models/test_identity.py similarity index 98% rename from users/tests/models/test_identity.py rename to tests/users/models/test_identity.py index 738abe3..13c08f0 100644 --- a/users/tests/models/test_identity.py +++ b/tests/users/models/test_identity.py @@ -7,7 +7,7 @@ from users.views.identity import CreateIdentity @pytest.mark.django_db -def test_create_identity_form(client): +def test_create_identity_form(config_system, client): """ """ # Make a user user = User.objects.create(email="test@example.com") @@ -62,7 +62,7 @@ def test_create_identity_form(client): @pytest.mark.django_db -def test_identity_max_per_user(client): +def test_identity_max_per_user(config_system, client): """ Ensures that the identity limit is functioning """ diff --git a/users/tests/test_activitypub.py b/tests/users/views/test_activitypub.py similarity index 100% rename from users/tests/test_activitypub.py rename to tests/users/views/test_activitypub.py diff --git a/users/tests/views/test_auth.py b/tests/users/views/test_auth.py similarity index 100% rename from users/tests/views/test_auth.py rename to tests/users/views/test_auth.py diff --git a/users/tests/__init__.py b/users/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/users/tests/models/__init__.py b/users/tests/models/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/users/tests/views/__init__.py b/users/tests/views/__init__.py deleted file mode 100644 index e69de29..0000000