From 584a75f812cecdd29d3f38095830f9d7bde821fe Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 24 May 2012 14:02:01 -0300 Subject: [PATCH] [stoa] Validating usp fields on profile basic info --- plugins/stoa/lib/stoa_plugin.rb | 21 +++++++++++++++++++++ plugins/stoa/test/functional/profile_editor_controller.rb | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 0 deletions(-) create mode 100644 plugins/stoa/test/functional/profile_editor_controller.rb diff --git a/plugins/stoa/lib/stoa_plugin.rb b/plugins/stoa/lib/stoa_plugin.rb index 2d4fbb0..581da28 100644 --- a/plugins/stoa/lib/stoa_plugin.rb +++ b/plugins/stoa/lib/stoa_plugin.rb @@ -56,6 +56,27 @@ class StoaPlugin < Noosfero::Plugin :block => block }] end + def profile_editor_controller_filters + block = lambda do + if request.post? + if !params[:profile_data][:usp_id].blank? && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]]) + @profile_data = profile + @profile_data.attributes = params[:profile_data] + @profile_data.valid? + @profile_data.errors.add(:usp_id, _(' validation failed')) + @profile_data.usp_id = nil + @possible_domains = profile.possible_domains + render :action => :edit + end + end + end + + [{ :type => 'before_filter', + :method_name => 'validate_usp_id', + :options => {:only => 'edit'}, + :block => block }] + end + def invite_controller_filters [{ :type => 'before_filter', :method_name => 'check_usp_id_existence', diff --git a/plugins/stoa/test/functional/profile_editor_controller.rb b/plugins/stoa/test/functional/profile_editor_controller.rb new file mode 100644 index 0000000..c0c69c1 --- /dev/null +++ b/plugins/stoa/test/functional/profile_editor_controller.rb @@ -0,0 +1,55 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../../../app/controllers/my_profile/profile_editor_controller' + +# Re-raise errors caught by the controller. +class ProfileEditorController; def rescue_action(e) raise e end; end + +class ProfileEditorTest < ActionController::TestCase + + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] + + def setup + @controller = ProfileEditorController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = User.create(:login => 'test_user', :email => 'test_user@example.com', :password => 'test', :password_confirmation => 'test').person + login_as(@person.identifier) + Environment.default.enable_plugin(StoaPlugin.name) + db = Tempfile.new('stoa-test') + ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => db.path} + end + + attr_accessor :person + + should 'show usp_id field if person did not filled it' do + get :edit, :profile => person.identifier + assert_match /USP number/, @response.body + end + + should 'not show usp_id field if person already filled it' do + person.usp_id = 12345 + person.save + get :edit, :profile => person.identifier + assert_no_match /USP number/, @response.body + end + + should 'not display field if profile is an organization' do + organization = fast_create(Organization) + get :edit, :profile => organization.identifier + assert_no_match /USP number/, @response.body + end + + should 'display error if usp_id does not match with supplied confirmation' do + StoaPlugin::UspUser.stubs(:matches?).returns(false) + post :edit, :profile => person.identifier, :profile_data => {:usp_id => 12345678}, :confirmation_field => 'cpf', :cpf => 99999999 + assert assigns(:profile_data).errors.invalid?(:usp_id) + end + + should 'save usp_id if everyhtings is ok' do + StoaPlugin::UspUser.stubs(:matches?).returns(true) + post :edit, :profile => person.identifier, :profile_data => {:usp_id => 12345678}, :confirmation_field => 'cpf', :cpf => 99999999 + person.reload + assert_equal '12345678', person.usp_id + end + +end -- libgit2 0.21.2