Refactoring to apply DRY principles and inherit flask directly

Modules should work without any change at all
Test cases will now need explicit website url (localhost) to be specified
This commit is contained in:
Sharoon Thomas 2012-04-24 12:02:39 -04:00
parent a05a2d85a7
commit d45e91e50d
6 changed files with 85 additions and 43 deletions

View File

@ -1,10 +1,10 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
nereid.static_file
Static file
:copyright: (c) 2011-2012 Openlabs Technologies & Consulting (P) Limited
:copyright: (c) 2010 by Sharoon Thomas.
:license: BSD, see LICENSE for more details
'''
@ -64,14 +64,14 @@ class NereidStaticFolder(ModelSQL, ModelView):
comments = fields.Text('Comments')
files = fields.One2Many('nereid.static.file', 'folder', 'Files')
folder_path = fields.Function(fields.Char('Folder Path'), 'get_path')
def __init__(self):
super(NereidStaticFolder, self).__init__()
self._constraints += [
('check_folder_name', 'invalid_folder_name'),
]
self._sql_constraints += [
('unique_folder', 'UNIQUE(folder_name)',
('unique_folder', 'UNIQUE(folder_name)',
'Folder name needs to be unique')
]
self._error_messages.update({

View File

@ -4,11 +4,11 @@
Template Management
:copyright: (c) 2010 by Sharoon Thomas,
:copyright: (c) 2010 by Openlabs Technologies & Consulting (P) Ltd
:copyright: (c) 2010-2012 by Openlabs Technologies & Consulting (P) Ltd
:copyright: (c) 2010 by Sharoon Thomas
:license: GPLv3, see LICENSE for more details
"""
from nereid import request, cache
from nereid import request
from trytond.model import ModelView, ModelSQL, fields
from trytond.transaction import Transaction
@ -22,7 +22,11 @@ class Template(ModelSQL, ModelView):
`source`: Source of the template, This should probably be a text field
`language`: Selection of Language
The name, language pair has to be unique
The name, language pair has to be unique.
.. tip::
The database based template loading is far from appropriate for
a production setup. This is mostly used in testing.
"""
_name = "nereid.template"
_description = "Nereid Template"
@ -44,11 +48,18 @@ class Template(ModelSQL, ModelView):
"""
Wraps _get_template_source for efficient caching
"""
return self._get_template_source(
name, request.nereid_website.id,
Transaction().context.get('language', 'en_US'))
website_obj = self.pool.get('nereid.website')
# Nereid tries to load template from different websites by passing
# the name of template like <website>/<template>.
website_name, template = name.split('/', 1)
website_id, = website_obj.search([('name', '=', website_name)])
return self._get_template_source(
template, website_id,
Transaction().context.get('language', 'en_US')
)
@cache.memoize_method('nereid.template', 60 * 60)
def _get_template_source(self, name, website, lang):
"""
Returns the source of the template requested

View File

@ -53,7 +53,7 @@ class TestAddress(TestCase):
cls.available_countries = country_obj.search([], limit=5)
cls.site = testing_proxy.create_site(
'testsite.com',
'localhost',
countries = [('set', cls.available_countries)],
application_user = 1, guest_user = cls.guest_user
)

View File

@ -53,7 +53,7 @@ class TestAuth(TestCase):
cls.available_countries = country_obj.search([], limit=5)
cls.site = testing_proxy.create_site(
'testsite.com',
'localhost',
countries = [('set', cls.available_countries)],
application_user = 1, guest_user = cls.guest_user
)
@ -62,28 +62,28 @@ class TestAuth(TestCase):
'home.jinja', '{{ get_flashed_messages() }}', cls.site
)
testing_proxy.create_template(
'login.jinja',
'{{ login_form.errors }} {{ get_flashed_messages() }}',
'login.jinja',
'{{ login_form.errors }} {{ get_flashed_messages() }}',
cls.site
)
testing_proxy.create_template(
'registration.jinja',
'{{ form.errors }} {{get_flashed_messages()}}',
'registration.jinja',
'{{ form.errors }} {{get_flashed_messages()}}',
cls.site
)
testing_proxy.create_template('reset-password.jinja',
'{{get_flashed_messages()}}', cls.site
)
testing_proxy.create_template(
'change-password.jinja',
'''{{ change_password_form.errors }}
{{ get_flashed_messages() }}''',
'''{{ change_password_form.errors }}
{{ get_flashed_messages() }}''',
cls.site
)
testing_proxy.create_template(
'address-edit.jinja',
'{{ form.errors }}',
'{{ form.errors }}',
cls.site
)
testing_proxy.create_template('address.jinja', '', cls.site)
@ -111,7 +111,7 @@ class TestAuth(TestCase):
with app.test_client() as c:
response = c.get('/en_US/registration')
self.assertEqual(response.status_code, 200)
with app.test_client() as c:
data = {
'name': 'New Test Registered User',
@ -152,9 +152,9 @@ class TestAuth(TestCase):
txn.cursor.commit()
with app.test_client() as c:
response = c.post('/en_US/login',
response = c.post('/en_US/login',
data={
'email': u'new.test@example.com',
'email': u'new.test@example.com',
'password': u'password'
})
self.assertEqual(response.status_code, 200)
@ -169,13 +169,13 @@ class TestAuth(TestCase):
self.assertEqual(response.status_code, 302)
# try login again
response = c.post('/en_US/login',
response = c.post('/en_US/login',
data={
'email': u'new.test@example.com',
'email': u'new.test@example.com',
'password': u'password'
})
self.assertEqual(response.status_code, 302)
def test_0030_change_password(self):
"""
Check if changing own password is possible
@ -194,11 +194,12 @@ class TestAuth(TestCase):
self.assertEqual(response.status_code, 302)
# Login now
response = c.post('/en_US/login',
response = c.post('/en_US/login',
data={
'email': u'new.test@example.com',
'email': u'new.test@example.com',
'password': u'password'
})
self.assertEqual(response.status_code, 302)
# send wrong password confirm
response = c.post('/en_US/change-password', data={
@ -214,6 +215,14 @@ class TestAuth(TestCase):
'confirm': 'new-password'
})
self.assertEqual(response.status_code, 200)
# send correct password confirm but not old password
response = c.post('/en_US/change-password', data={
'old_password': 'passw',
'password': 'new-password',
'confirm': 'new-password'
})
self.assertEqual(response.status_code, 200)
self.assertTrue(
"The current password you entered is invalid" in response.data
)
@ -256,9 +265,9 @@ class TestAuth(TestCase):
# Try a Login now and the existing activation code for reset should
# not be there
response = c.post(
'/en_US/login',
'/en_US/login',
data={
'email': 'new.test@example.com',
'email': 'new.test@example.com',
'password': 'new-password'
}
)
@ -277,7 +286,7 @@ class TestAuth(TestCase):
'email': 'new.test@example.com',
})
self.assertEqual(response.status_code, 302)
with Transaction().start(
testing_proxy.db_name, testing_proxy.user, None):
new_user = self.nereid_user_obj.browse(new_user_id)
@ -295,20 +304,20 @@ class TestAuth(TestCase):
'confirm': 'password'
})
self.assertEqual(response.status_code, 302)
with Transaction().start(
testing_proxy.db_name, testing_proxy.user, None):
new_user = self.nereid_user_obj.browse(new_user_id)
self.assertFalse(new_user.activation_code)
with app.test_client() as c:
response = c.post('/en_US/login',
response = c.post('/en_US/login',
data={
'email': 'new.test@example.com', 'password': 'new-password'
}
)
self.assertEqual(response.status_code, 200) # Login rejected
response = c.post('/en_US/login',
response = c.post('/en_US/login',
data={'email': 'new.test@example.com', 'password': 'password'})
self.assertEqual(response.status_code, 302) # Login approved
@ -318,7 +327,7 @@ class TestAuth(TestCase):
"""
app = self.get_app()
with app.test_client() as c:
response = c.post('/en_US/login?next=/en_US',
response = c.post('/en_US/login?next=/en_US',
data={'email': 'new.test@example.com', 'password': 'password'})
self.assertEqual(response.status_code, 302) # Login approved
self.assertTrue('<a href="/en_US">' in response.data)
@ -327,7 +336,24 @@ class TestAuth(TestCase):
"""
Check for logout and consistent behavior
"""
self.fail("Test not Implemented")
app = self.get_app()
with app.test_client() as c:
response = c.get("/en_US/account")
self.assertEqual(response.status_code, 302)
# Login and check again
response = c.post('/en_US/login',
data={'email': 'new.test@example.com', 'password': 'password'})
self.assertEqual(response.status_code, 302)
response = c.get("/en_US/account")
self.assertEqual(response.status_code, 200)
response = c.get("/en_US/logout")
self.assertEqual(response.status_code, 302)
response = c.get("/en_US/account")
self.assertEqual(response.status_code, 302)
def test_0100_my_account(self):
"""
@ -339,7 +365,7 @@ class TestAuth(TestCase):
self.assertEqual(response.status_code, 302)
# Login and check again
response = c.post('/en_US/login',
response = c.post('/en_US/login',
data={'email': 'new.test@example.com', 'password': 'password'})
self.assertEqual(response.status_code, 302)

View File

@ -1,6 +1,11 @@
#!/usr/bin/env python
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
"""
Test the currency
:copyright: (c) 2010-2012 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details.
"""
from ast import literal_eval
import unittest2 as unittest
@ -24,7 +29,7 @@ class TestCurrency(TestCase):
company = testing_proxy.create_company('Test Company')
cls.guest_user = testing_proxy.create_guest_user(company=company)
cls.site = testing_proxy.create_site(
'testsite.com',
'localhost',
application_user = 1, guest_user = cls.guest_user
)
testing_proxy.create_template(
@ -35,7 +40,7 @@ class TestCurrency(TestCase):
def get_app(self):
return testing_proxy.make_app(
SITE='testsite.com',
SITE='localhost',
GUEST_USER=self.guest_user)
def setUp(self):

View File

@ -37,7 +37,7 @@ class TestI18N(TestCase):
def get_app(self, **options):
options.update({
'SITE': 'testsite.com',
'SITE': 'localhost',
'GUEST_USER': self.guest_user,
})
return testing_proxy.make_app(**options)