Skip to content

Commit

Permalink
Inline test fixtures
Browse files Browse the repository at this point in the history
Test fixtures should not be shared so we can evolve unit tests
separately

Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
  • Loading branch information
pombredanne committed Aug 14, 2024
1 parent 8f743d9 commit 9a700d6
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 152 deletions.
11 changes: 11 additions & 0 deletions tests/__init__.py → tests/fedcode_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@
# See https://github.com/nexB/federatedcode for support or download.
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
#

import pytest
from django.db.models.signals import post_save


@pytest.fixture(autouse=True)
def mute_post_save_signal(request):
"""
copied from https://www.cameronmaske.com/muting-django-signals-with-a-pytest-fixture/
"""
post_save.receivers = []
92 changes: 80 additions & 12 deletions tests/test_activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import json

import pytest
from django.contrib.auth.models import User
from fedcode_test_utils import mute_post_save_signal # NOQA

from fedcode.activitypub import AP_CONTEXT
from fedcode.activitypub import Activity
Expand All @@ -19,21 +21,87 @@
from fedcode.activitypub import create_activity_obj
from fedcode.models import Follow
from fedcode.models import Note
from fedcode.models import Package
from fedcode.models import Person
from fedcode.models import Repository
from fedcode.models import Review
from fedcode.models import Service
from fedcode.models import SyncRequest
from fedcode.models import Vulnerability

from .test_models import fake_service
from .test_models import follow
from .test_models import mute_post_save_signal
from .test_models import note
from .test_models import package
from .test_models import person
from .test_models import pkg_note
from .test_models import repo
from .test_models import review
from .test_models import service
from .test_models import vulnerability

@pytest.fixture
def service(db):
user = User.objects.create(username="vcio", email="vcio@nexb.com", password="complex-password")
return Service.objects.create(user=user)


@pytest.fixture
def package(db, service):
return Package.objects.create(purl="pkg:maven/org.apache.logging", service=service)


@pytest.fixture
def person(db):
user1 = User.objects.create(username="ziad", email="ziad@nexb.com", password="complex-password")
return Person.objects.create(user=user1, summary="Hello World", public_key="PUBLIC_KEY")


@pytest.fixture
def repo(db, service, mute_post_save_signal):
"""Simple Git Repository"""
return Repository.objects.create(
url="https://github.com/nexB/fake-repo",
path="./review/tests/test_data/test_git_repo_v1",
admin=service,
)


@pytest.fixture
def vulnerability(db, repo):
return Vulnerability.objects.create(id="VCID-1155-4sem-aaaq", repo=repo)


@pytest.fixture
def note(db):
return Note.objects.create(acct="ziad@127.0.0.1:8000", content="Comment #1")


@pytest.fixture
def follow(db, package, person):
return Follow.objects.create(package=package, person=person)


@pytest.fixture
def fake_service(db):
user = User.objects.create(
username="fake_service",
email="vcio@nexb.com",
password="complex-password",
)
return Service.objects.create(user=user)


@pytest.fixture
def pkg_note(db, package):
return Note.objects.create(
acct=package.acct,
content="purl: "
"pkg:maven/org.apache.logging@2.23-r0?arch=aarch64&distroversion=edge&reponame=community\n"
" affected_by_vulnerabilities: ....",
)


@pytest.fixture
def review(db, repo, person):
return Review.objects.create(
headline="Review title 1",
author=person,
repository=repo,
filepath="/apache/httpd/VCID-1a68-fd5t-aaam.yml",
data="text diff",
commit="49d8c5fd4bea9488186a832b13ebdc83484f1b6a",
)


@pytest.mark.django_db
Expand Down Expand Up @@ -188,7 +256,7 @@ def test_person_delete_note(person, note):


@pytest.mark.django_db
def test_person_delete_note(person, note):
def test_person_delete_note2(person, note):
payload = json.dumps(
{
**AP_CONTEXT,
Expand Down
77 changes: 62 additions & 15 deletions tests/test_ap_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,29 @@
# See https://github.com/nexB/federatedcode for support or download.
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
#
import datetime
import json
from datetime import datetime
from datetime import timedelta

import pytest
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone
from fedcode_test_utils import mute_post_save_signal # NOQA
from oauth2_provider.models import AccessToken
from oauth2_provider.models import Application
from rest_framework.test import APIClient

from fedcode.activitypub import AP_CONTEXT
from fedcode.models import Follow
from fedcode.models import Note
from fedcode.models import Package
from fedcode.models import Person
from fedcode.models import Repository
from fedcode.models import Review
from fedcode.models import Service
from fedcode.models import Vulnerability
from fedcode.utils import generate_webfinger
from federatedcode.settings import AP_CONTENT_TYPE

from .test_models import follow
from .test_models import mute_post_save_signal
from .test_models import note
from .test_models import package
from .test_models import person
from .test_models import repo
from .test_models import review
from .test_models import service
from .test_models import vulnerability


def create_token(user):
app = Application.objects.create(
Expand All @@ -49,13 +41,68 @@ def create_token(user):
token = AccessToken.objects.create(
user=user,
scope="read write",
expires=datetime.now() + timedelta(seconds=500),
expires=timezone.now() + timedelta(seconds=500),
token="fake-access-key",
application=app,
)
return f"Bearer {token}"


@pytest.fixture
def service(db):
user = User.objects.create(
username="vcio",
email="vcio@nexb.com",
password="complex-password",
)
return Service.objects.create(
user=user,
)


@pytest.fixture
def package(db, service):
return Package.objects.create(purl="pkg:maven/org.apache.logging", service=service)


@pytest.fixture
def person(db):
user1 = User.objects.create(username="ziad", email="ziad@nexb.com", password="complex-password")
return Person.objects.create(user=user1, summary="Hello World", public_key="PUBLIC_KEY")


@pytest.fixture
def repo(db, service, mute_post_save_signal):
"""Simple Git Repository"""
return Repository.objects.create(
url="https://github.com/nexB/fake-repo",
path="./review/tests/test_data/test_git_repo_v1",
admin=service,
)


@pytest.fixture
def vulnerability(db, repo):
return Vulnerability.objects.create(id="VCID-1155-4sem-aaaq", repo=repo)


@pytest.fixture
def review(db, repo, person):
return Review.objects.create(
headline="Review title 1",
author=person,
repository=repo,
filepath="/apache/httpd/VCID-1a68-fd5t-aaam.yml",
data="text diff",
commit="49d8c5fd4bea9488186a832b13ebdc83484f1b6a",
)


@pytest.fixture
def note(db):
return Note.objects.create(acct="ziad@127.0.0.1:8000", content="Comment #1")


@pytest.mark.django_db
def test_get_ap_profile_user(person, service):
client = APIClient()
Expand Down Expand Up @@ -273,7 +320,7 @@ def test_post_user_outbox(person):
auth = create_token(person.user)
client.credentials(HTTP_AUTHORIZATION=auth)
path = reverse("user-outbox", args=[person.user.username])
response = client.post(
_response = client.post(
path,
{
**AP_CONTEXT,
Expand Down
30 changes: 26 additions & 4 deletions tests/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,40 @@
from unittest import mock

import pytest
from django.contrib.auth.models import User

from fedcode.activitypub import AP_CONTEXT
from fedcode.activitypub import create_activity_obj
from fedcode.models import Follow
from fedcode.models import Note
from fedcode.models import Package
from fedcode.models import Person
from fedcode.models import RemoteActor
from fedcode.models import Service
from fedcode.utils import file_data

from .test_models import package
from .test_models import person
from .test_models import remote_person
from .test_models import service

@pytest.fixture
def service(db):
user = User.objects.create(username="vcio", email="vcio@nexb.com", password="complex-password")
return Service.objects.create(user=user)


@pytest.fixture
def package(db, service):
return Package.objects.create(purl="pkg:maven/org.apache.logging", service=service)


@pytest.fixture
def person(db):
user1 = User.objects.create(username="ziad", email="ziad@nexb.com", password="complex-password")
return Person.objects.create(user=user1, summary="Hello World", public_key="PUBLIC_KEY")


@pytest.fixture
def remote_person(db):
remote_user1 = RemoteActor.objects.create(url="127.0.0.2", username="remote-ziad")
return Person.objects.create(remote_actor=remote_user1)


@mock.patch("httpx.Client")
Expand Down
28 changes: 25 additions & 3 deletions tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,37 @@
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
#
import pytest
from django.contrib.auth.models import User
from fedcode_test_utils import mute_post_save_signal # NOQA

from fedcode.importer import Importer
from fedcode.models import Note
from fedcode.models import Package
from fedcode.models import Repository
from fedcode.models import Service
from fedcode.models import Vulnerability

from .test_models import mute_post_save_signal
from .test_models import repo
from .test_models import service

@pytest.fixture
def service(db):
user = User.objects.create(
username="vcio",
email="vcio@nexb.com",
password="complex-password",
)
return Service.objects.create(
user=user,
)


@pytest.fixture
def repo(db, service, mute_post_save_signal):
"""Simple Git Repository"""
return Repository.objects.create(
url="https://github.com/nexB/fake-repo",
path="./review/tests/test_data/test_git_repo_v1",
admin=service,
)


@pytest.mark.skip(reason="Need a real git repo to test the importer")
Expand Down
Loading

0 comments on commit 9a700d6

Please sign in to comment.