Unit tests for dashboard container views. Includes support code for

future view tests
This commit is contained in:
Mark Gius 2011-07-06 12:07:39 -07:00
parent a24f3b630d
commit ed6a6489ba
6 changed files with 215 additions and 4 deletions

2
.gitignore vendored
View File

@ -1,10 +1,12 @@
*.pyc
*.swp
django-openstack/.coverage
django-openstack/.installed.cfg
django-openstack/bin
django-openstack/develop-eggs/
django-openstack/downloads/
django-openstack/eggs/
django-openstack/htmlcov
django-openstack/launchpad
django-openstack/parts/
django-openstack/django_nova.egg-info

View File

@ -27,7 +27,6 @@ from django import template
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django import shortcuts
from django.shortcuts import render_to_response
from django_openstack import api
from django_openstack import forms
@ -74,7 +73,7 @@ def index(request, tenant_id):
containers = api.swift_get_containers()
return render_to_response('dash_containers.html', {
return shortcuts.render_to_response('dash_containers.html', {
'containers': containers,
'delete_form': delete_form,
}, context_instance=template.RequestContext(request))
@ -86,6 +85,6 @@ def create(request, tenant_id):
if handled:
return handled
return render_to_response('dash_containers_create.html', {
return shortcuts.render_to_response('dash_containers_create.html', {
'create_form': form,
}, context_instance=template.RequestContext(request))

View File

@ -38,7 +38,7 @@ INSTALLED_APPS = ['django.contrib.auth',
'django_openstack.templatetags',
'mailer',
]
ROOT_URLCONF = 'django_openstack.tests.testurls'
ROOT_URLCONF = 'django_openstack.urls'
TEMPLATE_DIRS = (
os.path.join(ROOT_PATH, 'tests', 'templates')
)

View File

@ -0,0 +1,116 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2011 Fourth Paradigm Development Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Base classes for view based unit tests.
"""
import mox
from django import http
from django import shortcuts
from django import template as django_template
from django import test
from django.conf import settings
from django.contrib.auth import models as auth_models
class Object(object):
'''Inner Object for api objects'''
pass
def fake_render_to_response(template_name, context, context_instance=None,
mimetype='text/html'):
"""Replacement for render_to_response so that views can be tested
without having to stub out templates that belong in the frontend
implementation.
Should be able to be tested using the django unit test assertions like a
normal render_to_response return value can be.
"""
class Template(object):
def __init__(self, name):
self.name = name
if context_instance is None:
context_instance = django_template.Context(context)
else:
context_instance.update(context)
resp = http.HttpResponse()
template = Template(template_name)
resp.write('<html><body><p>'
'This is a fake httpresponse for testing purposes only'
'</p></body></html>')
# Allows django.test.client to populate fields on the response object
test.signals.template_rendered.send(template, template=template,
context=context_instance)
return resp
class BaseViewTests(test.TestCase):
TEST_PROJECT = 'test'
TEST_STAFF_USER = 'staffUser'
TEST_USER = 'test'
TEST_REGION = 'test'
@classmethod
def setUpClass(cls):
cls._real_render_to_response = shortcuts.render_to_response
shortcuts.render_to_response = fake_render_to_response
@classmethod
def tearDownClass(cls):
shortcuts.render_to_response = cls._real_render_to_response
def setUp(self):
self.mox = mox.Mox()
self.user = self.authenticateTestUser()
def tearDown(self):
self.mox.UnsetStubs()
def assertRedirectsNoFollow(self, response, expected_url):
self.assertEqual(response._headers['location'],
('Location', settings.TESTSERVER + expected_url))
self.assertEqual(response.status_code, 302)
def authenticateTestUser(self):
user = auth_models.User.objects.create_user(self.TEST_USER,
'test@test.com',
password='test')
login = self.client.login(username=self.TEST_USER, password='test')
self.failUnless(login, 'Unable to login')
return user
def authenticateTestStaffUser(self):
user = auth_models.User.objects.create_user(self.TEST_STAFF_USER,
'teststaff@test.com',
password='test')
user.is_staff = True
user.save()
login = self.client.login(username=self.TEST_STAFF_USER,
password='test')
self.failUnless(login, 'Unable to login')
return user

View File

@ -0,0 +1,94 @@
from cloudfiles.errors import ContainerNotEmpty
from django.contrib import messages
from django.core.urlresolvers import reverse
from django_openstack import api
from django_openstack.tests.view_tests import base
from mox import IgnoreArg, IsA
class ContainerViewTests(base.BaseViewTests):
def setUp(self):
super(ContainerViewTests, self).setUp()
container_inner = base.Object()
container_inner.name = 'containerName'
self.container = api.Container(container_inner)
def test_index(self):
self.mox.StubOutWithMock(api, 'swift_get_containers')
api.swift_get_containers().AndReturn([self.container])
self.mox.ReplayAll()
res = self.client.get(reverse('dash_containers', args=['tenant']))
self.assertTemplateUsed(res, 'dash_containers.html')
self.assertIn('containers', res.context)
containers = res.context['containers']
self.assertEqual(len(containers), 1)
self.assertEqual(containers[0].name, 'containerName')
self.mox.VerifyAll()
def test_delete_container(self):
formData = {'container_name': 'containerName',
'method': 'DeleteContainer'}
self.mox.StubOutWithMock(api, 'swift_delete_container')
api.swift_delete_container('containerName')
self.mox.ReplayAll()
res = self.client.post(reverse('dash_containers', args=['tenant']),
formData)
self.assertRedirectsNoFollow(res, reverse('dash_containers',
args=['tenant']))
self.mox.VerifyAll()
def test_delete_container_nonempty(self):
formData = {'container_name': 'containerName',
'method': 'DeleteContainer'}
exception = ContainerNotEmpty('containerNotEmpty')
self.mox.StubOutWithMock(api, 'swift_delete_container')
api.swift_delete_container('containerName').AndRaise(exception)
self.mox.StubOutWithMock(messages, 'error')
messages.error(IgnoreArg(), IsA(unicode))
self.mox.ReplayAll()
res = self.client.post(reverse('dash_containers', args=['tenant']),
formData)
self.assertRedirectsNoFollow(res, reverse('dash_containers',
args=['tenant']))
self.mox.VerifyAll()
def test_create_container_get(self):
res = self.client.get(reverse('dash_containers_create',
args=['tenant']))
self.assertTemplateUsed(res, 'dash_containers_create.html')
def test_create_container_post(self):
formData = {'name': 'containerName',
'method': 'CreateContainer'}
self.mox.StubOutWithMock(api, 'swift_create_container')
api.swift_create_container('CreateContainer')
self.mox.StubOutWithMock(messages, 'success')
messages.success(IgnoreArg(), IsA(str))
res = self.client.post(reverse('dash_containers_create',
args=['tenant']),
formData)
self.assertRedirectsNoFollow(res, reverse('dash_containers_create',
args=['tenant']))