Commit 8e7de0e4dc54e7d1c7fdd5cd2acb43de139e2c95
1 parent
d244c631
Exists in
master
and in
29 other branches
[stoa] Making invitation works only once
Showing
2 changed files
with
21 additions
and
3 deletions
Show diff stats
plugins/stoa/lib/ext/person.rb
... | ... | @@ -6,8 +6,13 @@ class Person |
6 | 6 | validate :usp_id_or_invitation, :if => lambda { |person| person.environment && person.environment.plugin_enabled?(StoaPlugin)} |
7 | 7 | |
8 | 8 | def usp_id_or_invitation |
9 | - if usp_id.blank? && (invitation_code.blank? || !Task.pending.find(:first, :conditions => {:code => invitation_code})) | |
9 | + if usp_id.blank? && (invitation_code.blank? || !invitation_task) | |
10 | 10 | errors.add(:usp_id, "can't register without usp_id or invitation") |
11 | 11 | end |
12 | 12 | end |
13 | + | |
14 | + def invitation_task | |
15 | + Task.pending.find(:first, :conditions => {:code => invitation_code}) || | |
16 | + Task.finished.find(:first, :conditions => {:code => invitation_code, :target_id => id}) | |
17 | + end | |
13 | 18 | end | ... | ... |
plugins/stoa/test/unit/person.rb
... | ... | @@ -39,8 +39,8 @@ class StoaPlugin::Person < ActiveSupport::TestCase |
39 | 39 | assert !person.errors.invalid?(:usp_id) |
40 | 40 | end |
41 | 41 | |
42 | - should 'not allow person register with a finished task' do | |
43 | - t = Task.create!(:code => 87654321) | |
42 | + should 'not allow person to be saved with a finished invitation that is not his own' do | |
43 | + t = Task.create!(:code => 87654321, :target_id => 1) | |
44 | 44 | t.finish |
45 | 45 | person = Person.new(:environment => environment, :invitation_code => 87654321) |
46 | 46 | person.valid? |
... | ... | @@ -48,5 +48,18 @@ class StoaPlugin::Person < ActiveSupport::TestCase |
48 | 48 | assert person.errors.invalid?(:usp_id) |
49 | 49 | end |
50 | 50 | |
51 | + should 'allow person to be saved with a finished invitation if it is his own' do | |
52 | + t = Task.create!(:code => 87654321) | |
53 | + user = User.new(:login => 'some-person', :email => 'some-person@example.com', :password => 'test', :password_confirmation => 'test', :person_data => {:environment => environment, :invitation_code => 87654321}) | |
54 | + user.save! | |
55 | + person = user.person | |
56 | + t.target_id = person.id | |
57 | + t.finish | |
58 | + | |
59 | + person.valid? | |
60 | + assert !person.errors.invalid?(:usp_id) | |
61 | + end | |
62 | + | |
63 | + | |
51 | 64 | end |
52 | 65 | ... | ... |