Skip to content
Snippets Groups Projects
Commit cdfd9d5e authored by jan.bednarik's avatar jan.bednarik
Browse files

Test mutation NewReport.

parent 17f351e4
Branches
No related tags found
No related merge requests found
import arrow
import graphene import graphene
from graphene import relay from graphene import relay
from graphene.types.datetime import DateTime from graphene.types.datetime import DateTime
from graphql_relay import from_global_id from graphql_relay import from_global_id
from oic.oic import rndstr from oic.oic import rndstr
from ..documents import ReportDoc from ..models import OpenIdClient, LoginAttempt, Report
from ..models import OpenIdClient, LoginAttempt
from ..openid import ( from ..openid import (
discover_issuer, discover_issuer,
init_client_for_shortcut, init_client_for_shortcut,
register_client, register_client,
get_authorization_url, get_authorization_url,
) )
from .types import Report from . import types
from .sanitizers import strip_all_tags from .sanitizers import strip_all_tags
...@@ -112,7 +110,7 @@ class NewReport(relay.ClientIDMutation): ...@@ -112,7 +110,7 @@ class NewReport(relay.ClientIDMutation):
other_participants = graphene.String() other_participants = graphene.String()
date = DateTime(required=True) date = DateTime(required=True)
report = graphene.Field(Report) report = graphene.Field(types.Report)
@classmethod @classmethod
def mutate_and_get_payload(cls, root, info, **input): def mutate_and_get_payload(cls, root, info, **input):
...@@ -121,20 +119,18 @@ class NewReport(relay.ClientIDMutation): ...@@ -121,20 +119,18 @@ class NewReport(relay.ClientIDMutation):
author = info.context.user author = info.context.user
data = { report = Report.objects.create(
'author_id': author.id, author=author,
'published': arrow.utcnow().isoformat(), date=input.get('date'),
'title': strip_all_tags(input.get('title', '')), title=strip_all_tags(input.get('title', '')),
'body': strip_all_tags(input.get('body', '')), body=strip_all_tags(input.get('body', '')),
'received_benefit': strip_all_tags(input.get('received_benefit', '')), received_benefit=strip_all_tags(input.get('received_benefit', '')),
'provided_benefit': strip_all_tags(input.get('provided_benefit', '')), provided_benefit=strip_all_tags(input.get('provided_benefit', '')),
'our_participants': strip_all_tags(input.get('our_participants', '')), our_participants=strip_all_tags(input.get('our_participants', '')),
'other_participants': strip_all_tags(input.get('other_participants', '')), other_participants=strip_all_tags(input.get('other_participants', '')),
'date': input.get('date'), )
}
report = ReportDoc(**data) return NewReport(report=types.Report.from_db(report))
report.save(using=info.context['es'], index=info.context['index'])
return NewReport(report=Report.from_es(report, author=author))
class Mutation: class Mutation:
......
...@@ -47,6 +47,22 @@ class Report(graphene.ObjectType): ...@@ -47,6 +47,22 @@ class Report(graphene.ObjectType):
extra=report.extra, 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 @classmethod
def get_node(cls, info, id): def get_node(cls, info, id):
try: try:
......
# -*- 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'
}
}
}
}
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment