diff --git a/tests/conftest.py b/tests/conftest.py index 9b345233e1b1a2feb570ce62dff7951625706bbd..e9c3f605586d58234a7bc65dd3ec3ecba79a1953 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,15 @@ import pytest +from pytest_factoryboy import register from django_elasticsearch_dsl.test import ESTestCase +import json + +from openlobby.core.auth import create_access_token + +from . import factories + +register(factories.UserFactory) +register(factories.AuthorFactory) +register(factories.ReportFactory) class DummyTestCase: @@ -38,3 +48,30 @@ def issuer(request): if url is None: pytest.skip("Missing OpenID Provider URL.") return url + + +@pytest.fixture +def author_fix(author_factory): + return author_factory( + id=42, + username="wolfe", + first_name="Winston", + last_name="Wolfe", + extra={"movies": 1}, + ) + + +@pytest.fixture +def call_api(client): + def _call_api(query, input=None, user=None): + variables = json.dumps({"input": input or {}}) + payload = {"query": query, "variables": variables} + if user is None: + res = client.post("/graphql", payload) + else: + token = create_access_token(user.username) + auth_header = "Bearer {}".format(token) + res = client.post("/graphql", payload, HTTP_AUTHORIZATION=auth_header) + return res.json() + + return _call_api diff --git a/tests/factories.py b/tests/factories.py new file mode 100644 index 0000000000000000000000000000000000000000..c6075eddfb99b2328cf871436a2127834a81ca68 --- /dev/null +++ b/tests/factories.py @@ -0,0 +1,37 @@ +from factory import DjangoModelFactory, Faker, SubFactory +from django.conf import settings + +from openlobby.core.models import Report + + +class UserFactory(DjangoModelFactory): + class Meta: + model = settings.AUTH_USER_MODEL + + username = Faker("user_name") + first_name = Faker("first_name") + last_name = Faker("last_name") + + +class AuthorFactory(DjangoModelFactory): + class Meta: + model = settings.AUTH_USER_MODEL + + username = Faker("user_name") + first_name = Faker("first_name") + last_name = Faker("last_name") + is_author = True + + +class ReportFactory(DjangoModelFactory): + class Meta: + model = Report + + author = SubFactory(AuthorFactory) + date = Faker("past_datetime") + title = Faker("sentence") + body = Faker("sentence") + received_benefit = Faker("word") + provided_benefit = Faker("word") + our_participants = Faker("name") + other_participants = Faker("name")