Commit bc0a550617d0d9c5b65d881e866dbbbd33dff9c4

Authored by Gabriel Silva
1 parent 8880af6d

Validates presence of license on software_info

 * Add a migration to fix softwares with no license
 * Add validates presence of license on software_info
 * Add test to not save software_info without license

Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr@gmail.com>
Signed-off-by: Omar Junior <omarroinuj@gmail.com>
Signed-off-by: Pedro de Lyra <pedrodelyra@gmail.com>
src/noosfero-spb/software_communities/db/migrate/20160112191716_add_license_to_softwares_with_none.rb 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +class AddLicenseToSoftwaresWithNone < ActiveRecord::Migration
  2 + def up
  3 + execute("UPDATE software_infos SET license_info_id=(SELECT id FROM license_infos WHERE version='Another') WHERE license_info_id IS NULL;")
  4 + end
  5 +
  6 + def down
  7 + say "This migration can't be reverted"
  8 + end
  9 +end
src/noosfero-spb/software_communities/lib/software_info.rb
@@ -84,11 +84,12 @@ class SoftwareInfo &lt; ActiveRecord::Base @@ -84,11 +84,12 @@ class SoftwareInfo &lt; ActiveRecord::Base
84 validates_length_of :finality, :maximum => 4000 84 validates_length_of :finality, :maximum => 4000
85 validates_length_of :objectives, :maximum => 4000 85 validates_length_of :objectives, :maximum => 4000
86 validates_length_of :features, :maximum => 4000 86 validates_length_of :features, :maximum => 4000
87 - validates_presence_of :finality, :community 87 + validates_presence_of :finality, :community, :license_info
88 88
89 validate :validate_acronym 89 validate :validate_acronym
90 90
91 - settings_items :another_license_version, :another_license_link 91 + settings_items :another_license_version, :default => 'Another'
  92 + settings_items :another_license_link, :default => '#'
92 settings_items :sisp, :default => false 93 settings_items :sisp, :default => false
93 94
94 serialize :agency_identification 95 serialize :agency_identification
@@ -136,16 +137,14 @@ class SoftwareInfo &lt; ActiveRecord::Base @@ -136,16 +137,14 @@ class SoftwareInfo &lt; ActiveRecord::Base
136 } 137 }
137 138
138 def license_info 139 def license_info
139 - license = LicenseInfo.find_by_id self.license_info_id  
140 license_another = LicenseInfo.find_by_version("Another") 140 license_another = LicenseInfo.find_by_version("Another")
141 141
142 - if license_another && license.id == license_another.id  
143 - LicenseInfo.new(  
144 - :version => self.another_license_version,  
145 - :link => self.another_license_link  
146 - ) 142 + if license_another && self.license_info_id == license_another.id
  143 + license_another.version = self.another_license_version
  144 + license_another.link = self.another_license_link
  145 + license_another
147 else 146 else
148 - license 147 + super
149 end 148 end
150 end 149 end
151 150
src/noosfero-spb/software_communities/test/functional/software_communities_plugin_myprofile_controller_test.rb
@@ -54,7 +54,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -54,7 +54,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
54 :license => fields[0], 54 :license => fields[0],
55 :software_info => fields[2] 55 :software_info => fields[2]
56 ) 56 )
57 - assert_equal SoftwareInfo.last.community.name, "Debian" 57 +
  58 + new_software = Community.find_by_identifier("debian").software_info
  59 + assert_equal new_software.community.name, "Debian"
58 end 60 end
59 61
60 should 'edit a new software adding basic information' do 62 should 'edit a new software adding basic information' do
@@ -76,7 +78,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -76,7 +78,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
76 :language => {}, 78 :language => {},
77 :database => {} 79 :database => {}
78 ) 80 )
79 - assert_equal SoftwareInfo.last.repository_link, "www.github.com/test" 81 +
  82 + edited_software = Community.find_by_identifier("debian").software_info
  83 + assert_equal edited_software.repository_link, "www.github.com/test"
80 end 84 end
81 85
82 should 'edit a new software adding specific information' do 86 should 'edit a new software adding specific information' do
@@ -98,7 +102,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -98,7 +102,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
98 :software => fields[4], 102 :software => fields[4],
99 :license => fields[5] 103 :license => fields[5]
100 ) 104 )
101 - assert_equal SoftwareInfo.last.acronym, "test" 105 +
  106 + edited_software = Community.find_by_identifier("debian").software_info
  107 + assert_equal edited_software.acronym, "test"
102 end 108 end
103 109
104 should 'non admin cant edit a new software' do 110 should 'non admin cant edit a new software' do
@@ -124,6 +130,32 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -124,6 +130,32 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
124 assert_response 302 130 assert_response 302
125 end 131 end
126 132
  133 + should 'edit a software and does not change Another License values' do
  134 + another_license = LicenseInfo.create(:version => "Another", :link => "#")
  135 + software = create_software(software_fields)
  136 + software.community.add_admin(@person)
  137 + software.save!
  138 +
  139 + post(
  140 + :edit_software,
  141 + :profile => software.community.identifier,
  142 + :license => { "license_infos_id"=>another_license.id, "version"=>"Another Version", "link"=>"www.link.com" },
  143 + :software => {},
  144 + :library => {},
  145 + :operating_system => {},
  146 + :language => {},
  147 + :database => {}
  148 + )
  149 +
  150 + another_license.reload
  151 + edited_software = Community.find_by_identifier("debian").software_info
  152 +
  153 + assert_equal edited_software.another_license_version, "Another Version"
  154 + assert_equal edited_software.another_license_link, "www.link.com"
  155 + assert_equal another_license.version, "Another"
  156 + assert_equal another_license.link, "#"
  157 + end
  158 +
127 should 'only admin upgrade a generic software to a public software' do 159 should 'only admin upgrade a generic software to a public software' do
128 admin_person = create_user('admin').person 160 admin_person = create_user('admin').person
129 @environment.add_admin(admin_person) 161 @environment.add_admin(admin_person)
@@ -142,7 +174,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -142,7 +174,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
142 :software => fields[4], 174 :software => fields[4],
143 ) 175 )
144 176
145 - assert SoftwareInfo.last.public_software? 177 + edited_software = Community.find_by_identifier("debian").software_info
  178 + assert edited_software.public_software?
146 end 179 end
147 180
148 should 'not upgrade a generic software to a public software if user is not an admin' do 181 should 'not upgrade a generic software to a public software if user is not an admin' do
@@ -158,7 +191,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -158,7 +191,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
158 :software => fields[4] 191 :software => fields[4]
159 ) 192 )
160 193
161 - refute SoftwareInfo.last.public_software? 194 + edited_software = Community.find_by_identifier("debian").software_info
  195 + refute edited_software.public_software?
162 end 196 end
163 197
164 ["e_ping","e_mag","icp_brasil","e_arq","intern"].map do |attr| 198 ["e_ping","e_mag","icp_brasil","e_arq","intern"].map do |attr|
@@ -176,7 +210,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -176,7 +210,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
176 :software => fields[4] 210 :software => fields[4]
177 ) 211 )
178 212
179 - refute SoftwareInfo.last.send(attr) 213 + edited_software = Community.find_by_identifier("debian").software_info
  214 + refute edited_software.send(attr)
180 end 215 end
181 end 216 end
182 217
@@ -199,7 +234,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -199,7 +234,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
199 :software => fields[4] 234 :software => fields[4]
200 ) 235 )
201 236
202 - assert SoftwareInfo.last.send(attr) 237 + edited_software = Community.find_by_identifier("debian").software_info
  238 + assert edited_software.send(attr)
203 end 239 end
204 end 240 end
205 241
@@ -214,7 +250,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -214,7 +250,8 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
214 :profile => @person.identifier 250 :profile => @person.identifier
215 ) 251 )
216 252
217 - assert_equal SoftwareInfo.last.license_info, LicenseInfo.last 253 + new_software = Community.find_by_identifier("new-software").software_info
  254 + assert_equal new_software.license_info, LicenseInfo.last
218 end 255 end
219 256
220 should "create software_info with 'Another' license_info" do 257 should "create software_info with 'Another' license_info" do
@@ -235,10 +272,11 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -235,10 +272,11 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
235 :profile => @person.identifier 272 :profile => @person.identifier
236 ) 273 )
237 274
238 - assert_equal license_another.id, SoftwareInfo.last.license_info_id  
239 - assert_equal nil, SoftwareInfo.last.license_info.id  
240 - assert_equal another_license_version, SoftwareInfo.last.license_info.version  
241 - assert_equal another_license_link, SoftwareInfo.last.license_info.link 275 + new_software = Community.find_by_identifier("new-software").software_info
  276 + assert_equal license_another.id, new_software.license_info_id
  277 + assert_equal license_another.id, new_software.license_info.id
  278 + assert_equal another_license_version, new_software.license_info.version
  279 + assert_equal another_license_link, new_software.license_info.link
242 end 280 end
243 281
244 should "create software_info after finish task with 'Another' license_info" do 282 should "create software_info after finish task with 'Another' license_info" do
@@ -251,6 +289,7 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -251,6 +289,7 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
251 :new_software, 289 :new_software,
252 :community => { :name => "New Software", :identifier => "new-software" }, 290 :community => { :name => "New Software", :identifier => "new-software" },
253 :software_info => { :finality => "something", :repository_link => "" }, 291 :software_info => { :finality => "something", :repository_link => "" },
  292 + :license_info => { :version => license_another.version },
254 :license => { :license_infos_id => license_another.id, 293 :license => { :license_infos_id => license_another.id,
255 :version => another_license_version, 294 :version => another_license_version,
256 :link=> another_license_link 295 :link=> another_license_link
@@ -261,10 +300,11 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -261,10 +300,11 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
261 @environment.add_admin(@person) 300 @environment.add_admin(@person)
262 Task.last.send('finish', @person) 301 Task.last.send('finish', @person)
263 302
264 - assert_equal license_another.id, SoftwareInfo.last.license_info_id  
265 - assert_equal nil, SoftwareInfo.last.license_info.id  
266 - assert_equal another_license_version, SoftwareInfo.last.license_info.version  
267 - assert_equal another_license_link, SoftwareInfo.last.license_info.link 303 + new_software = Community.find_by_identifier("new-software").software_info
  304 + assert_equal license_another.id, new_software.license_info_id
  305 + assert_equal license_another.id, new_software.license_info.id
  306 + assert_equal another_license_version, new_software.license_info.version
  307 + assert_equal another_license_link, new_software.license_info.link
268 end 308 end
269 309
270 should "show error messages on create software_info" do 310 should "show error messages on create software_info" do
@@ -316,8 +356,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC @@ -316,8 +356,9 @@ class SoftwareCommunitiesPluginMyprofileControllerTest &lt; ActionController::TestC
316 @environment.add_admin(@person) 356 @environment.add_admin(@person)
317 Task.last.send('finish', @person) 357 Task.last.send('finish', @person)
318 358
  359 + new_software = Community.find_by_identifier("new-software").software_info
319 assert_equal "New Software", Task.last.data[:name] 360 assert_equal "New Software", Task.last.data[:name]
320 - assert_equal "New Software", SoftwareInfo.last.community.name 361 + assert_equal "New Software", new_software.community.name
321 end 362 end
322 363
323 should "dont create software without accept task" do 364 should "dont create software without accept task" do
src/noosfero-spb/software_communities/test/helpers/plugin_test_helper.rb
@@ -9,9 +9,12 @@ module PluginTestHelper @@ -9,9 +9,12 @@ module PluginTestHelper
9 end 9 end
10 10
11 def create_software_info name, finality = "something", acronym = "" 11 def create_software_info name, finality = "something", acronym = ""
  12 + license = create_license_info("GPL")
12 community = create_community(name) 13 community = create_community(name)
  14 +
13 software_info = SoftwareInfo.new 15 software_info = SoftwareInfo.new
14 software_info.community = community 16 software_info.community = community
  17 + software_info.license_info = license
15 software_info.finality = finality 18 software_info.finality = finality
16 software_info.acronym = acronym 19 software_info.acronym = acronym
17 software_info.public_software = true 20 software_info.public_software = true
@@ -55,7 +58,7 @@ module PluginTestHelper @@ -55,7 +58,7 @@ module PluginTestHelper
55 end 58 end
56 59
57 def create_license_info version, link = "" 60 def create_license_info version, link = ""
58 - license = LicenseInfo.create(:version => version) 61 + license = LicenseInfo.find_or_create_by_version(version)
59 license.link = link 62 license.link = link
60 license.save 63 license.save
61 64
src/noosfero-spb/software_communities/test/unit/software_info_test.rb
@@ -36,7 +36,6 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase @@ -36,7 +36,6 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase
36 @software_info.another_license(another_license_version, another_license_link) 36 @software_info.another_license(another_license_version, another_license_link)
37 37
38 assert_equal @software_info.license_info_id, @license_another.id 38 assert_equal @software_info.license_info_id, @license_another.id
39 - assert_equal @software_info.license_info.id, nil  
40 assert_equal @software_info.license_info.version, another_license_version 39 assert_equal @software_info.license_info.version, another_license_version
41 assert_equal @software_info.license_info.link, another_license_link 40 assert_equal @software_info.license_info.link, another_license_link
42 end 41 end
@@ -53,7 +52,16 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase @@ -53,7 +52,16 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase
53 assert_equal 1, SoftwareInfo.search_by_query("", Environment.default).count 52 assert_equal 1, SoftwareInfo.search_by_query("", Environment.default).count
54 assert_equal software_info, SoftwareInfo.search_by_query("", Environment.default).first 53 assert_equal software_info, SoftwareInfo.search_by_query("", Environment.default).first
55 assert_equal 1, SoftwareInfo.search_by_query("", other_env).count 54 assert_equal 1, SoftwareInfo.search_by_query("", other_env).count
56 - assert_equal true, SoftwareInfo.search_by_query("", other_env).includes?(another_software_info) 55 + assert_equal true, SoftwareInfo.search_by_query("", other_env).include?(another_software_info)
57 end 56 end
58 57
  58 + should "start another license with default values" do
  59 + software_info = create_software_info("software_test")
  60 + license_another = create_license_info("Another")
  61 +
  62 + software_info.license_info_id = license_another.id
  63 +
  64 + assert_equal software_info.license_info.version, "Another"
  65 + assert_equal software_info.license_info.link, "#"
  66 + end
59 end 67 end
src/noosfero-spb/software_communities/test/unit/software_info_validation_test.rb
@@ -31,6 +31,8 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase @@ -31,6 +31,8 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase
31 @operating_system.operating_system_name = @operating_system_name 31 @operating_system.operating_system_name = @operating_system_name
32 @operating_system.save 32 @operating_system.save
33 33
  34 + @license_info = LicenseInfo.create(:version => 'New License', :link => '#')
  35 +
34 @software_info = SoftwareInfo.new( 36 @software_info = SoftwareInfo.new(
35 :acronym => "SFTW", 37 :acronym => "SFTW",
36 :e_mag => true, 38 :e_mag => true,
@@ -41,7 +43,8 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase @@ -41,7 +43,8 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase
41 :operating_platform => true, 43 :operating_platform => true,
42 :objectives => "", 44 :objectives => "",
43 :features => "", 45 :features => "",
44 - :finality => "something" 46 + :finality => "something",
  47 + :license_info => @license_info
45 ) 48 )
46 @software_info.software_languages << @software_language 49 @software_info.software_languages << @software_language
47 @software_info.software_databases << @software_database 50 @software_info.software_databases << @software_database
@@ -119,4 +122,10 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase @@ -119,4 +122,10 @@ class SoftwareInfoValidationTest &lt; ActiveSupport::TestCase
119 assert_equal false, @software_info.save 122 assert_equal false, @software_info.save
120 assert_equal true, @software_info.errors.full_messages.include?(error_msg) 123 assert_equal true, @software_info.errors.full_messages.include?(error_msg)
121 end 124 end
  125 +
  126 + should "not save software without license" do
  127 + @software_info.license_info = nil
  128 +
  129 + assert_equal false, @software_info.save
  130 + end
122 end 131 end
src/noosfero-spb/software_communities/test/unit/software_registration_test.rb
@@ -5,6 +5,8 @@ class SoftwareRegistrationTest &lt; ActiveSupport::TestCase @@ -5,6 +5,8 @@ class SoftwareRegistrationTest &lt; ActiveSupport::TestCase
5 def setup 5 def setup
6 @environment = Environment.default 6 @environment = Environment.default
7 @environment.enable_plugin(SoftwareCommunitiesPlugin) 7 @environment.enable_plugin(SoftwareCommunitiesPlugin)
  8 +
  9 + @license_info = LicenseInfo.create(:version => "New License", :link => "#")
8 end 10 end
9 11
10 def teardown 12 def teardown
@@ -31,7 +33,8 @@ class SoftwareRegistrationTest &lt; ActiveSupport::TestCase @@ -31,7 +33,8 @@ class SoftwareRegistrationTest &lt; ActiveSupport::TestCase
31 :name => "Teste Two", 33 :name => "Teste Two",
32 :requestor => person, 34 :requestor => person,
33 :environment => @environment, 35 :environment => @environment,
34 - :finality => "something" 36 + :finality => "something",
  37 + :license_info => @license_info
35 ) 38 )
36 39
37 software_count = SoftwareInfo.count 40 software_count = SoftwareInfo.count