restful-capabilities 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (20) hide show
  1. checksums.yaml +7 -0
  2. data/Rakefile +111 -0
  3. data/bin/razorrisk-microservice-capabilities +57 -0
  4. data/lib/razor_risk/cassini/applications/microservices/restful/capabilities/app.rb +306 -0
  5. data/lib/razor_risk/cassini/applications/microservices/restful/capabilities/version.rb +43 -0
  6. data/lib/razor_risk/cassini/applications/microservices/restful/capabilities.rb +17 -0
  7. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/data_subject_get.rb +65 -0
  8. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/data_subjects_get.rb +59 -0
  9. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/get_handler_mixin.rb +159 -0
  10. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/group_get.rb +65 -0
  11. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/group_users_get.rb +65 -0
  12. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/groups_get.rb +59 -0
  13. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/subject_get.rb +65 -0
  14. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/subjects_get.rb +59 -0
  15. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/user_get.rb +65 -0
  16. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities/users_get.rb +59 -0
  17. data/lib/razor_risk/cassini/applications/route_verb_adaptors/capabilities.rb +24 -0
  18. data/test/unit/adapters/tc_get_handler_mixin.rb +342 -0
  19. data/test/unit/microservice/tc_app.rb +765 -0
  20. metadata +254 -0
@@ -0,0 +1,342 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ #############################################################################
5
+ #
6
+ # Copyright (c) 2019 Razor Risk Technologies Pty Limited. All rights reserved.
7
+ #
8
+ #############################################################################
9
+
10
+ $:.unshift File.join(File.dirname(__FILE__), *(['..'] * 3), 'lib')
11
+
12
+
13
+ # ##########################################################
14
+ # requires
15
+
16
+ require 'razor_risk/cassini/testing/suppress_pantheios_logging' unless $DEBUG
17
+
18
+ require 'razor_risk/cassini/applications/route_verb_adaptors/capabilities/get_handler_mixin'
19
+
20
+ require 'razor_risk/razor/connectivity/test_doubles/razor_requester/returns_given_response'
21
+ require 'razor_risk/razor/connectivity/razor_3/response'
22
+
23
+ require 'test/unit'
24
+
25
+ # ##########################################################
26
+ # Tests
27
+
28
+ class TestGetHandlerMixin < Test::Unit::TestCase
29
+
30
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors
31
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors::Capabilities
32
+
33
+ include ::RazorRisk::Razor::Connectivity::Razor3
34
+ include ::RazorRisk::Razor::Connectivity::Razor3::EntityConnectors
35
+ include ::RazorRisk::Razor::Connectivity::TestDoubles::RazorRequester
36
+
37
+ class MockStruct
38
+
39
+ def self.create(*args)
40
+
41
+ args = [:procs] if args.empty?
42
+
43
+ Struct.new(*args) do
44
+
45
+ def method_missing(method, *args)
46
+ unless method == :procs
47
+ if procs
48
+ if procs[method]
49
+ procs[method].call(*args)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ yield if block_given?
56
+ end
57
+ end
58
+ end
59
+
60
+ class MockHandler
61
+
62
+ include ::RazorRisk::Cassini::Applications::RouteVerbAdaptors::Capabilities::GetHandlerMixin
63
+
64
+ attr_reader :settings
65
+ attr_reader :statuses
66
+ attr_reader :content_types
67
+
68
+ attr_accessor :procs
69
+
70
+ def initialize credentials, razor_requester
71
+
72
+ @procs = {}
73
+ @credentials = credentials
74
+ @settings = Struct
75
+ .new(:razor_requester, :request_options)
76
+ .new(razor_requester, {})
77
+ end
78
+
79
+ def get_required_credentials
80
+ @credentials
81
+ end
82
+
83
+ def status status
84
+ @statuses ||= []
85
+ @statuses << status
86
+ end
87
+
88
+ def content_type type
89
+ @content_types ||= []
90
+ @content_types << type
91
+ end
92
+
93
+ def halt *args
94
+ @procs[:halt].call(*args) if @procs[:halt]
95
+
96
+ raise HaltException
97
+ end
98
+
99
+ def method_missing(method, *args)
100
+ @procs[method].call(*args) if @procs[method]
101
+ end
102
+ end
103
+
104
+ class HaltException < StandardError; end
105
+
106
+ def test_is_defined
107
+
108
+ assert_const_defined Capabilities, :GetHandlerMixin
109
+ end
110
+ end
111
+
112
+ class TestInitialization < TestGetHandlerMixin
113
+
114
+ def test_initialization
115
+
116
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
117
+ cr = {}
118
+ h = MockHandler.new(cr, rr)
119
+
120
+ assert_not_nil h
121
+ assert_kind_of GetHandlerMixin, h
122
+ end
123
+ end
124
+
125
+ class TestYieldsEntityConnector < TestGetHandlerMixin
126
+
127
+ def test_yields_correct_entity_connector
128
+
129
+ # Create Handler
130
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
131
+ cr = {}
132
+ h = MockHandler.new(cr, rr)
133
+
134
+ # Handle request
135
+ result = MockStruct.create.new
136
+ response = MockStruct.create(:result, :succeeded?).new(result, true)
137
+ request = MockStruct.create.new(accept?: Proc.new { true })
138
+
139
+ h.handle 'env', 'params', request, 'response' do |ec|
140
+ assert_instance_of CapabilitiesConnector, ec
141
+ response
142
+ end
143
+ end
144
+ end
145
+
146
+ class TestHandlesResponse < TestGetHandlerMixin
147
+
148
+ def test_handles_unsuccessful_response
149
+
150
+ # Create Handler
151
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
152
+ cr = {}
153
+ h = MockHandler.new(cr, rr)
154
+
155
+ # Handle request
156
+ result = MockStruct.create(:message).new("hello world!")
157
+ reasons = MockStruct.create.new(
158
+ lookup?: Proc.new { |l| false },
159
+ join: Proc.new { |*args| 'reason string' },
160
+ empty?: Proc.new { true },
161
+ )
162
+ qlf = MockStruct.create(:reasons).new(reasons)
163
+ response = MockStruct.create(
164
+ :result, :succeeded?, :failure_qualifier
165
+ ).new(
166
+ result, false, qlf
167
+ )
168
+ request = MockStruct.create.new(
169
+ accept?: Proc.new{|a| a == 'application/xml'}
170
+ )
171
+
172
+
173
+ halt_args = nil
174
+ h.procs[:halt] = Proc.new { |*args| halt_args = *args }
175
+
176
+ assert_raise(HaltException) do
177
+ h.handle('env', 'params', request, 'response') { |ec| response }
178
+ end
179
+
180
+ # Check result
181
+ assert_not_nil halt_args
182
+ assert_equal(500, halt_args.shift)
183
+ assert_equal({ 'Content-Type' => 'text/plain'} , halt_args.shift)
184
+ assert_equal('Server Error', halt_args.shift)
185
+ end
186
+
187
+ def test_handles_nil_response
188
+
189
+ # Create Handler
190
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
191
+ cr = {}
192
+ h = MockHandler.new(cr, rr)
193
+
194
+ # Handle request
195
+ reasons = MockStruct.create.new(
196
+ lookup?: Proc.new { nil },
197
+ empty?: Proc.new { true }
198
+ )
199
+ qlf = MockStruct.create(:reasons).new(reasons)
200
+ response = MockStruct
201
+ .create(:result, :succeeded?, :failure_qualifier)
202
+ .new(nil, false, qlf)
203
+ halt_args = nil
204
+ h.procs[:halt] = Proc.new { |*args| halt_args = *args }
205
+
206
+ assert_raise(HaltException) do
207
+ h.handle('env', 'params', 'request', 'response') { |ec| response }
208
+ end
209
+
210
+ # Check result
211
+ assert_not_nil halt_args
212
+ assert_equal(404, halt_args.shift)
213
+ assert_equal({ 'Content-Type' => 'text/plain'} , halt_args.shift)
214
+ assert_equal('Capabilities not found', halt_args.shift)
215
+ end
216
+
217
+ def test_handles_RZ0011_error
218
+
219
+ # Create Handler
220
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
221
+ cr = {}
222
+ h = MockHandler.new(cr, rr)
223
+
224
+ # Handle request
225
+ resp = MockStruct.create.new
226
+ reason = MockStruct.create(:message, :details).new('message', 'details')
227
+ reasons = MockStruct.create.new(
228
+ lookup?: Proc.new { |l| 'RZ0011' == l ? reason : nil },
229
+ join: Proc.new { |*args| 'reason string' },
230
+ )
231
+ qlf = MockStruct.create(:reasons).new(reasons)
232
+ response = MockStruct.create(
233
+ :result,
234
+ :succeeded?,
235
+ :failure_qualifier
236
+ ).new(resp, false, qlf)
237
+
238
+ halt_args = nil
239
+ h.procs[:halt] = Proc.new { |*args| halt_args = *args }
240
+
241
+ assert_raise(HaltException) do
242
+ h.handle('env', 'params', 'request', 'response') { |ec| response }
243
+ end
244
+ # Check result
245
+
246
+ assert_not_nil halt_args
247
+ assert_equal(404, halt_args.shift)
248
+ assert_equal({ 'Content-Type' => 'text/plain'} , halt_args.shift)
249
+ assert_equal('message: details', halt_args.shift)
250
+ end
251
+
252
+ def test_handles_successful_response
253
+
254
+ # Create Handler
255
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
256
+ cr = {}
257
+ h = MockHandler.new(cr, rr)
258
+
259
+ # Handle request
260
+ result = MockStruct.create(:message).new("hello world!")
261
+ response = MockStruct.create(:result, :succeeded?).new(result, true)
262
+ request = MockStruct.create.new(
263
+ accept?: Proc.new{|a| a == 'application/xml'}
264
+ )
265
+ h.handle('env', 'params', request, 'response') { |ec| response }
266
+
267
+ # Check result
268
+ assert_equal 200, h.statuses.last
269
+ end
270
+
271
+ def test_handles_application_xml
272
+
273
+ # Create Handler
274
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
275
+ cr = {}
276
+ h = MockHandler.new(cr, rr)
277
+
278
+ # Handle request
279
+ result = MockStruct.create(:message).new("hello world!")
280
+ response = MockStruct.create(:result, :succeeded?).new(result, true)
281
+ request = MockStruct.create.new(
282
+ accept?: Proc.new{|a| a == 'application/xml'}
283
+ )
284
+ body = h.handle('env', 'params', request, 'response') { |ec| response }
285
+
286
+ # Check result
287
+ assert_equal 200, h.statuses.last
288
+ assert_equal ['application/xml'], h.content_types
289
+ assert_equal result.to_s, body
290
+ end
291
+
292
+ def test_handles_text_xml
293
+
294
+ # Create Handler
295
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
296
+ cr = {}
297
+ h = MockHandler.new(cr, rr)
298
+
299
+ # Handle request
300
+ result = MockStruct.create(:message).new("hello world!")
301
+ response = MockStruct.create(:result, :succeeded?).new(result, true)
302
+ request = MockStruct.create.new(
303
+ accept?: Proc.new { |a| a == 'text/xml' },
304
+ )
305
+ body = h.handle('env', 'params', request, 'response') { |ec| response }
306
+
307
+ # Check result
308
+ assert_equal 200, h.statuses.last
309
+ assert_equal ['text/xml'], h.content_types
310
+ assert_equal result.to_s, body
311
+ end
312
+
313
+ def test_handles_invalid_accept
314
+
315
+ # Create Handler
316
+ rr = ReturnsGivenResponse.new(nil, nil, given_response: '')
317
+ cr = {}
318
+ h = MockHandler.new(cr, rr)
319
+
320
+ # Handle request
321
+ result = MockStruct.create(:message).new("hello world!")
322
+ response = MockStruct.create(:result, :succeeded?).new(result, true)
323
+ request = MockStruct.create.new(accept?: Proc.new{ false })
324
+
325
+ halt_args = nil
326
+ h.procs[:halt] = Proc.new { |*args| halt_args = *args }
327
+
328
+ assert_raise(HaltException) do
329
+ h.handle('env', 'params', request, 'response') { |ec| response }
330
+ end
331
+
332
+ # Check result
333
+ assert_not_nil halt_args
334
+ assert_equal(500, halt_args.shift)
335
+ assert_equal({ 'Content-Type' => 'text/plain'} , halt_args.shift)
336
+ assert_equal('No supported Accept types', halt_args.shift)
337
+ end
338
+ end
339
+
340
+ # ############################## end of file ############################# #
341
+
342
+