Clean authorization mocking in tests

Remove remaining 'execute_wo_tests' as now authorization is
mocked in all tests by default.
Changed mocking from 'auth_client' to 'auth_required' to make
it simple to change in tests (now it's just a matter of changing
boolean).

Change-Id: Icfd0ef3058918e1578a21429f84a1583eddd3a8e
Closes-Bug: #1443279
This commit is contained in:
Sebastian Kalinowski
2015-04-13 09:30:10 +02:00
parent 9208ff4a08
commit ff186de358
7 changed files with 27 additions and 26 deletions

View File

@@ -71,24 +71,21 @@ class UnitTestCase(TestCase):
def setUp(self):
"""Mocks keystone authentication."""
self.mauth_client_patcher = mock.patch(
'fuelclient.client.auth_client')
self.mauth_client_patcher.start()
self.auth_required_patcher = mock.patch(
'fuelclient.client.Client.auth_required',
new_callable=mock.PropertyMock
)
self.auth_required_mock = self.auth_required_patcher.start()
self.auth_required_mock.return_value = False
super(UnitTestCase, self).setUp()
def tearDown(self):
super(UnitTestCase, self).tearDown()
self.mauth_client_patcher.stop()
self.auth_required_patcher.stop()
def execute(self, command):
return main(command)
def execute_wo_auth(self, command):
with mock.patch('fuelclient.client.Client.auth_required',
new_callable=mock.PropertyMock) as auth:
auth.return_value = False
return self.execute(command)
def mock_open(self, text, filename='some.file'):
"""Mocks builtin open function.
Usage example:

View File

@@ -44,7 +44,7 @@ class BaseSettings(base.UnitTestCase):
def check_upload_action(self, mrequests, test_command, test_url):
m = mock_open(read_data=YAML_SETTINGS_DATA)
with patch('__builtin__.open', m, create=True):
self.execute_wo_auth(test_command)
self.execute(test_command)
request = mrequests.put.call_args_list[0]
url = request[0][0]
@@ -62,7 +62,7 @@ class BaseSettings(base.UnitTestCase):
mrequests.get.return_value = mresponse
with patch('__builtin__.open', m, create=True):
self.execute_wo_auth(test_command)
self.execute(test_command)
request = mrequests.get.call_args_list[0]
url = request[0][0]
@@ -78,7 +78,7 @@ class BaseSettings(base.UnitTestCase):
mrequests.get.return_value = mresponse
with patch('__builtin__.open', m, create=True):
self.execute_wo_auth(test_command)
self.execute(test_command)
request = mrequests.get.call_args_list[0]
url = request[0][0]

View File

@@ -35,7 +35,7 @@ class TestClusterAttributesActions(base.UnitTestCase):
def test_attributes_download(self, mos, mopen, mrequests):
mrequests.get().json.return_value = self._input
self.execute_wo_auth(
self.execute(
['fuel', 'env', '--env', '1', '--attributes', '--download'])
url = mrequests.get.call_args[0][0]
@@ -45,7 +45,7 @@ class TestClusterAttributesActions(base.UnitTestCase):
def test_attributes_upload(self, mos, mopen, mrequests):
mopen().__enter__().read.return_value = self._output
self.execute_wo_auth(
self.execute(
['fuel', 'env', '--env', '1', '--attributes', '--upload'])
self.assertEqual(mrequests.put.call_count, 1)

View File

@@ -24,6 +24,10 @@ from fuelclient.tests import base
class TestAuthentication(base.UnitTestCase):
def setUp(self):
super(TestAuthentication, self).setUp()
self.auth_required_mock.return_value = True
def validate_credentials_response(self,
args,
username=None,

View File

@@ -111,7 +111,7 @@ class TestFuelVersion(base_tests.UnitTestCase):
with mock.patch('sys.stderr') as mstderr:
with self.assertRaises(SystemExit):
self.execute_wo_auth(['fuel', '--fuel-version', '--yaml'])
self.execute(['fuel', '--fuel-version', '--yaml'])
args, _ = mstderr.write.call_args
with self.assertRaisesRegexp(
ValueError, 'No JSON object could be decoded'):
@@ -123,6 +123,6 @@ class TestFuelVersion(base_tests.UnitTestCase):
with mock.patch('sys.stderr') as mstderr:
with self.assertRaises(SystemExit):
self.execute_wo_auth(['fuel', '--fuel-version', '--json'])
self.execute(['fuel', '--fuel-version', '--json'])
args, _ = mstderr.write.call_args
json.loads(args[0])

View File

@@ -36,13 +36,13 @@ class TestReleaseDeploymentTasksActions(base.UnitTestCase):
def test_release_tasks_download(self, mos, mopen, mrequests):
mrequests.get().json.return_value = API_INPUT
self.execute_wo_auth(
self.execute(
['fuel', 'rel', '--rel', '1', '--deployment-tasks', '--download'])
mopen().__enter__().write.assert_called_once_with(API_OUTPUT)
def test_release_tasks_upload(self, mos, mopen, mrequests):
mopen().__enter__().read.return_value = API_OUTPUT
self.execute_wo_auth(
self.execute(
['fuel', 'rel', '--rel', '1', '--deployment-tasks', '--upload'])
self.assertEqual(mrequests.put.call_count, 1)
call_args = mrequests.put.call_args_list[0]
@@ -60,13 +60,13 @@ class TestClusterDeploymentTasksActions(base.UnitTestCase):
def test_cluster_tasks_download(self, mos, mopen, mrequests):
mrequests.get().json.return_value = API_INPUT
self.execute_wo_auth(
self.execute(
['fuel', 'env', '--env', '1', '--deployment-tasks', '--download'])
mopen().__enter__().write.assert_called_once_with(API_OUTPUT)
def test_cluster_tasks_upload(self, mos, mopen, mrequests):
mopen().__enter__().read.return_value = API_OUTPUT
self.execute_wo_auth(
self.execute(
['fuel', 'env', '--env', '1', '--deployment-tasks', '--upload'])
self.assertEqual(mrequests.put.call_count, 1)
call_args = mrequests.put.call_args_list[0]
@@ -87,7 +87,7 @@ class TestSyncDeploymentTasks(base.UnitTestCase):
mfiles.return_value = ['/etc/puppet/2014.2-6.0/tasks.yaml']
mopen().__enter__().read.return_value = API_OUTPUT
file_pattern = '*tests*'
self.execute_wo_auth(
self.execute(
['fuel', 'rel', '--sync-deployment-tasks', '--fp', file_pattern])
mfiles.assert_called_once_with(
@@ -105,7 +105,7 @@ class TestSyncDeploymentTasks(base.UnitTestCase):
mfiles.return_value = ['/etc/puppet/2014.2-6.0/tasks.yaml']
mopen().__enter__().read.return_value = API_OUTPUT
real_path = '/etc/puppet'
self.execute_wo_auth(
self.execute(
['fuel', 'rel', '--sync-deployment-tasks', '--dir', real_path])
mfiles.assert_called_once_with(real_path, '*tasks.yaml')
@@ -115,7 +115,7 @@ class TestSyncDeploymentTasks(base.UnitTestCase):
'/etc/puppet/2014.3-6.1/tasks.yaml']
mopen().__enter__().read.return_value = API_OUTPUT
self.execute_wo_auth(
self.execute(
['fuel', 'rel', '--sync-deployment-tasks'])
self.assertEqual(mrequests.put.call_count, 1)
@@ -126,7 +126,7 @@ class TestSyncDeploymentTasks(base.UnitTestCase):
'/etc/puppet/2014.3-6.1/tasks.yaml']
mopen().__enter__().read.return_value = API_OUTPUT
self.execute_wo_auth(
self.execute(
['fuel', 'rel', '--sync-deployment-tasks'])
self.assertEqual(mrequests.put.call_count, 2)

View File

@@ -24,7 +24,7 @@ class TestParser(base.UnitTestCase):
def test_choose_only_one_format(self):
with mock.patch('sys.stderr') as mstderr:
with self.assertRaises(SystemExit):
self.execute_wo_auth(['fuel', '--json', '--yaml'])
self.execute(['fuel', '--json', '--yaml'])
args, _ = mstderr.write.call_args
self.assertRegexpMatches(
args[0],