Commit 669782c8 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

helpdesk: Fix issue in tests caused by mock

Helpdesk tests patched methods of the 'logic.backend' without reverting
the patch after the tests. This resulted in module's methods to be globally
patched, affecting other tests. This commit fixes this issue by using
the 'mock.patch' context manager that handles patching within the scope
of a test.
parent 0adc25f1
......@@ -14,8 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import mock
from mock import patch
from django.test import TestCase, Client
from django.conf import settings
from django.core.urlresolvers import reverse
......@@ -104,8 +103,8 @@ def get_user_mock(request, *args, **kwargs):
}
@mock.patch("astakosclient.AstakosClient", new=AstakosClientMock)
@mock.patch("snf_django.lib.astakos.get_user", new=get_user_mock)
@patch("astakosclient.AstakosClient", new=AstakosClientMock)
@patch("snf_django.lib.astakos.get_user", new=get_user_mock)
class HelpdeskTests(TestCase):
"""
Helpdesk tests. Test correctness of permissions and returned data.
......@@ -289,8 +288,6 @@ class HelpdeskTests(TestCase):
self.assertEqual(vms.count(), 0)
def test_start_shutdown(self):
from synnefo.logic import backend
self.vm1 = mfactory.VirtualMachineFactory(userid=USER1)
pk = self.vm1.pk
......@@ -301,24 +298,25 @@ class HelpdeskTests(TestCase):
data={'token': '0001'})
self.assertEqual(r.status_code, 403)
backend.shutdown_instance = shutdown = mock.Mock()
shutdown.return_value = 1
self.vm1.operstate = 'STARTED'
self.vm1.save()
with mocked_quotaholder():
r = self.client.post(reverse('helpdesk-vm-shutdown', args=(pk,)),
data={'token': '0001'}, user_token='0001')
self.assertEqual(r.status_code, 302)
self.assertTrue(shutdown.called)
self.assertEqual(len(shutdown.mock_calls), 1)
with patch("synnefo.logic.backend.shutdown_instance") as shutdown:
shutdown.return_value = 1
with mocked_quotaholder():
r = self.client.post(
reverse('helpdesk-vm-shutdown', args=(pk,)),
data={'token': '0001'}, user_token='0001')
self.assertEqual(r.status_code, 302)
self.assertTrue(shutdown.called)
self.assertEqual(len(shutdown.mock_calls), 1)
backend.startup_instance = startup = mock.Mock()
startup.return_value = 2
self.vm1.operstate = 'STOPPED'
self.vm1.save()
with mocked_quotaholder():
r = self.client.post(reverse('helpdesk-vm-start', args=(pk,)),
data={'token': '0001'}, user_token='0001')
self.assertEqual(r.status_code, 302)
self.assertTrue(startup.called)
self.assertEqual(len(startup.mock_calls), 1)
with patch("synnefo.logic.backend.startup_instance") as startup:
startup.return_value = 2
with mocked_quotaholder():
r = self.client.post(reverse('helpdesk-vm-start', args=(pk,)),
data={'token': '0001'}, user_token='0001')
self.assertEqual(r.status_code, 302)
self.assertTrue(startup.called)
self.assertEqual(len(startup.mock_calls), 1)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment