From 4d6c78ba82bc2a01d49d46448f3fe6b61b856a8a Mon Sep 17 00:00:00 2001 From: Fabio Teixeira Date: Thu, 14 Aug 2014 14:56:37 -0300 Subject: [PATCH] Set the defaults institution's community admin --- config/siorg.yml | 2 ++ controllers/mpog_software_plugin_controller.rb | 2 ++ lib/institution_helper.rb | 16 ++++++++++++++++ test/functional/mpog_software_plugin_controller_test.rb | 27 +++++++++++++++++++++++++++ test/integration/institution_helper_test.rb | 26 ++++++++++++++++++++++++++ test/unit/institution_helper_test.rb | 18 ------------------ 6 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 test/integration/institution_helper_test.rb delete mode 100644 test/unit/institution_helper_test.rb diff --git a/config/siorg.yml b/config/siorg.yml index a0b1aad..e87e656 100644 --- a/config/siorg.yml +++ b/config/siorg.yml @@ -7,3 +7,5 @@ web_service: legislative: 2 judiciary: 3 additional_params: retornarOrgaoEntidadeVinculados=NAO + environment_name: Noosfero + default_community_admin: adminuser \ No newline at end of file diff --git a/controllers/mpog_software_plugin_controller.rb b/controllers/mpog_software_plugin_controller.rb index 3d61316..4c4246b 100644 --- a/controllers/mpog_software_plugin_controller.rb +++ b/controllers/mpog_software_plugin_controller.rb @@ -76,6 +76,8 @@ class MpogSoftwarePluginController < ApplicationController response_message = if verify_recaptcha(:model=> institution, :message => _('Please type the word correctly')) if institution.errors.full_messages.empty? and institution.valid? and institution.save + institution.community.add_admin(environment.admins.first) unless environment.admins.empty? + {:success => true, :message => _("Institution successful created!"), :institution_data=>{:name=>institution.name, :id=>institution.id}} else {:success => false, :message => _("Institution could not be created!"), :errors => institution.errors.full_messages} diff --git a/lib/institution_helper.rb b/lib/institution_helper.rb index 095e716..ae4881d 100644 --- a/lib/institution_helper.rb +++ b/lib/institution_helper.rb @@ -9,6 +9,20 @@ module InstitutionHelper def self.mass_update @web_service_info = self.web_service_info + environment = Environment.find_by_name(@web_service_info["environment_name"]) + + if environment.nil? + puts "\n", "="*80, "Could not find the informed environment: #{@web_service_info["environment_name"]}", "="*80, "\n" + return + end + + admin = environment.people.find_by_name(@web_service_info["default_community_admin"]) + + if admin.nil? + puts "\n", "="*80, "Could not find the informed admin: #{@web_service_info["default_community_admin"]}", "="*80, "\n" + return + end + POWERS.each do |power| SPHERES.each do |sphere| json = self.get_json power, sphere @@ -26,6 +40,8 @@ module InstitutionHelper institution = self.set_institution_data institution, unit institution.save + + institution.community.add_admin(admin) end end end diff --git a/test/functional/mpog_software_plugin_controller_test.rb b/test/functional/mpog_software_plugin_controller_test.rb index d19fddc..8348b12 100644 --- a/test/functional/mpog_software_plugin_controller_test.rb +++ b/test/functional/mpog_software_plugin_controller_test.rb @@ -7,9 +7,18 @@ class MpogSoftwarePluginController; def rescue_action(e) raise e end; end class MpogSoftwarePluginControllerTest < ActionController::TestCase def setup + admin = create_user("adminuser").person + admin.stubs(:has_permission?).returns("true") + + @environment = Environment.default + @environment.enabled_plugins = ['MpogSoftwarePlugin'] + @environment.add_admin(admin) + @environment.save + @govPower = GovernmentalPower.create(:name=>"Some Gov Power") @govSphere = GovernmentalSphere.create(:name=>"Some Gov Sphere") @response = ActionController::TestResponse.new + @institution_list = [] @institution_list << create_public_institution("Ministerio Publico da Uniao", "MPU", @govPower, @govSphere) @institution_list << create_public_institution("Tribunal Regional da Uniao", "TRU", @govPower, @govSphere) @@ -75,6 +84,24 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase assert !json_response["success"] end + should "set the environment admin as institution community admin" do + @controller.stubs(:verify_recaptcha).returns(true) + + xhr :get, :new_institution, + :authenticity_token=>"dsa45a6das52sd", + :community=>{:name=>"Another instituon community"}, + :institution => {:cnpj=>"10.254.577/8910-12", :acronym=>"aic", :type=>"PublicInstitution"}, + :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, + :recaptcha_response_field=>'' + + json_response = ActiveSupport::JSON.decode(@response.body) + + assert json_response["success"] + + assert Community.last.admins.include?(@environment.admins.first) + assert_equal Community.last.name, "Another instituon community" + end + should "verify if institution name already exists" do xhr :get, :institution_already_exists, :name=>"Ministerio Publico da Uniao" assert_equal "true", @response.body diff --git a/test/integration/institution_helper_test.rb b/test/integration/institution_helper_test.rb new file mode 100644 index 0000000..087872a --- /dev/null +++ b/test/integration/institution_helper_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class InstitutionHelperTest < ActionController::IntegrationTest + + def setup + admin = create_user("test_admin").person + admin.stubs(:has_permission?).returns("true") + + @environment = Environment.default + @environment.enabled_plugins = ['MpogSoftwarePlugin'] + @environment.add_admin(admin) + @environment.name = "test_environment" + @environment.save + end + + should "not proceed with SIORG script if environment name isn't the informed" do + assert !InstitutionHelper.mass_update + end + + should "not proceed with SIORG script if admin name isn't the informed" do + @environment.name = "Noosfero" + @environment.save + + assert !InstitutionHelper.mass_update + end +end diff --git a/test/unit/institution_helper_test.rb b/test/unit/institution_helper_test.rb deleted file mode 100644 index e1ef948..0000000 --- a/test/unit/institution_helper_test.rb +++ /dev/null @@ -1,18 +0,0 @@ -require File.dirname(__FILE__) + '/../../../../test/test_helper' - -class InstitutionHelperTest < ActiveSupport::TestCase - - should "populate public institutions with data from SIORG" do - Institution.destroy_all - - InstitutionHelper.mass_update - - assert Institution.count != 0 - end - - should "receive json data from SIORG" do - data = InstitutionHelper.get_json(2, 1) - - assert data["unidades"].count != 0 - end -end -- libgit2 0.21.2