Commit 584a75f812cecdd29d3f38095830f9d7bde821fe
1 parent
619b7714
Exists in
master
and in
29 other branches
[stoa] Validating usp fields on profile basic info
Showing
2 changed files
with
76 additions
and
0 deletions
Show diff stats
plugins/stoa/lib/stoa_plugin.rb
... | ... | @@ -56,6 +56,27 @@ class StoaPlugin < Noosfero::Plugin |
56 | 56 | :block => block }] |
57 | 57 | end |
58 | 58 | |
59 | + def profile_editor_controller_filters | |
60 | + block = lambda do | |
61 | + if request.post? | |
62 | + if !params[:profile_data][:usp_id].blank? && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[params[:confirmation_field]]) | |
63 | + @profile_data = profile | |
64 | + @profile_data.attributes = params[:profile_data] | |
65 | + @profile_data.valid? | |
66 | + @profile_data.errors.add(:usp_id, _(' validation failed')) | |
67 | + @profile_data.usp_id = nil | |
68 | + @possible_domains = profile.possible_domains | |
69 | + render :action => :edit | |
70 | + end | |
71 | + end | |
72 | + end | |
73 | + | |
74 | + [{ :type => 'before_filter', | |
75 | + :method_name => 'validate_usp_id', | |
76 | + :options => {:only => 'edit'}, | |
77 | + :block => block }] | |
78 | + end | |
79 | + | |
59 | 80 | def invite_controller_filters |
60 | 81 | [{ :type => 'before_filter', |
61 | 82 | :method_name => 'check_usp_id_existence', | ... | ... |
plugins/stoa/test/functional/profile_editor_controller.rb
0 → 100644
... | ... | @@ -0,0 +1,55 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | |
2 | +require File.dirname(__FILE__) + '/../../../../app/controllers/my_profile/profile_editor_controller' | |
3 | + | |
4 | +# Re-raise errors caught by the controller. | |
5 | +class ProfileEditorController; def rescue_action(e) raise e end; end | |
6 | + | |
7 | +class ProfileEditorTest < ActionController::TestCase | |
8 | + | |
9 | + SALT=YAML::load(File.open(StoaPlugin.root_path + '/config.yml'))['salt'] | |
10 | + | |
11 | + def setup | |
12 | + @controller = ProfileEditorController.new | |
13 | + @request = ActionController::TestRequest.new | |
14 | + @response = ActionController::TestResponse.new | |
15 | + @person = User.create(:login => 'test_user', :email => 'test_user@example.com', :password => 'test', :password_confirmation => 'test').person | |
16 | + login_as(@person.identifier) | |
17 | + Environment.default.enable_plugin(StoaPlugin.name) | |
18 | + db = Tempfile.new('stoa-test') | |
19 | + ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => db.path} | |
20 | + end | |
21 | + | |
22 | + attr_accessor :person | |
23 | + | |
24 | + should 'show usp_id field if person did not filled it' do | |
25 | + get :edit, :profile => person.identifier | |
26 | + assert_match /USP number/, @response.body | |
27 | + end | |
28 | + | |
29 | + should 'not show usp_id field if person already filled it' do | |
30 | + person.usp_id = 12345 | |
31 | + person.save | |
32 | + get :edit, :profile => person.identifier | |
33 | + assert_no_match /USP number/, @response.body | |
34 | + end | |
35 | + | |
36 | + should 'not display field if profile is an organization' do | |
37 | + organization = fast_create(Organization) | |
38 | + get :edit, :profile => organization.identifier | |
39 | + assert_no_match /USP number/, @response.body | |
40 | + end | |
41 | + | |
42 | + should 'display error if usp_id does not match with supplied confirmation' do | |
43 | + StoaPlugin::UspUser.stubs(:matches?).returns(false) | |
44 | + post :edit, :profile => person.identifier, :profile_data => {:usp_id => 12345678}, :confirmation_field => 'cpf', :cpf => 99999999 | |
45 | + assert assigns(:profile_data).errors.invalid?(:usp_id) | |
46 | + end | |
47 | + | |
48 | + should 'save usp_id if everyhtings is ok' do | |
49 | + StoaPlugin::UspUser.stubs(:matches?).returns(true) | |
50 | + post :edit, :profile => person.identifier, :profile_data => {:usp_id => 12345678}, :confirmation_field => 'cpf', :cpf => 99999999 | |
51 | + person.reload | |
52 | + assert_equal '12345678', person.usp_id | |
53 | + end | |
54 | + | |
55 | +end | ... | ... |