diff --git a/tests/dummy.py b/tests/dummy.py index c1345fd11c147326c7ae347906afe2aaa7938ec1..ddd0fe018823076ad5c5201d44aa2b5e812d18c7 100644 --- a/tests/dummy.py +++ b/tests/dummy.py @@ -6,7 +6,7 @@ from openlobby.core.models import Report, User authors = [ { 'id': 1, - 'username': 'Wolf', + 'username': 'wolf', 'first_name': 'Winston', 'last_name': 'Wolfe', 'is_author': True, @@ -99,3 +99,7 @@ def prepare_reports(): Report.objects.create(author=author1, **reports[2]) Report.objects.create(author=author1, **reports[3]) Report.objects.create(author=author3, **reports[4]) + + +def prepare_author(): + User.objects.create(**authors[0]) diff --git a/tests/mutations/snapshots/snap_test_create_report.py b/tests/mutations/snapshots/snap_test_create_report.py index 5d774db5884c4cb35f494597e53db2aaee0095e0..96c83fc9a80e5061eba62b2641a38ee1bee7ccba 100644 --- a/tests/mutations/snapshots/snap_test_create_report.py +++ b/tests/mutations/snapshots/snap_test_create_report.py @@ -29,7 +29,7 @@ snapshots['test_full_report 1'] = { 'createReport': { 'report': { 'author': { - 'extra': None, + 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', 'lastName': 'Wolfe' @@ -54,7 +54,7 @@ snapshots['test_is_draft 1'] = { 'createReport': { 'report': { 'author': { - 'extra': None, + 'extra': '{"movies": 1}', 'firstName': 'Winston', 'id': 'QXV0aG9yOjE=', 'lastName': 'Wolfe' diff --git a/tests/mutations/test_create_report.py b/tests/mutations/test_create_report.py index 433a72babe837cee6e8354d1d5e7932ef3eaa1e6..9552ee23f6ada4212413414fe6a82f0f2cb21ce1 100644 --- a/tests/mutations/test_create_report.py +++ b/tests/mutations/test_create_report.py @@ -2,8 +2,9 @@ import pytest import arrow import re -from openlobby.core.models import User, Report +from openlobby.core.models import Report +from ..dummy import prepare_author from ..utils import call_api @@ -12,8 +13,7 @@ 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') + prepare_author() def test_unauthorized(client, snapshot): @@ -31,9 +31,7 @@ def test_unauthorized(client, snapshot): 'body': 'I told you!', 'date': arrow.utcnow().isoformat(), } - response = call_api(client, query, input) - snapshot.assert_match(response) @@ -79,7 +77,7 @@ def test_full_report(client, snapshot): 'date': date.isoformat(), } - response = call_api(client, query, input, 'wolfe') + response = call_api(client, query, input, 'wolf') # published date is set on save, changing between test runs, so we strip it # from snapshot @@ -127,7 +125,7 @@ def test_input_sanitization(client): 'date': arrow.utcnow().isoformat(), } - call_api(client, query, input, 'wolfe') + call_api(client, query, input, 'wolf') report = Report.objects.get() assert report.title == 'No tags' @@ -181,7 +179,7 @@ def test_is_draft(client, snapshot): 'isDraft': True, } - response = call_api(client, query, input, 'wolfe') + response = call_api(client, query, input, 'wolf') # published date is set on save, changing between test runs, so we strip it # from snapshot diff --git a/tests/mutations/test_login.py b/tests/mutations/test_login.py index aa489ba73a9fb51f39dbf6069cfda63eef0db174..4ea608c5ec2b6808a4b5365d86fba6c81902bd88 100644 --- a/tests/mutations/test_login.py +++ b/tests/mutations/test_login.py @@ -7,6 +7,8 @@ from unittest.mock import patch from openlobby.core.models import OpenIdClient, LoginAttempt from openlobby.core.openid import register_client +from ..utils import call_api + pytestmark = pytest.mark.django_db @@ -32,18 +34,18 @@ def test_login__known_openid_client(issuer, client, snapshot): app_redirect_uri = 'http://i.am.pirate' openid_uid = 'wolf@openid.provider' + query = """ + mutation {{ + login (input: {{ openidUid: "{uid}", redirectUri: "{uri}" }}) {{ + authorizationUrl + }} + }} + """.format(uid=openid_uid, uri=app_redirect_uri) # Keycloak server used for tests does not support issuer discovery by UID, so we mock it with patch('openlobby.core.api.mutations.discover_issuer', return_value=issuer) as mock: - res = client.post('/graphql', {'query': """ - mutation {{ - login (input: {{ openidUid: "{uid}", redirectUri: "{uri}" }}) {{ - authorizationUrl - }} - }} - """.format(uid=openid_uid, uri=app_redirect_uri)}) + response = call_api(client, query) mock.assert_called_once_with(openid_uid) - response = res.json() assert 'errors' not in response authorization_url = response['data']['login']['authorizationUrl'] @@ -57,18 +59,18 @@ def test_login__known_openid_client(issuer, client, snapshot): def test_login__new_openid_client(issuer, client, snapshot): app_redirect_uri = 'http://i.am.pirate' openid_uid = 'wolf@openid.provider' + query = """ + mutation {{ + login (input: {{ openidUid: "{uid}", redirectUri: "{uri}" }}) {{ + authorizationUrl + }} + }} + """.format(uid=openid_uid, uri=app_redirect_uri) # Keycloak server used for tests does not support issuer discovery by UID, so we mock it with patch('openlobby.core.api.mutations.discover_issuer', return_value=issuer) as mock: - res = client.post('/graphql', {'query': """ - mutation {{ - login (input: {{ openidUid: "{uid}", redirectUri: "{uri}" }}) {{ - authorizationUrl - }} - }} - """.format(uid=openid_uid, uri=app_redirect_uri)}) + response = call_api(client, query) mock.assert_called_once_with(openid_uid) - response = res.json() assert 'errors' not in response authorization_url = response['data']['login']['authorizationUrl'] diff --git a/tests/mutations/test_login_by_shortcut.py b/tests/mutations/test_login_by_shortcut.py index e26a320dce10051b87db1e2c9f0704845099cff7..8e9dee4acf9df7481b36fc01e5f4871478db5519 100644 --- a/tests/mutations/test_login_by_shortcut.py +++ b/tests/mutations/test_login_by_shortcut.py @@ -5,6 +5,7 @@ from openlobby.core.models import OpenIdClient, LoginAttempt from openlobby.core.openid import register_client from .test_login import check_authorization_url +from ..utils import call_api pytestmark = pytest.mark.django_db @@ -15,14 +16,15 @@ def test_login_by_shortcut(issuer, client, snapshot): issuer=issuer, client_id=oc.client_id, client_secret=oc.client_secret) app_redirect_uri = 'http://i.am.pirate' - res = client.post('/graphql', {'query': """ + query = """ mutation {{ loginByShortcut (input: {{ shortcutId: "{id}", redirectUri: "{uri}" }}) {{ authorizationUrl }} }} - """.format(id=to_global_id('LoginShortcut', oid_client.id), uri=app_redirect_uri)}) - response = res.json() + """.format(id=to_global_id('LoginShortcut', oid_client.id), uri=app_redirect_uri) + response = call_api(client, query) + assert 'errors' not in response authorization_url = response['data']['loginByShortcut']['authorizationUrl'] diff --git a/tests/schema/test_authors.py b/tests/schema/test_authors.py index 9de53bcc0574fe7514d83d0913316ccaac636d75..c0475ffe0f190b1202b597279385bd19f720264d 100644 --- a/tests/schema/test_authors.py +++ b/tests/schema/test_authors.py @@ -3,6 +3,7 @@ import pytest from openlobby.core.models import User from ..dummy import prepare_reports +from ..utils import call_api pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] @@ -11,7 +12,7 @@ pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] def test_all(client, snapshot): prepare_reports() User.objects.create(id=4, is_author=False, username='x') - res = client.post('/graphql', {'query': """ + query = """ query { authors { totalCount @@ -34,13 +35,14 @@ def test_all(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_first(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { authors (first: 2) { totalCount @@ -63,13 +65,14 @@ def test_first(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_first_after(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { authors (first: 1, after: "MQ==") { totalCount @@ -92,13 +95,14 @@ def test_first_after(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_last(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { authors (last: 2) { totalCount @@ -121,13 +125,14 @@ def test_last(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_last_before(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { authors (last: 1, before: "Mw==") { totalCount @@ -150,13 +155,14 @@ def test_last_before(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_with_reports(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { authors { edges { @@ -189,5 +195,6 @@ def test_with_reports(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) diff --git a/tests/schema/test_login_shortcuts.py b/tests/schema/test_login_shortcuts.py index 92f44fc4845c1429d155d2219bba100baf02166a..2b0ad968eb6ccffe8a6da89c099ac21cb2fc3174 100644 --- a/tests/schema/test_login_shortcuts.py +++ b/tests/schema/test_login_shortcuts.py @@ -2,6 +2,8 @@ import pytest from openlobby.core.models import OpenIdClient +from ..utils import call_api + pytestmark = pytest.mark.django_db @@ -9,25 +11,27 @@ pytestmark = pytest.mark.django_db def test_returns_only_shortcuts(client, snapshot): OpenIdClient.objects.create(id=10, name='foo', issuer='foo') OpenIdClient.objects.create(id=20, name='bar', issuer='bar', is_shortcut=True) - res = client.post('/graphql', {'query': """ + query = """ query { loginShortcuts { id name } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_none(client, snapshot): OpenIdClient.objects.create(id=10, name='foo') - res = client.post('/graphql', {'query': """ + query = """ query { loginShortcuts { id name } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) diff --git a/tests/schema/test_node.py b/tests/schema/test_node.py index 0ffa6374ee85f373a1d7604039833f6d22805c39..383b4a34c948f93f50622e31ee43c296b16b0858 100644 --- a/tests/schema/test_node.py +++ b/tests/schema/test_node.py @@ -1,7 +1,6 @@ import pytest from graphql_relay import to_global_id -from openlobby.core.auth import create_access_token from openlobby.core.models import OpenIdClient, User from ..dummy import prepare_reports @@ -13,7 +12,7 @@ pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] def test_login_shortcut(client, snapshot): OpenIdClient.objects.create(id=10, name='foo', issuer='foo', is_shortcut=True) - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on LoginShortcut {{ @@ -22,13 +21,14 @@ def test_login_shortcut(client, snapshot): }} }} }} - """.format(id=to_global_id('LoginShortcut', 10))}) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('LoginShortcut', 10)) + response = call_api(client, query) + snapshot.assert_match(response) def test_author(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on Author {{ @@ -41,13 +41,14 @@ def test_author(client, snapshot): }} }} }} - """.format(id=to_global_id('Author', 1))}) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('Author', 1)) + response = call_api(client, query) + snapshot.assert_match(response) def test_author__returns_only_if_is_author(client, snapshot): User.objects.create(id=7, is_author=False) - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on Author {{ @@ -55,13 +56,14 @@ def test_author__returns_only_if_is_author(client, snapshot): }} }} }} - """.format(id=to_global_id('Author', 7))}) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('Author', 7)) + response = call_api(client, query) + snapshot.assert_match(response) def test_report(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on Report {{ @@ -86,8 +88,9 @@ def test_report(client, snapshot): }} }} }} - """.format(id=to_global_id('Report', 1))}) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('Report', 1)) + response = call_api(client, query) + snapshot.assert_match(response) def test_report__is_draft(client, snapshot): @@ -102,7 +105,8 @@ def test_report__is_draft(client, snapshot): }} }} """.format(id=to_global_id('Report', 4)) - snapshot.assert_match(call_api(client, query, username='Wolf')) + response = call_api(client, query, username='wolf') + snapshot.assert_match(response) def test_report__is_draft__unauthorized_viewer(client, snapshot): @@ -117,7 +121,8 @@ def test_report__is_draft__unauthorized_viewer(client, snapshot): }} }} """.format(id=to_global_id('Report', 4)) - snapshot.assert_match(call_api(client, query)) + response = call_api(client, query) + snapshot.assert_match(response) def test_report__is_draft__viewer_is_not_author(client, snapshot): @@ -132,13 +137,14 @@ def test_report__is_draft__viewer_is_not_author(client, snapshot): }} }} """.format(id=to_global_id('Report', 4)) - snapshot.assert_match(call_api(client, query)) + response = call_api(client, query, username='spongebob') + snapshot.assert_match(response) def test_user__unauthorized(client, snapshot): User.objects.create(id=8, username='albert', openid_uid='albert@einstein.id', first_name='Albert', last_name='Einstein', extra={'e': 'mc2'}) - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on User {{ @@ -152,8 +158,9 @@ def test_user__unauthorized(client, snapshot): }} }} }} - """.format(id=to_global_id('User', 8))}) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('User', 8)) + response = call_api(client, query) + snapshot.assert_match(response) def test_user__not_a_viewer(client, snapshot): @@ -161,8 +168,7 @@ def test_user__not_a_viewer(client, snapshot): first_name='Albert', last_name='Einstein', extra={'e': 'mc2'}) User.objects.create(id=2, username='isaac', openid_uid='isaac@newton.id', first_name='Isaac', last_name='Newton', extra={'apple': 'hit'}) - auth_header = 'Bearer {}'.format(create_access_token('isaac')) - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on User {{ @@ -176,15 +182,15 @@ def test_user__not_a_viewer(client, snapshot): }} }} }} - """.format(id=to_global_id('User', 8))}, HTTP_AUTHORIZATION=auth_header) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('User', 8)) + response = call_api(client, query, username='isaac') + snapshot.assert_match(response) def test_user(client, snapshot): User.objects.create(id=8, username='albert', openid_uid='albert@einstein.id', first_name='Albert', last_name='Einstein', extra={'e': 'mc2'}) - auth_header = 'Bearer {}'.format(create_access_token('albert')) - res = client.post('/graphql', {'query': """ + query = """ query {{ node (id:"{id}") {{ ... on User {{ @@ -198,5 +204,6 @@ def test_user(client, snapshot): }} }} }} - """.format(id=to_global_id('User', 8))}, HTTP_AUTHORIZATION=auth_header) - snapshot.assert_match(res.json()) + """.format(id=to_global_id('User', 8)) + response = call_api(client, query, username='albert') + snapshot.assert_match(response) diff --git a/tests/schema/test_report_drafts.py b/tests/schema/test_report_drafts.py index f2fab64d26084f3f31f7b5b57cf72a31ee8f8a5e..18a87b97ef270453798d3a49d04a4e3596818e86 100644 --- a/tests/schema/test_report_drafts.py +++ b/tests/schema/test_report_drafts.py @@ -1,8 +1,7 @@ import pytest -from openlobby.core.auth import create_access_token - from ..dummy import prepare_reports +from ..utils import call_api pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] @@ -10,21 +9,20 @@ pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] def test_unauthenticated(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { reportDrafts { id } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_authenticated(client, snapshot): prepare_reports() - token = create_access_token('Wolf') - auth_header = 'Bearer {}'.format(token) - res = client.post('/graphql', {'query': """ + query = """ query { reportDrafts { id @@ -38,5 +36,6 @@ def test_authenticated(client, snapshot): otherParticipants } } - """}, HTTP_AUTHORIZATION=auth_header) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query, username='wolf') + snapshot.assert_match(response) diff --git a/tests/schema/test_search_reports.py b/tests/schema/test_search_reports.py index f62bc2353fbafb877019764b37453cc82f72006c..83d7c0ac6eef82a4fbcc9a55bb9494f422ef0b8a 100644 --- a/tests/schema/test_search_reports.py +++ b/tests/schema/test_search_reports.py @@ -1,6 +1,7 @@ import pytest from ..dummy import prepare_reports +from ..utils import call_api pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] @@ -8,7 +9,7 @@ pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')] def test_all(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports { totalCount @@ -43,13 +44,14 @@ def test_all(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_query(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports (query: "towers") { totalCount @@ -78,13 +80,14 @@ def test_query(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_highlight(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports (query: "Ring", highlight: true) { totalCount @@ -113,13 +116,14 @@ def test_highlight(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_first(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports (first: 1) { totalCount @@ -138,13 +142,14 @@ def test_first(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_first_after(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports (first: 1, after: "MQ==") { totalCount @@ -163,13 +168,14 @@ def test_first_after(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_last(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports (last: 2) { totalCount @@ -188,13 +194,14 @@ def test_last(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_last_before(client, snapshot): prepare_reports() - res = client.post('/graphql', {'query': """ + query = """ query { searchReports (last: 1, before: "Mw==") { totalCount @@ -213,5 +220,6 @@ def test_last_before(client, snapshot): } } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) diff --git a/tests/schema/test_viewer.py b/tests/schema/test_viewer.py index b5c08977d3d7af7d0942a4be4a44005cc112c851..3895debd1ea53ba632191471810acc7f99348912 100644 --- a/tests/schema/test_viewer.py +++ b/tests/schema/test_viewer.py @@ -3,32 +3,33 @@ import pytest from openlobby.core.auth import create_access_token from openlobby.core.models import User +from ..utils import call_api + pytestmark = pytest.mark.django_db @pytest.fixture(autouse=True) def setup(): - User.objects.create(id=1, is_author=True, username='wolfe', openid_uid='TheWolf', + User.objects.create(id=1, is_author=True, username='wolf', openid_uid='TheWolf', first_name='Winston', last_name='Wolfe', email='winston@wolfe.com', extra={'caliber': 45}) def test_unauthenticated(client, snapshot): - res = client.post('/graphql', {'query': """ + query = """ query { viewer { id } } - """}) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query) + snapshot.assert_match(response) def test_authenticated(client, snapshot): - token = create_access_token('wolfe') - auth_header = 'Bearer {}'.format(token) - res = client.post('/graphql', {'query': """ + query = """ query { viewer { id @@ -41,8 +42,9 @@ def test_authenticated(client, snapshot): extra } } - """}, HTTP_AUTHORIZATION=auth_header) - snapshot.assert_match(res.json()) + """ + response = call_api(client, query, username='wolf') + snapshot.assert_match(response) # integration tests of wrong authentication