Commit 2340a574b286dd8597c8541894187f7eb4f04820

Authored by Rodrigo Souto
1 parent 3a971de0

[stoa] Validating uniqueness of usp_id

(ActionItem2293)
plugins/stoa/controllers/stoa_plugin_controller.rb
@@ -26,7 +26,7 @@ class StoaPluginController < PublicController @@ -26,7 +26,7 @@ class StoaPluginController < PublicController
26 26
27 def check_usp_id 27 def check_usp_id
28 begin 28 begin
29 - render :text => { :exists => StoaPlugin::UspUser.exists?(params[:usp_id]) }.to_json 29 + render :text => { :exists => StoaPlugin::UspUser.exists?(params[:usp_id]) && Person.find_by_usp_id(params[:usp_id]).nil? }.to_json
30 rescue Exception => exception 30 rescue Exception => exception
31 render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json 31 render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json
32 end 32 end
plugins/stoa/lib/ext/person.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +require_dependency 'person'
  2 +
  3 +class Person
  4 + validates_uniqueness_of :usp_id
  5 +end
plugins/stoa/lib/stoa_plugin.rb
1 require_dependency 'person' 1 require_dependency 'person'
  2 +require_dependency 'ext/person'
2 3
3 class StoaPlugin < Noosfero::Plugin 4 class StoaPlugin < Noosfero::Plugin
4 5
plugins/stoa/test/unit/person.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class StoaPlugin::Person < ActiveSupport::TestCase
  4 +
  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)
  11 +
  12 + assert !another_person.valid?
  13 + assert another_person.errors.invalid?(:usp_id)
  14 + end
  15 +
  16 +end
  17 +