From 68600e9f31ef41709bc2e483cb63999b18caf511 Mon Sep 17 00:00:00 2001 From: Josue Kouka Date: Mon, 5 Mar 2018 20:03:34 +0100 Subject: [PATCH 1/2] misc: move api's tests into dedicated file --- tests/test_api.py | 152 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_mandayejs.py | 151 +---------------------------------------------- 2 files changed, 153 insertions(+), 150 deletions(-) create mode 100644 tests/test_api.py diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..7453512 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,152 @@ +import mock +import pytest + +from django.conf import settings + +from mandayejs.mandaye.models import UserCredentials +from utils import create_user, create_credentials, get_uuid, get_user + + +def test_api_get(client, url): + response = client.get(url) + if client.session.values(): + status_code = 200 + else: + status_code = 403 + + assert response.status_code == status_code + + if status_code == 200: + assert {'login': '', 'password': ''} == response.data + + +@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") +def test_signed_api_get(client_service, url_signed): + response = client_service.get(url_signed.url) + if url_signed.orig == 'testserver': + status_code = 200 + else: + status_code = 403 + + assert response.status_code == status_code + + if status_code == 200: + assert {'login': '', 'password': ''} == response.data + + +# POST +@mock.patch('mandayejs.mandaye.api.exec_phantom') +def test_api_post(mock_phantomjs_result, client, url, payload): + + if client.session.values(): + status_code = {'success': 200, 'failure': 401} + else: + status_code = {'success': 403, 'failure': 403} + + if payload.get('name_id_content') == '12345': + response = client.post(url, data=payload, format='json') + + assert response.status_code == status_code['failure'] + + if client.session.values(): + kevin = get_user(first_name='kevin') + assert kevin.username == payload['name_id_content'] + + kevin_uuid = get_uuid(name_id=payload['name_id_content']) + assert kevin_uuid.name_id == '12345' + else: + mock_phantomjs_result.return_value = {"result": "ok"} + + response = client.post(url, data=payload, format='json') + + assert response.status_code == status_code['success'] + + if client.session.values(): + josh = get_user(username='77777') + josh_creds = UserCredentials.objects.filter(user=josh)[0] + + assert josh_creds.to_login_info()['#login'] == 'josh' + assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password' + + +@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") +@mock.patch('mandayejs.mandaye.api.exec_phantom') +def test_signed_api_post(mock_phantomjs_result, client_service, url_signed, payload): + if url_signed.orig == 'testserver': + status_code = {'success': 200, 'failure': 401} + else: + status_code = {'success': 403, 'failure': 403} + + if payload.get('name_id_content') == '12345': + response = client_service.post(url_signed.url, data=payload, format='json') + + assert response.status_code == status_code['failure'] + + if url_signed.orig == 'testserver': + kevin = get_user(first_name='kevin') + assert kevin.username == payload['name_id_content'] + + kevin_uuid = get_uuid(name_id=payload['name_id_content']) + assert kevin_uuid.name_id == '12345' + else: + mock_phantomjs_result.return_value = {"result": "ok"} + + response = client_service.post(url_signed.url, data=payload, format='json') + + assert response.status_code == status_code['success'] + + if url_signed.orig == 'testserver': + josh = get_user(username='77777') + josh_creds = UserCredentials.objects.filter(user=josh)[0] + + assert josh_creds.to_login_info()['#login'] == 'josh' + assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password' + + +# DELETE +def test_api_delete(client, url): + if client.session.values(): + status_code = {'success': 200, 'failure': 404} + else: + status_code = {'success': 403, 'failure': 403} + + kevin = get_user(first_name='kevin') + assert UserCredentials.objects.filter(user=kevin).exists() is False + response = client.delete(url, data={'name_id_content': '12345'}, format='json') + assert response.status_code == status_code['failure'] + + josh = create_user(username='77777') + create_credentials(josh, { + 'login': 'josh', + 'password': 'josh password'}) + + assert UserCredentials.objects.filter(user=josh).exists() is True + response = client.delete(url, data={'name_id_content': '77777'}, format='json') + assert response.status_code == status_code['success'] + if client.session.values(): + assert UserCredentials.objects.filter(user=josh).exists() is False + + +@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") +def test_signed_api_delete(client_service, url_signed): + if url_signed.orig == 'testserver': + status_code = {'success': 200, 'failure': 404} + else: + status_code = {'success': 403, 'failure': 403} + + kevin = get_user(first_name='kevin') + assert UserCredentials.objects.filter(user=kevin).exists() is False + response = client_service.delete(url_signed.url, data={'name_id_content': '12345'}, format='json') + assert response.status_code == status_code['failure'] + + josh = create_user(username='77777') + create_credentials(josh, { + 'login': 'josh', + 'password': 'josh password' + }) + + assert UserCredentials.objects.filter(user=josh).exists() is True + response = client_service.delete(url_signed.url, data={'name_id_content': '77777'}, format='json') + assert response.status_code == status_code['success'] + if url_signed.orig == 'testserver': + assert UserCredentials.objects.filter(user=josh).exists() is False diff --git a/tests/test_mandayejs.py b/tests/test_mandayejs.py index 0ac9259..a7c0671 100644 --- a/tests/test_mandayejs.py +++ b/tests/test_mandayejs.py @@ -16,9 +16,8 @@ from django.core.urlresolvers import reverse from mandayejs.mandaye.models import UserCredentials from mandayejs.mandaye.utils import exec_phantom, get_login_info from mandayejs.mandaye.forms import FormFactory -from mandayejs.mandaye.views import associate, post_login_do +from mandayejs.mandaye.views import post_login_do -from utils import create_user, create_credentials, get_uuid, get_user pytestmark = pytest.mark.django_db @@ -98,154 +97,6 @@ def test_command_migrate_users(mocked_idps, command): assert cred.to_login_info(decrypt=True)['#password'] == cred.to_login_info()['#username'] -# MANDAYE API - -# GET -def test_api_get(client, url): - response = client.get(url) - if client.session.values(): - status_code = 200 - else: - status_code = 403 - - assert response.status_code == status_code - - if status_code == 200: - assert {'login': '', 'password': ''} == response.data - - -@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") -def test_signed_api_get(client_service, url_signed): - response = client_service.get(url_signed.url) - if url_signed.orig == 'testserver': - status_code = 200 - else: - status_code = 403 - - assert response.status_code == status_code - - if status_code == 200: - assert {'login': '', 'password': ''} == response.data - - -# POST -@mock.patch('mandayejs.mandaye.api.exec_phantom') -def test_api_post(mock_phantomjs_result, client, url, payload): - - if client.session.values(): - status_code = {'success': 200, 'failure': 401} - else: - status_code = {'success': 403, 'failure': 403} - - if payload.get('name_id_content') == '12345': - response = client.post(url, data=payload, format='json') - - assert response.status_code == status_code['failure'] - - if client.session.values(): - kevin = get_user(first_name='kevin') - assert kevin.username == payload['name_id_content'] - - kevin_uuid = get_uuid(name_id=payload['name_id_content']) - assert kevin_uuid.name_id == '12345' - else: - mock_phantomjs_result.return_value = {"result": "ok"} - - response = client.post(url, data=payload, format='json') - - assert response.status_code == status_code['success'] - - if client.session.values(): - josh = get_user(username='77777') - josh_creds = UserCredentials.objects.filter(user=josh)[0] - - assert josh_creds.to_login_info()['#login'] == 'josh' - assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password' - - -@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") -@mock.patch('mandayejs.mandaye.api.exec_phantom') -def test_signed_api_post(mock_phantomjs_result, client_service, url_signed, payload): - if url_signed.orig == 'testserver': - status_code = {'success': 200, 'failure': 401} - else: - status_code = {'success': 403, 'failure': 403} - - if payload.get('name_id_content') == '12345': - response = client_service.post(url_signed.url, data=payload, format='json') - - assert response.status_code == status_code['failure'] - - if url_signed.orig == 'testserver': - kevin = get_user(first_name='kevin') - assert kevin.username == payload['name_id_content'] - - kevin_uuid = get_uuid(name_id=payload['name_id_content']) - assert kevin_uuid.name_id == '12345' - else: - mock_phantomjs_result.return_value = {"result": "ok"} - - response = client_service.post(url_signed.url, data=payload, format='json') - - assert response.status_code == status_code['success'] - - if url_signed.orig == 'testserver': - josh = get_user(username='77777') - josh_creds = UserCredentials.objects.filter(user=josh)[0] - - assert josh_creds.to_login_info()['#login'] == 'josh' - assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password' - - -# DELETE -def test_api_delete(client, url): - if client.session.values(): - status_code = {'success': 200, 'failure': 404} - else: - status_code = {'success': 403, 'failure': 403} - - kevin = get_user(first_name='kevin') - assert UserCredentials.objects.filter(user=kevin).exists() is False - response = client.delete(url, data={'name_id_content': '12345'}, format='json') - assert response.status_code == status_code['failure'] - - josh = create_user(username='77777') - create_credentials(josh, { - 'login': 'josh', - 'password': 'josh password'}) - - assert UserCredentials.objects.filter(user=josh).exists() is True - response = client.delete(url, data={'name_id_content': '77777'}, format='json') - assert response.status_code == status_code['success'] - if client.session.values(): - assert UserCredentials.objects.filter(user=josh).exists() is False - - -@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") -def test_signed_api_delete(client_service, url_signed): - if url_signed.orig == 'testserver': - status_code = {'success': 200, 'failure': 404} - else: - status_code = {'success': 403, 'failure': 403} - - kevin = get_user(first_name='kevin') - assert UserCredentials.objects.filter(user=kevin).exists() is False - response = client_service.delete(url_signed.url, data={'name_id_content': '12345'}, format='json') - assert response.status_code == status_code['failure'] - - josh = create_user(username='77777') - create_credentials(josh, { - 'login': 'josh', - 'password': 'josh password' - }) - - assert UserCredentials.objects.filter(user=josh).exists() is True - response = client_service.delete(url_signed.url, data={'name_id_content': '77777'}, format='json') - assert response.status_code == status_code['success'] - if url_signed.orig == 'testserver': - assert UserCredentials.objects.filter(user=josh).exists() is False - - @mock.patch('mandayejs.mandaye.utils.subprocess.Popen') @mock.patch('mandayejs.applications.Test.SITE_LOCATORS', MOCKED_SITE_LOCATORS) def test_phantom_invalid_json(mocked_popen, caplog, user_john): -- 2.11.0