moonrope 1.4.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +9 -0
  3. data/Gemfile.lock +47 -0
  4. data/MIT-LICENCE +20 -0
  5. data/README.md +24 -0
  6. data/bin/moonrope +28 -0
  7. data/docs/authentication.md +114 -0
  8. data/docs/controllers.md +106 -0
  9. data/docs/exceptions.md +27 -0
  10. data/docs/introduction.md +29 -0
  11. data/docs/structures.md +214 -0
  12. data/example/authentication.rb +50 -0
  13. data/example/controllers/meta_controller.rb +14 -0
  14. data/example/controllers/users_controller.rb +92 -0
  15. data/example/structures/pet_structure.rb +12 -0
  16. data/example/structures/user_structure.rb +35 -0
  17. data/html/assets/lock.svg +3 -0
  18. data/html/assets/reset.css +101 -0
  19. data/html/assets/style.css +348 -0
  20. data/html/assets/tool.svg +4 -0
  21. data/html/assets/try.js +151 -0
  22. data/html/authenticators/default.html +191 -0
  23. data/html/controllers/meta/version.html +144 -0
  24. data/html/controllers/meta.html +73 -0
  25. data/html/controllers/users/create.html +341 -0
  26. data/html/controllers/users/list.html +348 -0
  27. data/html/controllers/users/show.html +261 -0
  28. data/html/controllers/users/update.html +387 -0
  29. data/html/controllers/users.html +93 -0
  30. data/html/index.html +166 -0
  31. data/html/moonrope.txt +0 -0
  32. data/html/structures/pet.html +176 -0
  33. data/html/structures/user.html +338 -0
  34. data/lib/moonrope/action.rb +165 -37
  35. data/lib/moonrope/authenticator.rb +39 -0
  36. data/lib/moonrope/base.rb +24 -6
  37. data/lib/moonrope/controller.rb +4 -2
  38. data/lib/moonrope/doc_context.rb +94 -0
  39. data/lib/moonrope/doc_server.rb +123 -0
  40. data/lib/moonrope/dsl/action_dsl.rb +159 -9
  41. data/lib/moonrope/dsl/authenticator_dsl.rb +31 -0
  42. data/lib/moonrope/dsl/base_dsl.rb +21 -18
  43. data/lib/moonrope/dsl/controller_dsl.rb +60 -9
  44. data/lib/moonrope/dsl/filterable_dsl.rb +27 -0
  45. data/lib/moonrope/dsl/structure_dsl.rb +27 -2
  46. data/lib/moonrope/errors.rb +3 -0
  47. data/lib/moonrope/eval_environment.rb +82 -3
  48. data/lib/moonrope/eval_helpers/filter_helper.rb +82 -0
  49. data/lib/moonrope/eval_helpers.rb +28 -5
  50. data/lib/moonrope/guard.rb +35 -0
  51. data/lib/moonrope/html_generator.rb +65 -0
  52. data/lib/moonrope/param_set.rb +11 -1
  53. data/lib/moonrope/rack_middleware.rb +1 -1
  54. data/lib/moonrope/railtie.rb +31 -14
  55. data/lib/moonrope/request.rb +25 -14
  56. data/lib/moonrope/structure.rb +74 -11
  57. data/lib/moonrope/structure_attribute.rb +15 -0
  58. data/lib/moonrope/version.rb +1 -1
  59. data/lib/moonrope.rb +5 -4
  60. data/moonrope.gemspec +21 -0
  61. data/spec/spec_helper.rb +32 -0
  62. data/spec/specs/action_spec.rb +455 -0
  63. data/spec/specs/base_spec.rb +29 -0
  64. data/spec/specs/controller_spec.rb +31 -0
  65. data/spec/specs/param_set_spec.rb +31 -0
  66. data/templates/basic/_action_form.erb +77 -0
  67. data/templates/basic/_errors_table.erb +32 -0
  68. data/templates/basic/_structure_attributes_list.erb +55 -0
  69. data/templates/basic/action.erb +168 -0
  70. data/templates/basic/assets/lock.svg +3 -0
  71. data/templates/basic/assets/reset.css +101 -0
  72. data/templates/basic/assets/style.css +348 -0
  73. data/templates/basic/assets/tool.svg +4 -0
  74. data/templates/basic/assets/try.js +151 -0
  75. data/templates/basic/authenticator.erb +51 -0
  76. data/templates/basic/controller.erb +20 -0
  77. data/templates/basic/index.erb +114 -0
  78. data/templates/basic/layout.erb +46 -0
  79. data/templates/basic/structure.erb +23 -0
  80. data/test/test_helper.rb +81 -0
  81. data/test/tests/action_access_test.rb +63 -0
  82. data/test/tests/actions_test.rb +524 -0
  83. data/test/tests/authenticators_test.rb +87 -0
  84. data/test/tests/base_test.rb +35 -0
  85. data/test/tests/controllers_test.rb +49 -0
  86. data/test/tests/eval_environment_test.rb +136 -0
  87. data/test/tests/evel_helpers_test.rb +60 -0
  88. data/test/tests/examples_test.rb +11 -0
  89. data/test/tests/helpers_test.rb +97 -0
  90. data/test/tests/param_set_test.rb +44 -0
  91. data/test/tests/rack_middleware_test.rb +109 -0
  92. data/test/tests/request_test.rb +232 -0
  93. data/test/tests/structures_param_extensions_test.rb +159 -0
  94. data/test/tests/structures_test.rb +335 -0
  95. metadata +82 -48
@@ -0,0 +1,524 @@
1
+ class ActionsTest < Test::Unit::TestCase
2
+
3
+ def setup
4
+ @base = Moonrope::Base.new
5
+ @controller = Moonrope::Controller.new(@base, :users)
6
+ end
7
+
8
+ def test_basic_definition
9
+ action = Moonrope::Action.new(@controller, :list) do
10
+ description "An example action with a description"
11
+ end
12
+ assert action.is_a?(Moonrope::Action)
13
+ assert_equal :list, action.name
14
+ assert action.description.is_a?(String)
15
+ assert action.description.length > 0
16
+ end
17
+
18
+ def test_defining_params
19
+ action = Moonrope::Action.new(@controller, :list) do
20
+ param :page
21
+ param :limit
22
+ end
23
+ assert action.params.is_a?(Hash)
24
+ assert_equal [:page, :limit], action.params.keys
25
+ assert action.params.values.all? { |p| p.is_a?(Hash) }
26
+ end
27
+
28
+ def test_using_shares
29
+ controller = Moonrope::Controller.new(@base, :users) do
30
+ shared_action :user_properties do
31
+ error 'InvalidUsername', "Some description"
32
+ param :username, "Blah"
33
+ param :first_name
34
+ end
35
+
36
+ action :create do
37
+ use :user_properties
38
+ param :last_name
39
+ end
40
+ end
41
+
42
+ action = controller / :create
43
+ assert_equal(Hash, action.params.class)
44
+ assert_equal(Hash, action.params[:username].class)
45
+ assert_equal("Blah", action.params[:username][:description])
46
+ assert_equal(Hash, action.errors['InvalidUsername'].class)
47
+ end
48
+
49
+ def test_action
50
+ action = Moonrope::Action.new(@controller, :list) do
51
+ action { true }
52
+ end
53
+ assert action.actions.is_a?(Array)
54
+ assert action.actions.first.is_a?(Proc)
55
+ assert_equal true, action.actions.first.call
56
+ end
57
+
58
+ def test_calling_actions
59
+ action = Moonrope::Action.new(@controller, :list) do
60
+ action { [1,2,3,4] }
61
+ end
62
+ assert result = action.execute
63
+ assert result.is_a?(Moonrope::ActionResult)
64
+ assert_equal 'success', result.status
65
+ assert_equal [1,2,3,4], result.data
66
+ assert_equal Float, result.time.class
67
+ assert_equal({}, result.flags)
68
+ assert_equal({}, result.headers)
69
+ end
70
+
71
+ def test_structure_method_can_be_called
72
+ # Create a new structure to test with
73
+ user_structure = Moonrope::Structure.new(@base, :user) do
74
+ basic { {:id => o.id, :username => o.username}}
75
+ end
76
+
77
+ # Create an action which uses this structure
78
+ action = Moonrope::Action.new(@controller, :list) do
79
+ action do
80
+ user = User.new(:id => 1, :username => 'adamcooke')
81
+ structure user_structure, user
82
+ end
83
+ end
84
+
85
+ # Test the structure was returned
86
+ assert result = action.execute
87
+ assert result.is_a?(Moonrope::ActionResult), "result is not a ActionResult"
88
+ assert_equal 1, result.data[:id]
89
+ assert_equal 'adamcooke', result.data[:username]
90
+ end
91
+
92
+ def test_structure_methods_can_be_called_with_opts_from_dsl
93
+ # Create a new structure to test with
94
+ user_structure = Moonrope::Structure.new(@base, :user) do
95
+ basic :id
96
+ full :username
97
+ end
98
+
99
+ # Create an action which uses this structure
100
+ action = Moonrope::Action.new(@controller, :list) do
101
+ returns :hash, :structure => :user, :structure_opts => {:full => true}
102
+ action do
103
+ user = User.new(:id => 1, :username => 'adamcooke')
104
+ structure user_structure, user, :return => true
105
+ end
106
+ end
107
+
108
+ # Test the structure was returned
109
+ assert result = action.execute
110
+ assert_equal 1, result.data[:id]
111
+ assert_equal 'adamcooke', result.data[:username]
112
+ end
113
+
114
+ def test_default_params
115
+ action = Moonrope::Action.new(@controller, :default_params_test) do
116
+ param :page, :default => 1234
117
+ param :limit
118
+ action { {:page => params.page, :limit => params.limit} }
119
+ end
120
+ result = action.execute
121
+ assert_equal({'page' => 1234}, action.default_params)
122
+ assert_equal 1234, result.data[:page]
123
+ assert_equal nil, result.data[:limit]
124
+ end
125
+
126
+ def test_before_filters_are_executed
127
+ controller = Moonrope::Controller.new(@base, :users) do
128
+ before { set_flag :before_all, true }
129
+ before(:other) { set_flag :before_other, true }
130
+ before(:list) { set_flag :before_list, true }
131
+ before(:list, :potato) { set_flag :before_list_and_potato, true }
132
+ end
133
+
134
+ action = Moonrope::Action.new(controller, :list) do
135
+ action { true }
136
+ end
137
+
138
+ assert result = action.execute
139
+ assert_equal true, result.flags[:before_all]
140
+ assert_equal true, result.flags[:before_list]
141
+ assert_equal true, result.flags[:before_list_and_potato]
142
+ assert_equal nil, result.flags[:before_other]
143
+ end
144
+
145
+ def test_result_can_be_expressed_as_a_hash
146
+ action = Moonrope::Action.new(@controller, :list) do
147
+ action { [1,2,3] }
148
+ end
149
+ assert result = action.execute
150
+ assert hash = result.to_hash
151
+ assert hash.is_a?(Hash), "result.to_hash does not return a hash"
152
+ assert_equal 'success', hash[:status]
153
+ assert hash[:time].is_a?(Float)
154
+ assert hash[:flags].is_a?(Hash)
155
+ end
156
+
157
+ def test_result_can_be_expressed_as_json
158
+ action = Moonrope::Action.new(@controller, :list) do
159
+ action { [1,2,3] }
160
+ end
161
+ assert result = action.execute
162
+ assert json = result.to_json
163
+ assert json.is_a?(String)
164
+ end
165
+
166
+ def test_that_param_validation_happens_on_executin
167
+ action = Moonrope::Action.new(@controller, :list) do
168
+ param :page, "Page number", :required => true
169
+ action { [1,2,3] }
170
+ end
171
+ assert result = action.execute
172
+ assert_equal 'parameter-error', result.status
173
+ end
174
+
175
+ def test_actions_params_can_be_validated_for_presence
176
+ action = Moonrope::Action.new(@controller, :list) do
177
+ param :page, "Page number", :required => true
178
+ end
179
+
180
+ # request without the param
181
+ assert_raises Moonrope::Errors::ParameterError do
182
+ action.validate_parameters(Moonrope::ParamSet.new)
183
+ end
184
+
185
+ # request with the param
186
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('page' => 1))
187
+ end
188
+
189
+ def test_actions_params_can_be_validated_for_type
190
+ action = Moonrope::Action.new(@controller, :list) do
191
+ param :page, "Page number", :type => Integer
192
+ end
193
+
194
+ # request with a string valuee
195
+ assert_raises Moonrope::Errors::ParameterError do
196
+ action.validate_parameters(Moonrope::ParamSet.new('page' => 'stringy'))
197
+ end
198
+
199
+ # request with an integer value
200
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('page' => 1))
201
+
202
+ # request with an nil value
203
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('page' => nil))
204
+ end
205
+
206
+ def test_actions_params_can_be_validated_for_boolean_types
207
+ action = Moonrope::Action.new(@controller, :list) do
208
+ param :hungry, "Are you hungry", :type => :boolean
209
+ end
210
+
211
+ # request with a string valuee
212
+ assert_raises Moonrope::Errors::ParameterError do
213
+ action.validate_parameters(Moonrope::ParamSet.new('hungry' => 'randomstring'))
214
+ end
215
+
216
+ assert_raises Moonrope::Errors::ParameterError do
217
+ action.validate_parameters(Moonrope::ParamSet.new('hungry' => 2))
218
+ end
219
+
220
+ assert_raises Moonrope::Errors::ParameterError do
221
+ action.validate_parameters(Moonrope::ParamSet.new('hungry' => 123))
222
+ end
223
+
224
+ # request with an boolean value
225
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('hungry' => true))
226
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('hungry' => false))
227
+
228
+ # request with string values
229
+ set = Moonrope::ParamSet.new('hungry' => 'true')
230
+ assert_equal true, action.validate_parameters(set)
231
+ assert_equal true, set.hungry
232
+
233
+ set = Moonrope::ParamSet.new('hungry' => 'false')
234
+ assert_equal true, action.validate_parameters(set)
235
+ assert_equal false, set.hungry
236
+
237
+ # request with an numeric values
238
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('hungry' => 1))
239
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('hungry' => 0))
240
+
241
+ # request with nil vlaues
242
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('hungry' => nil))
243
+ end
244
+
245
+ def test_actions_params_can_have_symbols_as_types_which_do_nothing
246
+ action = Moonrope::Action.new(@controller, :list) do
247
+ param :created_at, "Timestamp", :type => :timestamp
248
+ end
249
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('created_at' => 'something'))
250
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('created_at' => nil))
251
+ end
252
+
253
+ def test_actions_params_can_be_validated_for_regex_matches
254
+ action = Moonrope::Action.new(@controller, :list) do
255
+ param :username, "Username", :regex => /\A[a-z]+\z/
256
+ end
257
+ # request with a nil value
258
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new)
259
+
260
+ # request with a matching value
261
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('username' => 'adam'))
262
+
263
+ # request with a string valuee
264
+ assert_raises Moonrope::Errors::ParameterError do
265
+ action.validate_parameters(Moonrope::ParamSet.new('username' => 'invalid-username1234'))
266
+ end
267
+ end
268
+
269
+ def test_actions_params_can_be_validated_for_option_matches
270
+ action = Moonrope::Action.new(@controller, :list) do
271
+ param :sort_by, :options => ["name", "age"]
272
+ end
273
+ # request with a nil value
274
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new)
275
+
276
+ # request with a matching value
277
+ assert_equal true, action.validate_parameters(Moonrope::ParamSet.new('sort_by' => 'name'))
278
+
279
+ # request with a string valuee
280
+ assert_raises Moonrope::Errors::ParameterError do
281
+ action.validate_parameters(Moonrope::ParamSet.new('sort_by' => 'somethingelse'))
282
+ end
283
+ end
284
+
285
+
286
+ def test_actions_can_raise_errors
287
+ action = Moonrope::Action.new(@controller, :list) do
288
+ action do
289
+ error :not_found, "Something wasn't found"
290
+ end
291
+ end
292
+ assert result = action.execute
293
+ assert_equal "not-found", result.status
294
+ assert_equal({:message => "Something wasn't found"}, result.data)
295
+ end
296
+
297
+ def test_actions_can_raise_structured_errors
298
+ action = Moonrope::Action.new(@controller, :list) do
299
+ action do
300
+ structured_error 'feature-disabled', "The feature you have requested is not currently available for this resource.", :number => 1000
301
+ end
302
+ end
303
+ assert result = action.execute
304
+ assert_equal "error", result.status
305
+ assert_equal("feature-disabled", result.data[:code])
306
+ assert_equal("The feature you have requested is not currently available for this resource.", result.data[:message])
307
+ assert_equal(1000, result.data[:number])
308
+ end
309
+
310
+ def test_actions_can_raise_structured_errors_through_the_error_method
311
+ action = Moonrope::Action.new(@controller, :list) do
312
+ action do
313
+ error :structured_error, 'feature-disabled', "The feature you have requested is not currently available for this resource."
314
+ end
315
+ end
316
+ assert result = action.execute
317
+ assert_equal "error", result.status
318
+ assert_equal("feature-disabled", result.data[:code])
319
+ assert_equal("The feature you have requested is not currently available for this resource.", result.data[:message])
320
+ end
321
+
322
+ def test_actions_can_raise_structured_errors_through_the_error_method_using_a_string
323
+ action = Moonrope::Action.new(@controller, :list) do
324
+ action do
325
+ error 'feature-disabled', "The feature you have requested is not currently available for this resource."
326
+ end
327
+ end
328
+ assert result = action.execute
329
+ assert_equal "error", result.status
330
+ assert_equal("feature-disabled", result.data[:code])
331
+ assert_equal("The feature you have requested is not currently available for this resource.", result.data[:message])
332
+ end
333
+
334
+ def test_actions_can_raise_structured_errors_referencing_action_errors
335
+ action = Moonrope::Action.new(@controller, :list) do
336
+ error "NoWidgetsFound", "No widgets were found with level {widget_level}"
337
+ action do
338
+ error 'NoWidgetsFound', :widget_level => 42
339
+ end
340
+ end
341
+ assert result = action.execute
342
+ assert_equal "error", result.status
343
+ assert_equal "NoWidgetsFound", result.data[:code]
344
+ assert_equal "No widgets were found with level 42", result.data[:message]
345
+ assert_equal 42, result.data[:widget_level]
346
+ end
347
+
348
+ class DummyError < StandardError; end
349
+
350
+ def test_catching_external_errors
351
+ @controller.base.register_external_error DummyError do |exp, res|
352
+ res.status = 'dummy-error'
353
+ res.data = {:message => exp.message}
354
+ end
355
+
356
+ action = Moonrope::Action.new(@controller, :list) do
357
+ action do
358
+ raise DummyError, "Something happened"
359
+ end
360
+ end
361
+
362
+ assert result = action.execute
363
+ assert_equal 'dummy-error', result.status
364
+ assert_equal({:message => 'Something happened'}, result.data)
365
+ end
366
+
367
+ class DummyError2 < StandardError; end
368
+
369
+ def test_non_defined_errors_are_raised
370
+ action = Moonrope::Action.new(@controller, :list) do
371
+ action do
372
+ raise DummyError2, "Something happened"
373
+ end
374
+ end
375
+ assert_raises(DummyError2) { action.execute }
376
+ end
377
+
378
+ def test_can_change_full_attribute
379
+ # can't change when no ops
380
+ action = Moonrope::Action.new(@controller, :list) do
381
+ returns :hash, :structure => :animal
382
+ end
383
+ assert_equal(false, action.can_change_full?)
384
+
385
+ # can change when paramable is true
386
+ action = Moonrope::Action.new(@controller, :list) do
387
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => true}
388
+ end
389
+ assert_equal(true, action.can_change_full?)
390
+
391
+ # can change when specified
392
+ action = Moonrope::Action.new(@controller, :list) do
393
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {:full => true}}
394
+ end
395
+ assert_equal(true, action.can_change_full?)
396
+
397
+ # can't change when not specified
398
+ action = Moonrope::Action.new(@controller, :list) do
399
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {}}
400
+ end
401
+ assert_equal(false, action.can_change_full?)
402
+ end
403
+
404
+ def test_includes_full_attributes
405
+ #not included by default
406
+ action = Moonrope::Action.new(@controller, :list) do
407
+ returns :hash, :structure => :animal
408
+ end
409
+ assert_equal(false, action.includes_full_attributes?)
410
+
411
+ # not included when paramable is just true
412
+ action = Moonrope::Action.new(@controller, :list) do
413
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => true}
414
+ end
415
+ assert_equal(false, action.includes_full_attributes?)
416
+
417
+ # included when paramable sets the default to true
418
+ action = Moonrope::Action.new(@controller, :list) do
419
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {:full => true}}
420
+ end
421
+ assert_equal(true, action.includes_full_attributes?)
422
+
423
+ # included when it's full anyway
424
+ action = Moonrope::Action.new(@controller, :list) do
425
+ returns :hash, :structure => :animal, :structure_opts => {:full => true}
426
+ end
427
+ assert_equal(true, action.includes_full_attributes?)
428
+ end
429
+
430
+
431
+ def test_can_change_expansions_attribute
432
+ # can't change when no ops
433
+ action = Moonrope::Action.new(@controller, :list) do
434
+ returns :hash, :structure => :animal
435
+ end
436
+ assert_equal(false, action.can_change_expansions?)
437
+
438
+ # can change when paramable is true
439
+ action = Moonrope::Action.new(@controller, :list) do
440
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => true}
441
+ end
442
+ assert_equal(true, action.can_change_expansions?)
443
+
444
+ # can change when specified
445
+ action = Moonrope::Action.new(@controller, :list) do
446
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {:expansions => true}}
447
+ end
448
+ assert_equal(true, action.can_change_expansions?)
449
+
450
+ # can't change when not specified
451
+ action = Moonrope::Action.new(@controller, :list) do
452
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {}}
453
+ end
454
+ assert_equal(false, action.can_change_expansions?)
455
+ end
456
+
457
+ def test_includes_expansion
458
+ #not included by default
459
+ action = Moonrope::Action.new(@controller, :list) do
460
+ returns :hash, :structure => :animal
461
+ end
462
+ assert_equal(false, action.includes_expansion?(:blah))
463
+
464
+ # not included when paramable is just true
465
+ action = Moonrope::Action.new(@controller, :list) do
466
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => true}
467
+ end
468
+ assert_equal(false, action.includes_expansion?(:blah))
469
+
470
+ # included when paramable sets the default to true
471
+ action = Moonrope::Action.new(@controller, :list) do
472
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {:expansions => true}}
473
+ end
474
+ assert_equal(true, action.includes_expansion?(:blah))
475
+
476
+ # included when it's expansions anyway
477
+ action = Moonrope::Action.new(@controller, :list) do
478
+ returns :hash, :structure => :animal, :structure_opts => {:expansions => true}
479
+ end
480
+ assert_equal(true, action.includes_expansion?(:blah))
481
+
482
+ # included when expansions is an array
483
+ action = Moonrope::Action.new(@controller, :list) do
484
+ returns :hash, :structure => :animal, :structure_opts => {:expansions => [:blah]}
485
+ end
486
+ assert_equal(true, action.includes_expansion?(:blah))
487
+ assert_equal(false, action.includes_expansion?(:another))
488
+
489
+ # included when paramable expansions is an array
490
+ action = Moonrope::Action.new(@controller, :list) do
491
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {:expansions => [:blah]}}
492
+ end
493
+ assert_equal(true, action.includes_expansion?(:blah))
494
+ assert_equal(false, action.includes_expansion?(:another))
495
+ end
496
+
497
+ def test_available_expansions_array_on_actions
498
+ # if an array is provided, it should return the items in the array
499
+ action = Moonrope::Action.new(@controller, :list) do
500
+ returns :hash, :structure => :animal, :structure_opts => {:paramable => {:expansions => [:user]}}
501
+ end
502
+ assert_equal([:user], action.available_expansions)
503
+ end
504
+
505
+ def test_that_param_can_copy_data_from_structures
506
+ base = Moonrope::Base.new do
507
+ structure :user do
508
+ basic :username, "The username for the user", :type => String, :eg => 123
509
+ end
510
+
511
+ controller :users do
512
+ action :save do
513
+ param :username, :from_structure => :user
514
+ end
515
+ end
516
+ end
517
+
518
+ action = base/:users/:save
519
+ assert_equal "The username for the user", action.params[:username][:description]
520
+ assert_equal String, action.params[:username][:type]
521
+ end
522
+
523
+
524
+ end
@@ -0,0 +1,87 @@
1
+ class AuthenticatorsTest < Test::Unit::TestCase
2
+
3
+ def test_that_by_default_actions_dont_have_an_authenticator
4
+ base = Moonrope::Base.new do
5
+ controller :users do
6
+ action :list
7
+ end
8
+ end
9
+ assert_equal :none, (base/:users/:list).authenticator_to_use
10
+ end
11
+
12
+ def test_actions_will_use_default_authenticator
13
+ base = Moonrope::Base.new do
14
+ authenticator :default do
15
+ lookup { true }
16
+ end
17
+ controller :users do
18
+ action :list
19
+ end
20
+ end
21
+ assert_equal :default, (base/:users/:list).authenticator_to_use.name
22
+ end
23
+
24
+ def test_actions_will_use_controller_authenticator
25
+ base = Moonrope::Base.new do
26
+ authenticator :default
27
+ authenticator :controller_specific
28
+ authenticator :action_specific
29
+ controller :animals do
30
+ action :list
31
+ end
32
+ controller :users do
33
+ authenticator :controller_specific
34
+ action :list
35
+ action :show do
36
+ authenticator :action_specific
37
+ end
38
+ end
39
+ end
40
+ assert_equal :default, (base/:animals/:list).authenticator_to_use.name
41
+ assert_equal :controller_specific, (base/:users/:list).authenticator_to_use.name
42
+ assert_equal :action_specific, (base/:users/:show).authenticator_to_use.name
43
+ end
44
+
45
+ def test_that_missing_authenticators_return_not_found
46
+ base = Moonrope::Base.new do
47
+ authenticator :default
48
+ controller :animals do
49
+ authenticator :missing
50
+ action :list
51
+ end
52
+ end
53
+ assert_equal :not_found, (base/:animals/:list).authenticator_to_use
54
+ end
55
+
56
+ def test_authentication_can_be_defined_via_access_rule
57
+ base = Moonrope::Base.new do
58
+ authenticator :some_authenticator
59
+ controller :users do
60
+ action :list do
61
+ access_rule :some_authenticator => :some_rule
62
+ end
63
+ end
64
+ end
65
+ assert_equal :some_authenticator, (base/:users/:list).authenticator
66
+ assert_equal :some_rule, (base/:users/:list).access_rule
67
+ end
68
+
69
+ def test_that_access_rules_can_inherit
70
+ base = Moonrope::Base.new do
71
+ controller :animals do
72
+ action :list
73
+ end
74
+ controller :users do
75
+ access_rule :controller_specific
76
+ action :list
77
+ action :show do
78
+ access_rule :action_specific
79
+ end
80
+ end
81
+ end
82
+ assert_equal :default, (base/:animals/:list).access_rule_to_use
83
+ assert_equal :controller_specific, (base/:users/:list).access_rule_to_use
84
+ assert_equal :action_specific, (base/:users/:show).access_rule_to_use
85
+ end
86
+
87
+ end
@@ -0,0 +1,35 @@
1
+ class ControllersTest < Test::Unit::TestCase
2
+
3
+ def test_basic_definitions
4
+ base = Moonrope::Base.new do
5
+ authenticator :default do
6
+ lookup { :person }
7
+ end
8
+ controller :users do
9
+ action :list
10
+ end
11
+ controller :animals
12
+ structure :user
13
+ structure :animal
14
+ end
15
+
16
+ # Check they are added to the base
17
+ assert_equal 2, base.controllers.size
18
+ assert_equal true, base.controllers.all? { |s| s.is_a?(Moonrope::Controller)}
19
+ assert_equal 2, base.structures.size
20
+ assert_equal true, base.structures.all? { |s| s.is_a?(Moonrope::Structure)}
21
+ assert_equal 1, base.authenticators.size
22
+ assert_equal true, base.authenticators.all? { |_,v| v.is_a?(Moonrope::Authenticator) }
23
+
24
+ # Check they can be accessed
25
+ assert_equal :users, base.controller(:users).name
26
+ assert_equal :animals, base.controller(:animals).name
27
+ assert_equal :user, base.structure(:user).name
28
+ assert_equal :animal, base.structure(:animal).name
29
+
30
+ # Check controllers & actions can be found nicely
31
+ assert_equal :list, (base / :users / :list).name
32
+ assert_equal :user, (base[:user]).name
33
+ end
34
+
35
+ end
@@ -0,0 +1,49 @@
1
+ class ControllersTest < Test::Unit::TestCase
2
+
3
+ def setup
4
+ @base = Moonrope::Base.new
5
+ end
6
+
7
+ def test_controllers_actions_can_be_found_easily
8
+ controller = Moonrope::Controller.new(@base, :users) do
9
+ action :list do
10
+ action { true }
11
+ end
12
+ end
13
+ action = controller / :list
14
+ assert_equal :list, action.name
15
+ assert action.is_a?(Moonrope::Action)
16
+ end
17
+
18
+ def test_controllers_can_have_before_filters
19
+ controller = Moonrope::Controller.new(@base, :users) do
20
+ before { 1 }
21
+ before(:list) { 3 }
22
+ action :list do
23
+ action { true }
24
+ end
25
+
26
+ action :show do
27
+ action { true }
28
+ end
29
+ end
30
+ assert_equal 2, controller.befores.size
31
+ assert_equal 2, controller.before_actions_for(:list).size
32
+ assert_equal 1, controller.before_actions_for(:show).size
33
+ end
34
+
35
+ def test_definining_shared_params
36
+ controller = Moonrope::Controller.new(@base, :users) do
37
+ shared_action :crud do
38
+ param :username
39
+ param :first_name
40
+ error 'SomeError'
41
+ end
42
+ end
43
+
44
+ assert_equal(Hash, controller.shared_actions.class)
45
+ assert_equal(Proc, controller.shared_actions[:crud].class)
46
+
47
+ end
48
+
49
+ end