diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2011-11-25 10:54:41 -0300 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2011-11-25 10:54:41 -0300 |
commit | 7719260f0a9a0167ed6fcb193bf1e1c57c9ae479 (patch) | |
tree | 1ba3e985045b32a066b076dfc7c2212e4d999c06 | |
parent | f0f643a9630d910c10de52c107c29eaf4d020fc4 (diff) | |
parent | 350d8b9aa7849a86fcbd97667703df834184d511 (diff) | |
download | askbot-7719260f0a9a0167ed6fcb193bf1e1c57c9ae479.tar.gz askbot-7719260f0a9a0167ed6fcb193bf1e1c57c9ae479.tar.bz2 askbot-7719260f0a9a0167ed6fcb193bf1e1c57c9ae479.zip |
Merge branch 'master' of github.com:ASKBOT/askbot-devel
-rw-r--r-- | askbot/conf/__init__.py | 1 | ||||
-rw-r--r-- | askbot/conf/access_control.py | 21 | ||||
-rw-r--r-- | askbot/conf/login_providers.py | 10 | ||||
-rw-r--r-- | askbot/context.py | 3 | ||||
-rw-r--r-- | askbot/doc/source/changelog.rst | 4 | ||||
-rw-r--r-- | askbot/doc/source/contributors.rst | 2 | ||||
-rw-r--r-- | askbot/feed.py | 2 | ||||
-rw-r--r-- | askbot/middleware/anon_user.py | 3 | ||||
-rw-r--r-- | askbot/middleware/forum_mode.py | 35 | ||||
-rw-r--r-- | askbot/models/question.py | 4 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py | 1 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py.mustache | 1 | ||||
-rw-r--r-- | askbot/skins/default/media/style/style.css | 6 | ||||
-rw-r--r-- | askbot/skins/default/media/style/style.less | 5 | ||||
-rw-r--r-- | askbot/skins/default/templates/meta/html_head_stylesheets.html | 8 | ||||
-rw-r--r-- | askbot/skins/default/templates/widgets/system_messages.html | 2 | ||||
-rw-r--r-- | askbot/skins/loaders.py | 4 | ||||
-rw-r--r-- | askbot/startup_procedures.py | 1 | ||||
-rw-r--r-- | askbot/tests/page_load_tests.py | 182 | ||||
-rw-r--r-- | askbot/tests/permission_assertion_tests.py | 45 | ||||
-rw-r--r-- | askbot/tests/utils.py | 44 | ||||
-rw-r--r-- | askbot/views/writers.py | 2 |
22 files changed, 328 insertions, 58 deletions
diff --git a/askbot/conf/__init__.py b/askbot/conf/__init__.py index 64fe41fb..4f228ea7 100644 --- a/askbot/conf/__init__.py +++ b/askbot/conf/__init__.py @@ -19,6 +19,7 @@ import askbot.conf.markup import askbot.conf.social_sharing import askbot.conf.badges import askbot.conf.login_providers +import askbot.conf.access_control import askbot.conf.site_modes #import main settings object diff --git a/askbot/conf/access_control.py b/askbot/conf/access_control.py new file mode 100644 index 00000000..cd2364b5 --- /dev/null +++ b/askbot/conf/access_control.py @@ -0,0 +1,21 @@ +from askbot.conf.settings_wrapper import settings +from askbot.conf.super_groups import LOGIN_USERS_COMMUNICATION +from askbot.deps import livesettings +from django.utils.translation import ugettext as _ + +ACCESS_CONTROL = livesettings.ConfigurationGroup( + 'ACCESS_CONTROL', + _('Access control settings'), + super_group = LOGIN_USERS_COMMUNICATION + ) + +settings.register( + livesettings.BooleanValue( + ACCESS_CONTROL, + 'ASKBOT_CLOSED_FORUM_MODE', + default = False, + description=_('Allow only registered user to access the forum'), + ) +) + + diff --git a/askbot/conf/login_providers.py b/askbot/conf/login_providers.py index 0cc20a9a..b6073eea 100644 --- a/askbot/conf/login_providers.py +++ b/askbot/conf/login_providers.py @@ -56,11 +56,11 @@ settings.register( livesettings.ImageValue( LOGIN_PROVIDERS, 'WORDPRESS_SITE_ICON', - upload_directory = django_settings.ASKBOT_FILE_UPLOAD_DIR, - upload_url = '/' + django_settings.ASKBOT_UPLOADED_FILES_URL, - default = '/images/logo.gif', - description = _('Upload your icon'), - url_resolver = skin_utils.get_media_url + upload_directory=django_settings.ASKBOT_FILE_UPLOAD_DIR, + upload_url='/' + django_settings.ASKBOT_UPLOADED_FILES_URL, + default='/images/logo.gif', + description=_('Upload your icon'), + url_resolver=skin_utils.get_media_url ) ) diff --git a/askbot/context.py b/askbot/context.py index 6dc38f79..f3665240 100644 --- a/askbot/context.py +++ b/askbot/context.py @@ -13,8 +13,9 @@ from askbot.utils import url_utils def application_settings(request): """The context processor function""" my_settings = askbot_settings.as_dict() - my_settings['LANGUAGE_CODE'] = settings.LANGUAGE_CODE + my_settings['LANGUAGE_CODE'] = getattr(request, 'LANGUAGE_CODE', settings.LANGUAGE_CODE) my_settings['ASKBOT_URL'] = settings.ASKBOT_URL + my_settings['ASKBOT_CSS_DEVEL'] = getattr(settings, 'ASKBOT_CSS_DEVEL', False) my_settings['DEBUG'] = settings.DEBUG my_settings['ASKBOT_VERSION'] = askbot.get_version() my_settings['LOGIN_URL'] = url_utils.get_login_url() diff --git a/askbot/doc/source/changelog.rst b/askbot/doc/source/changelog.rst index 0ada3395..642924fd 100644 --- a/askbot/doc/source/changelog.rst +++ b/askbot/doc/source/changelog.rst @@ -15,7 +15,9 @@ Development version (not yet published) * Fixed sharing button bug, css fixes for new template (Alexander Werner) * Added ASKBOT_TRANSLATE_URL setting for url localization(Alexander Werner) * Changed javascript translation model, moved from jqueryi18n to django(Rosandra Cuello Suñol) - +* Private forum mode (Vlad Bokov) +* Improved text search query in Postgresql (Alexander Werner) +* Take LANGUAGE_CODE from request (Alexander Werner) 0.7.26 (Current Version) ------------------------ diff --git a/askbot/doc/source/contributors.rst b/askbot/doc/source/contributors.rst index 3d1303c1..aca7eb48 100644 --- a/askbot/doc/source/contributors.rst +++ b/askbot/doc/source/contributors.rst @@ -30,7 +30,7 @@ Programming and documentation * `Tomasz Zielinski <http://pyconsultant.eu/>`_ * `Tomasz Szynalski <http://antimoon.com>`_ * `Raghu Udiyar <http://raags.tumblr.com/>`_ -* Alexander Werner +* `Alexander Werner <https://twitter.com/#!/bundeswerner>`_ * Rosandra Cuello Suñol Translations diff --git a/askbot/feed.py b/askbot/feed.py index 20932f1a..7f41fd4f 100644 --- a/askbot/feed.py +++ b/askbot/feed.py @@ -155,7 +155,7 @@ class RssLastestQuestionsFeed(Feed): for tag in tags: qs = qs.filter(tags__name = tag) - return qs.order_by('-last_activity_at') + return qs.order_by('-last_activity_at')[:30] diff --git a/askbot/middleware/anon_user.py b/askbot/middleware/anon_user.py index adaf6fd2..7cd9279d 100644 --- a/askbot/middleware/anon_user.py +++ b/askbot/middleware/anon_user.py @@ -63,7 +63,8 @@ class ConnectToSessionMessagesMiddleware(object): """Adds the ``'askbot_visitor'``key to cookie if user ever authenticates so that the anonymous user message won't be shown after the user logs out""" - if request.user.is_authenticated() and \ + if hasattr(request, 'user') and \ + request.user.is_authenticated() and \ 'askbot_visitor' not in request.COOKIES : #import datetime #max_age = 365*24*60*60 diff --git a/askbot/middleware/forum_mode.py b/askbot/middleware/forum_mode.py new file mode 100644 index 00000000..874b5559 --- /dev/null +++ b/askbot/middleware/forum_mode.py @@ -0,0 +1,35 @@ +from django.http import HttpResponseRedirect +from django.utils.translation import ugettext as _ +from django.conf import settings +from django.core.urlresolvers import resolve + +from askbot.conf import settings as askbot_settings + +PROTECTED_URLS = [ + 'about', + 'feeds', + 'privacy', + 'tags', + 'badges', + 'questions', + 'question', + 'question_revisions', + 'users', + 'edit_user', + 'faq', + 'user_profile', + 'answer_revisions', + 'user_subscriptions'] + + +class ForumModeMiddleware(object): + + def process_request(self, request): + if (askbot_settings.ASKBOT_CLOSED_FORUM_MODE + and request.user.is_anonymous() + and resolve(request.path).url_name in PROTECTED_URLS): + request.user.message_set.create(_('Please log in to use %s') % \ + askbot_settings.APP_SHORT_NAME) + return HttpResponseRedirect(settings.LOGIN_URL) + else: + return None diff --git a/askbot/models/question.py b/askbot/models/question.py index 7f38bd5c..c2d4be11 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -139,12 +139,12 @@ class QuestionQuerySet(models.query.QuerySet): | models.Q(answers__text__search = search_query) ) elif 'postgresql_psycopg2' in askbot.get_database_engine_name(): - rank_clause = "ts_rank(question.text_search_vector, to_tsquery(%s))"; + rank_clause = "ts_rank(question.text_search_vector, plainto_tsquery(%s))"; search_query = '&'.join(search_query.split()) extra_params = (search_query,) extra_kwargs = { 'select': {'relevance': rank_clause}, - 'where': ['text_search_vector @@ to_tsquery(%s)'], + 'where': ['text_search_vector @@ plainto_tsquery(%s)'], 'params': extra_params, 'select_params': extra_params, } diff --git a/askbot/setup_templates/settings.py b/askbot/setup_templates/settings.py index ef24a82f..2f6a0f9b 100644 --- a/askbot/setup_templates/settings.py +++ b/askbot/setup_templates/settings.py @@ -102,6 +102,7 @@ MIDDLEWARE_CLASSES = ( #below is askbot stuff for this tuple 'askbot.middleware.anon_user.ConnectToSessionMessagesMiddleware', + 'askbot.middleware.forum_mode.ForumModeMiddleware', 'askbot.middleware.pagesize.QuestionsPageSizeMiddleware', 'askbot.middleware.cancel.CancelActionMiddleware', 'django.middleware.transaction.TransactionMiddleware', diff --git a/askbot/setup_templates/settings.py.mustache b/askbot/setup_templates/settings.py.mustache index 081f37fa..368c0564 100644 --- a/askbot/setup_templates/settings.py.mustache +++ b/askbot/setup_templates/settings.py.mustache @@ -101,6 +101,7 @@ MIDDLEWARE_CLASSES = ( #below is askbot stuff for this tuple 'askbot.middleware.anon_user.ConnectToSessionMessagesMiddleware', + 'askbot.middleware.forum_mode.ForumModeMiddleware', 'askbot.middleware.pagesize.QuestionsPageSizeMiddleware', 'askbot.middleware.cancel.CancelActionMiddleware', 'django.middleware.transaction.TransactionMiddleware', diff --git a/askbot/skins/default/media/style/style.css b/askbot/skins/default/media/style/style.css index 493acd6f..a1ac17d8 100644 --- a/askbot/skins/default/media/style/style.css +++ b/askbot/skins/default/media/style/style.css @@ -124,6 +124,9 @@ blockquote { height: 0; visibility: hidden; } +body.user-messages { + margin-top: 2.4em; +} .badges a { color: #763333; text-decoration: underline; @@ -170,7 +173,7 @@ body.user-messages { border-top: #fff 1px solid; font-family: 'Yanone Kaffeesatz', sans-serif; } -.notify p { +.notify p.notification { margin-top: 6px; margin-bottom: 6px; font-size: 16px; @@ -208,6 +211,7 @@ body.user-messages { padding: 5px 0px 5px 0px; height: 75px; width: auto; + float: left; } #userToolsNav { /* Navigation bar containing login link or user information, check widgets/user_navigation.html*/ diff --git a/askbot/skins/default/media/style/style.less b/askbot/skins/default/media/style/style.less index ca7a2336..71e2fc36 100644 --- a/askbot/skins/default/media/style/style.less +++ b/askbot/skins/default/media/style/style.less @@ -177,8 +177,8 @@ body.user-messages { border-top:#fff 1px solid; font-family:@main-font; - p { - margin-top: 6px; + p.notification { + margin-top: 6px; margin-bottom: 6px; font-size: 16px; color:#424242 @@ -220,6 +220,7 @@ body.user-messages { padding: 5px 0px 5px 0px; height: 75px; width: auto; + float: left; } #userToolsNav {/* Navigation bar containing login link or user information, check widgets/user_navigation.html*/ diff --git a/askbot/skins/default/templates/meta/html_head_stylesheets.html b/askbot/skins/default/templates/meta/html_head_stylesheets.html index 4e3f173b..14f3c106 100644 --- a/askbot/skins/default/templates/meta/html_head_stylesheets.html +++ b/askbot/skins/default/templates/meta/html_head_stylesheets.html @@ -1,9 +1,9 @@ -{%if settings.DEBUG %} +{% if settings.ASKBOT_CSS_DEVEL == False %} +<link href="{{"/style/style.css"|media }}" rel="stylesheet" type="text/css" /> +{% else %} <link href="{{"/style/style.less"|media }}" rel="stylesheet/less" type="text/css" /> <script type="text/javascript" src="{{"/js/less.min.js"|media}}"></script> -{%else%} -<link href="{{"/style/style.css"|media }}" rel="stylesheet" type="text/css" /> -{%endif%} +{% endif %} <link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:300,400,700' rel='stylesheet' type='text/css'> {{ skin.get_extra_css_link() }} {% if settings.USE_CUSTOM_CSS %} diff --git a/askbot/skins/default/templates/widgets/system_messages.html b/askbot/skins/default/templates/widgets/system_messages.html index 6b803846..10ba4a84 100644 --- a/askbot/skins/default/templates/widgets/system_messages.html +++ b/askbot/skins/default/templates/widgets/system_messages.html @@ -1,7 +1,7 @@ <div class="notify" style="display:none"> {% if user_messages %} {% for message in user_messages %} - <p>{{ message }}</p> + <p class="notification">{{ message }}</p> {% endfor %} {% endif %} <a id="closeNotify" onclick="notify.close(true)"></a> diff --git a/askbot/skins/loaders.py b/askbot/skins/loaders.py index 64d14072..24559512 100644 --- a/askbot/skins/loaders.py +++ b/askbot/skins/loaders.py @@ -108,9 +108,11 @@ def get_template(template, request = None): request variable will be used in the future to set template according to the user preference or admins preference - at this point request variable is not used though + request variable is used to localize the skin if possible """ skin = get_skin(request) + if hasattr(request,'LANGUAGE_CODE'): + skin.set_language(request.LANGUAGE_CODE) return skin.get_template(template) def render_into_skin(template, data, request, mimetype = 'text/html'): diff --git a/askbot/startup_procedures.py b/askbot/startup_procedures.py index bf988881..b6a97f22 100644 --- a/askbot/startup_procedures.py +++ b/askbot/startup_procedures.py @@ -76,6 +76,7 @@ def test_middleware(): 'django.middleware.common.CommonMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'askbot.middleware.anon_user.ConnectToSessionMessagesMiddleware', + 'askbot.middleware.forum_mode.ForumModeMiddleware', 'askbot.middleware.pagesize.QuestionsPageSizeMiddleware', 'askbot.middleware.cancel.CancelActionMiddleware', 'django.middleware.transaction.TransactionMiddleware', diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py index 442b1bd7..e0bcce86 100644 --- a/askbot/tests/page_load_tests.py +++ b/askbot/tests/page_load_tests.py @@ -1,6 +1,7 @@ from django.test import TestCase from django.test import signals from django.template import defaultfilters +from django.conf import settings from django.core.urlresolvers import reverse import coffin import coffin.template @@ -8,11 +9,15 @@ from askbot import models from askbot.utils.slug import slugify from askbot.deployment import package_utils from askbot.tests.utils import AskbotTestCase +from askbot.conf import settings as askbot_settings +from askbot.tests.utils import skipIf import sys + def patch_jinja2(): from jinja2 import Template ORIG_JINJA2_RENDERER = Template.render + def instrumented_render(template_object, *args, **kwargs): context = dict(*args, **kwargs) signals.template_rendered.send( @@ -28,26 +33,35 @@ if CMAJOR == 0 and CMINOR == 3 and CMICRO < 4: import ipdb; ipdb.set_trace() patch_jinja2() + class PageLoadTestCase(AskbotTestCase): def try_url( self, - url_name, status_code=200, template=None, + url_name, status_code=200, template=None, kwargs={}, redirect_url=None, follow=False, - data = {}, - ): - url = reverse(url_name, kwargs = kwargs) - url_info = 'getting url %s' % url + data={}): + url = reverse(url_name, kwargs=kwargs) + if status_code == 302: + url_info = 'redirecting to LOGIN_URL in closed_mode: %s' % url + else: + url_info = 'getting url %s' % url if data: url_info += '?' + '&'.join(['%s=%s' % (k, v) for k, v in data.iteritems()]) print url_info + # if redirect expected, but we wont' follow + if status_code == 302 and follow: + response = self.client.get(url, data=data) + self.assertTrue(settings.LOGIN_URL in response['Location']) + return + r = self.client.get(url, data=data, follow=follow) if hasattr(self.client, 'redirect_chain'): print 'redirect chain: %s' % ','.join(self.client.redirect_chain) self.assertEqual(r.status_code, status_code) - if template: + if template and status_code != 302: if isinstance(r.template, coffin.template.Template): self.assertEqual(r.template.name, template) elif isinstance(r.template, list): @@ -99,168 +113,236 @@ class PageLoadTestCase(AskbotTestCase): self.failUnless(response.redirect_chain[0][0].endswith('/questions/')) self.assertEquals(response.template.name, 'main_page.html') - def proto_test_non_user_urls(self): + def proto_test_ask_page(self, allow_anonymous, status_code): + prev_setting = askbot_settings.ALLOW_POSTING_BEFORE_LOGGING_IN + askbot_settings.update('ALLOW_POSTING_BEFORE_LOGGING_IN', allow_anonymous) + self.try_url( + 'ask', + status_code = status_code, + template = 'ask.html' + ) + askbot_settings.update('ALLOW_POSTING_BEFORE_LOGGING_IN', prev_setting) + + def test_ask_page_allowed_anonymous(self): + self.proto_test_ask_page(True, 200) + + def test_ask_page_disallowed_anonymous(self): + self.proto_test_ask_page(False, 302) + + def proto_test_non_user_urls(self, status_code): """test all reader views thoroughly on non-crashiness (no correcteness tests here) """ self.try_url('sitemap') - self.try_url('feeds', kwargs={'url':'rss'}) - self.try_url('about', template='about.html') - self.try_url('privacy', template='privacy.html') + self.try_url( + 'feeds', + status_code=status_code, + kwargs={'url':'rss'}) + self.try_url( + 'about', + status_code=status_code, + template='about.html') + self.try_url( + 'privacy', + status_code=status_code, + template='privacy.html') self.try_url('logout', template='authopenid/logout.html') #todo: test different tabs - self.try_url('tags', template='tags.html') - self.try_url('tags', data={'sort':'name'}, template='tags.html') - self.try_url('tags', data={'sort':'used'}, template='tags.html') - self.try_url('badges', template='badges.html') self.try_url( - 'answer_revisions', + 'tags', + status_code=status_code, + template='tags.html') + self.try_url( + 'tags', + status_code=status_code, + data={'sort':'name'}, template='tags.html') + self.try_url( + 'tags', + status_code=status_code, + data={'sort':'used'}, template='tags.html') + self.try_url( + 'badges', + status_code=status_code, + template='badges.html') + self.try_url( + 'answer_revisions', + status_code=status_code, template='revisions.html', kwargs={'id':38} ) #todo: test different sort methods and scopes self.try_url( 'questions', + status_code=status_code, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'start_over':'true'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'scope':'unanswered'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'scope':'favorite'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'scope':'unanswered', 'sort':'age-desc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'scope':'unanswered', 'sort':'age-asc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'scope':'unanswered', 'sort':'activity-desc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'scope':'unanswered', 'sort':'activity-asc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'sort':'answers-desc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'sort':'answers-asc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'sort':'votes-desc'}, template='main_page.html' ) self.try_url( 'questions', + status_code=status_code, data={'sort':'votes-asc'}, template='main_page.html' ) self.try_url( 'question', + status_code=status_code, kwargs={'id':1}, follow=True, template='question.html' ) self.try_url( 'question', + status_code=status_code, kwargs={'id':2}, follow=True, template='question.html' ) self.try_url( 'question', + status_code=status_code, kwargs={'id':3}, follow=True, template='question.html' ) self.try_url( 'question_revisions', + status_code=status_code, kwargs={'id':17}, template='revisions.html' ) - self.try_url('users', template='users.html') + self.try_url('users', + status_code=status_code, + template='users.html') #todo: really odd naming conventions for sort methods self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'reputation'}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'newest'}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'last'}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'user'}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'reputation', 'page':2}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'newest', 'page':2}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'last', 'page':2}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'user', 'page':2}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'reputation', 'page':1}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'newest', 'page':1}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'last', 'page':1}, ) self.try_url( 'users', + status_code=status_code, template='users.html', data={'sort':'user', 'page':1}, ) @@ -268,72 +350,99 @@ class PageLoadTestCase(AskbotTestCase): 'edit_user', template='authopenid/signin.html', kwargs={'id':4}, - status_code=200, + status_code=status_code, follow=True, ) self.try_url( 'faq', template='faq_static.html', - status_code=200, + status_code=status_code, ) def test_non_user_urls(self): - self.proto_test_non_user_urls() + self.proto_test_non_user_urls(status_code=200) + + @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ + not in settings.MIDDLEWARE_CLASSES, + 'no ForumModeMiddleware set') + def test_non_user_urls_in_closed_forum_mode(self): + askbot_settings.ASKBOT_CLOSED_FORUM_MODE = True + self.proto_test_non_user_urls(status_code=302) + askbot_settings.ASKBOT_CLOSED_FORUM_MODE = False #def test_non_user_urls_logged_in(self): #user = User.objects.get(id=1) #somehow login this user #self.proto_test_non_user_urls() - def test_user_urls(self): + def proto_test_user_urls(self, status_code): user = models.User.objects.get(id=2) name_slug = slugify(user.username) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'stats'}, + status_code=status_code, + data={'sort':'stats'}, template='user_profile/user_stats.html' ) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'recent'}, + status_code=status_code, + data={'sort':'recent'}, template='user_profile/user_recent.html' ) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'inbox'}, + status_code=status_code, + data={'sort':'inbox'}, template='authopenid/signin.html', follow=True ) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'reputation'}, + status_code=status_code, + data={'sort':'reputation'}, template='user_profile/user_reputation.html' ) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'votes'}, + status_code=status_code, + data={'sort':'votes'}, template='authopenid/signin.html', follow = True ) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'favorites'}, + status_code=status_code, + data={'sort':'favorites'}, template='user_profile/user_favorites.html' ) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': 2, 'slug': name_slug}, - data={'sort':'email_subscriptions'}, + status_code=status_code, + data={'sort':'email_subscriptions'}, template='authopenid/signin.html', follow = True ) + def test_user_urls(self): + self.proto_test_user_urls(status_code=200) + + @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ + not in settings.MIDDLEWARE_CLASSES, + 'no ForumModeMiddleware set') + def test_user_urls_in_closed_forum_mode(self): + askbot_settings.ASKBOT_CLOSED_FORUM_MODE = True + self.proto_test_user_urls(status_code=302) + askbot_settings.ASKBOT_CLOSED_FORUM_MODE = False + + def test_user_urls_logged_in(self): user = models.User.objects.get(id=2) name_slug = slugify(user.username) @@ -360,12 +469,13 @@ class PageLoadTestCase(AskbotTestCase): ) self.client.login(method = 'force', user_id = asker.id) self.try_url( - 'user_profile', + 'user_profile', kwargs={'id': asker.id, 'slug': slugify(asker.username)}, - data={'sort':'inbox'}, + data={'sort':'inbox'}, template='user_profile/user_inbox.html', ) + class AvatarTests(AskbotTestCase): def test_avatar_for_two_word_user_works(self): diff --git a/askbot/tests/permission_assertion_tests.py b/askbot/tests/permission_assertion_tests.py index fc1fbcb7..b83d7827 100644 --- a/askbot/tests/permission_assertion_tests.py +++ b/askbot/tests/permission_assertion_tests.py @@ -1,10 +1,15 @@ import datetime +from django.test.client import Client +from django.core.urlresolvers import reverse +from django.conf import settings from django.test import TestCase from django.core import exceptions from askbot.tests import utils from askbot.conf import settings as askbot_settings from askbot import models from askbot.templatetags import extra_filters as template_filters +from askbot.tests.utils import skipIf + class PermissionAssertionTestCase(TestCase): """base TestCase class for permission @@ -1573,3 +1578,43 @@ class UploadPermissionAssertionTests(PermissionAssertionTestCase): self.user.assert_can_upload_file() except exceptions.PermissionDenied: self.fail('high rep user must be able to upload') + +class ClosedForumTests(utils.AskbotTestCase): + def setUp(self): + self.password = '123' + self.create_user() + self.create_user(username = 'other_user') + self.other_user.set_password(self.password) + self.other_user.save() + self.question = self.post_question() + self.test_url = self.question.get_absolute_url() + self.redirect_to = settings.LOGIN_URL + self.client = Client() + askbot_settings.ASKBOT_CLOSED_FORUM_MODE = True + + @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ + not in settings.MIDDLEWARE_CLASSES, + 'no ForumModeMiddleware set') + def test_login_page_accessable(self): + # futher see in page_load_tests.py + response = self.client.get(reverse('user_signin')) + self.assertEquals(response.status_code, 200) + + @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ + not in settings.MIDDLEWARE_CLASSES, + 'no ForumModeMiddleware set') + def test_anonymous_access(self): + response = self.client.get(self.test_url) + self.assertEquals(response.status_code, 302) + self.assertTrue(self.redirect_to in response['Location']) + + @skipIf('askbot.middleware.forum_mode.ForumModeMiddleware' \ + not in settings.MIDDLEWARE_CLASSES, + 'no ForumModeMiddleware set') + def test_authenticated_access(self): + self.client.login(username=self.other_user.username, password=self.password) + response = self.client.get(self.test_url) + self.assertEquals(response.status_code, 200) + + def tearDown(self): + askbot_settings.ASKBOT_CLOSED_FORUM_MODE = False diff --git a/askbot/tests/utils.py b/askbot/tests/utils.py index 03478419..54a2a0ec 100644 --- a/askbot/tests/utils.py +++ b/askbot/tests/utils.py @@ -1,6 +1,7 @@ """utility functions used by Askbot test cases """ from django.test import TestCase +from django.utils.unittest.compatibility import wraps from askbot import models def create_user( @@ -176,3 +177,46 @@ class AskbotTestCase(TestCase): ) return comment + +""" +Some test decorators, taken from Django-1.3 +""" + + +class SkipTest(Exception): + """ + Raise this exception in a test to skip it. + + Usually you can use TestResult.skip() or one of the skipping decorators + instead of raising this directly. + """ + + +def _id(obj): + return obj + + +def skip(reason): + """ + Unconditionally skip a test. + """ + def decorator(test_item): + if not (isinstance(test_item, type) and issubclass(test_item, TestCase)): + @wraps(test_item) + def skip_wrapper(*args, **kwargs): + raise SkipTest(reason) + test_item = skip_wrapper + + test_item.__unittest_skip__ = True + test_item.__unittest_skip_why__ = reason + return test_item + return decorator + + +def skipIf(condition, reason): + """ + Skip a test if the condition is true. + """ + if condition: + return skip(reason) + return _id diff --git a/askbot/views/writers.py b/askbot/views/writers.py index b7b7e96d..5f073b37 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -274,7 +274,7 @@ def ask(request):#view used to ask a new question else: #attemp to get tags from search state search_state = request.session.get('search_state', None) - if search_state.tags: + if search_state and search_state.tags: tags = ' '.join(search_state.tags) form.initial['tags'] = tags |