From b306a9381dd5885bdceb48cf66e859856bdc5c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Sun, 21 Oct 2018 15:43:50 +0200 Subject: [PATCH] Factories for tests and call_api fixture --- tests/conftest.py | 37 +++++++++++++++++++++++++++++++++++++ tests/factories.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/factories.py diff --git a/tests/conftest.py b/tests/conftest.py index 9b34523..e9c3f60 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 0000000..c6075ed --- /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") -- GitLab