add git-server website test
[service-tests] / service-tests / service-tests.rb
1 # Make updates and copy to container
2 # docker cp service-tests.rb service-tests:/home/ist/service-tests/service-tests.rb
3 # even if container is stopped !
4
5 require 'minitest/autorun'
6 require 'rest-client'
7 require 'nokogiri'
8 require 'csv'
9
10 class WebServiceTests < Minitest::Test
11
12   def get_models
13     require '../lazar/lib/lazar.rb'
14     $models = OpenTox::Model::Validation.all
15     $r_model = $models.select{|m| m if m.model.name == "LOAEL (training_log10)"}[0].id.to_s
16     $c_model = $models.select{|m| m if m.model.name == "Mutagenicity (kazius)"}[0].id.to_s
17   end
18
19   def test_virtuoso
20     puts "\nhttps://virtuoso.in-silico.ch"
21     assert_raises RestClient::NotFound do
22       response = RestClient.get "https://virtuoso.in-silico.ch"
23     end
24   end
25   
26   def test_insilico_website
27     puts "\nhttps://in-silico.ch"
28     response = RestClient.get "https://in-silico.ch"
29     assert_equal response.code, 200
30     xml = Nokogiri::HTML.parse(response.body)
31     value = xml.at('li.active').text.strip
32     assert_equal "Software and services", value
33   end
34   
35   def test_predictive_toxicology
36     puts "\nhttps://predictive-toxicology.org/"
37     response = RestClient.get "https://predictive-toxicology.org/"
38     assert_equal response.code, 200
39     xml = Nokogiri::HTML.parse(response.body)
40     value = xml.at('title').text
41     assert_equal "Predictive Toxicology", value
42   end
43   
44   def test_lazar_batch
45     puts "\nhttps://lazar-batch.in-silico.ch"
46     require '../batch-login.rb'
47     assert_raises RestClient::Unauthorized do
48       response = RestClient.get "https://lazar-batch.in-silico.ch"
49     end
50     assert_raises RestClient::Unauthorized do
51       response = RestClient::Request.execute method: :get, url: "https://lazar-batch.in-silico.ch", user: $user+"f", password: $passwd
52     end
53     assert_raises RestClient::Unauthorized do
54       response = RestClient::Request.execute method: :get, url: "https://lazar-batch.in-silico.ch", user: $user, password: $passwd+"f"
55     end
56     response = RestClient::Request.execute method: :get, url: "https://lazar-batch.in-silico.ch", user: $user, password: $passwd
57     xml = Nokogiri::HTML.parse(response.body)
58     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "lazar toxicity predictions"
59     # batch
60     get_models
61     response = RestClient::Request.execute method: :post, 
62       url:  "https://lazar-batch.in-silico.ch/predict", 
63       user: $user,
64       password: $passwd,
65       payload: {
66         multipart: true,
67         fileselect: File.new('./test.csv', 'rb'),
68         selection: {$r_model => ""}
69       }
70     assert_equal response.code, 200
71     xml = Nokogiri::HTML.parse(response.body)
72     value = xml.css('h5').text
73     assert_equal "Lowest observed adverse effect level (LOAEL) (Rat)", value
74     value = xml.at_css('a#detailsbutton_0').text.strip
75     assert_equal "Details", value
76   end
77   
78   def test_dump
79     puts "\nhttps://dump.in-silico.ch"
80     response = RestClient.get "https://dump.in-silico.ch"
81     assert_equal response.code, 200
82     xml = Nokogiri::HTML.parse(response.body)
83     dumpsize = xml.at('pre').text.split.last.to_i 
84     response = RestClient.get "https://dump.in-silico.ch/dump.tar.gz"
85     assert_equal response.body.size.to_i, dumpsize
86   end
87   
88   def test_lazar
89     puts "\nhttps://lazar.in-silico.ch"
90     get_models
91     response = RestClient.get "https://lazar.in-silico.ch"
92     assert_equal response.code, 200
93     xml = Nokogiri::HTML.parse(response.body)
94     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "lazar toxicity predictions"
95     puts "\npredict regression: https://lazar.in-silico.ch/predict"
96     # regression
97     response = RestClient.post "https://lazar.in-silico.ch/predict", {identifier: "O=[N+]([O-])c1ccccc1", selection: {$r_model=>""}}
98     xml = Nokogiri::HTML.parse(response.body)
99     value = xml.css('td')[1].css('p')[2].text.split[1].to_f
100     assert value.between?(0.06,0.09)
101     puts "\npredict classification: https://lazar.in-silico.ch/predict"
102     # classification
103     response = RestClient.post "https://lazar.in-silico.ch/predict", {identifier: "O=[N+]([O-])c1ccccc1", selection: {$c_model=>""}}
104     xml = Nokogiri::HTML.parse(response.body)
105     value = xml.css('td')[1].css('p')[4].text.split.last
106     assert_equal "mutagenic", value
107     # regression
108     puts "\nmodel details regression: https://lazar.in-silico.ch/predict/modeldetails/#{$r_model}"
109     response = RestClient.get "https://lazar.in-silico.ch/predict/modeldetails/#{$r_model}"
110     xml = Nokogiri::HTML.parse(response.body)
111     puts "check metadata\n"
112     links = xml.css('a')
113     assert_equal "https://github.com/opentox/loael-paper/blob/revision/data/training_log10.csv", links[0].children.text.strip
114     assert_equal "training_log10", links[1].children.text.strip
115     assert_equal "Algorithm::Similarity.tanimoto", links[2].children.text.strip
116     assert_equal "Algorithm::Caret.rf", links[3].children.text.strip
117     text = xml.css('div.panel-body')[0]
118     a,b = text.children[4].text.strip.split("\n")
119     assert_equal "Type:", a.strip
120     assert_equal "Regression", b.strip
121     a,b = text.children[6].text.strip.split("\n")
122     assert_equal "Training compounds:", a.strip
123     assert_equal "671", b.strip
124     text = xml.css('div.panel-body')[1]
125     a,b,c = text.children[8].text.strip.split
126     assert_equal "Descriptors:", a.strip
127     assert_equal "fingerprint,", b.strip
128     assert_equal "MP2D", c.strip
129     # classification
130     puts "\nmodel details classification: https://lazar.in-silico.ch/predict/modeldetails/#{$c_model}"
131     response = RestClient.get "https://lazar.in-silico.ch/predict/modeldetails/#{$c_model}"
132     xml = Nokogiri::HTML.parse(response.body)
133     puts "check metadata\n"
134     links = xml.css('a')
135     assert_equal "http://cheminformatics.org/datasets/", links[0].children.text.strip
136     assert_equal "kazius", links[1].children.text.strip
137     assert_equal "Algorithm::Similarity.tanimoto", links[2].children.text.strip
138     assert_equal "Algorithm::Classification.weighted_majority_vote", links[3].children.text.strip
139     text = xml.css('div.panel-body')[0]
140     a,b = text.children[4].text.strip.split("\n")
141     assert_equal "Type:", a.strip
142     assert_equal "Classification", b.strip
143     a,b = text.children[6].text.strip.split("\n")
144     assert_equal "Training compounds:", a.strip
145     assert_equal "4069", b.strip
146     text = xml.css('div.panel-body')[1]
147     a,b,c = text.children[8].text.strip.split
148     assert_equal "Descriptors:", a.strip
149     assert_equal "fingerprint,", b.strip
150     assert_equal "MP2D", c.strip
151   end
152
153   def test_nano_lazar
154     puts "\nhttps://nano-lazar.in-silico.ch"
155     response = RestClient.get "https://nano-lazar.in-silico.ch"
156     assert_equal response.code, 200
157     xml = Nokogiri::HTML.parse(response.body)
158     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "nano-lazar toxicity predictions"
159     
160     params = {example_coating_0: "N-(2-Mercaptopropionyl)glycine",
161               example_core:     "Au",
162               example_id:       "5878a6097b6c9b003a862710",
163               example_pc:       "{}",
164               input_coating_0: "N-(2-Mercaptopropionyl)glycine",
165               input_core:       "Au",
166               prediction_model: "5878aa1d7b6c9b003a8629b1",
167               type:     "fingerprint"
168     }
169     puts "\npredict example: https://nano-lazar.in-silico.ch/predict"
170     response = RestClient.post "https://nano-lazar.in-silico.ch/predict", params
171     xml = Nokogiri::HTML.parse(response.body)
172     value = xml.css('tbody').css('tr')[1].css('td')[0].css('a').text.gsub(/\n/,"").strip
173     assert_equal value, "G15.AC"
174   end
175
176   def test_aop
177     puts "\nhttps://aop.in-silico.ch"
178     response = RestClient.get "https://aop.in-silico.ch"
179     assert_equal response.code, 200
180     xml = Nokogiri::HTML.parse(response.body)
181     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "PubChem read across"
182     puts "\nhttps://aop.in-silico.ch/cid/31703"
183     response = RestClient.get "https://aop.in-silico.ch/cid/31703"
184     assert_equal response.code, 200
185     xml = Nokogiri::HTML.parse(response.body)
186     value =  xml.at('th').text.split.first.strip
187     assert_equal "(7~{S},9~{S})-7-[(2~{R},4~{S},5~{S},6~{S})-4-amino-5-hydroxy-6-methyloxan-2-yl]oxy-6,9,11-trihydroxy-9-(2-hydroxyacetyl)-4-methoxy-8,10-dihydro-7~{H}-tetracene-5,12-dione", value
188   end
189
190   def test_dg2_Elena
191     puts "\nhttps://dg2.in-silico.ch"
192     response = RestClient.get "https://dg2.in-silico.ch"
193     assert_equal response.code, 200
194     xml = Nokogiri::HTML.parse(response.body)
195     batch_file_id = xml.search("label")[4].attributes["for"].value.gsub(/existing|\[|\]/,"")
196     batch_file_name = xml.search("label")[4].text.strip
197     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "lazar toxicity predictions"
198     puts "\nsingle prediction Mazzatorta: https://dg2.in-silico.ch/predict"
199     response = RestClient.post "https://dg2.in-silico.ch/predict", {identifier: "O=[N+]([O-])c1ccccc1", selection: {"Mazzatorta"=>""}}
200     xml = Nokogiri::HTML.parse(response.body)
201     value = xml.css('td')[1].css('p')[0].text.split[1].to_f
202     assert value, 0.384
203     puts "\nsingle prediction lazar LOAEL: https://dg2.in-silico.ch/predict"
204     response = RestClient.post "https://dg2.in-silico.ch/predict", {identifier: "O=[N+]([O-])c1ccccc1", selection: {"5ba4e38b5e110b01688fa0ca"=>""}}
205     xml = Nokogiri::HTML.parse(response.body)
206     value = xml.css('td')[1].css('p')[2].text.split[1].to_f
207     assert value, 0.0807
208     
209     puts "\nbatch prediction with first dataset in existing list\n"
210     response = RestClient.post "https://dg2.in-silico.ch/predict", {existing: {batch_file_id=>""}, selection: {"5ba4e38b5e110b01688fa0ca"=>""}}
211     assert_equal response.code, 200
212     xml = Nokogiri::HTML.parse(response.body)
213     title = xml.search("h3").text
214     assert_equal title, "Batch Prediction Results:RHC_input"
215     sleep 3
216     href = xml.search("a#downbutton_0")[0]["href"]
217     puts "\ndownload csv batch results\n"
218     response = RestClient.get href
219     csv = CSV.new(response.body)
220     header = csv.first
221     ["ID", "Original ID", "Input", "Endpoint"].each do |h|
222       assert_includes header, h
223     end
224     
225     puts "\nmodel details lazar LOAEL: https://dg2.in-silico.ch/predict/modeldetails/5ba4e38b5e110b01688fa0ca"
226     response = RestClient.get "https://dg2.in-silico.ch/predict/modeldetails/5ba4e38b5e110b01688fa0ca"
227     xml = Nokogiri::HTML.parse(response.body)
228     puts "check metadata\n"
229     links = xml.css('a')
230     assert_equal "https://github.com/opentox/loael-paper/blob/revision/data/training_log10.csv", links[0].children.text.strip
231     assert_equal "training_log10", links[1].children.text.strip
232     assert_equal "Algorithm::Similarity.tanimoto", links[2].children.text.strip
233     assert_equal "Algorithm::Caret.rf", links[3].children.text.strip
234     text = xml.css('div.panel-body')[0]
235     a,b = text.children[4].text.strip.split("\n")
236     assert_equal "Type:", a.strip
237     assert_equal "Regression", b.strip
238     a,b = text.children[6].text.strip.split("\n")
239     assert_equal "Training compounds:", a.strip
240     assert_equal "671", b.strip
241     text = xml.css('div.panel-body')[1]
242     a,b,c = text.children[8].text.strip.split
243     assert_equal "Descriptors:", a.strip
244     assert_equal "fingerprint,", b.strip
245     assert_equal "MP2D", c.strip
246   end
247
248   def test_dg3_Elena
249     puts "\nhttps://dg3.in-silico.ch"
250     response = RestClient.get "https://dg3.in-silico.ch"
251     assert_equal response.code, 200
252     xml = Nokogiri::HTML.parse(response.body)
253     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "lazar toxicity predictions"
254   end
255
256   def test_test1_Elena
257     puts "\nhttps://test1.in-silico.ch"
258     response = RestClient.get "https://test1.in-silico.ch"
259     assert_equal response.code, 200
260     xml = Nokogiri::HTML.parse(response.body)
261     assert_match xml.at('h1').text.gsub(/\n/,"").strip, "Nestec Toxicity Predictions"
262   end
263
264   #def test_git_server
265   #TODO Net::HTTPBadResponse: wrong status line: "SSH-2.0-OpenSSH_7.8"
266   #  response = RestClient.get "https://git.in-silico.ch"
267   #end
268
269   def test_git_server_website
270     puts "\nhttps://git.in-silico.ch"
271     response = RestClient.get "https://git.in-silico.ch"
272     assert_equal response.code, 200
273   end
274   
275   def test_server_invalid_cert
276     puts "\ninvalid cert test4"
277     assert_raises RestClient::SSLCertificateNotVerified do
278       response = RestClient.get "https://test4.in-silico.ch"
279     end
280   end
281
282 end
283