From 47fe72cd3346154480c883534164a138a4ecd594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Mon, 26 Feb 2018 01:05:25 +0100 Subject: [PATCH] List of Report drafts in schema. --- openlobby/core/api/schema.py | 13 +++++- tests/dummy.py | 15 ++++++- .../snapshots/snap_test_report_drafts.py | 33 +++++++++++++++ tests/schema/test_report_drafts.py | 42 +++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/schema/snapshots/snap_test_report_drafts.py create mode 100644 tests/schema/test_report_drafts.py diff --git a/openlobby/core/api/schema.py b/openlobby/core/api/schema.py index ffeaa6a..4bd4632 100644 --- a/openlobby/core/api/schema.py +++ b/openlobby/core/api/schema.py @@ -7,7 +7,7 @@ from ..models import OpenIdClient from .paginator import Paginator from .sanitizers import extract_text from .. import search -from ..models import User +from ..models import User, Report class AuthorsConnection(relay.Connection): @@ -50,6 +50,10 @@ class Query: types.LoginShortcut, description='Shortcuts for login. Use with LoginByShortcut mutation.', ) + report_drafts = graphene.List( + types.Report, + description='Saved drafts of reports for Viewer.', + ) def resolve_authors(self, info, **kwargs): paginator = Paginator(**kwargs) @@ -100,3 +104,10 @@ class Query: def resolve_login_shortcuts(self, info, **kwargs): clients = OpenIdClient.objects.filter(is_shortcut=True).order_by('name') return [types.LoginShortcut.from_db(c) for c in clients] + + def resolve_report_drafts(self, info, **kwargs): + if info.context.user.is_authenticated: + drafts = Report.objects.filter(author=info.context.user, is_draft=True) + return [types.Report.from_db(d) for d in drafts] + else: + return [] diff --git a/tests/dummy.py b/tests/dummy.py index e5ce2a0..c1345fd 100644 --- a/tests/dummy.py +++ b/tests/dummy.py @@ -75,14 +75,27 @@ reports = [ 'other_participants': '', 'is_draft': True, }, + { + 'id': 5, + 'date': arrow.get(2018, 1, 12).datetime, + 'published': arrow.get(2018, 1, 13).datetime, + 'title': 'The Hobbit', + 'body': 'Work in progress...', + 'received_benefit': '', + 'provided_benefit': '', + 'our_participants': '', + 'other_participants': '', + 'is_draft': True, + }, ] def prepare_reports(): author1 = User.objects.create(**authors[0]) author2 = User.objects.create(**authors[1]) + author3 = User.objects.create(**authors[2]) Report.objects.create(author=author1, **reports[0]) Report.objects.create(author=author2, **reports[1]) Report.objects.create(author=author1, **reports[2]) Report.objects.create(author=author1, **reports[3]) - User.objects.create(**authors[2]) + Report.objects.create(author=author3, **reports[4]) diff --git a/tests/schema/snapshots/snap_test_report_drafts.py b/tests/schema/snapshots/snap_test_report_drafts.py new file mode 100644 index 0000000..0bc252a --- /dev/null +++ b/tests/schema/snapshots/snap_test_report_drafts.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# snapshottest: v1 - https://goo.gl/zC4yUc +from __future__ import unicode_literals + +from snapshottest import Snapshot + + +snapshots = Snapshot() + +snapshots['test_unauthenticated 1'] = { + 'data': { + 'reportDrafts': [ + ] + } +} + +snapshots['test_authenticated 1'] = { + 'data': { + 'reportDrafts': [ + { + 'body': 'Not finished yet.', + 'date': '2018-01-09 00:00:00+00:00', + 'id': 'UmVwb3J0OjQ=', + 'otherParticipants': '', + 'ourParticipants': '', + 'providedBenefit': '', + 'published': '2018-01-11 00:00:00+00:00', + 'receivedBenefit': '', + 'title': 'The Silmarillion' + } + ] + } +} diff --git a/tests/schema/test_report_drafts.py b/tests/schema/test_report_drafts.py new file mode 100644 index 0000000..f2fab64 --- /dev/null +++ b/tests/schema/test_report_drafts.py @@ -0,0 +1,42 @@ +import pytest + +from openlobby.core.auth import create_access_token + +from ..dummy import prepare_reports + + +pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] + + +def test_unauthenticated(client, snapshot): + prepare_reports() + res = client.post('/graphql', {'query': """ + query { + reportDrafts { + id + } + } + """}) + snapshot.assert_match(res.json()) + + +def test_authenticated(client, snapshot): + prepare_reports() + token = create_access_token('Wolf') + auth_header = 'Bearer {}'.format(token) + res = client.post('/graphql', {'query': """ + query { + reportDrafts { + id + date + published + title + body + receivedBenefit + providedBenefit + ourParticipants + otherParticipants + } + } + """}, HTTP_AUTHORIZATION=auth_header) + snapshot.assert_match(res.json()) -- GitLab