Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE = testbed.settings
python_files = tests.py test_*.py *_tests.py
16 changes: 15 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
asgiref==3.8.1
autopep8==2.3.1
beautifulsoup4==4.12.3
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.4.0
coverage==7.6.10
cryptography==43.0.3
Django==5.1.3
django-activitypub==0.0.2
django-environ==0.11.2
django-oauth-toolkit==3.0.1
django-tree-queries==0.19.0
djangorestframework==3.15.2
html-sanitizer==2.4.4
idna==3.10
iniconfig==2.0.0
jwcrypto==1.5.6
lxml==5.3.0
lxml_html_clean==0.3.1
Markdown==3.7
oauthlib==3.2.2
packaging==24.2
pillow==11.0.0
pluggy==1.5.0
psycopg2==2.9.10
pycodestyle==2.12.1
pycparser==2.22
pytest==8.3.3
pytest-cov==6.0.0
pytest-django==4.9.0
requests==2.32.3
soupsieve==2.6
sqlparse==0.5.1
typing_extensions==4.12.2
urllib3==2.2.3
django-environ~=0.11.2
21 changes: 21 additions & 0 deletions testbed/core/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from django.contrib.auth.models import User
from testbed.core.models import Actor, PortabilityOutbox


# Create and return a user
@pytest.fixture
def user():
return User.objects.create_user(username='testuser', password='testpass')

# Create and return an actor linked to the user
@pytest.fixture
def actor(user):
return Actor.objects.create(user=user, username='testactor', full_name='Test Actor')

# Create and return a portability outbox linked to the actor
@pytest.fixture
def outbox(actor):
return PortabilityOutbox.objects.create(actor=actor)


26 changes: 26 additions & 0 deletions testbed/core/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from rest_framework.test import APIClient
from rest_framework import status
from django.urls import reverse

# Test that the Actor detail API returns the correct data
@pytest.mark.django_db
def test_actor_detail_api(actor):
client = APIClient()
url = reverse('actor-detail', kwargs={'pk': actor.id})
response = client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['id'] == actor.id
assert response.data['json_ld'] == actor.get_json_ld()

# Test that the PortabilityOutbox detail API returns the correct data
@pytest.mark.django_db
def test_outbox_detail_api(outbox):
client = APIClient()
url = reverse('actor-outbox', kwargs={'pk': outbox.actor.id})
response = client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['id'] == outbox.id
assert response.data['actor'] == str(outbox.actor)
assert response.data['activities'] == []
assert response.data['json_ld'] == outbox.get_json_ld()
34 changes: 34 additions & 0 deletions testbed/core/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest


# Test that an Actor is created correctly
@pytest.mark.django_db
def test_actor_creation(actor):
assert actor.username == 'testactor'
assert actor.full_name == 'Test Actor'
assert actor.user.username == 'testuser'

# Test that the Actor's JSON-LD representation is correct
@pytest.mark.django_db
def test_actor_json_ld(actor):
expected = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://swicg.github.io/activitypub-data-portability/lola.jsonld",
],
"type": "Person",
"id": "https://example.com/users/testactor",
"preferredUsername": "testactor",
"name": "testactor",
"previously": {},
}
assert actor.get_json_ld() == expected

# Test that a Portability Outbox is linked to an Actor
@pytest.mark.django_db
def test_outbox_creation(outbox):
assert outbox.actor.username == 'testactor'
assert outbox.actor.full_name == 'Test Actor'
assert outbox.actor.user.username == 'testuser'
assert outbox.actor.user.actor == outbox.actor
assert outbox.actor.actor_activities.count() == 0
13 changes: 13 additions & 0 deletions testbed/core/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from testbed.core.serializers import ActorSerializer, ActivitySerializer, PortabilityOutboxSerializer


# Test that the ActorSerializer returns the correct data
@pytest.mark.django_db
def test_actor_serializer(actor):
serializer = ActorSerializer(actor)
expected = {
'id': actor.id,
'json_ld': actor.get_json_ld(),
}
assert serializer.data == expected
12 changes: 12 additions & 0 deletions testbed/core/tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import resolve


# Test that the Actor detail URL resolves correctly
def test_actor_detail_url():
resolver = resolve('/api/actors/1/')
assert resolver.view_name == 'actor-detail'

# Test that the PortabilityOutbox URL resolves correctly
def test_outbox_detail_url():
resolver = resolve('/api/actors/1/outbox/')
assert resolver.view_name == 'actor-outbox'