From cdfd9d5ed6c2710c02310db233e5543dbe360d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com> Date: Sat, 17 Feb 2018 22:16:23 +0100 Subject: [PATCH] Test mutation NewReport. --- openlobby/core/api/mutations.py | 34 +++--- openlobby/core/api/types.py | 16 +++ .../snapshots/snap_test_new_report.py | 50 ++++++++ tests/mutations/test_new_report.py | 110 ++++++++++++++++++ 4 files changed, 191 insertions(+), 19 deletions(-) create mode 100644 tests/mutations/snapshots/snap_test_new_report.py create mode 100644 tests/mutations/test_new_report.py diff --git a/openlobby/core/api/mutations.py b/openlobby/core/api/mutations.py index 29b1ab5..cc5181d 100644 --- a/openlobby/core/api/mutations.py +++ b/openlobby/core/api/mutations.py @@ -1,19 +1,17 @@ -import arrow import graphene from graphene import relay from graphene.types.datetime import DateTime from graphql_relay import from_global_id from oic.oic import rndstr -from ..documents import ReportDoc -from ..models import OpenIdClient, LoginAttempt +from ..models import OpenIdClient, LoginAttempt, Report from ..openid import ( discover_issuer, init_client_for_shortcut, register_client, get_authorization_url, ) -from .types import Report +from . import types from .sanitizers import strip_all_tags @@ -112,7 +110,7 @@ class NewReport(relay.ClientIDMutation): other_participants = graphene.String() date = DateTime(required=True) - report = graphene.Field(Report) + report = graphene.Field(types.Report) @classmethod def mutate_and_get_payload(cls, root, info, **input): @@ -121,20 +119,18 @@ class NewReport(relay.ClientIDMutation): author = info.context.user - data = { - 'author_id': author.id, - 'published': arrow.utcnow().isoformat(), - 'title': strip_all_tags(input.get('title', '')), - 'body': strip_all_tags(input.get('body', '')), - 'received_benefit': strip_all_tags(input.get('received_benefit', '')), - 'provided_benefit': strip_all_tags(input.get('provided_benefit', '')), - 'our_participants': strip_all_tags(input.get('our_participants', '')), - 'other_participants': strip_all_tags(input.get('other_participants', '')), - 'date': input.get('date'), - } - report = ReportDoc(**data) - report.save(using=info.context['es'], index=info.context['index']) - return NewReport(report=Report.from_es(report, author=author)) + report = Report.objects.create( + author=author, + date=input.get('date'), + title=strip_all_tags(input.get('title', '')), + body=strip_all_tags(input.get('body', '')), + received_benefit=strip_all_tags(input.get('received_benefit', '')), + provided_benefit=strip_all_tags(input.get('provided_benefit', '')), + our_participants=strip_all_tags(input.get('our_participants', '')), + other_participants=strip_all_tags(input.get('other_participants', '')), + ) + + return NewReport(report=types.Report.from_db(report)) class Mutation: diff --git a/openlobby/core/api/types.py b/openlobby/core/api/types.py index 217902d..c3e9874 100644 --- a/openlobby/core/api/types.py +++ b/openlobby/core/api/types.py @@ -47,6 +47,22 @@ class Report(graphene.ObjectType): extra=report.extra, ) + @classmethod + def from_db(cls, report): + return cls( + id=report.id, + author=report.author, + date=report.date, + published=report.published, + title=report.title, + body=report.body, + received_benefit=report.received_benefit, + provided_benefit=report.provided_benefit, + our_participants=report.our_participants, + other_participants=report.other_participants, + extra=report.extra, + ) + @classmethod def get_node(cls, info, id): try: diff --git a/tests/mutations/snapshots/snap_test_new_report.py b/tests/mutations/snapshots/snap_test_new_report.py new file mode 100644 index 0000000..f4c7c05 --- /dev/null +++ b/tests/mutations/snapshots/snap_test_new_report.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# snapshottest: v1 - https://goo.gl/zC4yUc +from __future__ import unicode_literals + +from snapshottest import Snapshot + + +snapshots = Snapshot() + +snapshots['test_unauthorized 1'] = { + 'data': { + 'newReport': None + }, + 'errors': [ + { + 'locations': [ + { + 'column': 9, + 'line': 3 + } + ], + 'message': 'User must be logged in to perform this mutation.' + } + ] +} + +snapshots['test_full_report 1'] = { + 'data': { + 'newReport': { + 'report': { + 'author': { + 'extra': None, + 'firstName': 'Winston', + 'id': 'QXV0aG9yOjE=', + 'lastName': 'Wolfe' + }, + 'body': 'I visited Tesla factory and talked with Elon Musk.', + 'date': '2018-01-01 00:00:00+00:00', + 'extra': None, + 'id': 'UmVwb3J0OjE=', + 'otherParticipants': 'Elon Musk', + 'ourParticipants': 'me', + 'providedBenefit': 'nothing', + 'published': '__STRIPPED__', + 'receivedBenefit': 'Tesla Model S', + 'title': 'Free Tesla' + } + } + } +} diff --git a/tests/mutations/test_new_report.py b/tests/mutations/test_new_report.py new file mode 100644 index 0000000..d841e56 --- /dev/null +++ b/tests/mutations/test_new_report.py @@ -0,0 +1,110 @@ +import pytest +import arrow +import json +from unittest.mock import patch + +from openlobby.core.auth import create_access_token +from openlobby.core.models import User, Report + + +pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] + + +@pytest.fixture(autouse=True) +def setup(): + User.objects.create(id=1, is_author=True, username='wolfe', + first_name='Winston', last_name='Wolfe', email='winston@wolfe.com') + + +def call_api(client, query, input, username=None): + variables = json.dumps({'input': input}) + if username is None: + res = client.post('/graphql', {'query': query, 'variables': variables}) + else: + token = create_access_token(username) + auth_header = 'Bearer {}'.format(token) + res = client.post('/graphql', {'query': query, 'variables': variables}, + HTTP_AUTHORIZATION=auth_header) + return res.json() + + +def test_unauthorized(client, snapshot): + query = """ + mutation newReport ($input: NewReportInput!) { + newReport (input: $input) { + report { + id + } + } + } + """ + input = { + 'title': 'Short Story', + 'body': 'I told you!', + 'date': arrow.utcnow().isoformat(), + } + + response = call_api(client, query, input) + + snapshot.assert_match(response) + + +def test_full_report(client, snapshot): + query = """ + mutation newReport ($input: NewReportInput!) { + newReport (input: $input) { + report { + id + date + published + title + body + receivedBenefit + providedBenefit + ourParticipants + otherParticipants + extra + author { + id + firstName + lastName + extra + } + } + } + } + """ + date = arrow.get(2018, 1, 1) + title = 'Free Tesla' + body = 'I visited Tesla factory and talked with Elon Musk.' + received_benefit = 'Tesla Model S' + provided_benefit = 'nothing' + our_participants = 'me' + other_participants = 'Elon Musk' + input = { + 'title': title, + 'body': body, + 'receivedBenefit': received_benefit, + 'providedBenefit': provided_benefit, + 'ourParticipants': our_participants, + 'otherParticipants': other_participants, + 'date': date.isoformat(), + } + + response = call_api(client, query, input, 'wolfe') + + published = response['data']['newReport']['report']['published'] + response['data']['newReport']['report']['published'] = '__STRIPPED__' + snapshot.assert_match(response) + + report = Report.objects.get() + assert report.author_id == 1 + assert report.date == date.datetime + assert report.published == arrow.get(published).datetime + assert report.title == title + assert report.body == body + assert report.received_benefit == received_benefit + assert report.provided_benefit == provided_benefit + assert report.our_participants == our_participants + assert report.other_participants == other_participants + assert report.extra is None -- GitLab