Commit ecb7bac8 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Move common test code to snf_django.lib.testing

parent 4e3030f7
......@@ -36,7 +36,7 @@ import copy
import datetime
import functools
from synnefo.util.testing import with_settings, override_settings
from snf_django.utils.testing import with_settings, override_settings
from django.test import TestCase, Client
from django.core import mail
......
......@@ -33,7 +33,7 @@
import json
from synnefo.api.tests import BaseAPITest
from snf_django.utils.testing import BaseAPITest
from synnefo.db.models import Flavor
from synnefo.db.models_factory import FlavorFactory
......
......@@ -34,7 +34,7 @@
import json
from snf_django.lib.api import faults
from synnefo.api.tests import BaseAPITest
from snf_django.utils.testing import BaseAPITest
from mock import patch
from functools import wraps
......
......@@ -34,7 +34,7 @@
import json
from mock import patch
from synnefo.api.tests import BaseAPITest
from snf_django.utils.testing import BaseAPITest
from synnefo.db.models import Network, NetworkInterface
from synnefo.db import models_factory as mfactory
......
......@@ -33,7 +33,7 @@
import json
from synnefo.api.tests import BaseAPITest
from snf_django.utils.testing import BaseAPITest
from synnefo.db.models import VirtualMachine, VirtualMachineMetadata
from synnefo.db import models_factory as mfactory
from synnefo.logic.utils import get_rsapi_state
......
# Copyright 2012-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.utils import simplejson as json
from django.test import TestCase
from snf_django.utils.testing import astakos_user
class APITest(TestCase):
def test_api_version(self):
"""Check API version."""
with astakos_user('user'):
response = self.client.get('/api/v1.1/')
self.assertEqual(response.status_code, 200)
api_version = json.loads(response.content)['version']
self.assertEqual(api_version['id'], 'v1.1')
self.assertEqual(api_version['status'], 'CURRENT')
......@@ -31,87 +31,9 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from __future__ import with_statement
from django.utils import simplejson as json
from django.test import TestCase
from mock import patch
from contextlib import contextmanager
@contextmanager
def astakos_user(user):
"""
Context manager to mock astakos response.
usage:
with astakos_user("user@user.com"):
.... make api calls ....
"""
with patch("snf_django.lib.api.get_token") as get_token:
get_token.return_value = "DummyToken"
with patch('astakosclient.AstakosClient.get_user_info') as m:
m.return_value = {"uuid": user}
yield
class BaseAPITest(TestCase):
def get(self, url, user='user', *args, **kwargs):
with astakos_user(user):
response = self.client.get(url, *args, **kwargs)
return response
def delete(self, url, user='user'):
with astakos_user(user):
response = self.client.delete(url)
return response
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs):
if ctype == 'json':
content_type = 'application/json'
with astakos_user(user):
response = self.client.post(url, params, content_type=content_type,
*args, **kwargs)
return response
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs):
if ctype == 'json':
content_type = 'application/json'
with astakos_user(user):
response = self.client.put(url, params, content_type=content_type,
*args, **kwargs)
return response
def assertSuccess(self, response):
self.assertTrue(response.status_code in [200, 203, 204])
def assertFault(self, response, status_code, name):
self.assertEqual(response.status_code, status_code)
fault = json.loads(response.content)
self.assertEqual(fault.keys(), [name])
def assertBadRequest(self, response):
self.assertFault(response, 400, 'badRequest')
def assertItemNotFound(self, response):
self.assertFault(response, 404, 'itemNotFound')
class APITest(TestCase):
def test_api_version(self):
"""Check API version."""
with astakos_user('user'):
response = self.client.get('/api/v1.1/')
self.assertEqual(response.status_code, 200)
api_version = json.loads(response.content)['version']
self.assertEqual(api_version['id'], 'v1.1')
self.assertEqual(api_version['status'], 'CURRENT')
# Import TestCases
from synnefo.api.test.servers import *
from synnefo.api.test.networks import *
from synnefo.api.test.flavors import *
from synnefo.api.test.images import *
from synnefo.api.test.versions import *
......@@ -39,6 +39,7 @@ from contextlib import contextmanager
from mock import patch
from functools import wraps
from copy import deepcopy
from snf_django.utils.testing import astakos_user, BaseAPITest
FILTERS = ('name', 'container_format', 'disk_format', 'status', 'size_min',
......@@ -58,65 +59,6 @@ UPDATE_FIELDS = ('name', 'disk_format', 'container_format', 'is_public',
'owner', 'properties', 'status')
@contextmanager
def astakos_user(user):
"""
Context manager to mock astakos response.
usage:
with astakos_user("user@user.com"):
.... make api calls ....
"""
with patch("snf_django.lib.api.get_token") as get_token:
get_token.return_value = "DummyToken"
with patch('astakosclient.AstakosClient.get_user_info') as m:
m.return_value = {"uuid": user}
yield
class BaseAPITest(TestCase):
def get(self, url, user='user', *args, **kwargs):
with astakos_user(user):
response = self.client.get(url, *args, **kwargs)
return response
def delete(self, url, user='user'):
with astakos_user(user):
response = self.client.delete(url)
return response
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs):
if ctype == 'json':
content_type = 'application/json'
with astakos_user(user):
response = self.client.post(url, params, content_type=content_type,
*args, **kwargs)
return response
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs):
if ctype == 'json':
content_type = 'application/json'
with astakos_user(user):
response = self.client.put(url, params, content_type=content_type,
*args, **kwargs)
return response
def assertSuccess(self, response):
self.assertTrue(response.status_code in [200, 203, 204])
def assertFault(self, response, status_code, name):
self.assertEqual(response.status_code, status_code)
fault = response.content
self.assertEqual(fault, name)
def assertBadRequest(self, response):
self.assertFault(response, 400, '400 Bad Request')
def assertItemNotFound(self, response):
self.assertFault(response, 404, 'itemNotFound')
DummyImages = {
'0786a349-9725-48ec-8b86-8598eefc4043':
{'checksum': u'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
......
......@@ -38,10 +38,10 @@ from string import punctuation
from urllib import unquote
from django.conf import settings
from django.http import (HttpResponse, HttpResponseNotFound,
HttpResponseBadRequest)
from django.http import HttpResponse
from snf_django.lib import api
from snf_django.lib.api import faults
from synnefo.plankton.utils import plankton_method
......@@ -233,7 +233,7 @@ def get_image_meta(request, image_id):
image = request.backend.get_image(image_id)
if not image:
return HttpResponseNotFound()
raise faults.ItemNotFound()
return _create_image_response(image)
......@@ -285,13 +285,13 @@ def list_images(request, detail=False):
try:
filters['size_max'] = int(filters['size_max'])
except ValueError:
return HttpResponseBadRequest('400 Bad Request')
raise faults.BadRequest("Malformed request.")
if 'size_min' in filters:
try:
filters['size_min'] = int(filters['size_min'])
except ValueError:
return HttpResponseBadRequest('400 Bad Request')
raise faults.BadRequest("Malformed request.")
images = request.backend.list(filters, params)
......
......@@ -33,6 +33,9 @@
from contextlib import contextmanager
from django.test import TestCase
from django.utils import simplejson as json
from mock import patch
@contextmanager
......@@ -112,3 +115,62 @@ def with_settings(settings, prefix='', **override):
return ret
return inner
return wrapper
@contextmanager
def astakos_user(user):
"""
Context manager to mock astakos response.
usage:
with astakos_user("user@user.com"):
.... make api calls ....
"""
with patch("snf_django.lib.api.get_token") as get_token:
get_token.return_value = "DummyToken"
with patch('astakosclient.AstakosClient.get_user_info') as m:
m.return_value = {"uuid": user}
yield
class BaseAPITest(TestCase):
def get(self, url, user='user', *args, **kwargs):
with astakos_user(user):
response = self.client.get(url, *args, **kwargs)
return response
def delete(self, url, user='user'):
with astakos_user(user):
response = self.client.delete(url)
return response
def post(self, url, user='user', params={}, ctype='json', *args, **kwargs):
if ctype == 'json':
content_type = 'application/json'
with astakos_user(user):
response = self.client.post(url, params, content_type=content_type,
*args, **kwargs)
return response
def put(self, url, user='user', params={}, ctype='json', *args, **kwargs):
if ctype == 'json':
content_type = 'application/json'
with astakos_user(user):
response = self.client.put(url, params, content_type=content_type,
*args, **kwargs)
return response
def assertSuccess(self, response):
self.assertTrue(response.status_code in [200, 203, 204])
def assertFault(self, response, status_code, name):
self.assertEqual(response.status_code, status_code)
fault = json.loads(response.content)
self.assertEqual(fault.keys(), [name])
def assertBadRequest(self, response):
self.assertFault(response, 400, 'badRequest')
def assertItemNotFound(self, response):
self.assertFault(response, 404, 'itemNotFound')
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