snusnu-merb_resource_controller 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +168 -18
- data/Rakefile +1 -1
- data/TODO +0 -1
- data/lib/merb_resource_controller.rb +2 -1
- data/lib/merb_resource_controller/action_descriptor.rb +107 -0
- data/lib/merb_resource_controller/actions.rb +159 -13
- data/lib/merb_resource_controller/resource_controller.rb +28 -44
- data/lib/merb_resource_controller/resource_proxy.rb +43 -12
- data/spec/mrc_test_app/app/controllers/articles.rb +1 -0
- data/spec/mrc_test_app/app/models/community/comment.rb +20 -0
- data/spec/mrc_test_app/app/models/community/rating.rb +19 -0
- data/spec/mrc_test_app/app/views/community/ratings/new.html.erb +11 -0
- data/spec/mrc_test_app/config/init.rb +0 -1
- data/spec/mrc_test_app/spec/lib/action_descriptor_spec.rb +137 -0
- data/spec/mrc_test_app/spec/lib/resource_proxy_spec.rb +7 -7
- data/spec/mrc_test_app/spec/request/article_comment_ratings_spec.rb +196 -0
- data/spec/mrc_test_app/spec/request/articles_spec.rb +266 -0
- metadata +19 -18
- data/spec/mrc_test_app/Rakefile +0 -52
- data/spec/mrc_test_app/app/controllers/editors.rb +0 -7
- data/spec/mrc_test_app/app/views/community/ratings/edit.html.erb +0 -11
@@ -0,0 +1,196 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "GET" do
|
4
|
+
|
5
|
+
describe "resource(@article, @comment, :ratings)", :given => "3 Ratings exist" do
|
6
|
+
|
7
|
+
it "should respond successfully" do
|
8
|
+
request(resource(Article.first, Community::Comment.first, :ratings)).should be_successful
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should render the :index template" do
|
12
|
+
@response = request(resource(Article.get(1), Community::Comment.get(1), :ratings))
|
13
|
+
@response.should have_selector("tr:eq(2) td:nth-child(1):contains('1')")
|
14
|
+
@response.should have_selector("tr:eq(2) td:nth-child(2):contains('comment body')")
|
15
|
+
@response.should have_selector("tr:eq(2) td:nth-child(3):contains('1')")
|
16
|
+
@response.should have_selector("tr:eq(3) td:nth-child(1):contains('2')")
|
17
|
+
@response.should have_selector("tr:eq(3) td:nth-child(2):contains('comment body')")
|
18
|
+
@response.should have_selector("tr:eq(3) td:nth-child(3):contains('1')")
|
19
|
+
@response.should_not have_selector("tr:eq(4)")
|
20
|
+
|
21
|
+
@response = request(resource(Article.get(1), Community::Comment.get(2), :ratings))
|
22
|
+
@response.should have_selector("tr:eq(2) td:nth-child(1):contains('3')")
|
23
|
+
@response.should have_selector("tr:eq(2) td:nth-child(2):contains('comment body')")
|
24
|
+
@response.should have_selector("tr:eq(2) td:nth-child(3):contains('1')")
|
25
|
+
@response.should_not have_selector("tr:eq(3)")
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "resource(@article, @comment, :ratings, :new)", :given => "a Comment exists" do
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
@response = request(resource(Article.first, Community::Comment.first, :ratings, :new))
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should respond successfully" do
|
37
|
+
@response.should be_successful
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should render the :new template" do
|
41
|
+
@response.should have_selector("h2:contains('New Rating')")
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "resource(@article, @comment, @rating)", :given => "a Rating exists" do
|
47
|
+
|
48
|
+
before(:each) do
|
49
|
+
@response = request(resource(Article.first, Community::Comment.first, Community::Rating.first))
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should respond successfully" do
|
53
|
+
@response.should be_successful
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should render the :show template" do
|
57
|
+
@response.should have_selector("h2:contains('Show Rating')")
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "resource(@article, @comment, @rating, :edit)", :given => "a Rating exists" do
|
63
|
+
|
64
|
+
before(:each) do
|
65
|
+
@response = request(resource(Article.first, Community::Comment.first, Community::Rating.first, :edit))
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should respond successfully" do
|
69
|
+
@response.should be_successful
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should render the :edit template" do
|
73
|
+
@response.should have_selector("h2:contains('Edit Rating')")
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "POST resource(@article, @comment, :ratings)" do
|
81
|
+
|
82
|
+
describe "Success", :given => "a Comment exists" do
|
83
|
+
|
84
|
+
before(:each) do
|
85
|
+
@response = request(resource(Article.first, Community::Comment.first, :ratings),
|
86
|
+
:method => "POST",
|
87
|
+
:params => { :rating => { :id => nil, :rate => 1 }}
|
88
|
+
)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should redirect to resource(@article, @comment, @rating)" do
|
92
|
+
@response.should redirect_to(
|
93
|
+
resource(Article.first, Community::Comment.first, Community::Rating.first),
|
94
|
+
:message => {:notice => "Rating was successfully created"}
|
95
|
+
)
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "Failure", :given => "a Comment exists" do
|
101
|
+
|
102
|
+
before(:each) do
|
103
|
+
@response = request(resource(Article.first, Community::Comment.first, :ratings),
|
104
|
+
:method => "POST",
|
105
|
+
:params => { :rating => { :id => nil, :rate => nil }}
|
106
|
+
)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should not be successful" do
|
110
|
+
@response.should_not be_successful
|
111
|
+
@response.status.should == 406
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should render the :new action" do
|
115
|
+
@response.should have_selector("h2:contains('New Rating')")
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "PUT resource(@article, @comment, @rating)", :given => "a Rating exists" do
|
123
|
+
|
124
|
+
describe "Success" do
|
125
|
+
|
126
|
+
before(:each) do
|
127
|
+
@article = Article.first
|
128
|
+
@comment = Community::Comment.first
|
129
|
+
@rating = Community::Rating.first
|
130
|
+
@response = request(resource(@article, @comment, @rating), :method => "PUT",
|
131
|
+
:params => { :rating => { :id => @rating.id, :rate => 5 } })
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should redirect to resource(@article, @comment, @rating)" do
|
135
|
+
@response.should redirect_to(resource(@article, @comment, @rating))
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "Failure" do
|
141
|
+
|
142
|
+
before(:each) do
|
143
|
+
@article = Article.first
|
144
|
+
@comment = Community::Comment.first
|
145
|
+
@rating = Community::Rating.first
|
146
|
+
@response = request(resource(@article, @comment, @rating), :method => "PUT",
|
147
|
+
:params => { :rating => { :id => @rating.id, :rate => nil } })
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should not be successful" do
|
151
|
+
@response.should_not be_successful
|
152
|
+
@response.status.should == 406
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should render the :edit template" do
|
156
|
+
@response.should have_selector("h2:contains('Edit Rating')")
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "DELETE resource(@article, @comment, @rating)" do
|
164
|
+
|
165
|
+
describe "Success", :given => "a Rating exists" do
|
166
|
+
|
167
|
+
before(:each) do
|
168
|
+
@response = request(
|
169
|
+
resource(Article.first, Community::Comment.first, Community::Rating.first),
|
170
|
+
:method => "DELETE"
|
171
|
+
)
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should redirect to resource(@article, :comments)" do
|
175
|
+
@response.should redirect_to(resource(Article.first, Community::Comment.first, :ratings))
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
|
180
|
+
describe "Failure" do
|
181
|
+
|
182
|
+
before(:each) do
|
183
|
+
@response = request(
|
184
|
+
"/articles/#{Article.first.id}/comments/#{Community::Comment.first.id}/ratings/1",
|
185
|
+
:method => "DELETE"
|
186
|
+
)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should not be successful" do
|
190
|
+
@response.should_not be_successful
|
191
|
+
@response.status.should == 404
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
@@ -134,6 +134,108 @@ describe "POST resource(:articles)" do
|
|
134
134
|
|
135
135
|
end
|
136
136
|
|
137
|
+
describe "XML POST resource(:articles)" do
|
138
|
+
|
139
|
+
describe "Success" do
|
140
|
+
|
141
|
+
before(:each) do
|
142
|
+
Article.all.destroy!
|
143
|
+
@response = request(resource(:articles),
|
144
|
+
:method => "POST",
|
145
|
+
:params => { :article => { :id => nil, :title => "Hey there", :body => "Me like snusnu" }},
|
146
|
+
"HTTP_ACCEPT" => "application/xml"
|
147
|
+
)
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should return a 201 (Created) status" do
|
151
|
+
@response.status.should == 201
|
152
|
+
@response.body.length.should_not == 0
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "Failure" do
|
158
|
+
|
159
|
+
before(:each) do
|
160
|
+
Article.all.destroy!
|
161
|
+
@response = request(
|
162
|
+
resource(:articles),
|
163
|
+
:method => "POST",
|
164
|
+
:params => {
|
165
|
+
:article => {
|
166
|
+
:id => nil,
|
167
|
+
:title => nil,
|
168
|
+
:body => "article body"
|
169
|
+
}
|
170
|
+
},
|
171
|
+
"HTTP_ACCEPT" => "application/xml"
|
172
|
+
)
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should not be successful" do
|
176
|
+
@response.should_not be_successful
|
177
|
+
@response.status.should == 422
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should return the serialized errors that occured" do
|
181
|
+
@response.body.size.should >= 0
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
describe "XML POST resource(:articles)" do
|
189
|
+
|
190
|
+
describe "Success" do
|
191
|
+
|
192
|
+
before(:each) do
|
193
|
+
Article.all.destroy!
|
194
|
+
@response = request(resource(:articles),
|
195
|
+
:method => "POST",
|
196
|
+
:params => { :article => { :id => nil, :title => "Hey there", :body => "Me like snusnu" }},
|
197
|
+
"HTTP_ACCEPT" => "application/json"
|
198
|
+
)
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should return a 201 (Created) status" do
|
202
|
+
@response.status.should == 201
|
203
|
+
@response.body.length.should_not == 0
|
204
|
+
end
|
205
|
+
|
206
|
+
end
|
207
|
+
|
208
|
+
describe "Failure" do
|
209
|
+
|
210
|
+
before(:each) do
|
211
|
+
Article.all.destroy!
|
212
|
+
@response = request(
|
213
|
+
resource(:articles),
|
214
|
+
:method => "POST",
|
215
|
+
:params => {
|
216
|
+
:article => {
|
217
|
+
:id => nil,
|
218
|
+
:title => nil,
|
219
|
+
:body => "article body"
|
220
|
+
}
|
221
|
+
},
|
222
|
+
"HTTP_ACCEPT" => "application/json"
|
223
|
+
)
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should not be successful" do
|
227
|
+
@response.should_not be_successful
|
228
|
+
@response.status.should == 422
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should return the serialized errors that occured" do
|
232
|
+
@response.body.size.should >= 0
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
end
|
238
|
+
|
137
239
|
describe "PUT resource(@article)", :given => "an Article exists" do
|
138
240
|
|
139
241
|
describe "Success" do
|
@@ -177,6 +279,96 @@ describe "PUT resource(@article)", :given => "an Article exists" do
|
|
177
279
|
|
178
280
|
end
|
179
281
|
|
282
|
+
describe "XML PUT resource(@article)", :given => "an Article exists" do
|
283
|
+
|
284
|
+
describe "Success" do
|
285
|
+
|
286
|
+
before(:each) do
|
287
|
+
@article = Article.first
|
288
|
+
@response = request(resource(@article),
|
289
|
+
:method => "PUT",
|
290
|
+
:params => { :article => { :id => @article.id } },
|
291
|
+
"HTTP_ACCEPT" => "application/xml")
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should return an empty response with a 200 (OK) status" do
|
295
|
+
@response.status.should == 200
|
296
|
+
@response.body.length.should == 0
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
describe "Failure" do
|
303
|
+
|
304
|
+
before(:each) do
|
305
|
+
@article = Article.first
|
306
|
+
@response = request(
|
307
|
+
resource(@article),
|
308
|
+
:method => "PUT",
|
309
|
+
:params => { :article => { :id => @article.id, :title => nil, :body => "updated body" } },
|
310
|
+
"HTTP_ACCEPT" => "application/xml"
|
311
|
+
)
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should not be successful" do
|
315
|
+
@response.should_not be_successful
|
316
|
+
@response.status.should == 422
|
317
|
+
end
|
318
|
+
|
319
|
+
it "should return the serialized errors that occured" do
|
320
|
+
@response.body.size.should >= 0
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
end
|
326
|
+
|
327
|
+
describe "JSON PUT resource(@article)", :given => "an Article exists" do
|
328
|
+
|
329
|
+
describe "Success" do
|
330
|
+
|
331
|
+
before(:each) do
|
332
|
+
@article = Article.first
|
333
|
+
@response = request(resource(@article),
|
334
|
+
:method => "PUT",
|
335
|
+
:params => { :article => { :id => @article.id } },
|
336
|
+
"HTTP_ACCEPT" => "application/json")
|
337
|
+
end
|
338
|
+
|
339
|
+
it "should return an empty response with a 200 (OK) status" do
|
340
|
+
@response.status.should == 200
|
341
|
+
@response.body.length.should == 0
|
342
|
+
end
|
343
|
+
|
344
|
+
end
|
345
|
+
|
346
|
+
|
347
|
+
describe "Failure" do
|
348
|
+
|
349
|
+
before(:each) do
|
350
|
+
@article = Article.first
|
351
|
+
@response = request(
|
352
|
+
resource(@article),
|
353
|
+
:method => "PUT",
|
354
|
+
:params => { :article => { :id => @article.id, :title => nil, :body => "updated body" } },
|
355
|
+
"HTTP_ACCEPT" => "application/json"
|
356
|
+
)
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should not be successful" do
|
360
|
+
@response.should_not be_successful
|
361
|
+
@response.status.should == 422
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should render the serialized errors that occured" do
|
365
|
+
@response.body.size.should >= 0
|
366
|
+
end
|
367
|
+
|
368
|
+
end
|
369
|
+
|
370
|
+
end
|
371
|
+
|
180
372
|
describe "DELETE resource(@article)" do
|
181
373
|
|
182
374
|
describe "Success", :given => "an Article exists" do
|
@@ -206,3 +398,77 @@ describe "DELETE resource(@article)" do
|
|
206
398
|
end
|
207
399
|
|
208
400
|
end
|
401
|
+
|
402
|
+
describe "XML DELETE resource(@article)" do
|
403
|
+
|
404
|
+
describe "Success", :given => "an Article exists" do
|
405
|
+
|
406
|
+
before(:each) do
|
407
|
+
@response = request(resource(Article.first),
|
408
|
+
:method => "DELETE",
|
409
|
+
"HTTP_ACCEPT" => "application/xml"
|
410
|
+
)
|
411
|
+
end
|
412
|
+
|
413
|
+
it "should return an empty response with a 200 (OK) status" do
|
414
|
+
@response.status.should == 200
|
415
|
+
@response.body.length.should == 0
|
416
|
+
end
|
417
|
+
|
418
|
+
end
|
419
|
+
|
420
|
+
describe "Failure" do
|
421
|
+
|
422
|
+
before(:each) do
|
423
|
+
Article.all.destroy!
|
424
|
+
@response = request('/articles/1',
|
425
|
+
:method => "DELETE",
|
426
|
+
"HTTP_ACCEPT" => "application/xml"
|
427
|
+
)
|
428
|
+
end
|
429
|
+
|
430
|
+
it "should not be successful" do
|
431
|
+
@response.should_not be_successful
|
432
|
+
@response.status.should == 404
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|
436
|
+
|
437
|
+
end
|
438
|
+
|
439
|
+
describe "JSON DELETE resource(@article)" do
|
440
|
+
|
441
|
+
describe "Success", :given => "an Article exists" do
|
442
|
+
|
443
|
+
before(:each) do
|
444
|
+
@response = request(resource(Article.first),
|
445
|
+
:method => "DELETE",
|
446
|
+
"HTTP_ACCEPT" => "application/json"
|
447
|
+
)
|
448
|
+
end
|
449
|
+
|
450
|
+
it "should return an empty response with a 200 (OK) status" do
|
451
|
+
@response.status.should == 200
|
452
|
+
@response.body.length.should == 0
|
453
|
+
end
|
454
|
+
|
455
|
+
end
|
456
|
+
|
457
|
+
describe "Failure" do
|
458
|
+
|
459
|
+
before(:each) do
|
460
|
+
Article.all.destroy!
|
461
|
+
@response = request('/articles/1',
|
462
|
+
:method => "DELETE",
|
463
|
+
"HTTP_ACCEPT" => "application/json"
|
464
|
+
)
|
465
|
+
end
|
466
|
+
|
467
|
+
it "should not be successful" do
|
468
|
+
@response.should_not be_successful
|
469
|
+
@response.status.should == 404
|
470
|
+
end
|
471
|
+
|
472
|
+
end
|
473
|
+
|
474
|
+
end
|