fesplugas-typus 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. data/.gitignore +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +80 -0
  4. data/Rakefile +61 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/admin/master_controller.rb +324 -0
  7. data/app/controllers/typus_controller.rb +127 -0
  8. data/app/helpers/admin/form_helper.rb +351 -0
  9. data/app/helpers/admin/master_helper.rb +99 -0
  10. data/app/helpers/admin/public_helper.rb +24 -0
  11. data/app/helpers/admin/sidebar_helper.rb +259 -0
  12. data/app/helpers/admin/table_helper.rb +227 -0
  13. data/app/helpers/typus_helper.rb +169 -0
  14. data/app/models/typus_mailer.rb +14 -0
  15. data/app/models/typus_user.rb +5 -0
  16. data/app/views/admin/dashboard/_sidebar.html.erb +9 -0
  17. data/app/views/admin/resources/edit.html.erb +29 -0
  18. data/app/views/admin/resources/index.html.erb +28 -0
  19. data/app/views/admin/resources/new.html.erb +27 -0
  20. data/app/views/admin/resources/show.html.erb +21 -0
  21. data/app/views/admin/shared/_footer.html.erb +1 -0
  22. data/app/views/admin/shared/_pagination.html.erb +28 -0
  23. data/app/views/layouts/admin.html.erb +72 -0
  24. data/app/views/layouts/typus.html.erb +29 -0
  25. data/app/views/typus/dashboard.html.erb +13 -0
  26. data/app/views/typus/recover_password.html.erb +7 -0
  27. data/app/views/typus/reset_password.html.erb +13 -0
  28. data/app/views/typus/sign_in.html.erb +9 -0
  29. data/app/views/typus/sign_up.html.erb +7 -0
  30. data/app/views/typus_mailer/reset_password_link.erb +11 -0
  31. data/config/locales/es.yml +106 -0
  32. data/config/locales/pt-BR.yml +108 -0
  33. data/config/locales/typus_hacks.yml +14 -0
  34. data/config/routes.rb +14 -0
  35. data/generators/typus/templates/config/initializers/typus.rb +27 -0
  36. data/generators/typus/templates/config/typus/application.yml +45 -0
  37. data/generators/typus/templates/config/typus/application_roles.yml +23 -0
  38. data/generators/typus/templates/config/typus/typus.yml +14 -0
  39. data/generators/typus/templates/config/typus/typus_roles.yml +2 -0
  40. data/generators/typus/templates/db/create_typus_users.rb +21 -0
  41. data/generators/typus/templates/public/images/admin/arrow_down.gif +0 -0
  42. data/generators/typus/templates/public/images/admin/arrow_up.gif +0 -0
  43. data/generators/typus/templates/public/images/admin/spinner.gif +0 -0
  44. data/generators/typus/templates/public/images/admin/status_false.gif +0 -0
  45. data/generators/typus/templates/public/images/admin/status_true.gif +0 -0
  46. data/generators/typus/templates/public/images/admin/trash.gif +0 -0
  47. data/generators/typus/templates/public/javascripts/admin/application.js +14 -0
  48. data/generators/typus/templates/public/stylesheets/admin/reset.css +68 -0
  49. data/generators/typus/templates/public/stylesheets/admin/screen.css +709 -0
  50. data/generators/typus/typus_generator.rb +141 -0
  51. data/generators/typus_update_schema_to_01/templates/config/typus.yml +14 -0
  52. data/generators/typus_update_schema_to_01/templates/migration.rb +11 -0
  53. data/generators/typus_update_schema_to_01/typus_update_schema_to_01_generator.rb +19 -0
  54. data/init.rb +19 -0
  55. data/lib/typus/active_record.rb +298 -0
  56. data/lib/typus/authentication.rb +155 -0
  57. data/lib/typus/configuration.rb +92 -0
  58. data/lib/typus/format.rb +56 -0
  59. data/lib/typus/generator.rb +173 -0
  60. data/lib/typus/hash.rb +10 -0
  61. data/lib/typus/locale.rb +17 -0
  62. data/lib/typus/object.rb +22 -0
  63. data/lib/typus/quick_edit.rb +33 -0
  64. data/lib/typus/reloader.rb +17 -0
  65. data/lib/typus/string.rb +11 -0
  66. data/lib/typus/user.rb +137 -0
  67. data/lib/typus.rb +133 -0
  68. data/lib/vendor/active_record.rb +15 -0
  69. data/lib/vendor/paginator.rb +143 -0
  70. data/tasks/typus_tasks.rake +26 -0
  71. data/test/config/broken/application.yml +68 -0
  72. data/test/config/broken/application_roles.yml +20 -0
  73. data/test/config/broken/empty.yml +0 -0
  74. data/test/config/broken/empty_roles.yml +0 -0
  75. data/test/config/broken/undefined.yml +3 -0
  76. data/test/config/broken/undefined_roles.yml +6 -0
  77. data/test/config/default/typus.yml +14 -0
  78. data/test/config/default/typus_roles.yml +2 -0
  79. data/test/config/empty/empty_01.yml +0 -0
  80. data/test/config/empty/empty_01_roles.yml +0 -0
  81. data/test/config/empty/empty_02.yml +0 -0
  82. data/test/config/empty/empty_02_roles.yml +0 -0
  83. data/test/config/locales/es.yml +10 -0
  84. data/test/config/ordered/001_roles.yml +2 -0
  85. data/test/config/ordered/002_roles.yml +2 -0
  86. data/test/config/unordered/app_one_roles.yml +2 -0
  87. data/test/config/unordered/app_two_roles.yml +2 -0
  88. data/test/config/working/application.yml +67 -0
  89. data/test/config/working/application_roles.yml +22 -0
  90. data/test/config/working/typus.yml +14 -0
  91. data/test/config/working/typus_roles.yml +2 -0
  92. data/test/fixtures/app/controllers/admin/assets_controller.rb +2 -0
  93. data/test/fixtures/app/controllers/admin/categories_controller.rb +2 -0
  94. data/test/fixtures/app/controllers/admin/comments_controller.rb +2 -0
  95. data/test/fixtures/app/controllers/admin/pages_controller.rb +2 -0
  96. data/test/fixtures/app/controllers/admin/posts_controller.rb +2 -0
  97. data/test/fixtures/app/controllers/admin/status_controller.rb +6 -0
  98. data/test/fixtures/app/controllers/admin/typus_users_controller.rb +2 -0
  99. data/test/fixtures/app/controllers/admin/watch_dog_controller.rb +6 -0
  100. data/test/fixtures/app/views/admin/comments/_edit_bottom.html.erb +1 -0
  101. data/test/fixtures/app/views/admin/comments/_edit_sidebar.html.erb +1 -0
  102. data/test/fixtures/app/views/admin/comments/_edit_top.html.erb +1 -0
  103. data/test/fixtures/app/views/admin/comments/_index_bottom.html.erb +1 -0
  104. data/test/fixtures/app/views/admin/comments/_index_sidebar.html.erb +1 -0
  105. data/test/fixtures/app/views/admin/comments/_index_top.html.erb +1 -0
  106. data/test/fixtures/app/views/admin/comments/_new_bottom.html.erb +1 -0
  107. data/test/fixtures/app/views/admin/comments/_new_sidebar.html.erb +1 -0
  108. data/test/fixtures/app/views/admin/comments/_new_top.html.erb +1 -0
  109. data/test/fixtures/app/views/admin/comments/_show_bottom.html.erb +1 -0
  110. data/test/fixtures/app/views/admin/comments/_show_sidebar.html.erb +1 -0
  111. data/test/fixtures/app/views/admin/comments/_show_top.html.erb +1 -0
  112. data/test/fixtures/app/views/admin/dashboard/_bottom.html.erb +1 -0
  113. data/test/fixtures/app/views/admin/dashboard/_sidebar.html.erb +1 -0
  114. data/test/fixtures/app/views/admin/dashboard/_top.html.erb +1 -0
  115. data/test/fixtures/app/views/admin/shared/_footer.html.erb +1 -0
  116. data/test/fixtures/app/views/admin/status/index.html.erb +1 -0
  117. data/test/fixtures/app/views/admin/templates/_datepicker.html.erb +1 -0
  118. data/test/fixtures/assets.yml +11 -0
  119. data/test/fixtures/categories.yml +14 -0
  120. data/test/fixtures/comments.yml +27 -0
  121. data/test/fixtures/pages.yml +41 -0
  122. data/test/fixtures/posts.yml +37 -0
  123. data/test/fixtures/typus_users.yml +54 -0
  124. data/test/functional/admin/assets_controller_test.rb +57 -0
  125. data/test/functional/admin/categories_controller_test.rb +106 -0
  126. data/test/functional/admin/comments_controller_test.rb +121 -0
  127. data/test/functional/admin/master_controller_test.rb +5 -0
  128. data/test/functional/admin/posts_controller_test.rb +278 -0
  129. data/test/functional/admin/status_controller_test.rb +43 -0
  130. data/test/functional/admin/typus_users_controller_test.rb +239 -0
  131. data/test/functional/typus_controller_test.rb +315 -0
  132. data/test/helper.rb +51 -0
  133. data/test/helpers/admin/form_helper_test.rb +316 -0
  134. data/test/helpers/admin/master_helper_test.rb +65 -0
  135. data/test/helpers/admin/public_helper_test.rb +22 -0
  136. data/test/helpers/admin/sidebar_helper_test.rb +351 -0
  137. data/test/helpers/admin/table_helper_test.rb +255 -0
  138. data/test/helpers/typus_helper_test.rb +106 -0
  139. data/test/lib/active_record_test.rb +372 -0
  140. data/test/lib/configuration_test.rb +91 -0
  141. data/test/lib/hash_test.rb +11 -0
  142. data/test/lib/routes_test.rb +82 -0
  143. data/test/lib/string_test.rb +25 -0
  144. data/test/lib/typus_test.rb +105 -0
  145. data/test/models.rb +51 -0
  146. data/test/schema.rb +64 -0
  147. data/test/unit/typus_mailer_test.rb +33 -0
  148. data/test/unit/typus_test.rb +17 -0
  149. data/test/unit/typus_user_roles_test.rb +90 -0
  150. data/test/unit/typus_user_test.rb +177 -0
  151. data/test/vendor/active_record_test.rb +18 -0
  152. data/test/vendor/paginator_test.rb +138 -0
  153. data/typus.gemspec +225 -0
  154. metadata +241 -0
@@ -0,0 +1,278 @@
1
+ require 'test/helper'
2
+
3
+ ##
4
+ # Test CRUD actions and ...
5
+ #
6
+ # - Relate comment which is a has_many relationship.
7
+ # - Unrelate comment which is a has_many relationship.
8
+ #
9
+ class Admin::PostsControllerTest < ActionController::TestCase
10
+
11
+ def setup
12
+ typus_user = typus_users(:admin)
13
+ @request.session[:typus_user_id] = typus_user.id
14
+ end
15
+
16
+ def test_should_redirect_to_login
17
+
18
+ @request.session[:typus_user_id] = nil
19
+
20
+ get :index
21
+ assert_response :redirect
22
+ assert_redirected_to admin_sign_in_path(:back_to => '/admin/posts')
23
+ get :edit, { :id => 1 }
24
+ assert_response :redirect
25
+ assert_redirected_to admin_sign_in_path(:back_to => '/admin/posts')
26
+
27
+ end
28
+
29
+ def test_should_render_index
30
+ get :index
31
+ assert_response :success
32
+ assert_template 'index'
33
+ end
34
+
35
+ def test_should_render_new
36
+ test_should_update_item_and_redirect_to_index
37
+ get :new
38
+ assert_response :success
39
+ assert_template 'new'
40
+ end
41
+
42
+ def test_should_create_item_and_redirect_to_index
43
+
44
+ options = Typus::Configuration.options.merge(:index_after_save => true)
45
+ Typus::Configuration.stubs(:options).returns(options)
46
+
47
+ assert_difference 'Post.count' do
48
+ post :create, { :item => { :title => 'This is another title', :body => 'Body' } }
49
+ assert_response :redirect
50
+ assert_redirected_to :action => 'index'
51
+ end
52
+
53
+ end
54
+
55
+ def test_should_create_item_and_redirect_to_edit
56
+
57
+ options = Typus::Configuration.options.merge(:index_after_save => false)
58
+ Typus::Configuration.stubs(:options).returns(options)
59
+
60
+ assert_difference 'Post.count' do
61
+ post :create, { :item => { :title => 'This is another title', :body => 'Body' } }
62
+ assert_response :redirect
63
+ assert_redirected_to :action => 'edit'
64
+ end
65
+
66
+ end
67
+
68
+ def test_should_render_show
69
+ post_ = posts(:published)
70
+ get :show, { :id => post_.id }
71
+ assert_response :success
72
+ assert_template 'show'
73
+ end
74
+
75
+ def test_should_render_edit
76
+ post_ = posts(:published)
77
+ get :edit, { :id => post_.id }
78
+ assert_response :success
79
+ assert_template 'edit'
80
+ end
81
+
82
+ def test_should_update_item_and_redirect_to_index
83
+
84
+ options = Typus::Configuration.options.merge(:index_after_save => true)
85
+ Typus::Configuration.stubs(:options).returns(options)
86
+
87
+ post_ = posts(:published)
88
+ post :update, { :id => post_.id, :title => 'Updated' }
89
+ assert_response :redirect
90
+ assert_redirected_to :action => 'index'
91
+
92
+ end
93
+
94
+ def test_should_update_item_and_redirect_to_edit
95
+
96
+ options = Typus::Configuration.options.merge(:index_after_save => false)
97
+ Typus::Configuration.stubs(:options).returns(options)
98
+
99
+ post_ = posts(:published)
100
+ post :update, { :id => post_.id, :title => 'Updated' }
101
+ assert_response :redirect
102
+ assert_redirected_to :action => 'edit', :id => post_.id
103
+
104
+ end
105
+
106
+ def test_should_allow_admin_to_toggle_item
107
+ @request.env['HTTP_REFERER'] = '/admin/posts'
108
+ post = posts(:unpublished)
109
+ get :toggle, { :id => post.id, :field => 'status' }
110
+ assert_response :redirect
111
+ assert_redirected_to :action => 'index'
112
+ assert flash[:success]
113
+ assert Post.find(post.id).status
114
+ end
115
+
116
+ def test_should_perform_a_search
117
+ typus_user = typus_users(:admin)
118
+ @request.session[:typus_user_id] = typus_user.id
119
+ get :index, { :search => 'neinonon' }
120
+ assert_response :success
121
+ assert_template 'index'
122
+ end
123
+
124
+ def test_should_relate_category_to_post_which_is_a_habtm_relationship
125
+ category = categories(:first)
126
+ post_ = posts(:published)
127
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#categories"
128
+ assert_difference('category.posts.count') do
129
+ post :relate, { :id => post_.id, :related => { :model => 'Category', :id => category.id } }
130
+ end
131
+ assert_response :redirect
132
+ assert flash[:success]
133
+ assert_redirected_to @request.env['HTTP_REFERER']
134
+ end
135
+
136
+ def test_should_unrelate_category_from_post_which_is_a_habtm_relationship
137
+ category = categories(:first)
138
+ post_ = posts(:published)
139
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#categories"
140
+ assert_difference('category.posts.count', 0) do
141
+ post :unrelate, { :id => post_.id, :resource => 'Category', :resource_id => category.id, :association => 'has_and_belongs_to_many' }
142
+ end
143
+ assert_response :redirect
144
+ assert flash[:success]
145
+ assert_match /Category unrelated from/, flash[:success]
146
+ assert_redirected_to @request.env['HTTP_REFERER']
147
+ end
148
+
149
+ ##
150
+ # This is a polimorphic relationship.
151
+ #
152
+ def test_should_unrelate_an_asset_from_a_post
153
+
154
+ post_ = posts(:published)
155
+
156
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#assets"
157
+
158
+ assert_difference('post_.assets.count', -1) do
159
+ get :unrelate, { :id => post_.id, :resource => 'Asset', :resource_id => post_.assets.first.id, :association => 'has_many' }
160
+ end
161
+
162
+ assert_response :redirect
163
+ assert_redirected_to @request.env['HTTP_REFERER']
164
+ assert flash[:success]
165
+ assert_match /Asset removed from/, flash[:success]
166
+
167
+ end
168
+
169
+ def test_should_check_redirection_when_theres_no_http_referer_on_new
170
+
171
+ typus_user = typus_users(:designer)
172
+ @request.session[:typus_user_id] = typus_user.id
173
+
174
+ get :new
175
+ assert_response :redirect
176
+ assert_redirected_to admin_dashboard_path
177
+
178
+ assert flash[:notice]
179
+ assert_equal "Designer can't perform action (new).", flash[:notice]
180
+
181
+ @request.env['HTTP_REFERER'] = '/admin/posts'
182
+
183
+ typus_user = typus_users(:designer)
184
+ @request.session[:typus_user_id] = typus_user.id
185
+
186
+ get :new
187
+ assert_response :redirect
188
+ assert_redirected_to '/admin/posts'
189
+
190
+ assert flash[:notice]
191
+ assert_equal "Designer can't perform action (new).", flash[:notice]
192
+
193
+ end
194
+
195
+ def test_should_disable_toggle_and_check_links_are_disabled
196
+
197
+ options = Typus::Configuration.options.merge(:toggle => false)
198
+ Typus::Configuration.stubs(:options).returns(options)
199
+
200
+ @request.env['HTTP_REFERER'] = '/admin/posts'
201
+ post = posts(:unpublished)
202
+ get :toggle, { :id => post.id, :field => 'status' }
203
+ assert_response :redirect
204
+ assert_redirected_to :action => 'index'
205
+ assert !flash[:success]
206
+ assert !flash[:error]
207
+ assert flash[:notice]
208
+ assert_equal "Toggle is disabled.", flash[:notice]
209
+
210
+ end
211
+
212
+ def test_should_show_form_templates
213
+ get :new
214
+ assert_response :success
215
+ assert_match /datepicker_template_published_at/, @response.body
216
+ end
217
+
218
+ =begin
219
+
220
+ # FIXME
221
+
222
+ def test_should_add_back_to_params_for_comment_items
223
+
224
+ post_ = posts(:published)
225
+ get :edit, { :id => post_.id }
226
+ assert_response :success
227
+ back_to = "/posts/#{post_.id}/edit"
228
+ re = /<a href="\/typus\/assets\/1\/edit(.*)">This is the caption\.<\/a>/
229
+ params = re.match(@response.body)
230
+
231
+ assert_equal params[1], "?back_to=%2Ftypus%2Fposts%2F1%2Fedit&amp;resource=posts&amp;resource_id=#{post_.id}"
232
+
233
+ end
234
+
235
+ =end
236
+
237
+ def test_should_verify_root_can_edit_any_record
238
+ Post.find(:all).each do |post|
239
+ get :edit, { :id => post.id }
240
+ assert_response :success
241
+ assert_template 'edit'
242
+ end
243
+ end
244
+
245
+ def test_should_verify_editor_can_view_all_records
246
+ Post.find(:all).each do |post|
247
+ get :show, { :id => post.id }
248
+ assert_response :success
249
+ assert_template 'show'
250
+ end
251
+ end
252
+
253
+ def test_should_verify_editor_can_edit_their_records
254
+
255
+ typus_user = typus_users(:editor)
256
+ @request.session[:typus_user_id] = typus_user.id
257
+
258
+ post = posts(:owned_by_editor)
259
+ get :edit, { :id => post.id }
260
+ assert_response :success
261
+
262
+ end
263
+
264
+ def test_should_verify_editor_cannot_edit_other_users_records
265
+
266
+ typus_user = typus_users(:editor)
267
+ @request.session[:typus_user_id] = typus_user.id
268
+
269
+ post = posts(:owned_by_admin)
270
+ get :edit, { :id => post.id }
271
+ assert_response :redirect
272
+ assert_redirected_to :action => 'show', :id => post.id
273
+ assert flash[:notice]
274
+ assert_equal "Record owned by another user.", flash[:notice]
275
+
276
+ end
277
+
278
+ end
@@ -0,0 +1,43 @@
1
+ require 'test/helper'
2
+
3
+ ##
4
+ # Test resources which are not related to an ActiveRecord model.
5
+ #
6
+ class Admin::StatusControllerTest < ActionController::TestCase
7
+
8
+ def setup
9
+ @typus_user = typus_users(:admin)
10
+ @request.session[:typus_user_id] = @typus_user.id
11
+ end
12
+
13
+ def test_should_verify_admin_can_go_to_index
14
+ get :index
15
+ assert_response :success
16
+ assert_template 'index'
17
+ end
18
+
19
+ def test_should_verify_status_is_not_available_if_user_not_logged
20
+ @request.session[:typus_user_id] = nil
21
+ get :index
22
+ assert_response :redirect
23
+ assert_redirected_to admin_sign_in_path(:back_to => '/admin/status')
24
+ end
25
+
26
+ def test_should_verify_admin_can_not_go_to_show
27
+ get :show
28
+ assert_response :redirect
29
+ assert_redirected_to admin_dashboard_path
30
+ assert flash[:notice]
31
+ assert_equal "#{@typus_user.role.capitalize} can't go to show on status.", flash[:notice]
32
+ end
33
+
34
+ def test_should_verify_editor_can_not_go_to_index
35
+ typus_user = typus_users(:editor)
36
+ @request.session[:typus_user_id] = typus_user.id
37
+ get :index
38
+ assert_response :redirect
39
+ assert flash[:notice]
40
+ assert_equal "#{typus_user.role.capitalize} can't go to index on status.", flash[:notice]
41
+ end
42
+
43
+ end
@@ -0,0 +1,239 @@
1
+ require 'test/helper'
2
+
3
+ ##
4
+ # Test what TypusUsers can do.
5
+ #
6
+ class Admin::TypusUsersControllerTest < ActionController::TestCase
7
+
8
+ def setup
9
+ Typus::Configuration.options[:root] = 'admin'
10
+ @typus_user = typus_users(:admin)
11
+ @request.session[:typus_user_id] = @typus_user.id
12
+ end
13
+
14
+ def test_should_allow_admin_to_create_typus_users
15
+ get :new
16
+ assert_response :success
17
+ end
18
+
19
+ def test_should_not_allow_admin_to_toggle_her_status
20
+
21
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
22
+ get :toggle, { :id => @typus_user.id, :field => 'status' }
23
+
24
+ assert_response :redirect
25
+ assert_redirected_to @request.env['HTTP_REFERER']
26
+ assert flash[:notice]
27
+ assert_equal "You can't toggle your status.", flash[:notice]
28
+
29
+ end
30
+
31
+ def test_should_allow_admin_to_toggle_other_users_status
32
+
33
+ @request.env['HTTP_REFERER'] = '/typus/typus_users'
34
+ editor = typus_users(:editor)
35
+ get :toggle, { :id => editor.id, :field => 'status' }
36
+
37
+ assert_response :redirect
38
+ assert_redirected_to @request.env['HTTP_REFERER']
39
+ assert flash[:success]
40
+ assert_equal "Typus user status changed.", flash[:success]
41
+
42
+ end
43
+
44
+ def test_should_not_allow_non_root_typus_user_to_toggle_status
45
+
46
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
47
+ @typus_user = typus_users(:editor)
48
+ @request.session[:typus_user_id] = @typus_user.id
49
+ get :toggle, { :id => @typus_user.id, :field => 'status' }
50
+
51
+ assert_response :redirect
52
+ assert_redirected_to @request.env['HTTP_REFERER']
53
+ assert flash[:notice]
54
+ assert_equal "You're not allowed to toggle status.", flash[:notice]
55
+
56
+ end
57
+
58
+ def test_should_verify_admin_cannot_destroy_herself
59
+
60
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
61
+
62
+ assert_difference('TypusUser.count', 0) do
63
+ delete :destroy, :id => @typus_user.id
64
+ end
65
+
66
+ assert_response :redirect
67
+ assert_redirected_to @request.env['HTTP_REFERER']
68
+ assert flash[:notice]
69
+ assert_equal "You can't remove yourself.", flash[:notice]
70
+
71
+ end
72
+
73
+ def test_should_verify_admin_can_destroy_others
74
+
75
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
76
+
77
+ assert_difference('TypusUser.count', -1) do
78
+ delete :destroy, :id => typus_users(:editor).id
79
+ end
80
+
81
+ assert_response :redirect
82
+ assert_redirected_to @request.env['HTTP_REFERER']
83
+ assert flash[:success]
84
+ assert_equal "Typus user successfully removed.", flash[:success]
85
+
86
+ end
87
+
88
+ def test_should_not_allow_editor_to_create_typus_users
89
+
90
+ @request.env['HTTP_REFERER'] = '/typus/typus_users'
91
+ typus_user = typus_users(:editor)
92
+ @request.session[:typus_user_id] = typus_user.id
93
+ get :new
94
+
95
+ assert_response :redirect
96
+ assert_redirected_to @request.env['HTTP_REFERER']
97
+ assert flash[:notice]
98
+ assert_equal "Editor can't perform action (new).", flash[:notice].to_s
99
+
100
+ end
101
+
102
+ def test_should_allow_editor_to_update_himself
103
+
104
+ options = Typus::Configuration.options.merge(:index_after_save => false)
105
+ Typus::Configuration.stubs(:options).returns(options)
106
+
107
+ typus_user = typus_users(:editor)
108
+ @request.session[:typus_user_id] = typus_user.id
109
+ @request.env['HTTP_REFERER'] = "/admin/typus_users/edit/#{typus_user.id}"
110
+ get :edit, { :id => typus_user.id }
111
+
112
+ assert_response :success
113
+ assert_equal 'editor', typus_user.role
114
+
115
+ post :update, { :id => typus_user.id,
116
+ :item => { :first_name => 'Richard',
117
+ :last_name => 'Ashcroft',
118
+ :role => 'editor' } }
119
+
120
+ assert_response :redirect
121
+ assert_redirected_to @request.env['HTTP_REFERER']
122
+ assert flash[:success]
123
+ assert_equal "Typus user successfully updated.", flash[:success]
124
+
125
+ end
126
+
127
+ def test_should_not_allow_editor_to_update_himself_to_become_admin
128
+
129
+ typus_user = typus_users(:editor)
130
+ @request.session[:typus_user_id] = typus_user.id
131
+ @request.env['HTTP_REFERER'] = "/admin/typus_users/#{typus_user.id}/edit"
132
+
133
+ assert_equal 'editor', typus_user.role
134
+
135
+ post :update, { :id => typus_user.id,
136
+ :item => { :role => 'admin' } }
137
+
138
+ assert_response :redirect
139
+ assert_redirected_to @request.env['HTTP_REFERER']
140
+ assert flash[:notice]
141
+ assert_equal "You can't change your role.", flash[:notice]
142
+
143
+ end
144
+
145
+ def test_should_not_allow_editor_to_edit_other_users_profiles
146
+
147
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
148
+ typus_user = typus_users(:editor)
149
+ @request.session[:typus_user_id] = typus_user.id
150
+ get :edit, { :id => typus_user.id }
151
+
152
+ assert_response :success
153
+ assert_template 'edit'
154
+
155
+ get :edit, { :id => typus_users(:admin).id }
156
+
157
+ assert_response :redirect
158
+ assert_redirected_to @request.env['HTTP_REFERER']
159
+ assert flash[:notice]
160
+ assert_equal "As you're not the admin or the owner of this record you cannot edit it.", flash[:notice]
161
+
162
+ end
163
+
164
+ def test_should_not_allow_editor_to_destroy_users
165
+
166
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
167
+ typus_user = typus_users(:editor)
168
+ @request.session[:typus_user_id] = typus_user.id
169
+ delete :destroy, :id => typus_users(:admin).id
170
+
171
+ assert_response :redirect
172
+ assert_redirected_to @request.env['HTTP_REFERER']
173
+ assert flash[:notice]
174
+ assert_equal "You're not allowed to remove Typus Users.", flash[:notice]
175
+
176
+ end
177
+
178
+ def test_should_not_allow_editor_to_destroy_herself
179
+
180
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
181
+ typus_user = typus_users(:editor)
182
+ @request.session[:typus_user_id] = typus_user.id
183
+ delete :destroy, :id => typus_user.id
184
+
185
+ assert_response :redirect
186
+ assert_redirected_to @request.env['HTTP_REFERER']
187
+ assert flash[:notice]
188
+ assert_equal "You're not allowed to remove Typus Users.", flash[:notice]
189
+
190
+ end
191
+
192
+ def test_should_redirect_to_admin_dashboard_if_user_does_not_have_privileges
193
+
194
+ @request.env['HTTP_REFERER'] = '/admin'
195
+ typus_user = typus_users(:designer)
196
+ @request.session[:typus_user_id] = typus_user.id
197
+ get :index
198
+
199
+ assert_response :redirect
200
+ assert_redirected_to @request.env['HTTP_REFERER']
201
+ assert flash[:notice]
202
+ assert_equal "Designer can't display items.", flash[:notice]
203
+
204
+ end
205
+
206
+ def test_should_change_root_to_editor_so_editor_can_edit_others_content
207
+
208
+ typus_user = typus_users(:editor)
209
+ @request.session[:typus_user_id] = typus_user.id
210
+ @request.env['HTTP_REFERER'] = '/admin/typus_users'
211
+
212
+ assert_equal 'editor', typus_user.role
213
+
214
+ get :edit, :id => typus_user.id
215
+ assert_response :success
216
+
217
+ get :edit, :id => typus_users(:admin).id
218
+ assert_response :redirect
219
+ assert_redirected_to @request.env['HTTP_REFERER']
220
+ assert flash[:notice]
221
+ assert_equal "As you're not the admin or the owner of this record you cannot edit it.", flash[:notice]
222
+
223
+ ##
224
+ # Here we change the behavior, editor has become root, so he
225
+ # has access to all TypusUser records.
226
+ #
227
+
228
+ options = Typus::Configuration.options.merge(:root => 'editor')
229
+ Typus::Configuration.stubs(:options).returns(options)
230
+
231
+ get :edit, :id => typus_user.id
232
+ assert_response :success
233
+
234
+ get :edit, :id => typus_users(:admin).id
235
+ assert_response :success
236
+
237
+ end
238
+
239
+ end