Commit d2acf743aa7990ae87ec4b107f9f8a8c276380be
1 parent
04ac4427
Exists in
master
and in
79 other branches
Fix and add tests on institution
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Gabriela Navarro <navarro1703@gmail.com>
Showing
2 changed files
with
59 additions
and
71 deletions
Show diff stats
lib/public_institution.rb
| 1 | class PublicInstitution < Institution | 1 | class PublicInstitution < Institution |
| 2 | validates :governmental_power, :governmental_sphere, :juridical_nature, :presence=>true | 2 | validates :governmental_power, :governmental_sphere, :juridical_nature, :presence=>true |
| 3 | - validates_uniqueness_of :acronym, :allow_blank => true, :allow_nil => true | ||
| 4 | - validates_uniqueness_of :cnpj, :unit_code, :allow_nil => true, :allow_blank => true | 3 | + validates :acronym, :allow_blank => true, :allow_nil => true, :uniqueness=>true |
| 4 | + validates :cnpj, :uniqueness=>true | ||
| 5 | validates_format_of :cnpj, :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/, :allow_nil => true, :allow_blank => true | 5 | validates_format_of :cnpj, :with => /^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/, :allow_nil => true, :allow_blank => true |
| 6 | + | ||
| 6 | 7 | ||
| 7 | end | 8 | end |
test/functional/mpog_software_plugin_controller_test.rb
| @@ -20,8 +20,8 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | @@ -20,8 +20,8 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | ||
| 20 | @response = ActionController::TestResponse.new | 20 | @response = ActionController::TestResponse.new |
| 21 | 21 | ||
| 22 | @institution_list = [] | 22 | @institution_list = [] |
| 23 | - @institution_list << create_public_institution("Ministerio Publico da Uniao", "MPU", "BR", "DF", "Gama", @juridical_nature, @govPower, @govSphere) | ||
| 24 | - @institution_list << create_public_institution("Tribunal Regional da Uniao", "TRU", "BR", "DF", "Brasilia", @juridical_nature, @govPower, @govSphere) | 23 | + @institution_list << create_public_institution("Ministerio Publico da Uniao", "MPU", "BR", "DF", "Gama", @juridical_nature, @govPower, @govSphere, "12.345.678/9012-45") |
| 24 | + @institution_list << create_public_institution("Tribunal Regional da Uniao", "TRU", "BR", "DF", "Brasilia", @juridical_nature, @govPower, @govSphere, "12.345.678/9012-90") | ||
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | should "Search for institution with acronym" do | 27 | should "Search for institution with acronym" do |
| @@ -54,48 +54,15 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | @@ -54,48 +54,15 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | ||
| 54 | assert_template 'create_institution' | 54 | assert_template 'create_institution' |
| 55 | end | 55 | end |
| 56 | 56 | ||
| 57 | - should "create new institution with ajax" do | 57 | + should "create new institution with ajax without acronym" do |
| 58 | @controller.stubs(:verify_recaptcha).returns(true) | 58 | @controller.stubs(:verify_recaptcha).returns(true) |
| 59 | 59 | ||
| 60 | - xhr :get, :new_institution, | ||
| 61 | - :authenticity_token=>"dsa45a6das52sd", | ||
| 62 | - :name => "foo bar", | ||
| 63 | - :community=>{:name=>"foo bar", :country => "BR", :state => "DF", :city => "Brasilia"}, | ||
| 64 | - :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, | ||
| 65 | - :juridical => {:nature => @juridical_nature.id}, | ||
| 66 | - :institution => {:cnpj=>"12.234.567/8900-10", :acronym=>"fb", :type=>"PublicInstitution"}, | ||
| 67 | - :recaptcha_response_field=>'' | 60 | + fields = generate_form_fields "foo bar", "BR", "DF", "Brasilia", "12.234.567/8900-10", "PublicInstitution" |
| 61 | + fields[:institutions][:governmental_power] = @govPower.id | ||
| 62 | + fields[:institutions][:governmental_sphere] = @govSphere.id | ||
| 63 | + fields[:institutions][:juridical_nature] = @juridical_nature.id | ||
| 68 | 64 | ||
| 69 | - | ||
| 70 | - json_response = ActiveSupport::JSON.decode(@response.body) | ||
| 71 | - | ||
| 72 | - assert json_response["success"] | ||
| 73 | - end | ||
| 74 | - | ||
| 75 | - should "create more than one institution without acronym" do | ||
| 76 | - @controller.stubs(:verify_recaptcha).returns(true) | ||
| 77 | - | ||
| 78 | - xhr :get, :new_institution, | ||
| 79 | - :authenticity_token=>"dsa45a6das52sd", | ||
| 80 | - :name => "foo bar", | ||
| 81 | - :community=>{:name=>"foo bar", :country => "BR", :state => "DF", :city => "Brasilia"}, | ||
| 82 | - :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, | ||
| 83 | - :juridical => {:nature => @juridical_nature.id}, | ||
| 84 | - :institution => {:cnpj=>"12.234.567/8900-10", :acronym=>"", :type=>"PublicInstitution"}, | ||
| 85 | - :recaptcha_response_field=>'' | ||
| 86 | - | ||
| 87 | - json_response = ActiveSupport::JSON.decode(@response.body) | ||
| 88 | - | ||
| 89 | - assert json_response["success"] | ||
| 90 | - | ||
| 91 | - xhr :get, :new_institution, | ||
| 92 | - :authenticity_token=>"dsa45a6das52sd", | ||
| 93 | - :name => "foo bar 2", | ||
| 94 | - :community=>{:name=>"foo bar 2", :country => "BR", :state => "DF", :city => "Brasilia"}, | ||
| 95 | - :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, | ||
| 96 | - :juridical => {:nature => @juridical_nature.id}, | ||
| 97 | - :institution => {:cnpj=>"12.224.567/8900-10", :acronym=>"", :type=>"PublicInstitution"}, | ||
| 98 | - :recaptcha_response_field=>'' | 65 | + xhr :get, :new_institution, fields |
| 99 | 66 | ||
| 100 | json_response = ActiveSupport::JSON.decode(@response.body) | 67 | json_response = ActiveSupport::JSON.decode(@response.body) |
| 101 | 68 | ||
| @@ -105,14 +72,12 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | @@ -105,14 +72,12 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | ||
| 105 | should "not create a institution that already exists" do | 72 | should "not create a institution that already exists" do |
| 106 | @controller.stubs(:verify_recaptcha).returns(true) | 73 | @controller.stubs(:verify_recaptcha).returns(true) |
| 107 | 74 | ||
| 108 | - xhr :get, :new_institution, | ||
| 109 | - :authenticity_token=>"dsa45a6das52sd", | ||
| 110 | - :name => "Ministerio Publico da Uniao", | ||
| 111 | - :community=>{:name=>"Ministerio Publico da Uniao", :country => "BR", :state => "DF", :city => "Brasilia"}, | ||
| 112 | - :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, | ||
| 113 | - :juridical => {:nature => @juridical_nature.id}, | ||
| 114 | - :institution => {:cnpj=>"12.234.567/8900-10", :acronym=>"fb", :type=>"PublicInstitution"}, | ||
| 115 | - :recaptcha_response_field=>'' | 75 | + fields = generate_form_fields "Ministerio Publico da Uniao", "BR", "DF", "Brasilia", "12.234.567/8900-10", "PublicInstitution" |
| 76 | + fields[:institutions][:governmental_power] = @govPower.id | ||
| 77 | + fields[:institutions][:governmental_sphere] = @govSphere.id | ||
| 78 | + fields[:institutions][:juridical_nature] = @juridical_nature.id | ||
| 79 | + | ||
| 80 | + xhr :get, :new_institution, fields | ||
| 116 | 81 | ||
| 117 | json_response = ActiveSupport::JSON.decode(@response.body) | 82 | json_response = ActiveSupport::JSON.decode(@response.body) |
| 118 | 83 | ||
| @@ -122,14 +87,10 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | @@ -122,14 +87,10 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | ||
| 122 | should "not create a institution without cnpj" do | 87 | should "not create a institution without cnpj" do |
| 123 | @controller.stubs(:verify_recaptcha).returns(true) | 88 | @controller.stubs(:verify_recaptcha).returns(true) |
| 124 | 89 | ||
| 125 | - xhr :get, :new_institution, | ||
| 126 | - :authenticity_token=>"dsa45a6das52sd", | ||
| 127 | - :name => "Ministerio Publico da Uniao", | ||
| 128 | - :community=>{:name=>"Ministerio Publico da Uniao", :country => "BR", :state => "DF", :city => "Brasilia"}, | ||
| 129 | - :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, | ||
| 130 | - :juridical => {:nature => @juridical_nature.id}, | ||
| 131 | - :institution => {:cnpj=> "", :acronym=>"fb", :type=>"PublicInstitution"}, | ||
| 132 | - :recaptcha_response_field=>'' | 90 | + fields = generate_form_fields "Some Private Institution", "BR", "DF", "Brasilia", "", "PrivateInstitution" |
| 91 | + fields[:institutions][:acronym] = "SPI" | ||
| 92 | + | ||
| 93 | + xhr :get, :new_institution, fields | ||
| 133 | 94 | ||
| 134 | json_response = ActiveSupport::JSON.decode(@response.body) | 95 | json_response = ActiveSupport::JSON.decode(@response.body) |
| 135 | 96 | ||
| @@ -165,26 +126,51 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | @@ -165,26 +126,51 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | ||
| 165 | assert_equal "false", @response.body | 126 | assert_equal "false", @response.body |
| 166 | end | 127 | end |
| 167 | 128 | ||
| 168 | - should "update institution date_modification in your creation" do | 129 | + should "Create new institution with method post" do |
| 169 | @controller.stubs(:verify_recaptcha).returns(true) | 130 | @controller.stubs(:verify_recaptcha).returns(true) |
| 170 | 131 | ||
| 171 | - xhr :get, :new_institution, | ||
| 172 | - :authenticity_token=>"dsa45a6das52sd", | ||
| 173 | - :name => "foo bar", | ||
| 174 | - :community=>{:name=>"foo bar", :country => "BR", :state => "DF", :city => "Brasilia"}, | ||
| 175 | - :governmental=>{:power=>@govPower.id, :sphere=>@govSphere.id}, | ||
| 176 | - :juridical => {:nature => @juridical_nature.id}, | ||
| 177 | - :institution => {:cnpj=>"12.234.567/8900-10", :acronym=>"fb", :type=>"PublicInstitution"}, | ||
| 178 | - :recaptcha_response_field=>'' | 132 | + fields = generate_form_fields "Some Private Institution", "BR", "DF", "Brasilia", "12.345.567/8900-10", "PrivateInstitution" |
| 133 | + fields[:institutions][:acronym] = "SPI" | ||
| 134 | + | ||
| 135 | + post :new_institution, fields | ||
| 179 | 136 | ||
| 180 | - date = Time.now.day.to_s + "/" + Time.now.month.to_s + "/" + Time.now.year.to_s | ||
| 181 | - assert_equal date, Institution.last.date_modification | 137 | + assert_redirected_to(controller: "admin_panel", action: "index") |
| 182 | end | 138 | end |
| 183 | 139 | ||
| 140 | + should "not create new institution with method post without cnpj" do | ||
| 141 | + @controller.stubs(:verify_recaptcha).returns(true) | ||
| 142 | + | ||
| 143 | + fields = generate_form_fields "Some Private Institution", "BR", "DF", "Brasilia", "", "PrivateInstitution" | ||
| 144 | + fields[:institutions][:acronym] = "SPI" | ||
| 145 | + | ||
| 146 | + post :new_institution, fields | ||
| 147 | + | ||
| 148 | + assert_redirected_to(controller: "mpog_software_plugin", action: "create_institution_admin") | ||
| 149 | + end | ||
| 184 | 150 | ||
| 185 | private | 151 | private |
| 186 | 152 | ||
| 187 | - def create_public_institution name, acronym, country, state, city, juridical_nature, gov_p, gov_s | 153 | + def generate_form_fields name, country, state, city, cnpj, type |
| 154 | + fields = { | ||
| 155 | + :community => { | ||
| 156 | + :name => name, | ||
| 157 | + :country => country, | ||
| 158 | + :state => state, | ||
| 159 | + :city => city | ||
| 160 | + }, | ||
| 161 | + :institutions => { | ||
| 162 | + :cnpj=> cnpj, | ||
| 163 | + :type => type, | ||
| 164 | + :acronym => "", | ||
| 165 | + :governmental_power => "", | ||
| 166 | + :governmental_sphere => "", | ||
| 167 | + :juridical_nature => "" | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | + fields | ||
| 171 | + end | ||
| 172 | + | ||
| 173 | + def create_public_institution name, acronym, country, state, city, juridical_nature, gov_p, gov_s, cnpj | ||
| 188 | institution_community = fast_create(Community) | 174 | institution_community = fast_create(Community) |
| 189 | institution_community.name = name | 175 | institution_community.name = name |
| 190 | institution_community.country = country | 176 | institution_community.country = country |
| @@ -199,6 +185,7 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | @@ -199,6 +185,7 @@ class MpogSoftwarePluginControllerTest < ActionController::TestCase | ||
| 199 | institution.acronym = acronym | 185 | institution.acronym = acronym |
| 200 | institution.governmental_power = gov_p | 186 | institution.governmental_power = gov_p |
| 201 | institution.governmental_sphere = gov_s | 187 | institution.governmental_sphere = gov_s |
| 188 | + institution.cnpj = cnpj | ||
| 202 | institution.save! | 189 | institution.save! |
| 203 | institution | 190 | institution |
| 204 | end | 191 | end |