Commit 4f51c61bd4122ad52f69e71ee393b6f17f7d9e26

Authored by AntonioTerceiro
1 parent d8dd8aca

ActionItem3: saving features



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@93 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
@@ -84,4 +84,10 @@ class ApplicationController < ActionController::Base @@ -84,4 +84,10 @@ class ApplicationController < ActionController::Base
84 # TODO: check access control 84 # TODO: check access control
85 end 85 end
86 86
  87 + # declares that the given <tt>actions</tt> cannot be accessed by other HTTP
  88 + # method besides POST.
  89 + def self.post_only(actions, redirect = { :action => 'index'})
  90 + verify :method => :post, :only => actions, :redirect_to => redirect
  91 + end
  92 +
87 end 93 end
app/controllers/features_controller.rb
@@ -5,8 +5,14 @@ class FeaturesController &lt; ApplicationController @@ -5,8 +5,14 @@ class FeaturesController &lt; ApplicationController
5 @features = VirtualCommunity.available_features 5 @features = VirtualCommunity.available_features
6 end 6 end
7 7
  8 + post_only :update
8 def update 9 def update
9 - @virtual_community.enabled_features = params[:features].keys 10 + features = if params[:features].nil?
  11 + []
  12 + else
  13 + params[:features].keys
  14 + end
  15 + @virtual_community.enabled_features = features
10 @virtual_community.save! 16 @virtual_community.save!
11 flash[:notice] = _('Features updated successfully.') 17 flash[:notice] = _('Features updated successfully.')
12 redirect_to :action => 'index' 18 redirect_to :action => 'index'
test/functional/application_controller_test.rb 0 → 100644
@@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'test_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class TestController; def rescue_action(e) raise e end; end
  6 +
  7 +class ApplicationControllerTest < Test::Unit::TestCase
  8 +
  9 + fixtures :profiles, :virtual_communities, :domains
  10 +
  11 + def setup
  12 + @controller = TestController.new
  13 + @request = ActionController::TestRequest.new
  14 + @response = ActionController::TestResponse.new
  15 + end
  16 +
  17 + def test_get_against_post_only
  18 + get :post_only
  19 + assert_redirected_to :action => 'index'
  20 + end
  21 + def test_post_against_post_only
  22 + post :post_only
  23 + assert_response :success
  24 + assert_tag :tag => 'span', :content => 'post_only'
  25 + end
  26 +
  27 +end
test/functional/features_controller_test.rb
@@ -12,10 +12,10 @@ class FeaturesControllerTest &lt; Test::Unit::TestCase @@ -12,10 +12,10 @@ class FeaturesControllerTest &lt; Test::Unit::TestCase
12 @controller = FeaturesController.new 12 @controller = FeaturesController.new
13 @request = ActionController::TestRequest.new 13 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new 14 @response = ActionController::TestResponse.new
15 - uses_host 'anhetegua.net'  
16 end 15 end
17 16
18 def test_listing_features 17 def test_listing_features
  18 + uses_host 'anhetegua.net'
19 get :index 19 get :index
20 assert_template 'index' 20 assert_template 'index'
21 VirtualCommunity.available_features.each do |feature, text| 21 VirtualCommunity.available_features.each do |feature, text|
@@ -23,12 +23,32 @@ class FeaturesControllerTest &lt; Test::Unit::TestCase @@ -23,12 +23,32 @@ class FeaturesControllerTest &lt; Test::Unit::TestCase
23 end 23 end
24 end 24 end
25 25
26 - def test_update_features  
27 - get :update, :features => { 'feature1' => '1', 'feature2' => '1' } 26 + def test_update
  27 + uses_host 'anhetegua.net'
  28 + post :update, :features => { 'feature1' => '1', 'feature2' => '1' }
  29 + assert_redirected_to :action => 'index'
  30 + assert_kind_of String, flash[:notice]
  31 + v = VirtualCommunity.find(virtual_communities(:anhetegua_net).id)
  32 + assert v.enabled?('feature2')
  33 + assert v.enabled?('feature2')
  34 + assert !v.enabled?('feature3')
  35 + end
  36 +
  37 + def test_update_disable_all
  38 + uses_host 'anhetegua.net'
  39 + post :update # no features
28 assert_redirected_to :action => 'index' 40 assert_redirected_to :action => 'index'
29 assert_kind_of String, flash[:notice] 41 assert_kind_of String, flash[:notice]
30 - v = VirtualCommunity.find(virtual_communities(:colivre_net).id)  
31 - assert v.enabled?('feature1') && v.enabled?('feature2') && !v.enabled?('feature3') 42 + v = VirtualCommunity.find(virtual_communities(:anhetegua_net).id)
  43 + assert !v.enabled?('feature1')
  44 + assert !v.enabled?('feature2')
  45 + assert !v.enabled?('feature3')
  46 + end
  47 +
  48 + def test_update_no_post
  49 + uses_host 'anhetegua.net'
  50 + get :update
  51 + assert_redirected_to :action => 'index'
32 end 52 end
33 53
34 end 54 end
test/mocks/test/test_controller.rb
1 class TestController < ApplicationController 1 class TestController < ApplicationController
  2 + def index
  3 + render :text => 'index'
  4 + end
  5 +
  6 + post_only 'post_only'
  7 + def post_only
  8 + render :text => '<span>post_only</span>'
  9 + end
2 end 10 end