Commit 3b83c471fb038c53c27de6d9ec291153b38e4a04

Authored by Rodrigo Souto
1 parent 73458512

stoa: use mock instead of fake db on test

plugins/stoa/controllers/stoa_plugin_controller.rb
@@ -40,7 +40,7 @@ class StoaPluginController < PublicController @@ -40,7 +40,7 @@ class StoaPluginController < PublicController
40 40
41 def check_cpf 41 def check_cpf
42 begin 42 begin
43 - render :text => { :exists => !StoaPlugin::UspUser.find_by_codpes(params[:usp_id]).cpf.blank? }.to_json 43 + render :text => { :exists => StoaPlugin::UspUser.find_by_codpes(params[:usp_id]).cpf.present? }.to_json
44 rescue Exception => exception 44 rescue Exception => exception
45 render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json 45 render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json
46 end 46 end
plugins/stoa/install.rb
1 -#FIXME one functional test is failing  
2 -raise "Not ready yet..."  
3 -  
4 require 'fileutils' 1 require 'fileutils'
5 2
6 config_path = File.join('plugins', 'stoa', 'config.yml') 3 config_path = File.join('plugins', 'stoa', 'config.yml')
plugins/stoa/test/functional/stoa_plugin_controller_test.rb
@@ -12,27 +12,12 @@ class StoaPluginControllerTest < ActionController::TestCase @@ -12,27 +12,12 @@ class StoaPluginControllerTest < ActionController::TestCase
12 @controller = StoaPluginController.new 12 @controller = StoaPluginController.new
13 @request = ActionController::TestRequest.new 13 @request = ActionController::TestRequest.new
14 @response = ActionController::TestResponse.new 14 @response = ActionController::TestResponse.new
15 - @db = Tempfile.new('stoa-test')  
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) 15 + ActiveRecord::Base.configurations['stoa'] = {:adapter => 'sqlite3', :database => ':memory:', :verbosity => 'quiet'}
25 env = Environment.default 16 env = Environment.default
26 env.enable_plugin(StoaPlugin.name) 17 env.enable_plugin(StoaPlugin.name)
27 env.enable('skip_new_user_email_confirmation') 18 env.enable('skip_new_user_email_confirmation')
28 env.save! 19 env.save!
29 @user = create_user_full('real_user', {:password => '123456', :password_confirmation => '123456'}, {:usp_id => 9999999}) 20 @user = create_user_full('real_user', {:password => '123456', :password_confirmation => '123456'}, {:usp_id => 9999999})
30 - StoaPlugin::UspUser.create!(:codpes => 12345678, :cpf => Digest::MD5.hexdigest(SALT+'12345678'), :birth_date => '1970-01-30')  
31 - end  
32 -  
33 - def teardown  
34 - @db.unlink  
35 - @user.destroy  
36 end 21 end
37 22
38 attr_accessor :user 23 attr_accessor :user
@@ -70,23 +55,39 @@ class StoaPluginControllerTest < ActionController::TestCase @@ -70,23 +55,39 @@ class StoaPluginControllerTest < ActionController::TestCase
70 end 55 end
71 56
72 should 'check valid usp id' do 57 should 'check valid usp id' do
73 - get :check_usp_id, :usp_id => '12345678' 58 + usp_id = '12345678'
  59 + StoaPlugin::UspUser.stubs(:exists?).with(usp_id).returns(true)
  60 + get :check_usp_id, :usp_id => usp_id
74 assert json_response['exists'] 61 assert json_response['exists']
75 end 62 end
76 63
77 should 'check invalid usp id' do 64 should 'check invalid usp id' do
78 - get :check_usp_id, :usp_id => '87654321' 65 + usp_id = '87654321'
  66 + StoaPlugin::UspUser.stubs(:exists?).with(usp_id).returns(false)
  67 + get :check_usp_id, :usp_id => usp_id
79 assert !json_response['exists'] 68 assert !json_response['exists']
80 end 69 end
81 70
82 should 'check existent cpf' do 71 should 'check existent cpf' do
83 - get :check_cpf, :usp_id => '12345678' 72 + usp_id = '12345678'
  73 + user = mock
  74 + user.stubs(:cpf).returns('12345678')
  75 + StoaPlugin::UspUser.stubs(:find_by_codpes).with(usp_id).returns(user)
  76 + get :check_cpf, :usp_id => usp_id
84 assert json_response['exists'] 77 assert json_response['exists']
85 end 78 end
86 79
87 should 'check not existent cpf' do 80 should 'check not existent cpf' do
88 - StoaPlugin::UspUser.create(:codpes => 87654321, :birth_date => '1970-01-30')  
89 - get :check_cpf, :usp_id => '87654321' 81 + usp_id_with_cpf = '12345678'
  82 + user_with_cpf = mock
  83 + user_with_cpf.stubs(:cpf).returns('12345678')
  84 + StoaPlugin::UspUser.stubs(:find_by_codpes).with(usp_id_with_cpf).returns(user_with_cpf)
  85 + get :check_cpf, :usp_id => usp_id_with_cpf
  86 + usp_id_without_cpf = '87654321'
  87 + user_without_cpf = mock
  88 + user_with_cpf.stubs(:cpf).returns(nil)
  89 + StoaPlugin::UspUser.stubs(:find_by_codpes).with(usp_id_without_cpf).returns(user_without_cpf)
  90 + get :check_cpf, :usp_id => usp_id_without_cpf
90 assert !json_response['exists'] 91 assert !json_response['exists']
91 end 92 end
92 93