Commit 46ea17994199c92e236ff02338846e02db50c21d

Authored by Rodrigo Souto
2 parents 1d8274ca 56f774e6

Merge remote-tracking branch 'larissa/stoa-plugin-rails3' into larissa-stoa-plugin-rails3

app/models/image.rb
@@ -25,4 +25,7 @@ class Image < ActiveRecord::Base @@ -25,4 +25,7 @@ class Image < ActiveRecord::Base
25 25
26 attr_accessible :uploaded_data 26 attr_accessible :uploaded_data
27 27
  28 + def current_data
  29 + File.file?(full_filename) ? File.read(full_filename) : nil
  30 + end
28 end 31 end
lib/noosfero/plugin.rb
@@ -395,7 +395,7 @@ class Noosfero::Plugin @@ -395,7 +395,7 @@ class Noosfero::Plugin
395 end 395 end
396 396
397 # -> Adds fields to the signup form 397 # -> Adds fields to the signup form
398 - # returns = lambda block that creates html code 398 + # returns = proc that creates html code
399 def signup_extra_contents 399 def signup_extra_contents
400 nil 400 nil
401 end 401 end
@@ -470,7 +470,7 @@ class Noosfero::Plugin @@ -470,7 +470,7 @@ class Noosfero::Plugin
470 end 470 end
471 471
472 # -> Adds fields to the login form 472 # -> Adds fields to the login form
473 - # returns = lambda block that creates html code 473 + # returns = proc that creates html code
474 def login_extra_contents 474 def login_extra_contents
475 nil 475 nil
476 end 476 end
plugins/stoa/lib/ext/person.rb
1 require_dependency 'person' 1 require_dependency 'person'
2 2
3 class Person 3 class Person
  4 + attr_accessible :usp_id, :invitation_code
  5 +
4 validates_uniqueness_of :usp_id, :allow_nil => true 6 validates_uniqueness_of :usp_id, :allow_nil => true
5 settings_items :invitation_code 7 settings_items :invitation_code
6 validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)} 8 validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)}
plugins/stoa/lib/stoa_plugin.rb
@@ -2,8 +2,6 @@ require_dependency 'person' @@ -2,8 +2,6 @@ require_dependency 'person'
2 2
3 class StoaPlugin < Noosfero::Plugin 3 class StoaPlugin < Noosfero::Plugin
4 4
5 - Person.human_names[:usp_id] = _('USP number')  
6 -  
7 def self.plugin_name 5 def self.plugin_name
8 "Stoa" 6 "Stoa"
9 end 7 end
@@ -17,7 +15,7 @@ class StoaPlugin &lt; Noosfero::Plugin @@ -17,7 +15,7 @@ class StoaPlugin &lt; Noosfero::Plugin
17 end 15 end
18 16
19 def signup_extra_contents 17 def signup_extra_contents
20 - lambda { 18 + proc {
21 content_tag(:div, labelled_form_field(_('USP number'), text_field(:profile_data, :usp_id, :id => 'usp_id_field')) + 19 content_tag(:div, labelled_form_field(_('USP number'), text_field(:profile_data, :usp_id, :id => 'usp_id_field')) +
22 content_tag(:small, _('The usp id grants you special powers in the network. Don\'t forget to fill it with a valid number if you have one.'), :id => 'usp-id-balloon') + 20 content_tag(:small, _('The usp id grants you special powers in the network. Don\'t forget to fill it with a valid number if you have one.'), :id => 'usp-id-balloon') +
23 content_tag('p', _("Either this usp number is being used by another user or is not valid"), :id => 'usp-id-invalid') + 21 content_tag('p', _("Either this usp number is being used by another user or is not valid"), :id => 'usp-id-invalid') +
@@ -45,7 +43,7 @@ class StoaPlugin &lt; Noosfero::Plugin @@ -45,7 +43,7 @@ class StoaPlugin &lt; Noosfero::Plugin
45 end 43 end
46 44
47 def login_extra_contents 45 def login_extra_contents
48 - lambda { 46 + proc {
49 content_tag('div', labelled_form_field(_('USP number / Username'), text_field_tag('usp_id_login', '', :id => 'stoa_field_login')) + 47 content_tag('div', labelled_form_field(_('USP number / Username'), text_field_tag('usp_id_login', '', :id => 'stoa_field_login')) +
50 labelled_form_field(_('Password'), password_field_tag('password', '', :id => 'stoa_field_password')), :id => 'stoa-login-fields') 48 labelled_form_field(_('Password'), password_field_tag('password', '', :id => 'stoa_field_password')), :id => 'stoa-login-fields')
51 } 49 }
@@ -62,14 +60,13 @@ class StoaPlugin &lt; Noosfero::Plugin @@ -62,14 +60,13 @@ class StoaPlugin &lt; Noosfero::Plugin
62 end 60 end
63 61
64 def account_controller_filters 62 def account_controller_filters
65 - environment = context.environment  
66 - block = lambda do 63 + block = lambda do |context|
67 params[:profile_data] ||= {} 64 params[:profile_data] ||= {}
68 params[:profile_data][:invitation_code] = params[:invitation_code] 65 params[:profile_data][:invitation_code] = params[:invitation_code]
69 invitation = Task.pending.find(:first, :conditions => {:code => params[:invitation_code]}) 66 invitation = Task.pending.find(:first, :conditions => {:code => params[:invitation_code]})
70 if request.post? 67 if request.post?
71 if !invitation && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]]) 68 if !invitation && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]])
72 - @person = Person.new(:environment => environment) 69 + @person = Person.new(:environment => context.environment)
73 @person.errors.add(:usp_id, _(' validation failed')) 70 @person.errors.add(:usp_id, _(' validation failed'))
74 render :action => :signup 71 render :action => :signup
75 end 72 end
@@ -83,7 +80,7 @@ class StoaPlugin &lt; Noosfero::Plugin @@ -83,7 +80,7 @@ class StoaPlugin &lt; Noosfero::Plugin
83 end 80 end
84 81
85 def profile_editor_controller_filters 82 def profile_editor_controller_filters
86 - block = lambda do 83 + block = proc do
87 if request.post? 84 if request.post?
88 if !params[:profile_data][:usp_id].blank? && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]]) 85 if !params[:profile_data][:usp_id].blank? && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]])
89 @profile_data = profile 86 @profile_data = profile
@@ -106,7 +103,7 @@ class StoaPlugin &lt; Noosfero::Plugin @@ -106,7 +103,7 @@ class StoaPlugin &lt; Noosfero::Plugin
106 def invite_controller_filters 103 def invite_controller_filters
107 [{ :type => 'before_filter', 104 [{ :type => 'before_filter',
108 :method_name => 'check_usp_id_existence', 105 :method_name => 'check_usp_id_existence',
109 - :block => lambda {render_access_denied if !user || user.usp_id.blank?} }] 106 + :block => proc {render_access_denied if !user || user.usp_id.blank?} }]
110 end 107 end
111 108
112 def control_panel_buttons 109 def control_panel_buttons
plugins/stoa/lib/stoa_plugin/person_api.rb
@@ -28,7 +28,7 @@ class StoaPlugin::PersonApi &lt; Noosfero::FieldsDecorator @@ -28,7 +28,7 @@ class StoaPlugin::PersonApi &lt; Noosfero::FieldsDecorator
28 end 28 end
29 29
30 def tags 30 def tags
31 - articles.published.tag_counts({:order => 'tags.count desc', :limit => 10}).inject({}) do |memo,tag| 31 + articles.published.tag_counts({:order => 'count desc', :limit => 10}).inject({}) do |memo,tag|
32 memo[tag.name] = tag.count 32 memo[tag.name] = tag.count
33 memo 33 memo
34 end 34 end
plugins/stoa/lib/stoa_plugin/usp_user.rb
@@ -3,7 +3,7 @@ class StoaPlugin::UspUser &lt; ActiveRecord::Base @@ -3,7 +3,7 @@ class StoaPlugin::UspUser &lt; ActiveRecord::Base
3 establish_connection(:stoa) 3 establish_connection(:stoa)
4 set_table_name('pessoa') 4 set_table_name('pessoa')
5 5
6 - SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] 6 + SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']
7 7
8 alias_attribute :cpf, :numcpf 8 alias_attribute :cpf, :numcpf
9 alias_attribute :birth_date, :dtanas 9 alias_attribute :birth_date, :dtanas
plugins/stoa/test/functional/account_controller_test.rb
@@ -6,32 +6,29 @@ class AccountController; def rescue_action(e) raise e end; end @@ -6,32 +6,29 @@ class AccountController; def rescue_action(e) raise e end; end
6 6
7 class AccountControllerTest < ActionController::TestCase 7 class AccountControllerTest < ActionController::TestCase
8 8
9 - SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] 9 + SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']
  10 +
  11 + @db = Tempfile.new('stoa-test')
  12 + configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path}
  13 + ActiveRecord::Base.establish_connection(:stoa)
  14 + ActiveRecord::Schema.verbose = false
  15 + ActiveRecord::Schema.create_table "pessoa" do |t|
  16 + t.integer "codpes"
  17 + t.text "numcpf"
  18 + t.date "dtanas"
  19 + end
  20 + ActiveRecord::Base.establish_connection(:test)
10 21
11 def setup 22 def setup
12 @controller = AccountController.new 23 @controller = AccountController.new
13 @request = ActionController::TestRequest.new 24 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new 25 @response = ActionController::TestResponse.new
15 - @db = Tempfile.new('stoa-test')  
16 - configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path}  
17 - ActiveRecord::Base.establish_connection(:stoa)  
18 - ActiveRecord::Schema.verbose = false  
19 - ActiveRecord::Schema.create_table "pessoa" do |t|  
20 - t.integer "codpes"  
21 - t.text "numcpf"  
22 - t.date "dtanas"  
23 - end  
24 - ActiveRecord::Base.establish_connection(:test)  
25 - StoaPlugin::UspUser.create!(:codpes => 12345678, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30') 26 + StoaPlugin::UspUser.create!({:codpes => 12345678, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30'}, :without_protection => true)
26 Environment.default.enable_plugin(StoaPlugin.name) 27 Environment.default.enable_plugin(StoaPlugin.name)
27 @user = create_user('joao-stoa', {:password => 'pass', :password_confirmation => 'pass'},:usp_id=>'87654321') 28 @user = create_user('joao-stoa', {:password => 'pass', :password_confirmation => 'pass'},:usp_id=>'87654321')
28 @user.activate 29 @user.activate
29 end 30 end
30 31
31 - def teardown  
32 - @db.unlink  
33 - end  
34 -  
35 should 'fail if confirmation value doesn\'t match' do 32 should 'fail if confirmation value doesn\'t match' do
36 #StoaPlugin::UspUser.stubs(:matches?).returns(false) 33 #StoaPlugin::UspUser.stubs(:matches?).returns(false)
37 post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '00000000' 34 post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '00000000'
@@ -41,10 +38,10 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -41,10 +38,10 @@ class AccountControllerTest &lt; ActionController::TestCase
41 should 'pass if confirmation value matches' do 38 should 'pass if confirmation value matches' do
42 #StoaPlugin::UspUser.stubs(:matches?).returns(true) 39 #StoaPlugin::UspUser.stubs(:matches?).returns(true)
43 post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '12345678' 40 post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '12345678'
44 - assert_nil assigns(:person).errors[:usp_id] 41 + assert !assigns(:person).errors.include?(:usp_id)
45 end 42 end
46 43
47 - should 'inlude invitation_code param in the persons attributes' do 44 + should 'include invitation_code param in the persons attributes' do
48 get :signup, :invitation_code => 12345678 45 get :signup, :invitation_code => 12345678
49 assert assigns(:person).invitation_code == '12345678' 46 assert assigns(:person).invitation_code == '12345678'
50 end 47 end
plugins/stoa/test/functional/profile_editor_controller_test.rb
@@ -6,7 +6,7 @@ class ProfileEditorController; def rescue_action(e) raise e end; end @@ -6,7 +6,7 @@ class ProfileEditorController; def rescue_action(e) raise e end; end
6 6
7 class StoaPluginProfileEditorControllerTest < ActionController::TestCase 7 class StoaPluginProfileEditorControllerTest < ActionController::TestCase
8 8
9 - SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] 9 + SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']
10 10
11 def setup 11 def setup
12 @controller = ProfileEditorController.new 12 @controller = ProfileEditorController.new
@@ -45,7 +45,7 @@ class StoaPluginProfileEditorControllerTest &lt; ActionController::TestCase @@ -45,7 +45,7 @@ class StoaPluginProfileEditorControllerTest &lt; ActionController::TestCase
45 should 'display error if usp_id does not match with supplied confirmation' do 45 should 'display error if usp_id does not match with supplied confirmation' do
46 StoaPlugin::UspUser.stubs(:matches?).returns(false) 46 StoaPlugin::UspUser.stubs(:matches?).returns(false)
47 post :edit, :profile => person.identifier, :profile_data => {:usp_id => 12345678}, :confirmation_field => 'cpf', :cpf => 99999999 47 post :edit, :profile => person.identifier, :profile_data => {:usp_id => 12345678}, :confirmation_field => 'cpf', :cpf => 99999999
48 - assert assigns(:profile_data).errors.invalid?(:usp_id) 48 + assert assigns(:profile_data).errors.include?(:usp_id)
49 end 49 end
50 50
51 should 'save usp_id if everyhtings is ok' do 51 should 'save usp_id if everyhtings is ok' do
plugins/stoa/test/functional/stoa_plugin_controller_test.rb
@@ -6,7 +6,7 @@ class StoaPluginController; def rescue_action(e) raise e end; end @@ -6,7 +6,7 @@ class StoaPluginController; def rescue_action(e) raise e end; end
6 6
7 class StoaPluginControllerTest < ActionController::TestCase 7 class StoaPluginControllerTest < ActionController::TestCase
8 8
9 - SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] 9 + SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']
10 10
11 def setup 11 def setup
12 @controller = StoaPluginController.new 12 @controller = StoaPluginController.new
@@ -18,6 +18,7 @@ class StoaPluginControllerTest &lt; ActionController::TestCase @@ -18,6 +18,7 @@ class StoaPluginControllerTest &lt; ActionController::TestCase
18 env.enable('skip_new_user_email_confirmation') 18 env.enable('skip_new_user_email_confirmation')
19 env.save! 19 env.save!
20 @user = create_user_full('real_user', {:password => '123456', :password_confirmation => '123456'}, {:usp_id => 9999999}) 20 @user = create_user_full('real_user', {:password => '123456', :password_confirmation => '123456'}, {:usp_id => 9999999})
  21 + @user.activate
21 end 22 end
22 23
23 attr_accessor :user 24 attr_accessor :user
plugins/stoa/test/unit/person_test.rb
@@ -15,7 +15,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase @@ -15,7 +15,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase
15 another_person = Person.new(:usp_id => usp_id) 15 another_person = Person.new(:usp_id => usp_id)
16 another_person.valid? 16 another_person.valid?
17 17
18 - assert another_person.errors.invalid?(:usp_id) 18 + assert another_person.errors.include?(:usp_id)
19 end 19 end
20 20
21 should 'not allow usp_id as an empty string' do 21 should 'not allow usp_id as an empty string' do
@@ -28,17 +28,17 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase @@ -28,17 +28,17 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase
28 should 'allow nil usp_id only if person has an invitation_code or is a template' do 28 should 'allow nil usp_id only if person has an invitation_code or is a template' do
29 person = Person.new(:environment => environment) 29 person = Person.new(:environment => environment)
30 person.valid? 30 person.valid?
31 - assert person.errors.invalid?(:usp_id) 31 + assert person.errors.include?(:usp_id)
32 32
33 Task.create!(:code => 12345678) 33 Task.create!(:code => 12345678)
34 person.invitation_code = 12345678 34 person.invitation_code = 12345678
35 person.valid? 35 person.valid?
36 - assert !person.errors.invalid?(:usp_id) 36 + assert !person.errors.include?(:usp_id)
37 37
38 person.invitation_code = nil 38 person.invitation_code = nil
39 person.is_template = true 39 person.is_template = true
40 person.valid? 40 person.valid?
41 - assert !person.errors.invalid?(:usp_id) 41 + assert !person.errors.include?(:usp_id)
42 end 42 end
43 43
44 should 'allow multiple nil usp_id' do 44 should 'allow multiple nil usp_id' do
@@ -47,7 +47,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase @@ -47,7 +47,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase
47 person = Person.new(:invitation_code => 87654321) 47 person = Person.new(:invitation_code => 87654321)
48 person.valid? 48 person.valid?
49 49
50 - assert !person.errors.invalid?(:usp_id) 50 + assert !person.errors.include?(:usp_id)
51 end 51 end
52 52
53 should 'not allow person to be saved with a finished invitation that is not his own' do 53 should 'not allow person to be saved with a finished invitation that is not his own' do
@@ -56,7 +56,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase @@ -56,7 +56,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase
56 person = Person.new(:environment => environment, :invitation_code => 87654321) 56 person = Person.new(:environment => environment, :invitation_code => 87654321)
57 person.valid? 57 person.valid?
58 58
59 - assert person.errors.invalid?(:usp_id) 59 + assert person.errors.include?(:usp_id)
60 end 60 end
61 61
62 should 'allow person to be saved with a finished invitation if it is his own' do 62 should 'allow person to be saved with a finished invitation if it is his own' do
@@ -68,7 +68,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase @@ -68,7 +68,7 @@ class StoaPlugin::Person &lt; ActiveSupport::TestCase
68 t.finish 68 t.finish
69 69
70 person.valid? 70 person.valid?
71 - assert !person.errors.invalid?(:usp_id) 71 + assert !person.errors.include?(:usp_id)
72 end 72 end
73 73
74 74
plugins/stoa/test/unit/usp_user_test.rb
@@ -2,24 +2,21 @@ require File.dirname(__FILE__) + &#39;/../../../../test/test_helper&#39; @@ -2,24 +2,21 @@ require File.dirname(__FILE__) + &#39;/../../../../test/test_helper&#39;
2 2
3 class StoaPlugin::UspUserTest < ActiveSupport::TestCase 3 class StoaPlugin::UspUserTest < ActiveSupport::TestCase
4 4
5 - SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt']  
6 -  
7 - def setup  
8 - @db = Tempfile.new('stoa-test')  
9 - configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path}  
10 - ActiveRecord::Base.establish_connection(:stoa)  
11 - ActiveRecord::Schema.verbose = false  
12 - ActiveRecord::Schema.create_table "pessoa" do |t|  
13 - t.integer "codpes"  
14 - t.text "numcpf"  
15 - t.date "dtanas"  
16 - end  
17 - ActiveRecord::Base.establish_connection(:test)  
18 - StoaPlugin::UspUser.create!(:codpes => 123456, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30') 5 + SALT=YAML::load(File.open(StoaPlugin.root_path + 'config.yml'))['salt']
  6 +
  7 + @db = Tempfile.new('stoa-test')
  8 + configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path}
  9 + ActiveRecord::Base.establish_connection(:stoa)
  10 + ActiveRecord::Schema.verbose = false
  11 + ActiveRecord::Schema.create_table "pessoa" do |t|
  12 + t.integer "codpes"
  13 + t.text "numcpf"
  14 + t.date "dtanas"
19 end 15 end
  16 + ActiveRecord::Base.establish_connection(:test)
20 17
21 - def teardown  
22 - @db.unlink 18 + def setup
  19 + StoaPlugin::UspUser.create({:codpes => 123456, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30'}, :without_protection => true)
23 end 20 end
24 21
25 should 'check existence of usp_id' do 22 should 'check existence of usp_id' do
@@ -48,4 +45,3 @@ class StoaPlugin::UspUserTest &lt; ActiveSupport::TestCase @@ -48,4 +45,3 @@ class StoaPlugin::UspUserTest &lt; ActiveSupport::TestCase
48 assert !StoaPlugin::UspUser.matches?(123456, nil, '00012345678') 45 assert !StoaPlugin::UspUser.matches?(123456, nil, '00012345678')
49 end 46 end
50 end 47 end
51 -