diff --git a/openlobby/core/api/schema.py b/openlobby/core/api/schema.py index ffeaa6a22e1fb6fa448b1092bd509f592f5275cf..4bd463243cc9c44ea140f4a45419967e6475e65e 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 e5ce2a0bb7c51ec725b66439c5fbf860cec0fc84..c1345fd11c147326c7ae347906afe2aaa7938ec1 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 0000000000000000000000000000000000000000..0bc252a8acff0a2fe61a00ffb741fa200e789594 --- /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 0000000000000000000000000000000000000000..f2fab64d26084f3f31f7b5b57cf72a31ee8f8a5e --- /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())