This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
307 lines (243 loc) · 10.5 KB
/
app.rb
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
307
require 'yaml'
require 'sinatra'
require 'sinatra/config_file'
require 'sinatra/namespace'
require 'fhir_client'
require 'rest-client'
require 'time_difference'
require 'pry'
require 'dm-core'
require 'dm-migrations'
require 'jwt'
require 'json/jwt'
config_file './config.yml'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if settings.disable_verify_peer
DEFAULT_SCOPES = settings.default_scopes
BASE_PATH = "/#{settings.base_path.gsub(/[^0-9a-z_-]/i, '')}"
DataMapper::Logger.new($stdout, :debug) if settings.environment == :development
DataMapper::Model.raise_on_save_failure = true
DataMapper.setup(:default, "sqlite3:data/#{settings.environment.to_s}_data.db")
require './lib/sequence_base'
['lib', 'models'].each do |dir|
Dir.glob(File.join(File.dirname(File.absolute_path(__FILE__)),dir, '**','*.rb')).each do |file|
require file
end
end
require './lib/version'
DataMapper.finalize
[TestingInstance, SequenceResult, TestResult, TestWarning, RequestResponse, RequestResponseTestResult, SupportedResource, ResourceReference].each do |model|
if settings.purge_database_on_reload || settings.environment == :test
model.auto_migrate!
else
model.auto_upgrade!
end
end
helpers do
def request_headers
env.inject({}){|acc, (k,v)| acc[$1.downcase] = v if k =~ /^http_(.*)/i; acc}
end
def version
VERSION
end
def app_name
settings.app_name
end
def valid_json?(json)
JSON.parse(json)
return true
rescue JSON::ParserError => e
return false
end
def tls_testing_supported?
TlsTester.testing_supported?
end
def show_tutorial
settings.show_tutorial
end
end
get '/' do
status, headers, body = call! env.merge("PATH_INFO" => BASE_PATH)
end
get '/landing/?' do
# Custom landing page intended to be overwritten for branded deployments
erb :landing
end
namespace BASE_PATH do
get '/?' do
erb :index
end
get '/static/*' do
status, headers, body = call! env.merge("PATH_INFO" => '/' + params['splat'].first)
end
get '/:id/?' do
instance = TestingInstance.get(params[:id])
halt 404 if instance.nil?
sequence_results = instance.latest_results
erb :details, {}, {instance: instance, sequences: SequenceBase.ordered_sequences, sequence_results: sequence_results, error_code: params[:error]}
end
post '/?' do
url = params['fhir_server']
url = url.chomp('/') if url.end_with?('/')
@instance = TestingInstance.new(url: url, name: params['name'], base_url: request.base_url)
@instance.save!
redirect "#{BASE_PATH}/#{@instance.id}/"
end
get '/:id/test_result/:test_result_id/?' do
@test_result = TestResult.get(params[:test_result_id])
halt 404 if @test_result.sequence_result.testing_instance.id != params[:id]
erb :test_result_details, layout: false
end
get '/:id/sequence_result/:sequence_result_id/cancel' do
@sequence_result = SequenceResult.get(params[:sequence_result_id])
halt 404 if @sequence_result.testing_instance.id != params[:id]
@sequence_result.result = 'cancel'
cancel_message = 'Test cancelled by user.'
if @sequence_result.test_results.length > 0
last_result = @sequence_result.test_results.last
last_result.result = 'cancel'
last_result.message = cancel_message
end
sequence = SequenceBase.subclasses.find{|x| x.to_s.start_with?(@sequence_result.name)}
current_test_count = @sequence_result.test_results.length
sequence.tests.each_with_index do |test, index|
next if index < current_test_count
@sequence_result.test_results << TestResult.new(name: test[:name], result: 'cancel', url: test[:url], description: test[:description], test_index: test[:test_index], message: cancel_message)
end
@sequence_result.save!
redirect "#{BASE_PATH}/#{params[:id]}/##{@sequence_result.name}"
end
get '/:id/:sequence/?' do
instance = TestingInstance.get(params[:id])
client = FHIR::Client.new(instance.url)
client.use_dstu2
client.default_json
klass = SequenceBase.subclasses.find{|x| x.to_s.start_with?(params[:sequence])}
if klass
sequence = klass.new(instance, client, settings.disable_tls_tests)
stream do |out|
out << erb(:details, {}, {instance: instance, sequences: SequenceBase.ordered_sequences, sequence_results: instance.latest_results, tests_running: true})
out << "<script>$('#WaitModal').modal('hide')</script>"
out << "<script>$('#testsRunningModal').modal('show')</script>"
count = 0
sequence_result = sequence.start do |result|
count = count + 1
out << "<script>$('#testsRunningModal').find('.number-complete').html('(#{count} of #{sequence.test_count} complete)');</script>"
end
sequence_result.save!
if sequence_result.redirect_to_url
out << "<script>$('#testsRunningModal').find('.modal-body').html('Redirecting to <textarea readonly class=\"form-control\" rows=\"3\">#{sequence_result.redirect_to_url}</textarea>');</script>"
out << "<script> window.location = '#{sequence_result.redirect_to_url}'</script>"
else
out << "<script> window.location = '#{BASE_PATH}/#{params[:id]}/##{params[:sequence]}'</script>"
end
end
else
redirect "#{BASE_PATH}/#{params[:id]}/##{params[:sequence]}"
end
end
post '/:id/ConformanceSkip/?' do
instance = TestingInstance.get(params[:id])
conformance_sequence_result = SequenceResult.new(name: "Conformance", result: "skip")
conformance_sequence_result.save
instance.conformance_checked = false
instance.oauth_authorize_endpoint = params[:conformance_authorize_endpoint]
instance.oauth_token_endpoint = params[:conformance_token_endpoint]
instance.sequence_results.push(conformance_sequence_result)
instance.save!
redirect "#{BASE_PATH}/#{params[:id]}/"
end
post '/:id/DynamicRegistration' do
@instance = TestingInstance.get(params[:id])
@instance.update(dynamically_registered: false, oauth_register_endpoint: params['registration_url'], scopes: params['scope'], client_name: params['client_name'])
redirect "#{BASE_PATH}/#{@instance.id}/DynamicRegistration/"
end
post '/:id/ArgonautDataQuery' do
instance = TestingInstance.get(params[:id])
halt 404 if instance.nil?
instance.resource_references.select{|ref| ref.resource_type == 'Patient'}.each(&:destroy)
params['patient_id'].split(",").map(&:strip).each do |patient_id|
instance.resource_references << ResourceReference.new({resource_type: 'Patient', resource_id: patient_id})
end
instance.save
redirect "#{BASE_PATH}/#{instance.id}/ArgonautDataQuery/"
end
post '/:id/ArgonautProfiles' do
instance = TestingInstance.get(params[:id])
halt 404 if instance.nil?
redirect "/#{BASE_PATH}/#{instance.id}/ArgonautProfiles/"
end
post '/:id/dynamic_registration_skip/?' do
instance = TestingInstance.get(params[:id])
sequence_result = SequenceResult.new(name: "DynamicRegistration", result: "skip")
instance.sequence_results << sequence_result
instance.client_id = params[:client_id]
instance.scopes = params[:scope]
instance.dynamically_registered = false
instance.save!
redirect "/#{BASE_PATH}/#{params[:id]}/"
end
post '/:id/PatientStandaloneLaunch/?' do
@instance = TestingInstance.get(params[:id])
@instance.update(scopes: params['scopes'], id_token: nil, refresh_token: nil)
redirect "/#{BASE_PATH}/#{params[:id]}/PatientStandaloneLaunch/"
end
post '/:id/ProviderEHRLaunch/?' do
@instance = TestingInstance.get(params[:id])
@instance.update(scopes: params['scopes'], id_token: nil, refresh_token: nil)
redirect "/#{BASE_PATH}/#{params[:id]}/ProviderEHRLaunch/"
end
post '/:id/OpenIDConnect/?' do
@instance = TestingInstance.get(params[:id])
redirect "/#{BASE_PATH}/#{params[:id]}/OpenIDConnect/"
end
post '/:id/TokenIntrospectionSkip/?' do
instance = TestingInstance.get(params[:id])
sequence_result = SequenceResult.new(name: "TokenIntrospection", result: "skip")
instance.sequence_results << sequence_result
instance.save!
redirect "/#{BASE_PATH}/#{params[:id]}/"
end
post '/:id/TokenIntrospection/?' do
@instance = TestingInstance.get(params[:id])
@instance.update(oauth_introspection_endpoint: params['oauth_introspection_endpoint'])
@instance.update(resource_id: params['resource_id'])
@instance.update(resource_secret: params['resource_secret'])
# copy over the access token to a different place in case it's not the same
@instance.update(introspect_token: params['access_token'])
@instance.update(introspect_refresh_token: params['refresh_token'])
redirect "/#{BASE_PATH}/#{params[:id]}/TokenIntrospection/"
end
get '/:id/:key/:endpoint/?' do
instance = TestingInstance.get(params[:id])
halt 404 unless !instance.nil? && instance.client_endpoint_key == params[:key] && ['launch','redirect'].include?(params[:endpoint])
sequence_result = instance.waiting_on_sequence
if sequence_result.nil? || sequence_result.result != 'wait'
redirect "/#{BASE_PATH}/#{params[:id]}/?error=no_#{params[:endpoint]}"
else
klass = SequenceBase.subclasses.find{|x| x.to_s.start_with?(sequence_result.name)}
client = FHIR::Client.new(instance.url)
client.use_dstu2
client.default_json
sequence = klass.new(instance, client, settings.disable_tls_tests, sequence_result)
stream do |out|
out << erb(:details, {}, {instance: instance, sequences: SequenceBase.ordered_sequences, sequence_results: instance.latest_results, tests_running: true})
out << "<script>$('#WaitModal').modal('hide')</script>"
out << "<script>$('#testsRunningModal').modal('show')</script>"
count = sequence_result.test_results.length
sequence_result = sequence.resume(request, headers, request.params) do |result|
count = count + 1
out << "<script>$('#testsRunningModal').find('.number-complete').html('(#{count} of #{sequence.test_count} complete)');</script>"
instance.save!
end
instance.sequence_results.push(sequence_result)
instance.save!
if sequence_result.redirect_to_url
out << "<script>$('#testsRunningModal').find('.modal-body').html('Redirecting to <textarea readonly class=\"form-control\" rows=\"3\">#{sequence_result.redirect_to_url}</textarea>');</script>"
out << "<script> window.location = '#{sequence_result.redirect_to_url}'</script>"
else
out << "<script> window.location = '#{BASE_PATH}/#{params[:id]}/##{params[:sequence]}'</script>"
end
end
end
end
end