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

updated api tests to use new get fixture format

This commit is contained in:
Chris Meyers 2016-03-01 09:48:58 -05:00
parent f6fa11e383
commit 5340af2335
3 changed files with 26 additions and 20 deletions

View File

@ -7,9 +7,6 @@ import urllib
# AWX
from awx.main.models.fact import Fact
from awx.api.views import (
HostFactVersionsList,
)
from awx.main.utils import timestamp_apiformat
# Django
@ -19,12 +16,19 @@ from django.utils import timezone
def mock_feature_enabled(feature, bypass_database=None):
return True
def build_url(*args, **kwargs):
get = kwargs.pop('get', {})
url = reverse(*args, **kwargs)
if get:
url += '?' + urllib.urlencode(get)
return url
def setup_common(hosts, fact_scans, get, user, epoch=timezone.now(), get_params={}, host_count=1):
hosts = hosts(host_count=host_count)
fact_scans(fact_scans=3, timestamp_epoch=epoch)
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
response = get(HostFactVersionsList, user('admin', True), url, pk=hosts[0].id, params=get_params)
url = build_url('api:host_fact_versions_list', args=(hosts[0].pk,), get=get_params)
response = get(url, user('admin', True))
return (hosts[0], response)
@ -50,7 +54,7 @@ def check_response_facts(facts_known, response):
def test_no_facts_db(hosts, get, user):
hosts = hosts(host_count=1)
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
response = get(HostFactVersionsList, user('admin', True), url, pk=hosts[0].id)
response = get(url, user('admin', True))
response_expected = {
'results': []
@ -81,7 +85,7 @@ def test_basic_options_fields(hosts, fact_scans, options, user):
fact_scans(fact_scans=1)
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
response = options(HostFactVersionsList, user('admin', True), url, pk=hosts[0].id)
response = options(url, user('admin', True), pk=hosts[0].id)
#import json
#print(json.dumps(response.data))
@ -192,7 +196,7 @@ def _test_user_access_control(hosts, fact_scans, get, user_obj, team_obj):
team_obj.users.add(user_obj)
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
response = get(HostFactVersionsList, user_obj, url, pk=hosts[0].id)
response = get(url, user_obj)
return response
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)

View File

@ -1,10 +1,8 @@
import mock
import pytest
import json
import urllib
from awx.api.views import (
HostFactCompareView,
)
from awx.main.utils import timestamp_apiformat
from django.core.urlresolvers import reverse
from django.utils import timezone
@ -12,6 +10,13 @@ from django.utils import timezone
def mock_feature_enabled(feature, bypass_database=None):
return True
def build_url(*args, **kwargs):
get = kwargs.pop('get', {})
url = reverse(*args, **kwargs)
if get:
url += '?' + urllib.urlencode(get)
return url
# TODO: Consider making the fact_scan() fixture a Class, instead of a function, and move this method into it
def find_fact(facts, host_id, module_name, timestamp):
for f in facts:
@ -23,8 +28,8 @@ def setup_common(hosts, fact_scans, get, user, epoch=timezone.now(), module_name
hosts = hosts(host_count=1)
facts = fact_scans(fact_scans=1, timestamp_epoch=epoch)
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
response = get(HostFactCompareView, user('admin', True), url, pk=hosts[0].id, params=get_params)
url = build_url('api:host_fact_compare_view', args=(hosts[0].pk,), get=get_params)
response = get(url, user('admin', True), params=get_params)
fact_known = find_fact(facts, hosts[0].id, module_name, epoch)
return (fact_known, response)
@ -34,7 +39,7 @@ def setup_common(hosts, fact_scans, get, user, epoch=timezone.now(), module_name
def test_no_fact_found(hosts, get, user):
hosts = hosts(host_count=1)
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
response = get(HostFactCompareView, user('admin', True), url, pk=hosts[0].id)
response = get(url, user('admin', True))
expected_response = {
"detail": "Fact not found"
@ -49,7 +54,7 @@ def test_basic_fields(hosts, fact_scans, get, user):
fact_scans(fact_scans=1)
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
response = get(HostFactCompareView, user('admin', True), url, pk=hosts[0].id)
response = get(url, user('admin', True))
assert 'related' in response.data
assert 'id' in response.data
@ -111,7 +116,7 @@ def _test_user_access_control(hosts, fact_scans, get, user_obj, team_obj):
team_obj.users.add(user_obj)
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
response = get(HostFactCompareView, user_obj, url, pk=hosts[0].id)
response = get(url, user_obj)
return response
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)

View File

@ -2,9 +2,6 @@
# Other host tests should live here to make this test suite more complete.
import pytest
from awx.api.views import (
HostDetail,
)
from django.core.urlresolvers import reverse
@pytest.mark.django_db
@ -12,7 +9,7 @@ def test_basic_fields(hosts, fact_scans, get, user):
hosts = hosts(host_count=1)
url = reverse('api:host_detail', args=(hosts[0].pk,))
response = get(HostDetail, user('admin', True), url, pk=hosts[0].id)
response = get(url, user('admin', True))
assert 'related' in response.data
assert 'fact_versions' in response.data['related']