enju_question 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +36 -0
  4. data/app/controllers/answers_controller.rb +140 -0
  5. data/app/controllers/questions_controller.rb +171 -0
  6. data/app/models/answer.rb +59 -0
  7. data/app/models/answer_has_item.rb +20 -0
  8. data/app/models/question.rb +87 -0
  9. data/app/models/question_sweeper.rb +17 -0
  10. data/app/views/answers/_form.html.erb +33 -0
  11. data/app/views/answers/edit.html.erb +13 -0
  12. data/app/views/answers/index.atom.builder +16 -0
  13. data/app/views/answers/index.html.erb +44 -0
  14. data/app/views/answers/index.rss.builder +31 -0
  15. data/app/views/answers/new.html.erb +14 -0
  16. data/app/views/answers/new.mobile.erb +35 -0
  17. data/app/views/answers/show.html.erb +54 -0
  18. data/app/views/questions/_crd.html.erb +24 -0
  19. data/app/views/questions/_index.html.erb +22 -0
  20. data/app/views/questions/_index_user.html.erb +18 -0
  21. data/app/views/questions/_list.html.erb +69 -0
  22. data/app/views/questions/_solved_facet.html.erb +21 -0
  23. data/app/views/questions/edit.html.erb +44 -0
  24. data/app/views/questions/index.atom.builder +16 -0
  25. data/app/views/questions/index.html.erb +5 -0
  26. data/app/views/questions/index.js.erb +2 -0
  27. data/app/views/questions/index.mobile.erb +21 -0
  28. data/app/views/questions/index.rss.builder +41 -0
  29. data/app/views/questions/index_crd.xml.builder +36 -0
  30. data/app/views/questions/new.html.erb +35 -0
  31. data/app/views/questions/show.html.erb +62 -0
  32. data/app/views/questions/show.mobile.erb +46 -0
  33. data/app/views/questions/show_crd.xml.builder +34 -0
  34. data/config/routes.rb +10 -0
  35. data/db/migrate/098_create_questions.rb +21 -0
  36. data/db/migrate/099_create_answers.rb +21 -0
  37. data/db/migrate/20100217054028_create_answer_has_items.rb +17 -0
  38. data/lib/enju_question/engine.rb +4 -0
  39. data/lib/enju_question/version.rb +3 -0
  40. data/lib/enju_question.rb +4 -0
  41. data/lib/tasks/enju_question_tasks.rake +4 -0
  42. data/spec/cassette_library/enju_ndl/crd.yml +440 -0
  43. data/spec/controllers/answers_controller_spec.rb +486 -0
  44. data/spec/controllers/questions_controller_spec.rb +559 -0
  45. data/spec/dummy/Rakefile +7 -0
  46. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  47. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  48. data/spec/dummy/app/controllers/application_controller.rb +106 -0
  49. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  50. data/spec/dummy/app/models/ability.rb +42 -0
  51. data/spec/dummy/app/models/item.rb +5 -0
  52. data/spec/dummy/app/models/role.rb +8 -0
  53. data/spec/dummy/app/models/user.rb +29 -0
  54. data/spec/dummy/app/models/user_group.rb +2 -0
  55. data/spec/dummy/app/models/user_has_role.rb +4 -0
  56. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  57. data/spec/dummy/app/views/page/403.html.erb +9 -0
  58. data/spec/dummy/app/views/page/403.mobile.erb +5 -0
  59. data/spec/dummy/app/views/page/403.xml.erb +4 -0
  60. data/spec/dummy/app/views/page/404.html.erb +9 -0
  61. data/spec/dummy/app/views/page/404.mobile.erb +5 -0
  62. data/spec/dummy/app/views/page/404.xml.erb +4 -0
  63. data/spec/dummy/config/application.rb +46 -0
  64. data/spec/dummy/config/boot.rb +10 -0
  65. data/spec/dummy/config/database.yml +25 -0
  66. data/spec/dummy/config/environment.rb +5 -0
  67. data/spec/dummy/config/environments/development.rb +30 -0
  68. data/spec/dummy/config/environments/production.rb +60 -0
  69. data/spec/dummy/config/environments/test.rb +39 -0
  70. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  71. data/spec/dummy/config/initializers/devise.rb +209 -0
  72. data/spec/dummy/config/initializers/inflections.rb +10 -0
  73. data/spec/dummy/config/initializers/mime_types.rb +6 -0
  74. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  75. data/spec/dummy/config/initializers/session_store.rb +8 -0
  76. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  77. data/spec/dummy/config/locales/en.yml +5 -0
  78. data/spec/dummy/config/routes.rb +60 -0
  79. data/spec/dummy/config.ru +4 -0
  80. data/spec/dummy/db/development.sqlite3 +0 -0
  81. data/spec/dummy/db/migrate/006_create_items.rb +36 -0
  82. data/spec/dummy/db/migrate/20100525171356_acts_as_taggable_on_migration.rb +30 -0
  83. data/spec/dummy/db/migrate/20111201121636_create_languages.rb +16 -0
  84. data/spec/dummy/db/migrate/20111201121844_create_roles.rb +12 -0
  85. data/spec/dummy/db/migrate/20111201155456_create_users.rb +13 -0
  86. data/spec/dummy/db/migrate/20111201155513_add_devise_to_users.rb +31 -0
  87. data/spec/dummy/db/migrate/20111201163342_create_user_groups.rb +12 -0
  88. data/spec/dummy/db/migrate/20111201163718_create_user_has_roles.rb +10 -0
  89. data/spec/dummy/db/schema.rb +168 -0
  90. data/spec/dummy/db/test.sqlite3 +0 -0
  91. data/spec/dummy/lib/expire_editable_fragment.rb +62 -0
  92. data/spec/dummy/log/development.log +2 -0
  93. data/spec/dummy/log/sunspot-solr-test.log +222 -0
  94. data/spec/dummy/log/test.log +77918 -0
  95. data/spec/dummy/public/404.html +26 -0
  96. data/spec/dummy/public/422.html +26 -0
  97. data/spec/dummy/public/500.html +26 -0
  98. data/spec/dummy/public/favicon.ico +0 -0
  99. data/spec/dummy/script/rails +6 -0
  100. data/spec/dummy/solr/conf/admin-extra.html +31 -0
  101. data/spec/dummy/solr/conf/elevate.xml +36 -0
  102. data/spec/dummy/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
  103. data/spec/dummy/solr/conf/protwords.txt +21 -0
  104. data/spec/dummy/solr/conf/schema.xml +238 -0
  105. data/spec/dummy/solr/conf/scripts.conf +24 -0
  106. data/spec/dummy/solr/conf/solrconfig.xml +934 -0
  107. data/spec/dummy/solr/conf/spellings.txt +2 -0
  108. data/spec/dummy/solr/conf/stopwords.txt +58 -0
  109. data/spec/dummy/solr/conf/synonyms.txt +31 -0
  110. data/spec/dummy/solr/data/test/index/segments.gen +0 -0
  111. data/spec/dummy/solr/data/test/index/segments_1 +0 -0
  112. data/spec/dummy/solr/data/test/spellchecker/segments.gen +0 -0
  113. data/spec/dummy/solr/data/test/spellchecker/segments_1 +0 -0
  114. data/spec/factories/answer.rb +7 -0
  115. data/spec/factories/question.rb +6 -0
  116. data/spec/factories/user.rb +34 -0
  117. data/spec/fixtures/answer_has_items.yml +26 -0
  118. data/spec/fixtures/answers.yml +61 -0
  119. data/spec/fixtures/questions.yml +61 -0
  120. data/spec/fixtures/roles.yml +21 -0
  121. data/spec/fixtures/user_groups.yml +25 -0
  122. data/spec/fixtures/user_has_roles.yml +41 -0
  123. data/spec/fixtures/users.yml +69 -0
  124. data/spec/models/answer_has_item_spec.rb +20 -0
  125. data/spec/models/answer_spec.rb +25 -0
  126. data/spec/spec_helper.rb +48 -0
  127. data/spec/support/controller_macros.rb +48 -0
  128. data/spec/support/devise.rb +4 -0
  129. data/spec/support/vcr.rb +6 -0
  130. metadata +306 -0
@@ -0,0 +1,486 @@
1
+ require 'spec_helper'
2
+
3
+ describe AnswersController do
4
+ fixtures :all
5
+
6
+ describe "GET index" do
7
+ describe "When logged in as Administrator" do
8
+ login_fixture_admin
9
+
10
+ it "assigns all answers as @answers" do
11
+ get :index
12
+ assigns(:answers).should_not be_empty
13
+ end
14
+ end
15
+
16
+ describe "When logged in as Librarian" do
17
+ login_fixture_librarian
18
+
19
+ it "assigns all answers as @answers" do
20
+ get :index
21
+ assigns(:answers).should_not be_empty
22
+ end
23
+ end
24
+
25
+ describe "When logged in as User" do
26
+ login_fixture_user
27
+
28
+ it "assigns nil as @answers" do
29
+ get :index
30
+ assigns(:answers).should be_nil
31
+ end
32
+
33
+ it "should not get index" do
34
+ get :index
35
+ response.should be_forbidden
36
+ assigns(:answers).should be_nil
37
+ end
38
+
39
+ it "should get to my index if user_id is specified" do
40
+ get :index, :user_id => users(:user1).username
41
+ response.should be_success
42
+ assigns(:answers).should eq users(:user1).answers.order('answers.id DESC').page(1)
43
+ end
44
+
45
+ it "should not get other user's index without user_id" do
46
+ get :index, :user_id => users(:user2).username
47
+ response.should be_forbidden
48
+ end
49
+
50
+ it "should get my index feed" do
51
+ get :index, :user_id => users(:user1).username, :format => 'rss'
52
+ response.should be_success
53
+ assigns(:answers).should_not be_empty
54
+ end
55
+
56
+ it "should not get other user's index if question is not shared" do
57
+ get :index, :user_id => users(:librarian1).username, :question_id => 2
58
+ response.should be_forbidden
59
+ end
60
+ end
61
+
62
+ describe "When not logged in" do
63
+ it "assigns nil as @answers" do
64
+ get :index
65
+ assigns(:answers).should be_nil
66
+ response.should redirect_to new_user_session_url
67
+ end
68
+
69
+ it "should not get index with other user's question_id" do
70
+ get :index, :question_id => 1
71
+ assigns(:answers).should eq assigns(:question).answers.order('answers.id DESC').page(1)
72
+ response.should be_success
73
+ end
74
+
75
+ it "should get other user's index if question is shared" do
76
+ get :index, :question_id => 5
77
+ response.should be_success
78
+ assigns(:answers).should eq assigns(:question).answers.order('answers.id DESC').page(1)
79
+ end
80
+
81
+ it "should not get other user's index feed if question is not shared" do
82
+ get :index, :question_id => 2, :format => 'rss'
83
+ response.should be_client_error
84
+ end
85
+
86
+ it "should get other user's index feed if question is shared" do
87
+ get :index, :question_id => 5, :format => 'rss'
88
+ response.should be_success
89
+ assigns(:answers).should eq assigns(:question).answers.order('answers.id DESC').page(1)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "GET show" do
95
+ describe "When logged in as Administrator" do
96
+ login_fixture_admin
97
+
98
+ it "assigns the requested answer as @answer" do
99
+ answer = FactoryGirl.create(:answer)
100
+ get :show, :id => answer.id
101
+ assigns(:answer).should eq(answer)
102
+ end
103
+ end
104
+
105
+ describe "When logged in as Librarian" do
106
+ login_fixture_user
107
+
108
+ it "assigns the requested answer as @answer" do
109
+ answer = FactoryGirl.create(:answer)
110
+ get :show, :id => answer.id
111
+ assigns(:answer).should eq(answer)
112
+ end
113
+ end
114
+
115
+ describe "When logged in as User" do
116
+ login_fixture_user
117
+
118
+ it "assigns the requested answer as @answer" do
119
+ answer = FactoryGirl.create(:answer)
120
+ get :show, :id => answer.id
121
+ assigns(:answer).should eq(answer)
122
+ end
123
+
124
+ it "should show answer without user_id" do
125
+ get :show, :id => 1, :question_id => 1
126
+ assigns(:answer).should eq(answers(:answer_00001))
127
+ assert_response :success
128
+ end
129
+
130
+ it "should show public answer without question_id" do
131
+ get :show, :id => 3, :user_id => users(:user1).username
132
+ assigns(:answer).should eq(Answer.find(3))
133
+ assert_response :success
134
+ end
135
+
136
+ it "should show my answer" do
137
+ get :show, :id => 3, :user_id => users(:user1).username
138
+ assigns(:answer).should eq(Answer.find(3))
139
+ assert_response :success
140
+ end
141
+
142
+ it "should not show private answer" do
143
+ get :show, :id => 4, :user_id => users(:user1).username
144
+ response.should be_forbidden
145
+ end
146
+
147
+ it "should not show missing answer" do
148
+ get :show, :id => 'missing', :user_id => users(:user1).username, :question_id => 1
149
+ response.should be_missing
150
+ end
151
+
152
+ it "should not show answer with other user's user_id" do
153
+ get :show, :id => 5, :user_id => users(:user2).username, :question_id => 2
154
+ response.should be_forbidden
155
+ end
156
+
157
+ it "should not show answer without other user's user_id" do
158
+ get :show, :id => 5, :question_id => 2
159
+ response.should be_forbidden
160
+ end
161
+ end
162
+
163
+ describe "When not logged in" do
164
+ it "assigns the requested answer as @answer" do
165
+ answer = FactoryGirl.create(:answer)
166
+ get :show, :id => answer.id
167
+ assigns(:answer).should eq(answer)
168
+ end
169
+
170
+ it "should show public_answer" do
171
+ get :show, :id => 1, :question_id => 1
172
+ assigns(:answer).should eq(Answer.find(1))
173
+ response.should be_success
174
+ end
175
+
176
+ it "should not show private answer" do
177
+ get :show, :id => 4, :question_id => 1
178
+ assigns(:answer).should eq(Answer.find(4))
179
+ response.should redirect_to new_user_session_url
180
+ end
181
+ end
182
+ end
183
+
184
+ describe "GET new" do
185
+ describe "When logged in as Administrator" do
186
+ login_fixture_admin
187
+
188
+ it "assigns the requested answer as @answer" do
189
+ get :new
190
+ assigns(:answer).should_not be_valid
191
+ end
192
+ end
193
+
194
+ describe "When logged in as Librarian" do
195
+ login_fixture_user
196
+
197
+ it "assigns the requested answer as @answer" do
198
+ get :new
199
+ assigns(:answer).should_not be_valid
200
+ end
201
+ end
202
+
203
+ describe "When logged in as User" do
204
+ login_fixture_user
205
+
206
+ it "should assign the requested answer as @answer" do
207
+ get :new
208
+ assigns(:answer).should_not be_valid
209
+ end
210
+
211
+ it "should get new template with question_id" do
212
+ get :new, :question_id => 1
213
+ assigns(:answer).should_not be_valid
214
+ response.should be_success
215
+ end
216
+ end
217
+
218
+ describe "When not logged in" do
219
+ it "should not assign the requested answer as @answer" do
220
+ get :new
221
+ assigns(:answer).should_not be_valid
222
+ response.should redirect_to(new_user_session_url)
223
+ end
224
+ end
225
+ end
226
+
227
+ describe "GET edit" do
228
+ describe "When logged in as Administrator" do
229
+ login_fixture_admin
230
+
231
+ it "assigns the requested answer as @answer" do
232
+ answer = FactoryGirl.create(:answer)
233
+ get :edit, :id => answer.id
234
+ assigns(:answer).should eq(answer)
235
+ end
236
+ end
237
+
238
+ describe "When logged in as Librarian" do
239
+ login_fixture_user
240
+
241
+ it "assigns the requested answer as @answer" do
242
+ answer = FactoryGirl.create(:answer)
243
+ get :edit, :id => answer.id
244
+ assigns(:answer).should eq(answer)
245
+ end
246
+ end
247
+
248
+ describe "When logged in as User" do
249
+ login_fixture_user
250
+
251
+ it "assigns the requested answer as @answer" do
252
+ answer = FactoryGirl.create(:answer)
253
+ get :edit, :id => answer.id
254
+ response.should be_forbidden
255
+ end
256
+
257
+ it "should edit my answer without user_id" do
258
+ get :edit, :id => 3, :question_id => 1
259
+ response.should be_success
260
+ end
261
+
262
+ it "should not edit other answer without user_id" do
263
+ get :edit, :id => 4, :question_id => 1
264
+ response.should be_forbidden
265
+ end
266
+
267
+ it "should edit answer without question_id" do
268
+ get :edit, :id => 3 , :user_id => users(:user1).username
269
+ response.should be_success
270
+ end
271
+
272
+ it "should not edit missing answer" do
273
+ get :edit, :id => 100, :user_id => users(:user1).username, :question_id => 1
274
+ response.should be_missing
275
+ end
276
+
277
+ it "should edit my answer" do
278
+ get :edit, :id => 3, :user_id => users(:user1).username, :question_id => 1
279
+ response.should be_success
280
+ end
281
+
282
+ it "should not edit other user's answer" do
283
+ get :edit, :id => 5, :user_id => users(:user2).username, :question_id => 2
284
+ response.should be_forbidden
285
+ end
286
+ end
287
+
288
+ describe "When not logged in" do
289
+ it "should not assign the requested answer as @answer" do
290
+ answer = FactoryGirl.create(:answer)
291
+ get :edit, :id => answer.id
292
+ response.should redirect_to(new_user_session_url)
293
+ end
294
+ end
295
+ end
296
+
297
+ describe "POST create" do
298
+ before(:each) do
299
+ @attrs = FactoryGirl.attributes_for(:answer)
300
+ @invalid_attrs = {:body => ''}
301
+ end
302
+
303
+ describe "When logged in as User" do
304
+ login_fixture_user
305
+
306
+ it "should create answer sithout user_id" do
307
+ post :create, :answer => {:question_id => 1, :body => 'hoge'}
308
+ response.should redirect_to answer_url(assigns(:answer))
309
+ end
310
+
311
+ it "should not create answer without question_id" do
312
+ post :create, :answer => {:body => 'hoge'}
313
+ assigns(:answer).should_not be_valid
314
+ response.should redirect_to questions_url
315
+ end
316
+
317
+ it "should create answer with question_id" do
318
+ post :create, :answer => {:question_id => 1, :body => 'hoge'}
319
+ assigns(:answer).should be_valid
320
+ response.should redirect_to answer_url(assigns(:answer))
321
+ end
322
+ end
323
+
324
+ describe "When not logged in" do
325
+ describe "with valid params" do
326
+ it "assigns a newly created answer as @answer" do
327
+ post :create, :answer => @attrs
328
+ assigns(:answer).should be_valid
329
+ end
330
+
331
+ it "redirects to the created answer" do
332
+ post :create, :answer => @attrs
333
+ response.should redirect_to new_user_session_url
334
+ end
335
+ end
336
+
337
+ describe "with invalid params" do
338
+ it "assigns a newly created but unsaved answer as @answer" do
339
+ post :create, :answer => @invalid_attrs
340
+ assigns(:answer).should_not be_valid
341
+ end
342
+
343
+ it "re-renders the 'new' template" do
344
+ post :create, :answer => @invalid_attrs
345
+ response.should redirect_to new_user_session_url
346
+ end
347
+ end
348
+ end
349
+ end
350
+
351
+ describe "PUT update" do
352
+ before(:each) do
353
+ @answer = answers(:answer_00001)
354
+ @attrs = {:body => 'test'}
355
+ @invalid_attrs = {:body => ''}
356
+ end
357
+
358
+ describe "When logged in as Administrator" do
359
+ login_fixture_admin
360
+
361
+ describe "with valid params" do
362
+ it "updates the requested answer" do
363
+ put :update, :id => answers(:answer_00003).id, :answer => @attrs
364
+ end
365
+
366
+ it "assigns the requested answer as @answer" do
367
+ put :update, :id => answers(:answer_00003).id, :answer => @attrs
368
+ assigns(:answer).should eq(Answer.find(3))
369
+ end
370
+ end
371
+
372
+ describe "with invalid params" do
373
+ it "assigns the requested answer as @answer" do
374
+ put :update, :id => answers(:answer_00003).id, :answer => @invalid_attrs
375
+ response.should render_template("edit")
376
+ end
377
+ end
378
+ end
379
+
380
+ describe "When logged in as Librarian" do
381
+ login_fixture_librarian
382
+
383
+ describe "with valid params" do
384
+ it "updates the requested answer" do
385
+ put :update, :id => answers(:answer_00003).id, :answer => @attrs
386
+ end
387
+
388
+ it "assigns the requested answer as @answer" do
389
+ put :update, :id => answers(:answer_00003).id, :answer => @attrs
390
+ assigns(:answer).should eq(Answer.find(3))
391
+ end
392
+ end
393
+
394
+ describe "with invalid params" do
395
+ it "assigns the requested answer as @answer" do
396
+ put :update, :id => answers(:answer_00003).id, :answer => @invalid_attrs
397
+ response.should render_template("edit")
398
+ end
399
+ end
400
+
401
+ it "should update other user's answer" do
402
+ put :update, :id => 3, :answer => { }, :user_id => users(:user1).username
403
+ response.should redirect_to answer_url(assigns(:answer))
404
+ end
405
+ end
406
+
407
+ describe "When logged in as User" do
408
+ login_fixture_user
409
+
410
+ describe "with valid params" do
411
+ it "updates the requested answer" do
412
+ put :update, :id => answers(:answer_00003).id, :answer => @attrs
413
+ end
414
+
415
+ it "assigns the requested answer as @answer" do
416
+ put :update, :id => answers(:answer_00003).id, :answer => @attrs
417
+ assigns(:answer).should eq(Answer.find(3))
418
+ end
419
+ end
420
+
421
+ describe "with invalid params" do
422
+ it "assigns the requested answer as @answer" do
423
+ put :update, :id => answers(:answer_00003).id, :answer => @invalid_attrs
424
+ response.should render_template("edit")
425
+ end
426
+ end
427
+
428
+ it "should update my answer" do
429
+ put :update, :id => answers(:answer_00003), :answer => { }, :user_id => users(:user1).username
430
+ response.should redirect_to answer_url(assigns(:answer))
431
+ end
432
+
433
+ it "should not update missing answer" do
434
+ put :update, :id => 'missing', :answer => { }, :user_id => users(:user1).username
435
+ response.should be_missing
436
+ end
437
+
438
+ it "should not update other user's answer" do
439
+ put :update, :id => 5, :answer => { }, :user_id => users(:user2).username
440
+ response.should be_forbidden
441
+ end
442
+
443
+ it "should update my answer with question_id" do
444
+ put :update, :id => 3, :answer => { }, :user_id => users(:user1).username, :question_id => 1
445
+ response.should redirect_to answer_url(assigns(:answer))
446
+ end
447
+ end
448
+
449
+ describe "When not logged in" do
450
+ describe "with valid params" do
451
+ it "updates the requested answer" do
452
+ put :update, :id => @answer.id, :answer => @attrs
453
+ end
454
+
455
+ it "assigns the requested answer as @answer" do
456
+ put :update, :id => @answer.id, :answer => @attrs
457
+ assigns(:answer).should eq(@answer)
458
+ response.should redirect_to new_user_session_url
459
+ end
460
+ end
461
+ end
462
+ end
463
+
464
+ describe "DELETE destroy" do
465
+ describe "When logged in as User" do
466
+ login_fixture_user
467
+
468
+ it "should destroy my answer" do
469
+ delete :destroy, :id => 3
470
+ response.should redirect_to question_answers_url(assigns(:answer).question)
471
+ end
472
+
473
+ it "should not destroy other user's answer" do
474
+ delete :destroy, :id => 5
475
+ response.should be_forbidden
476
+ end
477
+ end
478
+
479
+ describe "When not logged in" do
480
+ it "should be forbidden" do
481
+ delete :destroy, :id => 1
482
+ response.should redirect_to new_user_session_url
483
+ end
484
+ end
485
+ end
486
+ end