typus 1.0.0.pre8 → 1.0.0.pre9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (247) hide show
  1. data/CHANGELOG +4 -6
  2. data/Gemfile +16 -13
  3. data/Gemfile.lock +26 -2
  4. data/Rakefile +2 -3
  5. data/app/controllers/admin/base_controller.rb +3 -5
  6. data/app/controllers/admin/resources_controller.rb +24 -26
  7. data/app/helpers/admin/base_helper.rb +14 -22
  8. data/app/helpers/admin/dashboard_helper.rb +2 -5
  9. data/app/helpers/admin/filters_helper.rb +6 -8
  10. data/app/helpers/admin/form_helper.rb +3 -9
  11. data/app/helpers/admin/preview_helper.rb +4 -6
  12. data/app/helpers/admin/relationships_helper.rb +9 -11
  13. data/app/helpers/admin/resources_helper.rb +6 -24
  14. data/app/helpers/admin/search_helper.rb +1 -1
  15. data/app/helpers/admin/sidebar_helper.rb +12 -15
  16. data/app/helpers/admin/table_helper.rb +13 -13
  17. data/app/views/admin/dashboard/show.html.erb +1 -1
  18. data/app/views/admin/helpers/_apps.html.erb +4 -3
  19. data/app/views/admin/helpers/_header.html.erb +2 -2
  20. data/app/views/admin/helpers/dashboard/_applications.html.erb +3 -3
  21. data/app/views/admin/helpers/dashboard/_resources.html.erb +17 -13
  22. data/app/views/admin/helpers/filters/_filters.html.erb +9 -27
  23. data/app/views/admin/helpers/resources/_pagination.html.erb +8 -9
  24. data/app/views/admin/helpers/search/_search.html.erb +1 -1
  25. data/app/views/admin/mailer/reset_password_link.text.erb +3 -0
  26. data/app/views/admin/resources/_form.html.erb +4 -4
  27. data/app/views/admin/resources/edit.html.erb +2 -3
  28. data/app/views/admin/resources/index.html.erb +15 -13
  29. data/app/views/admin/resources/new.html.erb +2 -2
  30. data/app/views/admin/resources/show.html.erb +1 -2
  31. data/app/views/admin/templates/_boolean.html.erb +1 -1
  32. data/app/views/admin/templates/_has_many.html.erb +2 -4
  33. data/app/views/admin/templates/_has_n.html.erb +1 -1
  34. data/app/views/admin/templates/_selector.html.erb +0 -1
  35. data/app/views/admin/templates/_string.html.erb +1 -1
  36. data/app/views/layouts/admin/session.html.erb +1 -1
  37. data/config/available_locales/ca.yml +3 -5
  38. data/config/available_locales/de.yml +2 -4
  39. data/config/available_locales/es.yml +2 -4
  40. data/config/available_locales/fr.yml +40 -39
  41. data/config/available_locales/fr_models.yml +2 -2
  42. data/config/available_locales/hu.yml +3 -5
  43. data/config/available_locales/it.yml +0 -4
  44. data/config/available_locales/language.yml.template +2 -4
  45. data/config/available_locales/pt-BR.yml +2 -4
  46. data/config/available_locales/ru.yml +2 -4
  47. data/config/routes.rb +1 -0
  48. data/lib/generators/templates/config/initializers/typus.rb +5 -5
  49. data/lib/generators/templates/config/initializers/typus_authentication.rb +5 -5
  50. data/lib/generators/templates/config/initializers/typus_resources.rb +7 -7
  51. data/lib/generators/templates/config/typus/README +11 -13
  52. data/lib/generators/templates/controller.rb +0 -2
  53. data/lib/generators/templates/functional_test.rb +4 -15
  54. data/lib/generators/typus/migration_generator.rb +0 -1
  55. data/lib/support/active_record.rb +18 -9
  56. data/lib/support/fake_user.rb +42 -0
  57. data/lib/support/object.rb +1 -0
  58. data/lib/support/string.rb +0 -32
  59. data/lib/tasks/typus.rake +0 -8
  60. data/lib/typus.rb +34 -18
  61. data/lib/typus/authentication/base.rb +26 -0
  62. data/lib/typus/authentication/http_basic.rb +22 -0
  63. data/lib/typus/authentication/none.rb +19 -0
  64. data/lib/typus/authentication/session.rb +190 -0
  65. data/lib/typus/format.rb +12 -19
  66. data/lib/typus/orm/active_record.rb +13 -51
  67. data/lib/typus/resources.rb +0 -9
  68. data/lib/typus/version.rb +1 -1
  69. data/test/factories.rb +43 -0
  70. data/test/fixtures/rails_app/app/controllers/application_controller.rb +0 -1
  71. data/test/fixtures/rails_app/app/models/page.rb +0 -4
  72. data/test/fixtures/rails_app/config/application.rb +6 -3
  73. data/test/fixtures/rails_app/config/database.yml +12 -20
  74. data/test/fixtures/rails_app/config/typus/application.yml +4 -8
  75. data/test/fixtures/rails_app/db/test.sqlite3 +0 -0
  76. data/test/fixtures/rails_app/log/test.log +227115 -0
  77. data/test/fixtures/rails_app/tmp/export-posts-20101012213057.csv +2 -0
  78. data/test/fixtures/rails_app/tmp/export-posts-20101012213132.csv +2 -0
  79. data/test/fixtures/rails_app/tmp/export-posts-20101012213234.csv +2 -0
  80. data/test/fixtures/rails_app/tmp/export-posts-20101012213506.csv +2 -0
  81. data/test/fixtures/rails_app/tmp/export-posts-20101012215057.csv +2 -0
  82. data/test/fixtures/rails_app/tmp/export-posts-20101012215217.csv +2 -0
  83. data/test/fixtures/rails_app/tmp/export-posts-20101012215415.csv +2 -0
  84. data/test/fixtures/rails_app/tmp/export-posts-20101012215443.csv +2 -0
  85. data/test/fixtures/rails_app/tmp/export-posts-20101012215509.csv +2 -0
  86. data/test/fixtures/rails_app/tmp/export-posts-20101012215528.csv +2 -0
  87. data/test/fixtures/rails_app/tmp/export-posts-20101012215551.csv +2 -0
  88. data/test/fixtures/rails_app/tmp/export-posts-20101012215615.csv +2 -0
  89. data/test/fixtures/rails_app/tmp/export-posts-20101012215640.csv +2 -0
  90. data/test/fixtures/rails_app/tmp/export-posts-20101012215714.csv +2 -0
  91. data/test/fixtures/rails_app/tmp/export-posts-20101012215744.csv +2 -0
  92. data/test/fixtures/rails_app/tmp/export-posts-20101012215814.csv +2 -0
  93. data/test/fixtures/rails_app/tmp/export-posts-20101012215836.csv +2 -0
  94. data/test/fixtures/rails_app/tmp/export-posts-20101012215906.csv +2 -0
  95. data/test/fixtures/rails_app/tmp/export-posts-20101012220659.csv +2 -0
  96. data/test/fixtures/rails_app/tmp/export-posts-20101012221043.csv +2 -0
  97. data/test/fixtures/rails_app/tmp/export-posts-20101012221258.csv +2 -0
  98. data/test/fixtures/rails_app/tmp/export-posts-20101012222558.csv +2 -0
  99. data/test/fixtures/rails_app/tmp/export-posts-20101012222752.csv +2 -0
  100. data/test/fixtures/rails_app/tmp/export-posts-20101012222848.csv +2 -0
  101. data/test/fixtures/rails_app/tmp/export-posts-20101012223433.csv +2 -0
  102. data/test/fixtures/rails_app/tmp/export-posts-20101012223535.csv +2 -0
  103. data/test/fixtures/rails_app/tmp/export-posts-20101012224003.csv +2 -0
  104. data/test/fixtures/rails_app/tmp/export-posts-20101012224052.csv +2 -0
  105. data/test/fixtures/rails_app/tmp/export-posts-20101012224109.csv +2 -0
  106. data/test/fixtures/rails_app/tmp/export-posts-20101012224139.csv +2 -0
  107. data/test/fixtures/rails_app/tmp/export-posts-20101012224205.csv +2 -0
  108. data/test/fixtures/rails_app/tmp/export-posts-20101012224243.csv +2 -0
  109. data/test/fixtures/rails_app/tmp/export-posts-20101012225119.csv +2 -0
  110. data/test/fixtures/rails_app/tmp/export-posts-20101013063215.csv +2 -0
  111. data/test/fixtures/rails_app/tmp/export-posts-20101013063300.csv +2 -0
  112. data/test/fixtures/rails_app/tmp/export-posts-20101013063352.csv +2 -0
  113. data/test/fixtures/rails_app/tmp/export-posts-20101013063427.csv +2 -0
  114. data/test/fixtures/rails_app/tmp/export-posts-20101013063448.csv +2 -0
  115. data/test/fixtures/rails_app/tmp/export-posts-20101013063530.csv +2 -0
  116. data/test/fixtures/rails_app/tmp/export-posts-20101013063559.csv +2 -0
  117. data/test/fixtures/rails_app/tmp/export-posts-20101013063646.csv +2 -0
  118. data/test/fixtures/rails_app/tmp/export-posts-20101013063737.csv +2 -0
  119. data/test/fixtures/rails_app/tmp/export-posts-20101013063822.csv +2 -0
  120. data/test/fixtures/rails_app/tmp/export-posts-20101013063837.csv +2 -0
  121. data/test/fixtures/rails_app/tmp/export-posts-20101013063919.csv +2 -0
  122. data/test/fixtures/rails_app/tmp/export-posts-20101013063939.csv +2 -0
  123. data/test/fixtures/rails_app/tmp/export-posts-20101013063952.csv +2 -0
  124. data/test/fixtures/rails_app/tmp/export-posts-20101013064220.csv +2 -0
  125. data/test/fixtures/rails_app/tmp/export-posts-20101013064253.csv +2 -0
  126. data/test/fixtures/rails_app/tmp/export-posts-20101013064318.csv +2 -0
  127. data/test/fixtures/rails_app/tmp/export-posts-20101013064404.csv +2 -0
  128. data/test/fixtures/rails_app/tmp/export-posts-20101013064452.csv +2 -0
  129. data/test/fixtures/rails_app/tmp/export-posts-20101013064536.csv +2 -0
  130. data/test/fixtures/rails_app/tmp/export-posts-20101013064652.csv +2 -0
  131. data/test/fixtures/rails_app/tmp/export-posts-20101013064845.csv +2 -0
  132. data/test/fixtures/rails_app/tmp/export-posts-20101013065023.csv +2 -0
  133. data/test/fixtures/rails_app/tmp/export-posts-20101013065137.csv +2 -0
  134. data/test/fixtures/rails_app/tmp/export-posts-20101013065713.csv +2 -0
  135. data/test/fixtures/rails_app/tmp/export-posts-20101013065954.csv +2 -0
  136. data/test/fixtures/rails_app/tmp/export-posts-20101013070057.csv +2 -0
  137. data/test/fixtures/rails_app/tmp/export-posts-20101013070116.csv +2 -0
  138. data/test/fixtures/rails_app/tmp/export-posts-20101013070156.csv +2 -0
  139. data/test/fixtures/rails_app/tmp/export-posts-20101013070349.csv +2 -0
  140. data/test/fixtures/rails_app/tmp/export-posts-20101013070429.csv +2 -0
  141. data/test/fixtures/rails_app/tmp/export-posts-20101013070448.csv +2 -0
  142. data/test/fixtures/rails_app/tmp/export-posts-20101013070514.csv +2 -0
  143. data/test/fixtures/rails_app/tmp/export-posts-20101013070709.csv +2 -0
  144. data/test/fixtures/rails_app/tmp/export-posts-20101013070745.csv +2 -0
  145. data/test/fixtures/rails_app/tmp/export-posts-20101013070809.csv +2 -0
  146. data/test/fixtures/rails_app/tmp/export-posts-20101013070905.csv +2 -0
  147. data/test/fixtures/rails_app/tmp/export-posts-20101013070955.csv +2 -0
  148. data/test/fixtures/rails_app/tmp/export-posts-20101013071116.csv +2 -0
  149. data/test/fixtures/rails_app/tmp/export-posts-20101013071209.csv +2 -0
  150. data/test/fixtures/rails_app/tmp/export-posts-20101013071335.csv +2 -0
  151. data/test/fixtures/rails_app/tmp/export-posts-20101013071632.csv +2 -0
  152. data/test/fixtures/rails_app/tmp/export-posts-20101013071643.csv +2 -0
  153. data/test/fixtures/rails_app/tmp/export-posts-20101013071735.csv +2 -0
  154. data/test/fixtures/rails_app/tmp/export-posts-20101013071749.csv +2 -0
  155. data/test/fixtures/rails_app/tmp/export-posts-20101013072030.csv +2 -0
  156. data/test/fixtures/rails_app/tmp/export-posts-20101013072050.csv +2 -0
  157. data/test/fixtures/rails_app/tmp/export-posts-20101013072108.csv +2 -0
  158. data/test/fixtures/rails_app/tmp/export-posts-20101013072713.csv +2 -0
  159. data/test/fixtures/rails_app/tmp/export-posts-20101013072751.csv +2 -0
  160. data/test/fixtures/rails_app/tmp/export-posts-20101013072912.csv +2 -0
  161. data/test/fixtures/rails_app/tmp/export-posts-20101013074546.csv +2 -0
  162. data/test/fixtures/rails_app/tmp/export-posts-20101018180512.csv +2 -0
  163. data/test/fixtures/rails_app/tmp/export-posts-20101018183359.csv +2 -0
  164. data/test/fixtures/rails_app/tmp/export-posts-20101018183608.csv +2 -0
  165. data/test/fixtures/rails_app/tmp/export-posts-20101018183759.csv +2 -0
  166. data/test/functional/admin/account_controller_test.rb +1 -1
  167. data/test/functional/admin/{resources_controller_assets_relationships.rb → assets_controller_test.rb} +12 -16
  168. data/test/functional/admin/categories_controller_test.rb +62 -0
  169. data/test/functional/admin/{resources_controller_comments_toggle_test.rb → comments_controller_test.rb} +0 -0
  170. data/test/functional/admin/dashboard_controller_test.rb +34 -2
  171. data/test/functional/admin/posts_controller_test.rb +548 -0
  172. data/test/functional/admin/session_controller_test.rb +2 -2
  173. data/test/functional/admin/{resources_controller_tableless_resource_test.rb → status_controller_test.rb} +0 -0
  174. data/test/functional/admin/{resources_controller_typus_users_test.rb → typus_users_controller_test.rb} +2 -2
  175. data/test/helpers/admin/base_helper_test.rb +35 -37
  176. data/test/helpers/admin/dashboard_helper_test.rb +4 -8
  177. data/test/helpers/admin/form_helper_test.rb +9 -34
  178. data/test/helpers/admin/resources_helper_test.rb +28 -16
  179. data/test/helpers/admin/sidebar_helper_test.rb +14 -27
  180. data/test/helpers/admin/table_helper_test.rb +24 -68
  181. data/test/integration/login_test.rb +13 -0
  182. data/test/integration_test_helper.rb +21 -0
  183. data/test/lib/support/active_record_test.rb +72 -21
  184. data/test/lib/support/array_test.rb +2 -6
  185. data/test/lib/support/string_test.rb +21 -30
  186. data/test/lib/typus/active_record_test.rb +162 -100
  187. data/test/lib/typus/resource_test.rb +2 -3
  188. data/test/lib/typus_test.rb +25 -22
  189. data/test/test_helper.rb +4 -17
  190. data/test/unit/admin/mailer_test.rb +2 -2
  191. data/test/unit/typus_user_roles_test.rb +23 -23
  192. data/test/unit/typus_user_test.rb +30 -24
  193. data/typus.gemspec +0 -1
  194. metadata +111 -60
  195. data/app/models/admin/fake_user.rb +0 -46
  196. data/lib/generators/templates/model.yml +0 -10
  197. data/lib/typus/authentication.rb +0 -241
  198. data/lib/vendor/paginator.rb +0 -143
  199. data/test/factories/assets.rb +0 -5
  200. data/test/factories/categories.rb +0 -3
  201. data/test/factories/comments.rb +0 -6
  202. data/test/factories/pages.rb +0 -5
  203. data/test/factories/pictures.rb +0 -7
  204. data/test/factories/posts.rb +0 -6
  205. data/test/factories/typus_users.rb +0 -8
  206. data/test/fixtures/rails_app/README +0 -244
  207. data/test/fixtures/rails_app/app/helpers/application_helper.rb +0 -2
  208. data/test/fixtures/rails_app/app/views/layouts/application.html.erb +0 -14
  209. data/test/fixtures/rails_app/config/environments/development.rb +0 -19
  210. data/test/fixtures/rails_app/config/environments/production.rb +0 -42
  211. data/test/fixtures/rails_app/config/initializers/acts_as_list.rb +0 -258
  212. data/test/fixtures/rails_app/config/initializers/acts_as_tree.rb +0 -98
  213. data/test/fixtures/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  214. data/test/fixtures/rails_app/config/initializers/inflections.rb +0 -10
  215. data/test/fixtures/rails_app/config/initializers/mime_types.rb +0 -5
  216. data/test/fixtures/rails_app/config/locales/en.yml +0 -5
  217. data/test/fixtures/rails_app/db/seeds.rb +0 -7
  218. data/test/fixtures/rails_app/public/404.html +0 -26
  219. data/test/fixtures/rails_app/public/422.html +0 -26
  220. data/test/fixtures/rails_app/public/500.html +0 -26
  221. data/test/fixtures/rails_app/public/favicon.ico +0 -0
  222. data/test/fixtures/rails_app/public/images/rails.png +0 -0
  223. data/test/fixtures/rails_app/public/index.html +0 -279
  224. data/test/fixtures/rails_app/public/javascripts/application.js +0 -2
  225. data/test/fixtures/rails_app/public/javascripts/controls.js +0 -965
  226. data/test/fixtures/rails_app/public/javascripts/dragdrop.js +0 -974
  227. data/test/fixtures/rails_app/public/javascripts/effects.js +0 -1123
  228. data/test/fixtures/rails_app/public/javascripts/prototype.js +0 -4874
  229. data/test/fixtures/rails_app/public/javascripts/rails.js +0 -118
  230. data/test/fixtures/rails_app/public/robots.txt +0 -5
  231. data/test/fixtures/rails_app/script/rails +0 -9
  232. data/test/functional/admin/dashboard_controller_http_basic_test.rb +0 -24
  233. data/test/functional/admin/dashboard_controller_none_test.rb +0 -18
  234. data/test/functional/admin/resources_controller_categories_before_test.rb +0 -10
  235. data/test/functional/admin/resources_controller_categories_lists_test.rb +0 -44
  236. data/test/functional/admin/resources_controller_categories_views_test.rb +0 -10
  237. data/test/functional/admin/resources_controller_posts_before_test.rb +0 -10
  238. data/test/functional/admin/resources_controller_posts_crud_custom_test.rb +0 -27
  239. data/test/functional/admin/resources_controller_posts_crud_test.rb +0 -44
  240. data/test/functional/admin/resources_controller_posts_formats_test.rb +0 -39
  241. data/test/functional/admin/resources_controller_posts_forms_test.rb +0 -58
  242. data/test/functional/admin/resources_controller_posts_permissions_test.rb +0 -100
  243. data/test/functional/admin/resources_controller_posts_relationships_test.rb +0 -91
  244. data/test/functional/admin/resources_controller_posts_roles.rb +0 -52
  245. data/test/functional/admin/resources_controller_posts_views_test.rb +0 -143
  246. data/test/lib/vendor/paginator_test.rb +0 -141
  247. data/typus-1.0.0.pre7.gem +0 -0
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class Admin::CategoriesControllerTest < ActionController::TestCase
4
+
5
+ setup do
6
+ @request.session[:typus_user_id] = Factory(:typus_user).id
7
+ @request.env['HTTP_REFERER'] = '/admin/categories'
8
+ end
9
+
10
+ context "Categories Views" do
11
+
12
+ should "verify form partial can overwrited by model" do
13
+ get :new
14
+ assert_match "categories#_form.html.erb", @response.body
15
+ end
16
+
17
+ end
18
+
19
+ context "Categories List" do
20
+
21
+ setup do
22
+ @first_category = Factory(:category, :position => 1)
23
+ @second_category = Factory(:category, :position => 2)
24
+ end
25
+
26
+ should "verify referer" do
27
+ get :position, { :id => @first_category.id, :go => 'move_lower' }
28
+ assert_response :redirect
29
+ assert_redirected_to @request.env['HTTP_REFERER']
30
+ end
31
+
32
+ should "position item one step down" do
33
+ get :position, { :id => @first_category.id, :go => 'move_lower' }
34
+
35
+ assert_equal "Record moved lower.", flash[:notice]
36
+ assert_equal 2, @first_category.reload.position
37
+ assert_equal 1, @second_category.reload.position
38
+ end
39
+
40
+ should "position item one step up" do
41
+ get :position, { :id => @second_category.id, :go => 'move_higher' }
42
+
43
+ assert_equal "Record moved higher.", flash[:notice]
44
+ assert_equal 2, @first_category.reload.position
45
+ assert_equal 1, @second_category.reload.position
46
+ end
47
+
48
+ should "position top item to bottom" do
49
+ get :position, { :id => @first_category.id, :go => 'move_to_bottom' }
50
+ assert_equal "Record moved to bottom.", flash[:notice]
51
+ assert_equal 2, @first_category.reload.position
52
+ end
53
+
54
+ should "position bottom item to top" do
55
+ get :position, { :id => @second_category.id, :go => 'move_to_top' }
56
+ assert_equal "Record moved to top.", flash[:notice]
57
+ assert_equal 1, @second_category.reload.position
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -2,6 +2,38 @@ require "test_helper"
2
2
 
3
3
  class Admin::DashboardControllerTest < ActionController::TestCase
4
4
 
5
+ context "When authentication is http_basic" do
6
+
7
+ setup do
8
+ Typus.stubs(:authentication).returns(:http_basic)
9
+ end
10
+
11
+ should_eventually "return a 401 message when no credentials sent" do
12
+ get :show
13
+ assert_response 401
14
+ end
15
+
16
+ should_eventually "authenticate user" do
17
+ @request.env['HTTP_AUTHORIZATION'] = 'Basic ' + Base64::encode64("admin:columbia")
18
+ get :show
19
+ assert_response :success
20
+ end
21
+
22
+ end
23
+
24
+ context "When authentication is none" do
25
+
26
+ setup do
27
+ Typus.stubs(:authentication).returns(:none)
28
+ end
29
+
30
+ should_eventually "render dashboard" do
31
+ get :show
32
+ assert_response :success
33
+ end
34
+
35
+ end
36
+
5
37
  context "Not logged" do
6
38
 
7
39
  setup do
@@ -24,7 +56,7 @@ class Admin::DashboardControllerTest < ActionController::TestCase
24
56
 
25
57
  assert_response :redirect
26
58
  assert_redirected_to new_admin_session_path
27
- assert @request.session[:typus_user_id].nil?
59
+ assert_nil @request.session[:typus_user_id]
28
60
  assert_equal "Role does no longer exists.", flash[:notice]
29
61
  end
30
62
 
@@ -81,7 +113,7 @@ class Admin::DashboardControllerTest < ActionController::TestCase
81
113
  assert_response :redirect
82
114
  assert_redirected_to new_admin_session_path
83
115
  assert_equal "Typus user has been disabled.", flash[:notice]
84
- assert @request.session[:typus_user_id].nil?
116
+ assert_nil @request.session[:typus_user_id]
85
117
  end
86
118
 
87
119
  end
@@ -0,0 +1,548 @@
1
+ require "test_helper"
2
+
3
+ class Admin::PostsControllerTest < ActionController::TestCase
4
+
5
+ setup do
6
+ @typus_user = Factory(:typus_user)
7
+ @request.session[:typus_user_id] = @typus_user.id
8
+ @post = Factory(:post)
9
+ end
10
+
11
+ teardown do
12
+ Post.delete_all
13
+ TypusUser.delete_all
14
+ end
15
+
16
+ ##############################################################################
17
+ #
18
+ ##############################################################################
19
+
20
+ context "CRUD" do
21
+
22
+ should "render index" do
23
+ get :index
24
+ assert_response :success
25
+ assert_template 'index'
26
+ end
27
+
28
+ should "render new" do
29
+ get :new
30
+ assert_response :success
31
+ assert_template 'new'
32
+ end
33
+
34
+ should "create" do
35
+ assert_difference('Post.count') do
36
+ post :create, { :post => { :title => 'This is another title', :body => 'Body' } }
37
+ assert_response :redirect
38
+ assert_redirected_to :controller => 'admin/posts', :action => 'edit', :id => Post.last
39
+ end
40
+ end
41
+
42
+ should "render show" do
43
+ get :show, { :id => @post.id }
44
+ assert_response :success
45
+ assert_template 'show'
46
+ end
47
+
48
+ should "render edit" do
49
+ get :edit, { :id => @post.id }
50
+ assert_response :success
51
+ assert_template 'edit'
52
+ end
53
+
54
+ should "update" do
55
+ post :update, { :id => @post.id, :title => 'Updated' }
56
+ assert_response :redirect
57
+ assert_redirected_to :controller => 'admin/posts', :action => 'edit', :id => @post.id
58
+ end
59
+
60
+ end
61
+
62
+ context "Forms" do
63
+
64
+ setup do
65
+ get :new
66
+ end
67
+
68
+ should "verify forms" do
69
+ assert_select "form"
70
+ end
71
+
72
+ # We have 3 inputs: 1 hidden which is the UTF8 stuff, one which is the
73
+ # Post#title and finally the submit button.
74
+ should "have 3 inputs" do
75
+ assert_select "form input", 3
76
+
77
+ # Post#title: Input
78
+ assert_select 'label[for="post_title"]'
79
+ assert_select 'input#post_title[type="text"]'
80
+ end
81
+
82
+ should "have 1 textarea" do
83
+ assert_select "form textarea", 1
84
+
85
+ # Post#body: Text Area
86
+ assert_select 'label[for="post_body"]'
87
+ assert_select 'textarea#post_body'
88
+ end
89
+
90
+ should "have 6 selectors" do
91
+ assert_select "form select", 6
92
+
93
+ # Post#created_at: Datetime
94
+ assert_select 'label[for="post_created_at"]'
95
+ assert_select 'select#post_created_at_1i'
96
+ assert_select 'select#post_created_at_2i'
97
+ assert_select 'select#post_created_at_3i'
98
+ assert_select 'select#post_created_at_4i'
99
+ assert_select 'select#post_created_at_5i'
100
+
101
+ # Post#status: Selector
102
+ assert_select 'label[for="post_status"]'
103
+ assert_select 'select#post_status'
104
+ end
105
+
106
+ should "have 1 template" do
107
+ assert_match "templates#datepicker_template_published_at", @response.body
108
+ end
109
+
110
+ end
111
+
112
+ context "Overwrite action_after_save" do
113
+
114
+ setup do
115
+ Typus::Resources.expects(:action_after_save).returns("index")
116
+ end
117
+
118
+ should "create an item and redirect to index" do
119
+ assert_difference('Post.count') do
120
+ post :create, { :post => { :title => 'This is another title', :body => 'Body' } }
121
+ assert_response :redirect
122
+ assert_redirected_to :action => 'index'
123
+ end
124
+ end
125
+
126
+ should "update an item and redirect to index" do
127
+ post :update, { :id => Factory(:post).id, :title => 'Updated' }
128
+ assert_response :redirect
129
+ assert_redirected_to :action => 'index'
130
+ end
131
+
132
+ end
133
+
134
+ context "Formats" do
135
+
136
+ should "render index and return xml" do
137
+ expected = <<-RAW
138
+ <?xml version="1.0" encoding="UTF-8"?>
139
+ <posts type="array">
140
+ <post>
141
+ <title>#{@post.title}</title>
142
+ <status>#{@post.status}</status>
143
+ </post>
144
+ </posts>
145
+ RAW
146
+
147
+ get :index, :format => "xml"
148
+
149
+ assert_response :success
150
+ assert_equal expected, @response.body
151
+ end
152
+
153
+ should "render index and return csv" do
154
+ expected = <<-RAW
155
+ title;status
156
+ #{@post.title};#{@post.status}
157
+ RAW
158
+
159
+ get :index, :format => "csv"
160
+
161
+ assert_response :success
162
+ assert_equal expected, @response.body
163
+ end
164
+
165
+ end
166
+
167
+ context "Permissions" do
168
+
169
+ context "Root" do
170
+
171
+ setup do
172
+ editor = Factory(:typus_user, :email => "editor@example.com", :role => "editor")
173
+ @post = Factory(:post, :typus_user => editor)
174
+ end
175
+
176
+ should "verify_root_can_edit_any_record" do
177
+ Post.all.each do |post|
178
+ get :edit, { :id => post.id }
179
+ assert_response :success
180
+ assert_template 'edit'
181
+ end
182
+ end
183
+
184
+ should "verify_admin_updating_an_item_does_not_change_typus_user_id_if_not_defined" do
185
+ post :update, { :id => @post.id, :post => { :title => 'Updated by admin' } }
186
+ post_updated = Post.find(@post.id)
187
+ assert_equal @post.typus_user_id, post_updated.typus_user_id
188
+ end
189
+
190
+ should "verify_admin_updating_an_item_does_change_typus_user_id_to_whatever_admin_wants" do
191
+ post :update, { :id => @post.id, :post => { :title => 'Updated', :typus_user_id => 108 } }
192
+ post_updated = Post.find(@post.id)
193
+ assert_equal 108, post_updated.typus_user_id
194
+ end
195
+
196
+ end
197
+
198
+ context "No root" do
199
+
200
+ setup do
201
+ @typus_user = Factory(:typus_user, :email => "editor@example.com", :role => "editor")
202
+ @request.session[:typus_user_id] = @typus_user.id
203
+ @request.env['HTTP_REFERER'] = '/admin/posts'
204
+ end
205
+
206
+ should "not be root" do
207
+ assert @typus_user.is_not_root?
208
+ end
209
+
210
+ should "verify_editor_can_show_any_record" do
211
+ Post.all.each do |post|
212
+ get :show, { :id => post.id }
213
+ assert_response :success
214
+ assert_template 'show'
215
+ end
216
+ end
217
+
218
+ should "verify_editor_tried_to_edit_a_post_owned_by_himself" do
219
+ post_ = Factory(:post, :typus_user => @typus_user)
220
+ get :edit, { :id => post_.id }
221
+ assert_response :success
222
+ end
223
+
224
+ should "verify_editor_tries_to_edit_a_post_owned_by_the_admin" do
225
+ get :edit, { :id => Factory(:post).id }
226
+
227
+ assert_response :redirect
228
+ assert_redirected_to @request.env['HTTP_REFERER']
229
+ assert_equal "You don't have permission to access this item.", flash[:alert]
230
+ end
231
+
232
+ should "verify_editor_tries_to_show_a_post_owned_by_the_admin" do
233
+ get :show, { :id => Factory(:post).id }
234
+ assert_response :success
235
+ end
236
+
237
+ should "verify_editor_tries_to_show_a_post_owned_by_the_admin whe only user items" do
238
+ Typus::Resources.expects(:only_user_items).returns(true)
239
+ post = Factory(:post)
240
+ get :show, { :id => post.id }
241
+
242
+ assert_response :redirect
243
+ assert_redirected_to @request.env['HTTP_REFERER']
244
+ assert_equal "You don't have permission to access this item.", flash[:alert]
245
+ end
246
+
247
+ should "verify_typus_user_id_of_item_when_creating_record" do
248
+ post :create, { :post => { :title => "Chunky Bacon", :body => "Lorem ipsum ..." } }
249
+ post_ = Post.find_by_title("Chunky Bacon")
250
+
251
+ assert_equal @request.session[:typus_user_id], post_.typus_user_id
252
+ end
253
+
254
+ should "verify_editor_updating_an_item_does_not_change_typus_user_id" do
255
+ [ 108, nil ].each do |typus_user_id|
256
+ post_ = Factory(:post, :typus_user => @typus_user)
257
+ post :update, { :id => post_.id, :post => { :title => 'Updated', :typus_user_id => @typus_user.id } }
258
+ post_updated = Post.find(post_.id)
259
+ assert_equal @request.session[:typus_user_id], post_updated.typus_user_id
260
+ end
261
+ end
262
+
263
+ end
264
+
265
+ end
266
+
267
+ context "Roles" do
268
+
269
+ setup do
270
+ @request.env['HTTP_REFERER'] = '/admin/posts'
271
+ end
272
+
273
+ context "Admin" do
274
+
275
+ should "be able to add posts" do
276
+ assert @typus_user.can?("create", "Post")
277
+ end
278
+
279
+ should "be able to destroy posts" do
280
+ get :destroy, { :id => Factory(:post).id, :method => :delete }
281
+
282
+ assert_response :redirect
283
+ assert_equal "Post successfully removed.", flash[:notice]
284
+ assert_redirected_to :action => :index
285
+ end
286
+
287
+ end
288
+
289
+ context "Designer" do
290
+
291
+ setup do
292
+ Typus.user_class.delete_all
293
+ @designer = Factory(:typus_user, :role => "designer")
294
+ @request.session[:typus_user_id] = @designer.id
295
+ @post = Factory(:post)
296
+ end
297
+
298
+ should "not be able to add posts" do
299
+ get :new
300
+
301
+ assert_response :redirect
302
+ assert_equal "Designer can't perform action. (new)", flash[:notice]
303
+ assert_redirected_to :action => :index
304
+ end
305
+
306
+ should "not_be_able_to_destroy_posts" do
307
+ assert_no_difference('Post.count') do
308
+ get :destroy, { :id => @post.id, :method => :delete }
309
+ end
310
+ assert_response :redirect
311
+ assert_equal "You don't have permission to access this item.", flash[:alert]
312
+ assert_redirected_to :action => :index
313
+ end
314
+
315
+ end
316
+
317
+ end
318
+
319
+ context "Relationships" do
320
+
321
+ ##
322
+ # Post => has_many :comments
323
+ ##
324
+
325
+ should "relate_comment_with_post_and_then_unrelate" do
326
+
327
+ comment = Factory(:comment, :post => nil)
328
+ post_ = Factory(:post)
329
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#comments"
330
+
331
+ assert_difference('post_.comments.count') do
332
+ post :relate, { :id => post_.id,
333
+ :related => { :model => 'Comment', :id => comment.id } }
334
+ end
335
+
336
+ assert_response :redirect
337
+ assert_redirected_to @request.env['HTTP_REFERER']
338
+ assert_equal "Comment related to Post", flash[:notice]
339
+
340
+ assert_difference('post_.comments.count', -1) do
341
+ post :unrelate, { :id => post_.id,
342
+ :resource => 'Comment', :resource_id => comment.id }
343
+ end
344
+
345
+ assert_response :redirect
346
+ assert_redirected_to @request.env['HTTP_REFERER']
347
+ assert_equal "Comment unrelated from Post", flash[:notice]
348
+
349
+ end
350
+
351
+ ##
352
+ # Post => has_and_belongs_to_many :categories
353
+ ##
354
+
355
+ should "relate_category_with_post_and_then_unrelate" do
356
+ category = Factory(:category)
357
+ post_ = Factory(:post)
358
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#categories"
359
+
360
+ ##
361
+ # First Step: Relate
362
+ #
363
+
364
+ assert_difference('category.posts.count') do
365
+ post :relate, { :id => post_.id, :related => { :model => 'Category', :id => category.id } }
366
+ end
367
+
368
+ assert_response :redirect
369
+ assert_redirected_to @request.env['HTTP_REFERER']
370
+ assert_equal "Category related to Post", flash[:notice]
371
+
372
+ ##
373
+ # Second Step: Unrelate
374
+ #
375
+
376
+ assert_difference('category.posts.count', -1) do
377
+ post :unrelate, { :id => post_.id, :resource => 'Category', :resource_id => category.id }
378
+ end
379
+
380
+ assert_response :redirect
381
+ assert_redirected_to @request.env['HTTP_REFERER']
382
+ assert_equal "Category unrelated from Post", flash[:notice]
383
+ end
384
+
385
+ ##
386
+ # Post => has_many :assets, :as => resource (Polimorphic)
387
+ ##
388
+
389
+ should "relate_asset_with_post_and_then_unrelate" do
390
+ post_ = Factory(:post)
391
+ asset = Factory(:asset)
392
+ post_.assets << asset
393
+
394
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#assets"
395
+
396
+ assert_difference('post_.assets.count', -1) do
397
+ get :unrelate, { :id => post_.id, :resource => 'Asset', :resource_id => asset.id }
398
+ end
399
+ assert_response :redirect
400
+ assert_redirected_to @request.env['HTTP_REFERER']
401
+ assert_equal "Asset unrelated from Post", flash[:notice]
402
+ end
403
+
404
+ end
405
+
406
+ context "Views" do
407
+
408
+ context "Index" do
409
+
410
+ setup do
411
+ get :index
412
+ end
413
+
414
+ should "render index and validates_presence_of_custom_partials" do
415
+ assert_match "posts#_index.html.erb", @response.body
416
+ end
417
+
418
+ should "render_index_and_verify_page_title" do
419
+ assert_select "title", "Posts"
420
+ end
421
+
422
+ should "render index_and_show_add_entry_link" do
423
+ assert_select "#sidebar ul" do
424
+ assert_select "li", "Add new"
425
+ end
426
+ end
427
+
428
+ should "render_index_and_show_trash_item_image" do
429
+ assert_response :success
430
+ assert_select '.trash', 'Trash'
431
+ end
432
+
433
+ end
434
+
435
+ context "New" do
436
+
437
+ setup do
438
+ get :new
439
+ end
440
+
441
+ should "render new and partials_on_new" do
442
+ assert_match "posts#_new.html.erb", @response.body
443
+ end
444
+
445
+ should "render new and verify page title" do
446
+ assert_select "title", "New Post"
447
+ end
448
+
449
+ end
450
+
451
+ context "Edit" do
452
+
453
+ setup do
454
+ get :edit, { :id => Factory(:post).id }
455
+ end
456
+
457
+ should "render_edit_and_verify_presence_of_custom_partials" do
458
+ assert_match "posts#_edit.html.erb", @response.body
459
+ end
460
+
461
+ should "render_edit_and_verify_page_title" do
462
+ assert_select "title", "Edit Post"
463
+ end
464
+
465
+ end
466
+
467
+ context "Show" do
468
+
469
+ setup do
470
+ get :show, { :id => Factory(:post).id }
471
+ end
472
+
473
+ should "render_show_and_verify_presence_of_custom_partials" do
474
+ assert_match "posts#_show.html.erb", @response.body
475
+ end
476
+
477
+ should "render_show_and_verify_page_title" do
478
+ assert_select "title", "Show Post"
479
+ end
480
+
481
+ end
482
+
483
+ should "get_index_and_render_edit_or_show_links" do
484
+ %w(edit show).each do |action|
485
+ Typus::Resources.expects(:default_action_on_item).at_least_once.returns(action)
486
+ get :index
487
+ Post.all.each do |post|
488
+ assert_match "/posts/#{action}/#{post.id}", @response.body
489
+ end
490
+ end
491
+ end
492
+
493
+ context "Designer" do
494
+
495
+ setup do
496
+ @typus_user = Factory(:typus_user, :email => "designer@example.com", :role => "designer")
497
+ @request.session[:typus_user_id] = @typus_user.id
498
+ end
499
+
500
+ should "render_index_and_not_show_add_entry_link" do
501
+ get :index
502
+ assert_response :success
503
+ assert_no_match /Add Post/, @response.body
504
+ end
505
+
506
+ should "render_index_and_not_show_trash_image" do
507
+ get :index
508
+ assert_response :success
509
+ assert_select ".trash", false
510
+ end
511
+
512
+ end
513
+
514
+ context "Editor" do
515
+
516
+ setup do
517
+ @typus_user = Factory(:typus_user, :email => "editor@example.com", :role => "editor")
518
+ @request.session[:typus_user_id] = @typus_user.id
519
+ end
520
+
521
+ should "get_index_and_render_edit_or_show_links_on_owned_records" do
522
+ get :index
523
+ Post.all.each do |post|
524
+ action = post.owned_by?(@typus_user) ? "edit" : "show"
525
+ assert_match "/posts/#{action}/#{post.id}", @response.body
526
+ end
527
+ end
528
+
529
+ should "get_index_and_render_edit_or_show_on_only_user_items" do
530
+ %w(edit show).each do |action|
531
+ Typus::Resources.stubs(:only_user_items).returns(true)
532
+ Typus::Resources.stubs(:default_action_on_item).returns(action)
533
+ get :index
534
+ Post.all.each do |post|
535
+ if post.owned_by?(@typus_user)
536
+ assert_match "/posts/#{action}/#{post.id}", @response.body
537
+ else
538
+ assert_no_match /\/posts\/#{action}\/#{post.id}/, @response.body
539
+ end
540
+ end
541
+ end
542
+ end
543
+
544
+ end
545
+
546
+ end
547
+
548
+ end