Merge lp:~registry/horizon/25_dashboard_model_tests into lp:~hudson-openstack/horizon/trunk

Proposed by Mark Gius
Status: Merged
Approved by: Devin Carlen
Approved revision: 69
Merged at revision: 62
Proposed branch: lp:~registry/horizon/25_dashboard_model_tests
Merge into: lp:~hudson-openstack/horizon/trunk
Diff against target: 268 lines (+193/-6)
5 files modified
django-openstack/src/django_openstack/models.py (+6/-5)
django-openstack/src/django_openstack/tests/__init__.py (+2/-0)
django-openstack/src/django_openstack/tests/test_models.py (+169/-0)
django-openstack/src/django_openstack/testsettings.py (+7/-1)
django-openstack/src/django_openstack/utils.py (+9/-0)
To merge this branch: bzr merge lp:~registry/horizon/25_dashboard_model_tests
Reviewer Review Type Date Requested Status
Devin Carlen Approve
Review via email: mp+63913@code.launchpad.net

Description of the change

Unit tests for django-openstack.models

To post a comment you must log in.
66. By Mark Gius

Merge localization changes from trunk

Revision history for this message
Devin Carlen (devcamcar) wrote :

A few style nits:

These should go above the from ... imports for consistency:

65 +import datetime
66 +import hashlib
67 +import mox
68 +import random

Use the full module path in this case, which I think is probably from django_openstack.test_models or something:

51 from django_openstack.nova.tests import *
52 +
53 +from test_models import *

Prefer underscores to camelCase (ignore the fact that the setUp and tearDown methods ignore this - its a weird holdover from the old pyunit library):

84 + testCred = nova_models.CredentialsAuthorization()
85 + testCred.username = TEST_USER
86 + testCred.project = TEST_PROJECT
87 + testCred.auth_date = TEST_AUTH_DATE
88 + testCred.auth_token = TEST_AUTH_TOKEN
89 + testCred.save()

Use utcnow() instead of now():

122 + cred.auth_date = datetime.datetime.now() - AUTH_EXPIRATION_LENGTH \
123 + - HOUR

Better yet, don't use datetime directly at all, since it makes testing hard. Nova gets around this problem by supplying a utils.utcnow() method that wraps this and makes it a little more syntactically pretty.

180 + # testing with time is tricky. Mock out "right now" test to avoid
181 + # timing issues
182 + time = datetime.datetime.now()

Supernit! Need a space to left of equals:

248 +CREDENTIAL_DOWNLOAD_URL= TESTSERVER + '/credentials/'

review: Needs Fixing
67. By Mark Gius

Address review comments

68. By Mark Gius

Remove hacky post_save short-circuit. Post_save tests to follow later

Revision history for this message
Devin Carlen (devcamcar) wrote :

Looks good. Only one minor nit:

156 + self.assertTrue(cred.username == TEST_USER2)
157 + self.assertTrue(cred.project == TEST_PROJECT)
158 + self.assertTrue(cred.auth_token == TEST_AUTH_TOKEN_2)

self.assertTrue should be replaced with assertEquals.

review: Needs Fixing
69. By Mark Gius

Fix final nit

Revision history for this message
Devin Carlen (devcamcar) wrote :

lgtm

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'django-openstack/src/django_openstack/models.py'
2--- django-openstack/src/django_openstack/models.py 2011-06-08 04:58:56 +0000
3+++ django-openstack/src/django_openstack/models.py 2011-06-14 19:58:47 +0000
4@@ -31,7 +31,8 @@
5 from django.db.models.signals import post_save
6 from django.template.loader import render_to_string
7 from django_openstack import log as logging
8-from django_openstack.core.connection import get_nova_admin_connection
9+from django_openstack.core import connection
10+from django_openstack import utils
11
12
13 LOG = logging.getLogger('django_openstack')
14@@ -75,13 +76,13 @@
15 expiration_date = datetime.timedelta(
16 days=int(settings.CREDENTIAL_AUTHORIZATION_DAYS))
17
18- return self.auth_date + expiration_date <= datetime.datetime.now()
19+ return self.auth_date + expiration_date <= utils.utcnow()
20
21 def get_download_url(self):
22 return settings.CREDENTIAL_DOWNLOAD_URL + self.auth_token
23
24 def get_zip(self):
25- nova = get_nova_admin_connection()
26+ nova = connection.get_nova_admin_connection()
27 self.delete()
28 return nova.get_zip(self.username, self.project)
29
30@@ -118,11 +119,11 @@
31 """
32
33 # NOTE(devcamcar): If running unit tests, don't use a real endpoint.
34- if settings.NOVA_DEFAULT_ENDPOINT == 'none':
35+ if settings.NOVA_DEFAULT_ENDPOINT is None:
36 return
37
38 if created:
39- nova = get_nova_admin_connection()
40+ nova = connection.get_nova_admin_connection()
41 if not nova.has_user(instance.username):
42 nova.create_user(instance.username)
43 LOG.info('User "%s" created in Nova' % instance.username)
44
45=== modified file 'django-openstack/src/django_openstack/tests/__init__.py'
46--- django-openstack/src/django_openstack/tests/__init__.py 2011-05-16 19:57:48 +0000
47+++ django-openstack/src/django_openstack/tests/__init__.py 2011-06-14 19:58:47 +0000
48@@ -1,1 +1,3 @@
49 from django_openstack.nova.tests import *
50+
51+from django_openstack.tests.test_models import *
52
53=== added file 'django-openstack/src/django_openstack/tests/test_models.py'
54--- django-openstack/src/django_openstack/tests/test_models.py 1970-01-01 00:00:00 +0000
55+++ django-openstack/src/django_openstack/tests/test_models.py 2011-06-14 19:58:47 +0000
56@@ -0,0 +1,169 @@
57+import datetime
58+import hashlib
59+import mox
60+import random
61+
62+from django import test
63+from django.conf import settings
64+from django.db.models.signals import post_save
65+from django_openstack import models as nova_models
66+from django_openstack import utils
67+from django_openstack.core import connection
68+from nova_adminclient import NovaAdminClient
69+
70+
71+TEST_USER = 'testUser'
72+TEST_PROJECT = 'testProject'
73+TEST_AUTH_TOKEN = hashlib.sha1('').hexdigest()
74+TEST_AUTH_DATE = utils.utcnow()
75+TEST_BAD_AUTH_TOKEN = 'badToken'
76+
77+HOUR = datetime.timedelta(seconds=3600)
78+AUTH_EXPIRATION_LENGTH = \
79+ datetime.timedelta(days=int(settings.CREDENTIAL_AUTHORIZATION_DAYS))
80+
81+
82+class CredentialsAuthorizationTests(test.TestCase):
83+ @classmethod
84+ def setUpClass(cls):
85+ # these post_save methods interact with external resources, shut them
86+ # down to test credentials
87+ post_save.disconnect(sender=nova_models.CredentialsAuthorization,
88+ dispatch_uid='django_openstack.CredentialsAuthorization.post_save')
89+ post_save.disconnect(sender=nova_models.CredentialsAuthorization,
90+ dispatch_uid='django_openstack.User.post_save')
91+
92+ def setUp(self):
93+ test_cred = nova_models.CredentialsAuthorization()
94+ test_cred.username = TEST_USER
95+ test_cred.project = TEST_PROJECT
96+ test_cred.auth_date = TEST_AUTH_DATE
97+ test_cred.auth_token = TEST_AUTH_TOKEN
98+ test_cred.save()
99+
100+ badTestCred = nova_models.CredentialsAuthorization()
101+ badTestCred.username = TEST_USER
102+ badTestCred.project = TEST_PROJECT
103+ badTestCred.auth_date = TEST_AUTH_DATE
104+ badTestCred.auth_token = TEST_BAD_AUTH_TOKEN
105+ badTestCred.save()
106+
107+ self.mox = mox.Mox()
108+
109+ def tearDown(self):
110+ self.mox.UnsetStubs()
111+
112+ def test_get_by_token(self):
113+ TEST_MISSING_AUTH_TOKEN = hashlib.sha1('notAToken').hexdigest()
114+
115+ # Token not a sha1, but exists in system
116+ cred = nova_models.CredentialsAuthorization.get_by_token(
117+ TEST_BAD_AUTH_TOKEN)
118+ self.assertTrue(cred is None)
119+
120+ # Token doesn't exist
121+ cred = nova_models.CredentialsAuthorization.get_by_token(
122+ TEST_MISSING_AUTH_TOKEN)
123+ self.assertTrue(cred is None)
124+
125+ # Good token
126+ cred = nova_models.CredentialsAuthorization.get_by_token(
127+ TEST_AUTH_TOKEN)
128+ self.assertTrue(cred is not None)
129+
130+ # Expire the token
131+ cred.auth_date = utils.utcnow() - AUTH_EXPIRATION_LENGTH \
132+ - HOUR
133+ cred.save()
134+
135+ # Expired token
136+ cred = nova_models.CredentialsAuthorization.get_by_token(
137+ TEST_AUTH_TOKEN)
138+ self.assertTrue(cred is None)
139+
140+ def test_authorize(self):
141+ TEST_USER2 = TEST_USER + '2'
142+ TEST_AUTH_TOKEN_2 = hashlib.sha1('token2').hexdigest()
143+
144+ cred_class = nova_models.CredentialsAuthorization
145+ self.mox.StubOutWithMock(cred_class, 'create_auth_token')
146+ cred_class.create_auth_token(TEST_USER2).AndReturn(
147+ TEST_AUTH_TOKEN_2)
148+
149+ self.mox.ReplayAll()
150+
151+ cred = cred_class.authorize(TEST_USER2, TEST_PROJECT)
152+
153+ self.mox.VerifyAll()
154+
155+ self.assertTrue(cred is not None)
156+ self.assertEqual(cred.username, TEST_USER2)
157+ self.assertEqual(cred.project, TEST_PROJECT)
158+ self.assertEqual(cred.auth_token, TEST_AUTH_TOKEN_2)
159+ self.assertFalse(cred.auth_token_expired())
160+
161+ cred = cred_class.get_by_token(TEST_AUTH_TOKEN_2)
162+ self.assertTrue(cred is not None)
163+
164+ def test_create_auth_token(self):
165+ rand_state = random.getstate()
166+ expected_salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
167+ expected_token = hashlib.sha1(expected_salt + TEST_USER).hexdigest()
168+
169+ random.setstate(rand_state)
170+ auth_token = \
171+ nova_models.CredentialsAuthorization.create_auth_token(TEST_USER)
172+ self.assertEqual(expected_token, auth_token)
173+
174+ def test_auth_token_expired(self):
175+ '''
176+ Test expired in past, expires in future, expires _right now_
177+ '''
178+ cred = \
179+ nova_models.CredentialsAuthorization.get_by_token(TEST_AUTH_TOKEN)
180+
181+ cred.auth_date = utils.utcnow() - AUTH_EXPIRATION_LENGTH \
182+ - HOUR
183+ self.assertTrue(cred.auth_token_expired())
184+
185+ cred.auth_date = utils.utcnow()
186+
187+ self.assertFalse(cred.auth_token_expired())
188+
189+ # testing with time is tricky. Mock out "right now" test to avoid
190+ # timing issues
191+ time = utils.utcnow.override_time = utils.utcnow()
192+ cred.auth_date = time - AUTH_EXPIRATION_LENGTH
193+
194+ self.assertTrue(cred.auth_token_expired())
195+
196+ utils.utcnow.override_time = None
197+
198+ def test_get_download_url(self):
199+ cred = \
200+ nova_models.CredentialsAuthorization.get_by_token(TEST_AUTH_TOKEN)
201+
202+ expected_url = settings.CREDENTIAL_DOWNLOAD_URL + TEST_AUTH_TOKEN
203+ self.assertEqual(expected_url, cred.get_download_url())
204+
205+ def test_get_zip(self):
206+ cred = \
207+ nova_models.CredentialsAuthorization.get_by_token(TEST_AUTH_TOKEN)
208+
209+ admin_mock = self.mox.CreateMock(NovaAdminClient)
210+
211+ self.mox.StubOutWithMock(connection, 'get_nova_admin_connection')
212+ connection.get_nova_admin_connection().AndReturn(admin_mock)
213+
214+ admin_mock.get_zip(TEST_USER, TEST_PROJECT)
215+
216+ self.mox.ReplayAll()
217+
218+ cred.get_zip()
219+
220+ self.mox.VerifyAll()
221+
222+ cred = \
223+ nova_models.CredentialsAuthorization.get_by_token(TEST_AUTH_TOKEN)
224+
225+ self.assertTrue(cred is None)
226
227=== modified file 'django-openstack/src/django_openstack/testsettings.py'
228--- django-openstack/src/django_openstack/testsettings.py 2011-05-26 00:48:41 +0000
229+++ django-openstack/src/django_openstack/testsettings.py 2011-06-14 19:58:47 +0000
230@@ -8,17 +8,23 @@
231 INSTALLED_APPS = ['django.contrib.auth',
232 'django.contrib.contenttypes',
233 'django.contrib.sessions',
234+ 'django.contrib.sites',
235 'django_openstack',
236+ 'django_openstack.tests',
237 'django_openstack.templatetags',
238 'django_openstack.nova']
239 ROOT_URLCONF = 'django_openstack.testurls'
240 TEMPLATE_DIRS = (
241 os.path.join(ROOT_PATH, 'tests', 'templates')
242 )
243+SITE_ID = 1
244 SITE_BRANDING = 'OpenStack'
245 SITE_NAME = 'openstack'
246 ENABLE_VNC = True
247-NOVA_DEFAULT_ENDPOINT = 'none'
248+NOVA_DEFAULT_ENDPOINT = None
249 NOVA_DEFAULT_REGION = 'test'
250 NOVA_ACCESS_KEY = 'test'
251 NOVA_SECRET_KEY = 'test'
252+
253+CREDENTIAL_AUTHORIZATION_DAYS = 2
254+CREDENTIAL_DOWNLOAD_URL = TESTSERVER + '/credentials/'
255
256=== added file 'django-openstack/src/django_openstack/utils.py'
257--- django-openstack/src/django_openstack/utils.py 1970-01-01 00:00:00 +0000
258+++ django-openstack/src/django_openstack/utils.py 2011-06-14 19:58:47 +0000
259@@ -0,0 +1,9 @@
260+import datetime
261+
262+def utcnow():
263+ '''Overridable version of datetime.datetime.utcnow'''
264+ if utcnow.override_time:
265+ return utcnow.override_time
266+ return datetime.datetime.utcnow()
267+
268+utcnow.override_time = None

Subscribers

People subscribed via source and target branches