Commit b30ec443f836a8285cb826c6df4839cbe7409b2c
1 parent
f123bb04
Exists in
staging
and in
42 other branches
[stoa] Allowing invited users to register without usp id
* Including invitation_code in the person * Also removing verbosity of schema on testing
Showing
5 changed files
with
45 additions
and
8 deletions
Show diff stats
plugins/stoa/lib/ext/person.rb
1 | require_dependency 'person' | 1 | require_dependency 'person' |
2 | 2 | ||
3 | class Person | 3 | class Person |
4 | - validates_uniqueness_of :usp_id | 4 | + validates_uniqueness_of :usp_id, :allow_nil => true |
5 | + settings_items :invitation_code | ||
6 | + validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)} | ||
7 | + | ||
8 | + def usp_id_or_invitation | ||
9 | + if usp_id.blank? && (invitation_code.blank? || !Task.find(:first, :conditions => {:code => invitation_code})) | ||
10 | + errors.add(:usp_id, "can't register without usp_id or invitation") | ||
11 | + end | ||
12 | + end | ||
5 | end | 13 | end |
plugins/stoa/lib/stoa_plugin.rb
@@ -49,8 +49,10 @@ class StoaPlugin < Noosfero::Plugin | @@ -49,8 +49,10 @@ class StoaPlugin < Noosfero::Plugin | ||
49 | 49 | ||
50 | def account_controller_filters | 50 | def account_controller_filters |
51 | block = lambda do | 51 | block = lambda do |
52 | + params[:profile_data] ||= {} | ||
53 | + params[:profile_data][:invitation_code] = params[:invitation_code] | ||
52 | if request.post? | 54 | if request.post? |
53 | - if !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[:confirmation_value]) | 55 | + if !params[:invitation_code] && !StoaPlugin::UspUser.matches?(params[:profile_data][:usp_id], params[:confirmation_field], params[:confirmation_value]) |
54 | @person = Person.new | 56 | @person = Person.new |
55 | @person.errors.add(:usp_id, _(' validation failed')) | 57 | @person.errors.add(:usp_id, _(' validation failed')) |
56 | render :action => :signup | 58 | render :action => :signup |
plugins/stoa/test/functional/account_controller_test.rb
@@ -29,4 +29,9 @@ class AccountControllerTest < ActionController::TestCase | @@ -29,4 +29,9 @@ class AccountControllerTest < ActionController::TestCase | ||
29 | assert_nil assigns(:person).errors[:usp_id] | 29 | assert_nil assigns(:person).errors[:usp_id] |
30 | end | 30 | end |
31 | 31 | ||
32 | + should 'inlude invitation_code param in the person\'s attributes' do | ||
33 | + get :signup, :invitation_code => 12345678 | ||
34 | + assert assigns(:person).invitation_code == '12345678' | ||
35 | + end | ||
36 | + | ||
32 | end | 37 | end |
plugins/stoa/test/unit/person.rb
@@ -3,15 +3,36 @@ require File.dirname(__FILE__) + '/../../../../test/test_helper' | @@ -3,15 +3,36 @@ require File.dirname(__FILE__) + '/../../../../test/test_helper' | ||
3 | class StoaPlugin::Person < ActiveSupport::TestCase | 3 | class StoaPlugin::Person < ActiveSupport::TestCase |
4 | 4 | ||
5 | should 'validates uniqueness of usp_id' do | 5 | should 'validates uniqueness of usp_id' do |
6 | - usp_id = 12345678 | ||
7 | - person = create_user('some-person').person | ||
8 | - person.usp_id = usp_id | ||
9 | - person.save! | ||
10 | - another_person = Person.new(:name => "Another person", :identifier => 'another-person', :usp_id => usp_id) | 6 | + usp_id = 87654321 |
7 | + fast_create(Person, :usp_id => usp_id) | ||
8 | + another_person = Person.new(:usp_id => usp_id) | ||
9 | + another_person.valid? | ||
11 | 10 | ||
12 | - assert !another_person.valid? | ||
13 | assert another_person.errors.invalid?(:usp_id) | 11 | assert another_person.errors.invalid?(:usp_id) |
14 | end | 12 | end |
15 | 13 | ||
14 | + should 'allow nil usp_id only if person has an invitation_code' do | ||
15 | + environment = Environment.default | ||
16 | + environment.enable_plugin(StoaPlugin) | ||
17 | + person = Person.new(:environment => environment) | ||
18 | + person.valid? | ||
19 | + assert person.errors.invalid?(:usp_id) | ||
20 | + | ||
21 | + Task.create!(:code => 12345678) | ||
22 | + person.invitation_code = 12345678 | ||
23 | + person.valid? | ||
24 | + | ||
25 | + assert !person.errors.invalid?(:usp_id) | ||
26 | + end | ||
27 | + | ||
28 | + should 'allow multiple nil usp_id' do | ||
29 | + fast_create(Person) | ||
30 | + Task.create!(:code => 87654321) | ||
31 | + person = Person.new(:invitation_code => 87654321) | ||
32 | + person.valid? | ||
33 | + | ||
34 | + assert !person.errors.invalid?(:usp_id) | ||
35 | + end | ||
36 | + | ||
16 | end | 37 | end |
17 | 38 |
plugins/stoa/test/unit/usp_user.rb
@@ -8,6 +8,7 @@ class StoaPlugin::UspUserTest < ActiveSupport::TestCase | @@ -8,6 +8,7 @@ class StoaPlugin::UspUserTest < ActiveSupport::TestCase | ||
8 | @db = Tempfile.new('stoa-test') | 8 | @db = Tempfile.new('stoa-test') |
9 | configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} | 9 | configs = ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => @db.path} |
10 | ActiveRecord::Base.establish_connection(:stoa) | 10 | ActiveRecord::Base.establish_connection(:stoa) |
11 | + ActiveRecord::Schema.verbose = false | ||
11 | ActiveRecord::Schema.create_table "pessoa" do |t| | 12 | ActiveRecord::Schema.create_table "pessoa" do |t| |
12 | t.integer "codpes" | 13 | t.integer "codpes" |
13 | t.text "numcpf" | 14 | t.text "numcpf" |