From 0ab11e53dd3b732b7e119b44aba7f050181ded36 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Fri, 5 Feb 2016 21:38:48 -0500 Subject: [PATCH 1/3] refactoring activity_stream functional tests --- awx/main/tests/functional/conftest.py | 56 +++++++++++++++++ .../tests/functional/test_activity_streams.py | 61 +++++++++++++++++++ awx/main/tests/old/activity_stream.py | 55 ----------------- 3 files changed, 117 insertions(+), 55 deletions(-) create mode 100644 awx/main/tests/functional/conftest.py create mode 100644 awx/main/tests/functional/test_activity_streams.py delete mode 100644 awx/main/tests/old/activity_stream.py diff --git a/awx/main/tests/functional/conftest.py b/awx/main/tests/functional/conftest.py new file mode 100644 index 0000000000..7e8ae5f1a0 --- /dev/null +++ b/awx/main/tests/functional/conftest.py @@ -0,0 +1,56 @@ +import pytest + +from awx.main.models.organization import Organization +from awx.main.models.ha import Instance +from django.contrib.auth.models import User +from rest_framework.test import ( + APIRequestFactory, + force_authenticate, +) + +@pytest.fixture +def user(): + def u(name, is_superuser=False): + try: + user = User.objects.get(username=name) + except User.DoesNotExist: + user = User(username=name, is_superuser=is_superuser, password=name) + user.save() + return user + return u + +@pytest.fixture +def post(): + def rf(_cls, _user, _url, pk=None, kwargs={}, middleware=None): + view = _cls.as_view() + request = APIRequestFactory().post(_url, kwargs, format='json') + if middleware: + middleware.process_request(request) + force_authenticate(request, user=_user) + response = view(request, pk=pk) + if middleware: + middleware.process_response(request, response) + return response + return rf + +@pytest.fixture +def get(): + def rf(_cls, _user, _url, pk=None, middleware=None): + view = _cls.as_view() + request = APIRequestFactory().get(_url, format='json') + if middleware: + middleware.process_request(request) + force_authenticate(request, user=_user) + response = view(request, pk=pk) + if middleware: + middleware.process_response(request, response) + return response + return rf + +@pytest.fixture +def instance(settings): + return Instance.objects.create(uuid=settings.SYSTEM_UUID, primary=True, hostname="instance.example.org") + +@pytest.fixture +def organization(instance): + return Organization.objects.create(name="test-org", description="test-org-desc") diff --git a/awx/main/tests/functional/test_activity_streams.py b/awx/main/tests/functional/test_activity_streams.py new file mode 100644 index 0000000000..39cda25514 --- /dev/null +++ b/awx/main/tests/functional/test_activity_streams.py @@ -0,0 +1,61 @@ +import mock +import pytest + +from awx.api.views import ( + ActivityStreamList, + ActivityStreamDetail, + OrganizationList, +) +from awx.main.middleware import ActivityStreamMiddleware +from awx.main.models.activity_stream import ActivityStream +from django.core.urlresolvers import reverse + +def mock_feature_enabled(): + return True + +@mock.patch('awx.api.license.feature_enabled', new=mock_feature_enabled) +@pytest.mark.django_db +def test_get_activity_stream_list(monkeypatch, organization, get, user): + url = reverse('api:activity_stream_list') + response = get(ActivityStreamList, user('admin', True), url) + + assert response.status_code == 200 + +@mock.patch('awx.api.license.feature_enabled', new=mock_feature_enabled) +@pytest.mark.django_db +def test_basic_fields(monkeypatch, organization, get, user): + u = user('admin', True) + activity_stream = ActivityStream.objects.latest('pk') + activity_stream.actor = u + activity_stream.save() + + aspk = activity_stream.pk + url = reverse('api:activity_stream_detail', args=(aspk,)) + response = get(ActivityStreamDetail, user('admin', True), url, pk=aspk) + + assert response.status_code == 200 + assert 'related' in response.data + assert 'organization' in response.data['related'] + assert 'summary_fields' in response.data + assert 'organization' in response.data['summary_fields'] + assert response.data['summary_fields']['organization'][0]['name'] == 'test-org' + +@mock.patch('awx.api.license.feature_enabled', new=mock_feature_enabled) +@pytest.mark.django_db +def test_middleware_actor_added(monkeypatch, post, get, user): + u = user('admin-poster', True) + + url = reverse('api:organization_list') + response = post(OrganizationList, u, url, + kwargs=dict(name='test-org', description='test-desc'), + middleware=ActivityStreamMiddleware()) + assert response.status_code == 201 + + org_id = response.data['id'] + activity_stream = ActivityStream.objects.filter(organization__pk=org_id).first() + + url = reverse('api:activity_stream_detail', args=(activity_stream.pk,)) + response = get(ActivityStreamDetail, u, url, pk=activity_stream.pk) + + assert response.status_code == 200 + assert response.data['summary_fields']['actor']['username'] == 'admin-poster' diff --git a/awx/main/tests/old/activity_stream.py b/awx/main/tests/old/activity_stream.py deleted file mode 100644 index 9798695954..0000000000 --- a/awx/main/tests/old/activity_stream.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright (c) 2015 Ansible, Inc. -# All Rights Reserved. - -# Python -from django.core.urlresolvers import reverse - -# AWX -from awx.main.models import * # noqa -from awx.main.tests.base import BaseTest - -class ActivityStreamTest(BaseTest): - - def collection(self): - return reverse('api:activity_stream_list') - - def item(self, item_id): - return reverse('api:activity_stream_detail', args=(item_id,)) - - def setUp(self): - super(ActivityStreamTest, self).setUp() - self.setup_instances() - self.create_test_license_file() - # TODO: Test non-enterprise license - self.setup_users() - self.org_created = self.post(reverse('api:organization_list'), dict(name='test org', description='test descr'), expect=201, auth=self.get_super_credentials()) - - def test_get_activity_stream_list(self): - url = self.collection() - - with self.current_user(self.super_django_user): - self.options(url, expect=200) - self.head(url, expect=200) - response = self.get(url, expect=200) - self.check_pagination_and_size(response, 1, previous=None, next=None) - - def test_basic_fields(self): - item_id = ActivityStream.objects.order_by('pk')[0].pk - org_item = self.item(item_id) - - with self.current_user(self.super_django_user): - response = self.get(org_item, expect=200) - - self.assertTrue("related" in response) - self.assertTrue("organization" in response['related']) - self.assertTrue("summary_fields" in response) - self.assertTrue("organization" in response['summary_fields']) - self.assertTrue(response['summary_fields']['organization'][0]['name'] == self.org_created['name']) - - def test_changeby_user(self): - item_id = ActivityStream.objects.order_by('pk')[0].pk - org_item = self.item(item_id) - - with self.current_user(self.super_django_user): - response = self.get(org_item, expect=200) - self.assertEqual(response['summary_fields']['actor']['username'], self.super_django_user.username) From 18ea2e0201e800519547ea4d5500b7020b9a1d86 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Fri, 5 Feb 2016 21:40:01 -0500 Subject: [PATCH 2/3] default to reuse-db for testing --- Makefile | 2 +- pytest.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c5735982b9..90db6a796c 100644 --- a/Makefile +++ b/Makefile @@ -363,7 +363,7 @@ test_unit: # Run all API unit tests with coverage enabled. test_coverage: - py.test --cov=awx --cov-report=xml --junitxml=./reports/junit.xml awx/main/tests awx/api/tests awx/fact/tests + py.test --create-db --cov=awx --cov-report=xml --junitxml=./reports/junit.xml awx/main/tests awx/api/tests awx/fact/tests # Output test coverage as HTML (into htmlcov directory). coverage_html: diff --git a/pytest.ini b/pytest.ini index a679c1bdc4..90f45f0b2a 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,4 +3,4 @@ DJANGO_SETTINGS_MODULE = awx.settings.development python_paths = awx/lib/site-packages site_dirs = awx/lib/site-packages python_files = *.py -addopts = --create-db +addopts = --reuse-db From 702ce85074bd43346a562448be637f319a2c7a23 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Sat, 6 Feb 2016 08:57:44 -0500 Subject: [PATCH 3/3] change mocking behavior to be closer to the source --- awx/main/tests/functional/test_activity_streams.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/awx/main/tests/functional/test_activity_streams.py b/awx/main/tests/functional/test_activity_streams.py index 39cda25514..cf2f81c9d9 100644 --- a/awx/main/tests/functional/test_activity_streams.py +++ b/awx/main/tests/functional/test_activity_streams.py @@ -10,10 +10,10 @@ from awx.main.middleware import ActivityStreamMiddleware from awx.main.models.activity_stream import ActivityStream from django.core.urlresolvers import reverse -def mock_feature_enabled(): +def mock_feature_enabled(feature, bypass_database=None): return True -@mock.patch('awx.api.license.feature_enabled', new=mock_feature_enabled) +@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @pytest.mark.django_db def test_get_activity_stream_list(monkeypatch, organization, get, user): url = reverse('api:activity_stream_list') @@ -21,7 +21,7 @@ def test_get_activity_stream_list(monkeypatch, organization, get, user): assert response.status_code == 200 -@mock.patch('awx.api.license.feature_enabled', new=mock_feature_enabled) +@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @pytest.mark.django_db def test_basic_fields(monkeypatch, organization, get, user): u = user('admin', True) @@ -40,7 +40,7 @@ def test_basic_fields(monkeypatch, organization, get, user): assert 'organization' in response.data['summary_fields'] assert response.data['summary_fields']['organization'][0]['name'] == 'test-org' -@mock.patch('awx.api.license.feature_enabled', new=mock_feature_enabled) +@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled) @pytest.mark.django_db def test_middleware_actor_added(monkeypatch, post, get, user): u = user('admin-poster', True)