From 5340af2335fec62b7bcedd7aa74e1e687b7b7f92 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Tue, 1 Mar 2016 09:48:58 -0500 Subject: [PATCH] updated api tests to use new get fixture format --- .../functional/api/test_fact_versions.py | 20 +++++++++++------- .../tests/functional/api/test_fact_view.py | 21 ++++++++++++------- .../tests/functional/api/test_host_detail.py | 5 +---- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/awx/main/tests/functional/api/test_fact_versions.py b/awx/main/tests/functional/api/test_fact_versions.py index b9ff345172..84a3c0c04e 100644 --- a/awx/main/tests/functional/api/test_fact_versions.py +++ b/awx/main/tests/functional/api/test_fact_versions.py @@ -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) diff --git a/awx/main/tests/functional/api/test_fact_view.py b/awx/main/tests/functional/api/test_fact_view.py index afa6361dcd..cb73ac7948 100644 --- a/awx/main/tests/functional/api/test_fact_view.py +++ b/awx/main/tests/functional/api/test_fact_view.py @@ -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) diff --git a/awx/main/tests/functional/api/test_host_detail.py b/awx/main/tests/functional/api/test_host_detail.py index 18e183f396..79213490b0 100644 --- a/awx/main/tests/functional/api/test_host_detail.py +++ b/awx/main/tests/functional/api/test_host_detail.py @@ -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']