Commit 0b4c072dea87683a324665144be219848dabaa00
1 parent
24b37d9a
Exists in
master
and in
29 other branches
[stoa] Signup Complement
* Checking wheather the user has cpf to decide what confirmation field to show (cpf of birth date)
Showing
8 changed files
with
143 additions
and
42 deletions
Show diff stats
plugins/stoa/controllers/stoa_plugin_controller.rb
... | ... | @@ -32,4 +32,12 @@ class StoaPluginController < PublicController |
32 | 32 | end |
33 | 33 | end |
34 | 34 | |
35 | + def check_cpf | |
36 | + begin | |
37 | + render :text => { :exists => !StoaPlugin::UspUser.find_by_codpes(params[:usp_id]).cpf.blank? }.to_json | |
38 | + rescue Exception => exception | |
39 | + render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json | |
40 | + end | |
41 | + end | |
42 | + | |
35 | 43 | end | ... | ... |
plugins/stoa/lib/stoa_plugin.rb
... | ... | @@ -20,37 +20,16 @@ class StoaPlugin < Noosfero::Plugin |
20 | 20 | def signup_extra_contents |
21 | 21 | lambda { |
22 | 22 | required(labelled_form_field(_('USP number'), text_field_tag('profile_data[usp_id]', '', :id => 'usp_id_field'))) + |
23 | - labelled_form_field(_('Select a confirmation data'), select_tag('confirmation_field', | |
24 | - options_for_select([['CPF','cpf'], [_('Birth date (yyyy-mm-dd)'), 'birth_date']]) | |
25 | - )) + | |
26 | - required(labelled_form_field(_('Confirmation value'), text_field_tag('confirmation_value', '', :placeholder=>_('Confirmation value')))) + | |
27 | - javascript_tag(<<-EOF | |
28 | - jQuery("#usp_id_field").change(function(){ | |
29 | - var me=this; | |
30 | - jQuery(this).addClass('checking').removeClass('validated'); | |
31 | - jQuery.getJSON('#{url_for(:controller => 'stoa_plugin', :action => 'check_usp_id')}?usp_id='+this.value, | |
32 | - function(data){ | |
33 | - if(data.exists) jQuery(me).removeClass('checking').addClass('validated'); | |
34 | - else jQuery(me).removeClass('checking').addClass('invalid'); | |
35 | - if(data.error) displayValidationUspIdError(data.error); | |
36 | - } | |
37 | - ); | |
38 | - }); | |
39 | - | |
40 | - function displayValidationUspIdError(error){ | |
41 | - jQuery.colorbox({html: '<h2>'+error.message+'</h2>'+error.backtrace.join("<br />"), | |
42 | - height: "80%", | |
43 | - width: "70%" }); | |
44 | - } | |
45 | - EOF | |
46 | - ) | |
23 | + content_tag('div', required(labelled_form_field(_('Birth date (yyyy-mm-dd)'), text_field_tag('birth_date', ''))), :id => 'signup-birth-date', :style => 'display: none') + | |
24 | + content_tag('div', required(labelled_form_field(_('CPF'), text_field_tag('cpf', ''))), :id => 'signup-cpf', :style => 'display:none') + | |
25 | + javascript_include_tag('../plugins/stoa/javascripts/jquery.observe_field', '../plugins/stoa/javascripts/signup_complement') | |
47 | 26 | } |
48 | 27 | end |
49 | 28 | |
50 | 29 | def account_controller_filters |
51 | 30 | block = lambda do |
52 | 31 | if request.post? |
53 | - if !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[:confirmation_value]) | |
32 | + if !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]]) | |
54 | 33 | @person = Person.new |
55 | 34 | @person.errors.add(:usp_id, _(' validation failed')) |
56 | 35 | render :action => :signup | ... | ... |
187 Bytes
... | ... | @@ -0,0 +1,39 @@ |
1 | +// jquery.observe_field.js | |
2 | + | |
3 | + | |
4 | +(function( $ ){ | |
5 | + | |
6 | + jQuery.fn.observe_field = function(frequency, callback) { | |
7 | + | |
8 | + frequency = frequency * 1000; // translate to milliseconds | |
9 | + | |
10 | + return this.each(function(){ | |
11 | + var $this = $(this); | |
12 | + var prev = $this.val(); | |
13 | + | |
14 | + var check = function() { | |
15 | + var val = $this.val(); | |
16 | + if(prev != val){ | |
17 | + prev = val; | |
18 | + $this.map(callback); // invokes the callback on $this | |
19 | + } | |
20 | + }; | |
21 | + | |
22 | + var reset = function() { | |
23 | + if(ti){ | |
24 | + clearInterval(ti); | |
25 | + ti = setInterval(check, frequency); | |
26 | + } | |
27 | + }; | |
28 | + | |
29 | + check(); | |
30 | + var ti = setInterval(check, frequency); // invoke check periodically | |
31 | + | |
32 | + // reset counter after user interaction | |
33 | + $this.bind('keyup click mousemove', reset); //mousemove is for selects | |
34 | + }); | |
35 | + | |
36 | + }; | |
37 | + | |
38 | +})( jQuery ); | |
39 | + | ... | ... |
... | ... | @@ -0,0 +1,41 @@ |
1 | +jQuery("#usp_id_field").observe_field(1, function(){ | |
2 | + var me=this; | |
3 | + jQuery(this).addClass('checking').removeClass('validated'); | |
4 | + jQuery.getJSON('/plugin/stoa/check_usp_id?usp_id='+me.value, | |
5 | + function(data){ | |
6 | + if(data.exists) { | |
7 | + jQuery.getJSON('/plugin/stoa/check_cpf?usp_id='+me.value, | |
8 | + function(data){ | |
9 | + if(data.exists){ | |
10 | + jQuery('#signup-birth-date').hide(); | |
11 | + jQuery('#signup-cpf').show(); | |
12 | + jQuery('#confirmation_field').remove(); | |
13 | + jQuery('#signup-form').append('<input id="confirmation_field" type="hidden" value="cpf" name="confirmation_field">') | |
14 | + } | |
15 | + else { | |
16 | + jQuery('#signup-cpf').hide(); | |
17 | + jQuery('#signup-birth-date').show(); | |
18 | + jQuery('#confirmation_field').remove(); | |
19 | + jQuery('#signup-form').append('<input id="confirmation_field" type="hidden" value="birth_date" name="confirmation_field">') | |
20 | + } | |
21 | + jQuery('#signup-form .submit').attr('disabled', false); | |
22 | + jQuery(me).removeClass('checking').addClass('validated'); | |
23 | + }); | |
24 | + } | |
25 | + else { | |
26 | + jQuery('#signup-form .submit').attr('disabled', true); | |
27 | + jQuery('#signup-birth-date').hide(); | |
28 | + jQuery('#signup-cpf').hide(); | |
29 | + jQuery(me).removeClass('checking').addClass('invalid'); | |
30 | + } | |
31 | + if(data.error) displayValidationUspIdError(data.error); | |
32 | + } | |
33 | +); | |
34 | +}); | |
35 | + | |
36 | +function displayValidationUspIdError(error){ | |
37 | + jQuery.colorbox({html: '<h2>'+error.message+'</h2>'+error.backtrace.join("<br />"), | |
38 | + height: "80%", | |
39 | + width: "70%" }); | |
40 | +} | |
41 | + | ... | ... |
plugins/stoa/public/style.css
1 | 1 | #signup-form label[for="usp_id_field"], |
2 | -#signup-form label[for="confirmation_field"] { | |
2 | +#signup-form label[for="cpf"], | |
3 | +#signup-form label[for="birth_date"] { | |
3 | 4 | display: block; |
4 | 5 | } |
5 | 6 | |
7 | +#content #signup-form input.submit[disabled=""] { | |
8 | + background-image: url(/plugins/stoa/images/grey-bg.png); | |
9 | + text-shadow: 0 -1px 0 #686156; | |
10 | + cursor: default; | |
11 | +} | |
12 | + | ... | ... |
plugins/stoa/test/functional/account_controller_test.rb
... | ... | @@ -6,26 +6,35 @@ class AccountController; def rescue_action(e) raise e end; end |
6 | 6 | |
7 | 7 | class AccountControllerTest < ActionController::TestCase |
8 | 8 | |
9 | + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] | |
10 | + | |
9 | 11 | def setup |
10 | 12 | @controller = AccountController.new |
11 | 13 | @request = ActionController::TestRequest.new |
12 | 14 | @response = ActionController::TestResponse.new |
13 | - environment = Environment.default | |
14 | - environment.enabled_plugins = ['StoaPlugin'] | |
15 | - environment.save! | |
16 | 15 | @db = Tempfile.new('stoa-test') |
17 | 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 | + Environment.default.enable_plugin(StoaPlugin.name) | |
18 | 27 | end |
19 | 28 | |
20 | 29 | should 'fail if confirmation value doesn\'t match' do |
21 | - StoaPlugin::UspUser.stubs(:matches?).returns(false) | |
22 | - post :signup, :profile_data => {:usp_id => '87654321'}, :confirmation_field => 'cpf', :confirmation_value => '00000000' | |
30 | + #StoaPlugin::UspUser.stubs(:matches?).returns(false) | |
31 | + post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '00000000' | |
23 | 32 | assert_not_nil assigns(:person).errors[:usp_id] |
24 | 33 | end |
25 | 34 | |
26 | 35 | should 'pass if confirmation value matches' do |
27 | - StoaPlugin::UspUser.stubs(:matches?).returns(true) | |
28 | - post :signup, :profile_data => {:usp_id => '87654321'}, :confirmation_field => 'cpf', :confirmation_value => '12345678' | |
36 | + #StoaPlugin::UspUser.stubs(:matches?).returns(true) | |
37 | + post :signup, :profile_data => {:usp_id => '12345678'}, :confirmation_field => 'cpf', :cpf => '12345678' | |
29 | 38 | assert_nil assigns(:person).errors[:usp_id] |
30 | 39 | end |
31 | 40 | ... | ... |
plugins/stoa/test/functional/stoa_plugin_controller_test.rb
... | ... | @@ -6,16 +6,25 @@ class StoaPluginController; def rescue_action(e) raise e end; end |
6 | 6 | |
7 | 7 | class StoaPluginControllerTest < ActionController::TestCase |
8 | 8 | |
9 | + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] | |
10 | + | |
9 | 11 | def setup |
10 | 12 | @controller = StoaPluginController.new |
11 | 13 | @request = ActionController::TestRequest.new |
12 | 14 | @response = ActionController::TestResponse.new |
13 | - @user = create_user('real_user', :password => '123456', :password_confirmation => '123456') | |
14 | - environment = Environment.default | |
15 | - environment.enabled_plugins = ['StoaPlugin'] | |
16 | - environment.save! | |
17 | 15 | @db = Tempfile.new('stoa-test') |
18 | 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 | + @user = User.find_by_login('real_user') || create_user('real_user', :password => '123456', :password_confirmation => '123456') | |
26 | + StoaPlugin::UspUser.create!(:codpes => 12345678, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30') | |
27 | + Environment.default.enable_plugin(StoaPlugin.name) | |
19 | 28 | end |
20 | 29 | |
21 | 30 | attr_accessor :user |
... | ... | @@ -52,18 +61,27 @@ class StoaPluginControllerTest < ActionController::TestCase |
52 | 61 | assert_equal user.login, json_response['username'] |
53 | 62 | end |
54 | 63 | |
64 | + should 'check valid usp id' do | |
65 | + get :check_usp_id, :usp_id => '12345678' | |
66 | + assert json_response['exists'] | |
67 | + end | |
68 | + | |
55 | 69 | should 'check invalid usp id' do |
56 | - StoaPlugin::UspUser.stubs(:exists?).returns(false) | |
57 | - get :check_usp_id, :usp_id => '987654321' | |
70 | + get :check_usp_id, :usp_id => '87654321' | |
58 | 71 | assert !json_response['exists'] |
59 | 72 | end |
60 | 73 | |
61 | - should 'check valid usp id' do | |
62 | - StoaPlugin::UspUser.stubs(:exists?).returns(true) | |
63 | - get :check_usp_id, :usp_id => '987654321' | |
74 | + should 'check existent cpf' do | |
75 | + get :check_cpf, :usp_id => '12345678' | |
64 | 76 | assert json_response['exists'] |
65 | 77 | end |
66 | 78 | |
79 | + should 'check not existent cpf' do | |
80 | + StoaPlugin::UspUser.create(:codpes => 87654321, :birth_date => '1970-01-30') | |
81 | + get :check_cpf, :usp_id => '87654321' | |
82 | + assert !json_response['exists'] | |
83 | + end | |
84 | + | |
67 | 85 | private |
68 | 86 | |
69 | 87 | def json_response | ... | ... |