mirror of
https://github.com/ansible/awx.git
synced 2026-03-20 18:37:39 -02:30
updated api tests to use new get fixture format
This commit is contained in:
@@ -7,9 +7,6 @@ import urllib
|
|||||||
|
|
||||||
# AWX
|
# AWX
|
||||||
from awx.main.models.fact import Fact
|
from awx.main.models.fact import Fact
|
||||||
from awx.api.views import (
|
|
||||||
HostFactVersionsList,
|
|
||||||
)
|
|
||||||
from awx.main.utils import timestamp_apiformat
|
from awx.main.utils import timestamp_apiformat
|
||||||
|
|
||||||
# Django
|
# Django
|
||||||
@@ -19,12 +16,19 @@ from django.utils import timezone
|
|||||||
def mock_feature_enabled(feature, bypass_database=None):
|
def mock_feature_enabled(feature, bypass_database=None):
|
||||||
return True
|
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):
|
def setup_common(hosts, fact_scans, get, user, epoch=timezone.now(), get_params={}, host_count=1):
|
||||||
hosts = hosts(host_count=host_count)
|
hosts = hosts(host_count=host_count)
|
||||||
fact_scans(fact_scans=3, timestamp_epoch=epoch)
|
fact_scans(fact_scans=3, timestamp_epoch=epoch)
|
||||||
|
|
||||||
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
|
url = build_url('api:host_fact_versions_list', args=(hosts[0].pk,), get=get_params)
|
||||||
response = get(HostFactVersionsList, user('admin', True), url, pk=hosts[0].id, params=get_params)
|
response = get(url, user('admin', True))
|
||||||
|
|
||||||
return (hosts[0], response)
|
return (hosts[0], response)
|
||||||
|
|
||||||
@@ -50,7 +54,7 @@ def check_response_facts(facts_known, response):
|
|||||||
def test_no_facts_db(hosts, get, user):
|
def test_no_facts_db(hosts, get, user):
|
||||||
hosts = hosts(host_count=1)
|
hosts = hosts(host_count=1)
|
||||||
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
|
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 = {
|
response_expected = {
|
||||||
'results': []
|
'results': []
|
||||||
@@ -81,7 +85,7 @@ def test_basic_options_fields(hosts, fact_scans, options, user):
|
|||||||
fact_scans(fact_scans=1)
|
fact_scans(fact_scans=1)
|
||||||
|
|
||||||
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
|
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
|
#import json
|
||||||
#print(json.dumps(response.data))
|
#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)
|
team_obj.users.add(user_obj)
|
||||||
|
|
||||||
url = reverse('api:host_fact_versions_list', args=(hosts[0].pk,))
|
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
|
return response
|
||||||
|
|
||||||
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
import json
|
import json
|
||||||
|
import urllib
|
||||||
|
|
||||||
from awx.api.views import (
|
|
||||||
HostFactCompareView,
|
|
||||||
)
|
|
||||||
from awx.main.utils import timestamp_apiformat
|
from awx.main.utils import timestamp_apiformat
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@@ -12,6 +10,13 @@ from django.utils import timezone
|
|||||||
def mock_feature_enabled(feature, bypass_database=None):
|
def mock_feature_enabled(feature, bypass_database=None):
|
||||||
return True
|
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
|
# 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):
|
def find_fact(facts, host_id, module_name, timestamp):
|
||||||
for f in facts:
|
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)
|
hosts = hosts(host_count=1)
|
||||||
facts = fact_scans(fact_scans=1, timestamp_epoch=epoch)
|
facts = fact_scans(fact_scans=1, timestamp_epoch=epoch)
|
||||||
|
|
||||||
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
|
url = build_url('api:host_fact_compare_view', args=(hosts[0].pk,), get=get_params)
|
||||||
response = get(HostFactCompareView, user('admin', True), url, pk=hosts[0].id, params=get_params)
|
response = get(url, user('admin', True), params=get_params)
|
||||||
|
|
||||||
fact_known = find_fact(facts, hosts[0].id, module_name, epoch)
|
fact_known = find_fact(facts, hosts[0].id, module_name, epoch)
|
||||||
return (fact_known, response)
|
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):
|
def test_no_fact_found(hosts, get, user):
|
||||||
hosts = hosts(host_count=1)
|
hosts = hosts(host_count=1)
|
||||||
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
|
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 = {
|
expected_response = {
|
||||||
"detail": "Fact not found"
|
"detail": "Fact not found"
|
||||||
@@ -49,7 +54,7 @@ def test_basic_fields(hosts, fact_scans, get, user):
|
|||||||
fact_scans(fact_scans=1)
|
fact_scans(fact_scans=1)
|
||||||
|
|
||||||
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
|
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 'related' in response.data
|
||||||
assert 'id' 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)
|
team_obj.users.add(user_obj)
|
||||||
|
|
||||||
url = reverse('api:host_fact_compare_view', args=(hosts[0].pk,))
|
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
|
return response
|
||||||
|
|
||||||
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
@mock.patch('awx.api.views.feature_enabled', new=mock_feature_enabled)
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
# Other host tests should live here to make this test suite more complete.
|
# Other host tests should live here to make this test suite more complete.
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from awx.api.views import (
|
|
||||||
HostDetail,
|
|
||||||
)
|
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
@@ -12,7 +9,7 @@ def test_basic_fields(hosts, fact_scans, get, user):
|
|||||||
hosts = hosts(host_count=1)
|
hosts = hosts(host_count=1)
|
||||||
|
|
||||||
url = reverse('api:host_detail', args=(hosts[0].pk,))
|
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 'related' in response.data
|
||||||
assert 'fact_versions' in response.data['related']
|
assert 'fact_versions' in response.data['related']
|
||||||
|
|||||||
Reference in New Issue
Block a user