summaryrefslogtreecommitdiff
path: root/test/toxbank-investigation-workflow.rb
blob: 6bc8a70c4f5e0956fdc8d60804a8c181df2fb488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
require_relative "toxbank-setup.rb"
require File.join(File.expand_path(File.dirname(__FILE__)),".." ,".." ,"toxbank-investigation", "util.rb")

begin
  puts "Service URI is: #{$investigation[:uri]}"
rescue
  puts "Configuration Error: $investigation[:uri] is not defined in: " + File.join(ENV["HOME"],".opentox","config","test.rb")
  exit
end

class TBInvestigationWorkflow < MiniTest::Test
  i_suck_and_my_tests_are_order_dependent!
# Permission Matrix for owner, user1 (with GET permission (e.G.: group-permission) and user2 (no permission)
# Sum    = isSummarySearchable=true
# noSum  = isSummarySearchable=false
# Pub    = isPublished = true
# noPub  = isPublished = false
#                      noPub  noSum         noPub  noSum
#                      noSum   Pub  Pub+Sum noSum   Pub  Pub+Sum
#               owner  user1  user1  user1  user2  user2  user2
# GET             y      n      y      y      n      n      n
# POST,PUT,DEL    y      n      n      n      n      n      n
# /metadata       y      n      y      y      n      n      y
# /protocol       y      n      y      y      n      n      y
# Download        y      n      y      y      n      n      n
# Search          ?      n      n      y      n      n      y

  def setup
    OpenTox::RestClientWrapper.subjectid = $pi[:subjectid] # set pi as the logged in user
  end

  # create a new investigation by uploading a zip file,
  # owner is $pi, Summary is not searchable, access=custom(owner only), not published
  def test_01_post_investigation
    @@uri = ""
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "BII-I-1b-tb2.zip"
    response = OpenTox::RestClientWrapper.post $investigation[:uri], {:file => File.open(file)}, { :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal "Completed", task.hasStatus, "Task should be completed but is: #{task.hasStatus}. Task URI is #{task_uri} ."
    @@uri = URI(uri)
  end

  # check if @@uri is not in search-index
  def test_02_investigation_not_in_searchindex
    response = OpenTox::RestClientWrapper.get "#{$search_service[:uri]}/search/index/investigation?resourceUri=#{CGI.escape(@@uri.to_s)}",{},{:subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
    refute_match /#{@@uri}/, response.to_s
  end

  # check for flag "isPublished" is false,
  # @note default behaviour on new investigations
  def test_03_check_published_false
    data = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $pi[:subjectid]}
    @g = RDF::Graph.new
    RDF::Reader.for(:rdfxml).new(data.to_s){|r| r.each{|s| @g << s}}
    @g.query(:predicate => RDF::TB.isPublished){|r| assert_match /false/, r[2].to_s}
  end

  # check all permissions for owner
  def test_04a_all_permission
    ["GET","POST","PUT","DELETE"].each do |permission|
      response = OpenTox::Authorization.authorize "#{@@uri}", permission, $pi[:subjectid]
      assert_equal true, response
    end
  end

  # get metadata for owner
  def test_04b_get_metadata_pi
    response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
  end

  # get related protocol uris for owner
  def test_04c_get_protocol_pi
    response = OpenTox::RestClientWrapper.get "#{@@uri}/protocol", {}, {:accept => "application/rdf+xml", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
  end

  def test_04d_get_download_owner
    response = OpenTox::RestClientWrapper.get "#{@@uri}", {}, {:accept => "application/zip", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
  end

  # no get permission for user1
  def test_05a_no_get_permission
    response = OpenTox::Authorization.authorize "#{@@uri}", "GET", $secondpi[:subjectid]
    assert_equal false, response
  end

  # do not get metadata for user1
  def test_05b_get_metadata_secondpi
    assert_raises OpenTox::UnauthorizedError do
      response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $secondpi[:subjectid]}
    end
  end

  # do not get protocol for user1
  def test_05c_get_protocol_secondpi
    assert_raises OpenTox::UnauthorizedError do
      response = OpenTox::RestClientWrapper.get "#{@@uri}/protocol", {}, {:accept => "application/rdf+xml", :subjectid => $secondpi[:subjectid]}
    end
  end

  # do not get download for user1
  def test_05d_get_download_secondpi
    assert_raises OpenTox::UnauthorizedError do
      response = OpenTox::RestClientWrapper.get "#{@@uri}", {}, {:accept => "application/zip", :subjectid => $secondpi[:subjectid]}
    end
  end

  # no post/put/delete permission for user1
  def test_05e_no_cud_permission
    ["POST", "PUT", "DELETE"].each do |permission|
      response = OpenTox::Authorization.authorize "#{@@uri}", permission, $secondpi[:subjectid]
      assert_equal false, response
    end
  end

  def test_06_put_group_access
    @@toxbank_uri = `curl -Lk -X GET -H "Accept:text/uri-list" -H "subjectid:#{$pi[:subjectid]}" #{$user_service[:uri]}/project?search=ToxBank`.chomp.sub("\n","")
    response = OpenTox::RestClientWrapper.put @@uri.to_s, { :allowReadByGroup => "#{@@toxbank_uri}"},{ :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal uri, @@uri.to_s
  end

  # get permission for user1
  def test_07a_get_permission
    response = OpenTox::Authorization.authorize "#{@@uri}", "GET", $secondpi[:subjectid]
    assert_equal true, response
  end

  # repeat with permissions for toxbank group
  def test_07b_repeat_05bcd
    test_05b_get_metadata_secondpi
    test_05c_get_protocol_secondpi
    test_05d_get_download_secondpi
    test_05e_no_cud_permission
    test_02_investigation_not_in_searchindex
  end

  def test_08_put_published
    response = OpenTox::RestClientWrapper.put @@uri.to_s, { :published => "true"},{ :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal uri, @@uri.to_s
  end

  def test_09a_get_user1
    response = OpenTox::RestClientWrapper.get "#{@@uri}", {}, {:accept => "text/uri-list", :subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
  end

  def test_09b_put_user1
    assert_raises OpenTox::UnauthorizedError do
      response = OpenTox::RestClientWrapper.put "#{@@uri}", {}, {:published => "true", :subjectid => $secondpi[:subjectid]}
    end
  end

  def test_09c_post_user1
    assert_raises OpenTox::UnauthorizedError do
      response = OpenTox::RestClientWrapper.post "#{@@uri}", {}, {:published => "true", :subjectid => $secondpi[:subjectid]}
    end
  end

  def test_09d_delete_user1
    assert_raises OpenTox::UnauthorizedError do
      response = OpenTox::RestClientWrapper.delete "#{@@uri}", {}, {:subjectid => $secondpi[:subjectid]}
    end
  end

  # get metadata for user1
  def test_09e_get_metadata_user1
    response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $secondpi[:subjectid]}
    assert_equal 200, response.code
  end

  # get related protocol uris for user1
  def test_09f_get_protocol_user1
    response = OpenTox::RestClientWrapper.get "#{@@uri}/protocol", {}, {:accept => "application/rdf+xml", :subjectid => $secondpi[:subjectid]}
    assert_equal 200, response.code
  end
  
  def test_09g_get_download_user1
    response = OpenTox::RestClientWrapper.get "#{@@uri}", {}, {:accept => "application/zip", :subjectid => $secondpi[:subjectid]}
    assert_equal 200, response.code
  end

  def test_09h_not_indexed
    test_02_investigation_not_in_searchindex
  end

  def test_10_put_searchable
    response = OpenTox::RestClientWrapper.put @@uri.to_s, { :summarySearchable => "true"},{ :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal uri, @@uri.to_s
  end

  def test_11a_repeat_user1_tests
    test_09a_get_user1
    test_09b_put_user1
    test_09c_post_user1
    test_09d_delete_user1
    test_09e_get_metadata_user1
    test_09f_get_protocol_user1
    test_09g_get_download_user1
  end

  def test_11b_is_indexed
    response = OpenTox::RestClientWrapper.get "#{$search_service[:uri]}/search/index/investigation?resourceUri=#{CGI.escape(@@uri.to_s)}",{},{:subjectid => $pi[:subjectid]}
    assert_equal 200, response.code
    assert_match /#{@@uri}/, response.to_s
  end

  def test_12_remove_group_access
    response = OpenTox::RestClientWrapper.put @@uri.to_s, { :allowReadByGroup => ""},{ :subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal uri, @@uri.to_s
  end

  # searchable + published without GET policy
  def test_13a_repeat_05bcd
    test_05a_no_get_permission
    test_05d_get_download_secondpi
    test_05e_no_cud_permission
    test_11b_is_indexed
  end

  # get metadata for user1
  def test_13c_get_metadata_second_pi
    response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $secondpi[:subjectid]}
    assert_equal 200, response.code
  end

  # get related protocol uris for user1
  def test_13d_get_protocol_second_pi
    response = OpenTox::RestClientWrapper.get "#{@@uri}/protocol", {}, {:accept => "application/rdf+xml", :subjectid => $secondpi[:subjectid]}
    assert_equal 200, response.code
  end

  def test_20_update_modified_time
    response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $pi[:subjectid]}
    g = RDF::Graph.new
    RDF::Reader.for(:rdfxml).new(response.to_s){|r| r.each{|s| g << s}}
    g.query(:predicate => RDF::DC.modified){|r| @modified_time1 = r[2].to_s}
    t_start = Time.parse(@modified_time1).to_i
    response = OpenTox::RestClientWrapper.put @@uri.to_s, { :allowReadByGroup => "#{@@toxbank_uri}"},{ :subjectid => $pi[:subjectid] }
    sleep 2
    response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $pi[:subjectid]}
    g = RDF::Graph.new
    RDF::Reader.for(:rdfxml).new(response.to_s){|r| r.each{|s| g << s}}
    g.query(:predicate => RDF::DC.modified){|r| @modified_time2 = r[2].to_s}
    t_end =  Time.parse(@modified_time2).to_i
    assert t_end > t_start, "modified time is not updated"
  end

  # delete investigation/{id}
  # @note expect code 200
  def test_99_a_delete_investigation
    result = OpenTox::RestClientWrapper.delete @@uri.to_s, {}, {:subjectid => $pi[:subjectid]}
    assert_equal 200, result.code
    #assert result.match(/^Investigation [a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12} deleted$/)
    assert !OpenTox::Authorization.uri_has_policy(@@uri.to_s)
  end

  # create an investigation
  def test_99_b_post_investigation
    @@uri = ""
    file = File.join File.dirname(__FILE__), "data/toxbank-investigation/valid", "BII-I-1b-tb2.zip"
    response = OpenTox::RestClientWrapper.post $investigation[:uri], {:file => File.open(file), :summarySearchable => "true"}, {:subjectid => $pi[:subjectid] }
    task_uri = response.chomp
    task = OpenTox::Task.new task_uri
    task.wait
    uri = task.resultURI
    assert_equal "Completed", task.hasStatus, "Task should be completed but is: #{task.hasStatus}. Task URI is #{task_uri} ."
    @@uri = URI(uri)
  end

  #TODO user2 can get metadata of an unpublished investigation, this is wrong workflow
  def test_99_c_get_metadata_for_user2
    response = OpenTox::RestClientWrapper.get "#{@@uri}/metadata", {}, {:accept => "application/rdf+xml", :subjectid => $guestid}
    assert_equal 200, response.code
  end

  # delete investigation/{id}
  # @note expect code 200
  def test_99_d_delete_investigation
    result = OpenTox::RestClientWrapper.delete @@uri.to_s, {}, {:subjectid => $pi[:subjectid]}
    assert_equal 200, result.code
    assert !OpenTox::Authorization.uri_has_policy(@@uri.to_s)
  end

end