1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Switch to resolve in api test fixtures to automatically resolve View's and their arguments

Also changed the signature of the post/get methods to better align with
libraries like `requests`
This commit is contained in:
Akita Noek 2016-02-18 11:19:27 -05:00
parent 7dbb69dd6d
commit 6aeaec2b76
2 changed files with 25 additions and 20 deletions

View File

@ -1,5 +1,8 @@
import pytest
from django.core.urlresolvers import resolve
from django.utils.six.moves.urllib.parse import urlparse
from awx.main.models.organization import Organization
from awx.main.models.ha import Instance
from django.contrib.auth.models import User
@ -21,13 +24,16 @@ def user():
@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')
def rf(url, data, user=None, middleware=None, **kwargs):
view, view_args, view_kwargs = resolve(urlparse(url)[2])
if 'format' not in kwargs:
kwargs['format'] = 'json'
request = APIRequestFactory().post(url, data, **kwargs)
if middleware:
middleware.process_request(request)
force_authenticate(request, user=_user)
response = view(request, pk=pk)
if user:
force_authenticate(request, user=user)
response = view(request, *view_args, **view_kwargs)
if middleware:
middleware.process_response(request, response)
return response
@ -35,13 +41,16 @@ def post():
@pytest.fixture
def get():
def rf(_cls, _user, _url, pk=None, middleware=None):
view = _cls.as_view()
request = APIRequestFactory().get(_url, format='json')
def rf(url, user=None, middleware=None, **kwargs):
view, view_args, view_kwargs = resolve(urlparse(url)[2])
if 'format' not in kwargs:
kwargs['format'] = 'json'
request = APIRequestFactory().get(url, **kwargs)
if middleware:
middleware.process_request(request)
force_authenticate(request, user=_user)
response = view(request, pk=pk)
if user:
force_authenticate(request, user=user)
response = view(request, *view_args, **view_kwargs)
if middleware:
middleware.process_response(request, response)
return response

View File

@ -1,11 +1,6 @@
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
@ -17,7 +12,7 @@ def mock_feature_enabled(feature, bypass_database=None):
@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)
response = get(url, user('admin', True))
assert response.status_code == 200
@ -31,7 +26,7 @@ def test_basic_fields(monkeypatch, organization, get, user):
aspk = activity_stream.pk
url = reverse('api:activity_stream_detail', args=(aspk,))
response = get(ActivityStreamDetail, user('admin', True), url, pk=aspk)
response = get(url, user('admin', True))
assert response.status_code == 200
assert 'related' in response.data
@ -46,8 +41,9 @@ 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'),
response = post(url,
dict(name='test-org', description='test-desc'),
u,
middleware=ActivityStreamMiddleware())
assert response.status_code == 201
@ -55,7 +51,7 @@ def test_middleware_actor_added(monkeypatch, post, get, user):
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)
response = get(url, u)
assert response.status_code == 200
assert response.data['summary_fields']['actor']['username'] == 'admin-poster'