Commit 3b83c471fb038c53c27de6d9ec291153b38e4a04
1 parent
73458512
Exists in
master
and in
22 other branches
stoa: use mock instead of fake db on test
Showing
3 changed files
with
23 additions
and
25 deletions
Show diff stats
plugins/stoa/controllers/stoa_plugin_controller.rb
| ... | ... | @@ -40,7 +40,7 @@ class StoaPluginController < PublicController |
| 40 | 40 | |
| 41 | 41 | def check_cpf |
| 42 | 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 | 44 | rescue Exception => exception |
| 45 | 45 | render :text => { :exists => false, :error => {:message => exception.to_s, :backtrace => exception.backtrace} }.to_json |
| 46 | 46 | end | ... | ... |
plugins/stoa/install.rb
plugins/stoa/test/functional/stoa_plugin_controller_test.rb
| ... | ... | @@ -12,27 +12,12 @@ class StoaPluginControllerTest < ActionController::TestCase |
| 12 | 12 | @controller = StoaPluginController.new |
| 13 | 13 | @request = ActionController::TestRequest.new |
| 14 | 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 | 16 | env = Environment.default |
| 26 | 17 | env.enable_plugin(StoaPlugin.name) |
| 27 | 18 | env.enable('skip_new_user_email_confirmation') |
| 28 | 19 | env.save! |
| 29 | 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 | 21 | end |
| 37 | 22 | |
| 38 | 23 | attr_accessor :user |
| ... | ... | @@ -70,23 +55,39 @@ class StoaPluginControllerTest < ActionController::TestCase |
| 70 | 55 | end |
| 71 | 56 | |
| 72 | 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 | 61 | assert json_response['exists'] |
| 75 | 62 | end |
| 76 | 63 | |
| 77 | 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 | 68 | assert !json_response['exists'] |
| 80 | 69 | end |
| 81 | 70 | |
| 82 | 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 | 77 | assert json_response['exists'] |
| 85 | 78 | end |
| 86 | 79 | |
| 87 | 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 | 91 | assert !json_response['exists'] |
| 91 | 92 | end |
| 92 | 93 | ... | ... |