Commit 90df319e0f15b4b6328cadb2a5771e2c1e09286a
1 parent
c3900442
Exists in
master
and in
28 other branches
First version of RequireAuthToCommentPlugin
(ActionItem2310)
Showing
7 changed files
with
121 additions
and
0 deletions
Show diff stats
plugins/require_auth_to_comment/features/require_auth_to_comment.feature
0 → 100644
| @@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
| 1 | +Feature: require authentication to comment | ||
| 2 | + | ||
| 3 | + Background: | ||
| 4 | + Given plugin RequireAuthToCommentPlugin is enabled on environment | ||
| 5 | + And the following users | ||
| 6 | + | login | | ||
| 7 | + | bozo | | ||
| 8 | + | ||
| 9 | + Scenario: enabling unauthenticated comment per profile | ||
| 10 | + Given I am logged in as "bozo" | ||
| 11 | + When I edit my profile | ||
| 12 | + And I check "Accept comments from unauthenticated users" | ||
| 13 | + And I press "Save" | ||
| 14 | + Then I edit my profile | ||
| 15 | + And the "Accept comments from unauthenticated users" checkbox should be checked |
plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin.rb
0 → 100644
| @@ -0,0 +1,45 @@ | @@ -0,0 +1,45 @@ | ||
| 1 | +require_dependency 'require_auth_to_comment_plugin/ext/profile' | ||
| 2 | + | ||
| 3 | +class RequireAuthToCommentPlugin < Noosfero::Plugin | ||
| 4 | + | ||
| 5 | + include ActionView::Helpers::TagHelper | ||
| 6 | + include ActionView::Helpers::FormTagHelper | ||
| 7 | + include ApplicationHelper | ||
| 8 | + | ||
| 9 | + def self.plugin_name | ||
| 10 | + "RequireAuthToCommentPlugin" | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + def self.plugin_description | ||
| 14 | + _("Requires users to authenticate in order to post comments.") | ||
| 15 | + end | ||
| 16 | + | ||
| 17 | + def filter_comment(c) | ||
| 18 | + c.reject! unless logged_in? || allowed_by_profile | ||
| 19 | + end | ||
| 20 | + | ||
| 21 | + def profile_editor_extras | ||
| 22 | + expanded_template('profile-editor-extras.rhtml') | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + def stylesheet? | ||
| 26 | + true | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + def js_files | ||
| 30 | + ['hide_comment_form.js'] | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + def body_beginning | ||
| 34 | + "<meta name='profile.allow_unauthenticated_comments'/>" if allowed_by_profile | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + protected | ||
| 38 | + | ||
| 39 | + delegate :logged_in?, :to => :context | ||
| 40 | + | ||
| 41 | + def allowed_by_profile | ||
| 42 | + context.profile && context.profile.allow_unauthenticated_comments | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | +end |
plugins/require_auth_to_comment/lib/require_auth_to_comment_plugin/ext/profile.rb
0 → 100644
plugins/require_auth_to_comment/public/hide_comment_form.js
0 → 100644
| @@ -0,0 +1,8 @@ | @@ -0,0 +1,8 @@ | ||
| 1 | +(function($) { | ||
| 2 | + $(window).bind('userDataLoaded', function(event, data) { | ||
| 3 | + if (data.login || $('meta[name=profile.allow_unauthenticated_comments]').length > 0) { | ||
| 4 | + $('.post-comment-button').show(); | ||
| 5 | + $('#page-comment-form').show(); | ||
| 6 | + } | ||
| 7 | + }); | ||
| 8 | +})(jQuery); |
plugins/require_auth_to_comment/test/unit/require_auth_to_comment_plugin_test.rb
0 → 100644
| @@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
| 2 | + | ||
| 3 | +class RequireAuthToCommentPluginTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + def setup | ||
| 6 | + @plugin = RequireAuthToCommentPlugin.new | ||
| 7 | + @comment = Comment.new | ||
| 8 | + end | ||
| 9 | + | ||
| 10 | + attr_reader :plugin, :comment | ||
| 11 | + | ||
| 12 | + should 'reject comments for unauthenticated users' do | ||
| 13 | + plugin.context = logged_in(false) | ||
| 14 | + plugin.filter_comment(comment) | ||
| 15 | + assert comment.rejected? | ||
| 16 | + end | ||
| 17 | + | ||
| 18 | + should 'allow comments from authenticated users' do | ||
| 19 | + plugin.context = logged_in(true) | ||
| 20 | + plugin.filter_comment(comment) | ||
| 21 | + assert !comment.rejected? | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + should 'allow comments from unauthenticated users if allowed by profile' do | ||
| 25 | + plugin.context = logged_in(false) | ||
| 26 | + plugin.context.profile.allow_unauthenticated_comments = true | ||
| 27 | + | ||
| 28 | + plugin.filter_comment(comment) | ||
| 29 | + assert !comment.rejected? | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + protected | ||
| 33 | + | ||
| 34 | + def logged_in(boolean) | ||
| 35 | + controller = mock() | ||
| 36 | + controller.stubs(:logged_in?).returns(boolean) | ||
| 37 | + controller.stubs(:profile).returns(Profile.new) | ||
| 38 | + Noosfero::Plugin::Context.new(controller) | ||
| 39 | + end | ||
| 40 | + | ||
| 41 | +end |
plugins/require_auth_to_comment/views/profile-editor-extras.rhtml
0 → 100644