typus 0.9.29 → 0.9.30

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/README.rdoc +4 -54
  2. data/VERSION +1 -1
  3. data/app/controllers/admin/master_controller.rb +36 -18
  4. data/app/helpers/admin/form_helper.rb +40 -7
  5. data/app/helpers/admin/table_helper.rb +6 -17
  6. data/app/views/admin/resources/show.html.erb +1 -2
  7. data/app/views/admin/templates/_file.html.erb +2 -40
  8. data/app/views/layouts/admin.html.erb +3 -0
  9. data/config/locales/de.yml +0 -1
  10. data/config/locales/es.yml +0 -1
  11. data/config/locales/fr.yml +0 -1
  12. data/config/locales/language.yml.template +0 -1
  13. data/config/locales/pt-BR.yml +0 -1
  14. data/config/locales/ru.yml +0 -1
  15. data/generators/typus/templates/public/stylesheets/admin/screen.css +1 -1
  16. data/generators/typus/typus_generator.rb +1 -1
  17. data/lib/typus/preview.rb +66 -2
  18. data/tasks/typus_tasks.rake +0 -2
  19. data/test/config/working/application.yml +9 -3
  20. data/test/config/working/application_roles.yml +3 -1
  21. data/test/fixtures/app/views/admin/{comments → posts}/_edit.html.erb +0 -0
  22. data/test/fixtures/app/views/admin/{comments → posts}/_index.html.erb +0 -0
  23. data/test/fixtures/app/views/admin/{comments → posts}/_new.html.erb +0 -0
  24. data/test/fixtures/app/views/admin/{comments → posts}/_show.html.erb +0 -0
  25. data/test/fixtures/app/views/admin/{comments → posts}/_sidebar.html.erb +0 -0
  26. data/test/functional/admin/{assets_controller_test.rb → master_controller_assets_relationships.rb} +23 -13
  27. data/test/functional/admin/master_controller_categories_lists_test.rb +64 -0
  28. data/test/functional/admin/master_controller_posts_before_test.rb +10 -0
  29. data/test/functional/admin/master_controller_posts_crud_test.rb +97 -0
  30. data/test/functional/admin/master_controller_posts_formats_test.rb +59 -0
  31. data/test/functional/admin/master_controller_posts_permissions_test.rb +127 -0
  32. data/test/functional/admin/master_controller_posts_relationships_test.rb +86 -0
  33. data/test/functional/admin/master_controller_posts_roles.rb +50 -0
  34. data/test/functional/admin/master_controller_posts_toggle_test.rb +35 -0
  35. data/test/functional/admin/master_controller_posts_views_test.rb +209 -0
  36. data/test/functional/admin/{status_controller_test.rb → master_controller_tableless_resource_test.rb} +5 -11
  37. data/test/functional/admin/{typus_users_controller_test.rb → master_controller_typus_users_test.rb} +10 -37
  38. data/test/functional/typus_controller_test.rb +154 -136
  39. data/test/helpers/admin/form_helper_test.rb +49 -27
  40. data/test/helpers/admin/sidebar_helper_test.rb +4 -7
  41. data/test/lib/active_record_test.rb +8 -8
  42. data/test/lib/typus_test.rb +1 -1
  43. data/test/models.rb +8 -1
  44. data/test/schema.rb +6 -0
  45. data/test/unit/typus_user_roles_test.rb +4 -4
  46. data/typus.gemspec +31 -21
  47. metadata +31 -21
  48. data/test/functional/admin/categories_controller_test.rb +0 -105
  49. data/test/functional/admin/comments_controller_test.rb +0 -121
  50. data/test/functional/admin/master_controller_test.rb +0 -51
  51. data/test/functional/admin/posts_controller_test.rb +0 -300
@@ -0,0 +1,127 @@
1
+ require 'test/helper'
2
+
3
+ class Admin::PostsControllerTest < ActionController::TestCase
4
+
5
+ def setup_for_no_root
6
+ @typus_user = typus_users(:editor)
7
+ @request.session[:typus_user_id] = @typus_user.id
8
+ assert !@typus_user.is_root?
9
+ end
10
+
11
+ ##
12
+ # Tests around what happens with records with typus_user_id when editing
13
+ # and showing.
14
+ ##
15
+
16
+ def test_should_verify_root_can_edit_any_record
17
+
18
+ assert @typus_user.is_root?
19
+
20
+ Post.find(:all).each do |post|
21
+ get :edit, { :id => post.id }
22
+ assert_response :success
23
+ assert_template 'edit'
24
+ end
25
+
26
+ end
27
+
28
+ def test_should_verify_editor_can_show_any_record
29
+
30
+ setup_for_no_root
31
+
32
+ Post.find(:all).each do |post|
33
+ get :show, { :id => post.id }
34
+ assert_response :success
35
+ assert_template 'show'
36
+ end
37
+
38
+ end
39
+
40
+ def test_should_verify_editor_can_not_edit_all_records
41
+
42
+ setup_for_no_root
43
+
44
+ # Editor tries to edit a post owned by hiself.
45
+ post = posts(:owned_by_editor)
46
+ get :edit, { :id => post.id }
47
+ assert_response :success
48
+
49
+ # Editor tries to edit a post owned by the admin.
50
+ @request.env['HTTP_REFERER'] = '/admin/posts'
51
+ post = posts(:owned_by_admin)
52
+ get :edit, { :id => post.id }
53
+ assert_response :redirect
54
+ assert_redirected_to @request.env['HTTP_REFERER']
55
+ assert_equal "You don't have permission to access this item.", flash[:notice]
56
+
57
+ # Editor tries to show a post owned by the admin.
58
+ post = posts(:owned_by_admin)
59
+ get :show, { :id => post.id }
60
+ assert_response :success
61
+ assert_template 'show'
62
+
63
+ # Editor tries to show a post owned by the admin.
64
+ options = Typus::Configuration.options.merge(:only_user_items => true)
65
+ Typus::Configuration.stubs(:options).returns(options)
66
+ post = posts(:owned_by_admin)
67
+ get :show, { :id => post.id }
68
+ assert_response :redirect
69
+ assert_redirected_to @request.env['HTTP_REFERER']
70
+
71
+ end
72
+
73
+ ##
74
+ # Tests around what happens with the typus_user_id when creating items.
75
+ ##
76
+
77
+ def test_should_verify_typus_user_id_of_item_when_creating_record
78
+
79
+ setup_for_no_root
80
+
81
+ post :create, { :item => { :title => "Chunky Bacon", :body => "Lorem ipsum ..." } }
82
+ post_ = Post.find_by_title("Chunky Bacon")
83
+
84
+ assert_equal @request.session[:typus_user_id], post_.typus_user_id
85
+
86
+ end
87
+
88
+ ##
89
+ # Tests around what happens with the typus_user_id when updating items.
90
+ ##
91
+
92
+ def test_should_verify_admin_updating_an_item_does_not_change_typus_user_id_if_not_defined
93
+
94
+ assert @typus_user.is_root?
95
+
96
+ post_ = posts(:owned_by_editor)
97
+ post :update, { :id => post_.id, :item => { :title => 'Updated by admin' } }
98
+ post_updated = Post.find(post_.id)
99
+ assert_equal post_.typus_user_id, post_updated.typus_user_id
100
+
101
+ end
102
+
103
+ def test_should_verify_admin_updating_an_item_does_change_typus_user_id_to_whatever_admin_wants
104
+
105
+ assert @typus_user.is_root?
106
+
107
+ post_ = posts(:owned_by_editor)
108
+ post :update, { :id => post_.id, :item => { :title => 'Updated', :typus_user_id => 108 } }
109
+ post_updated = Post.find(post_.id)
110
+ assert_equal 108, post_updated.typus_user_id
111
+
112
+ end
113
+
114
+ def test_should_verify_editor_updating_an_item_does_not_change_typus_user_id
115
+
116
+ setup_for_no_root
117
+
118
+ [ 108, nil ].each do |typus_user_id|
119
+ post_ = posts(:owned_by_editor)
120
+ post :update, { :id => post_.id, :item => { :title => 'Updated', :typus_user_id => typus_user_id } }
121
+ post_updated = Post.find(post_.id)
122
+ assert_equal @request.session[:typus_user_id], post_updated.typus_user_id
123
+ end
124
+
125
+ end
126
+
127
+ end
@@ -0,0 +1,86 @@
1
+ require 'test/helper'
2
+
3
+ class Admin::PostsControllerTest < ActionController::TestCase
4
+
5
+ ##
6
+ # Post => has_many :comments
7
+ ##
8
+
9
+ def test_should_relate_comment_with_post_and_then_unrelate
10
+
11
+ comment = comments(:without_post_id)
12
+ post_ = posts(:published)
13
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#comments"
14
+
15
+ assert_difference('post_.comments.count') do
16
+ post :relate, { :id => post_.id,
17
+ :related => { :model => 'Comment', :id => comment.id } }
18
+ end
19
+
20
+ assert_response :redirect
21
+ assert_redirected_to @request.env['HTTP_REFERER']
22
+ assert_equal "Comment related to Post.", flash[:success]
23
+
24
+ assert_difference('post_.comments.count', -1) do
25
+ post :unrelate, { :id => post_.id,
26
+ :resource => 'Comment', :resource_id => comment.id }
27
+ end
28
+
29
+ assert_response :redirect
30
+ assert_redirected_to @request.env['HTTP_REFERER']
31
+ assert_equal "Comment unrelated from Post.", flash[:success]
32
+
33
+ end
34
+
35
+ ##
36
+ # Post => has_and_belongs_to_many :categories
37
+ ##
38
+
39
+ def test_should_relate_category_with_post_and_then_unrelate
40
+
41
+ category = categories(:first)
42
+ post_ = posts(:published)
43
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#categories"
44
+
45
+ assert_difference('category.posts.count') do
46
+ post :relate, { :id => post_.id,
47
+ :related => { :model => 'Category', :id => category.id } }
48
+ end
49
+
50
+ assert_response :redirect
51
+ assert_redirected_to @request.env['HTTP_REFERER']
52
+ assert_equal "Category related to Post.", flash[:success]
53
+
54
+ assert_difference('category.posts.count', -1) do
55
+ post :unrelate, { :id => post_.id,
56
+ :resource => 'Category', :resource_id => category.id }
57
+ end
58
+
59
+ assert_response :redirect
60
+ assert_redirected_to @request.env['HTTP_REFERER']
61
+ assert_equal "Category unrelated from Post.", flash[:success]
62
+
63
+ end
64
+
65
+ ##
66
+ # Post => has_many :assets, :as => resource (Polimorphic)
67
+ ##
68
+
69
+ def test_should_relate_asset_with_post_and_then_unrelate
70
+
71
+ post_ = posts(:published)
72
+
73
+ @request.env['HTTP_REFERER'] = "/admin/posts/edit/#{post_.id}#assets"
74
+
75
+ assert_difference('post_.assets.count', -1) do
76
+ get :unrelate, { :id => post_.id,
77
+ :resource => 'Asset', :resource_id => post_.assets.first.id }
78
+ end
79
+
80
+ assert_response :redirect
81
+ assert_redirected_to @request.env['HTTP_REFERER']
82
+ assert_equal "Asset unrelated from Post.", flash[:success]
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,50 @@
1
+ require 'test/helper'
2
+
3
+ class Admin::PostsControllerTest < ActionController::TestCase
4
+
5
+ def test_should_allow_admin_to_add_a_category
6
+ admin = typus_users(:admin)
7
+ @request.session[:typus_user_id] = admin.id
8
+ assert admin.can_perform?('Post', 'create')
9
+ end
10
+
11
+ def test_should_not_allow_designer_to_add_a_post
12
+
13
+ designer = typus_users(:designer)
14
+ @request.session[:typus_user_id] = designer.id
15
+
16
+ get :new
17
+
18
+ assert_response :redirect
19
+ assert_equal "Designer can't perform action. (new)", flash[:notice]
20
+ assert_redirected_to :action => :index
21
+
22
+ end
23
+
24
+ def test_should_allow_admin_to_destroy_a_post
25
+
26
+ admin = typus_users(:admin)
27
+ @request.session[:typus_user_id] = admin.id
28
+
29
+ get :destroy, { :id => posts(:published).id, :method => :delete }
30
+
31
+ assert_response :redirect
32
+ assert_equal "Post successfully removed.", flash[:success]
33
+ assert_redirected_to :action => :index
34
+
35
+ end
36
+
37
+ def test_should_not_allow_designer_to_destroy_a_post
38
+
39
+ designer = typus_users(:designer)
40
+ @request.session[:typus_user_id] = designer.id
41
+
42
+ get :destroy, { :id => posts(:published).id, :method => :delete }
43
+
44
+ assert_response :redirect
45
+ assert_equal "Designer can't delete this item.", flash[:notice]
46
+ assert_redirected_to :action => :index
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,35 @@
1
+ require 'test/helper'
2
+
3
+ class Admin::PostsControllerTest < ActionController::TestCase
4
+
5
+ def test_should_toggle_an_item
6
+
7
+ @request.env['HTTP_REFERER'] = '/admin/posts'
8
+
9
+ post = posts(:unpublished)
10
+ get :toggle, { :id => post.id, :field => 'status' }
11
+
12
+ assert_response :redirect
13
+ assert_redirected_to @request.env['HTTP_REFERER']
14
+ assert_equal "Post status changed.", flash[:success]
15
+ assert Post.find(post.id).status
16
+
17
+ end
18
+
19
+ def test_should_not_toggle_an_item_when_disabled
20
+
21
+ @request.env['HTTP_REFERER'] = '/admin/posts'
22
+
23
+ options = Typus::Configuration.options.merge(:toggle => false)
24
+ Typus::Configuration.stubs(:options).returns(options)
25
+
26
+ post = posts(:unpublished)
27
+ get :toggle, { :id => post.id, :field => 'status' }
28
+
29
+ assert_response :redirect
30
+ assert_redirected_to @request.env['HTTP_REFERER']
31
+ assert_equal "Toggle is disabled.", flash[:notice]
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,209 @@
1
+ require 'test/helper'
2
+
3
+ class Admin::PostsControllerTest < ActionController::TestCase
4
+
5
+ ##
6
+ # get :index
7
+ ##
8
+
9
+ def test_should_render_index_and_verify_presence_of_custom_partials
10
+ get :index
11
+ partials = %w( _index.html.erb _sidebar.html.erb )
12
+ partials.each { |p| assert_match p, @response.body }
13
+ end
14
+
15
+ def test_should_render_index_and_verify_page_title
16
+ get :index
17
+ assert_select 'title', "#{Typus::Configuration.options[:app_name]} - Posts"
18
+ end
19
+
20
+ def test_should_render_index_and_show_add_entry_link
21
+
22
+ get :index
23
+
24
+ assert_select "#sidebar ul" do
25
+ assert_select "li", "Add entry"
26
+ end
27
+
28
+ end
29
+
30
+ def test_should_render_index_and_not_show_add_entry_link
31
+
32
+ typus_user = typus_users(:designer)
33
+ @request.session[:typus_user_id] = typus_user.id
34
+
35
+ get :index
36
+ assert_response :success
37
+
38
+ assert_no_match /Add entry/, @response.body
39
+
40
+ end
41
+
42
+ def test_should_render_index_and_show_trash_item_image
43
+ get :index
44
+ assert_response :success
45
+ assert_select '.trash', 'Trash'
46
+ end
47
+
48
+ def test_should_render_index_and_not_show_trash_image
49
+
50
+ typus_user = typus_users(:designer)
51
+ @request.session[:typus_user_id] = typus_user.id
52
+
53
+ get :index
54
+ assert_response :success
55
+ assert_select '.trash', false
56
+
57
+ end
58
+
59
+ def test_should_get_index_and_render_edit_or_show_links
60
+
61
+ %w( edit show ).each do |action|
62
+
63
+ options = Typus::Configuration.options.merge(:default_action_on_item => action)
64
+ Typus::Configuration.stubs(:options).returns(options)
65
+
66
+ get :index
67
+
68
+ Post.find(:all).each do |post|
69
+ assert_match "/posts/#{action}/#{post.id}", @response.body
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ def test_should_get_index_and_render_edit_or_show_links_on_owned_records
77
+
78
+ typus_user = typus_users(:editor)
79
+ @request.session[:typus_user_id] = typus_user.id
80
+
81
+ get :index
82
+
83
+ Post.find(:all).each do |post|
84
+ action = post.owned_by?(typus_user) ? 'edit' : 'show'
85
+ assert_match "/posts/#{action}/#{post.id}", @response.body
86
+ end
87
+
88
+ end
89
+
90
+ def test_should_get_index_and_render_edit_or_show_on_only_user_items
91
+
92
+ typus_user = typus_users(:editor)
93
+ @request.session[:typus_user_id] = typus_user.id
94
+
95
+ %w( edit show ).each do |action|
96
+
97
+ options = Typus::Configuration.options.merge(:only_user_items => true,
98
+ :default_action_on_item => action)
99
+ Typus::Configuration.stubs(:options).returns(options)
100
+
101
+ get :index
102
+
103
+ Post.find(:all).each do |post|
104
+ if post.owned_by?(typus_user)
105
+ assert_match "/posts/#{action}/#{post.id}", @response.body
106
+ else
107
+ assert_no_match /\/posts\/#{action}\/#{post.id}/, @response.body
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ ##
116
+ # get :new
117
+ ##
118
+
119
+ def test_should_render_posts_partials_on_new
120
+ get :new
121
+ partials = %w( _new.html.erb _sidebar.html.erb )
122
+ partials.each { |p| assert_match p, @response.body }
123
+ end
124
+
125
+ def test_should_render_new_and_verify_page_title
126
+ get :new
127
+ assert_select 'title', "#{Typus::Configuration.options[:app_name]} - Posts &rsaquo; New"
128
+ end
129
+
130
+ ##
131
+ # get :edit
132
+ ##
133
+
134
+ def test_should_render_edit_and_verify_presence_of_custom_partials
135
+ get :edit, { :id => posts(:published).id }
136
+ partials = %w( _edit.html.erb _sidebar.html.erb )
137
+ partials.each { |p| assert_match p, @response.body }
138
+ end
139
+
140
+ def test_should_render_edit_and_verify_page_title
141
+ get :edit, { :id => posts(:published).id }
142
+ assert_select 'title', "#{Typus::Configuration.options[:app_name]} - Posts &rsaquo; Edit"
143
+ end
144
+
145
+ ##
146
+ # get :show
147
+ ##
148
+
149
+ def test_should_render_show_and_verify_presence_of_custom_partials
150
+ get :show, { :id => posts(:published).id }
151
+ partials = %w( _show.html.erb _sidebar.html.erb )
152
+ partials.each { |p| assert_match p, @response.body }
153
+ end
154
+
155
+ def test_should_render_show_and_verify_page_title
156
+ get :show, { :id => posts(:published).id }
157
+ assert_select 'title', "#{Typus::Configuration.options[:app_name]} - Posts &rsaquo; Show"
158
+ end
159
+
160
+ def test_should_render_show_and_verify_add_relationships_links
161
+
162
+ ##
163
+ # Admin
164
+ ##
165
+
166
+ [ posts(:owned_by_admin), posts(:owned_by_editor) ].each do |post|
167
+
168
+ get :show, { :id => post.id }
169
+
170
+ %w( assets categories comments views ).each do |model|
171
+ assert_select "div##{model} h2", "#{model.capitalize}\n Add new"
172
+ end
173
+
174
+ end
175
+
176
+ ##
177
+ # Editor
178
+ ##
179
+
180
+ typus_user = typus_users(:editor)
181
+ @request.session[:typus_user_id] = typus_user.id
182
+
183
+ get :show, { :id => posts(:owned_by_admin).id }
184
+
185
+ # This is a has_many relationship, and record is owned by admin, so the
186
+ # editor can only list. Assets it's not shown because the editor doesn't
187
+ # have access to this resource.
188
+ assert_select 'div#assets h2', false
189
+ assert_select 'div#categories h2', "Categories"
190
+ assert_select 'div#comments h2', "Comments"
191
+ assert_select 'div#views h2', "Views"
192
+
193
+ get :show, { :id => posts(:owned_by_editor).id }
194
+
195
+ # This is a has_many (polimorphic) relationship, but editor can't add new items.
196
+ assert_select 'div#assets h2', false
197
+ # This is a has_and_belongs_to_many relationship and editor can add new items.
198
+ assert_select 'div#categories h2', "Categories\n Add new"
199
+ # This is a has_many relationship, but editor can't add items.
200
+ assert_select 'div#comments h2', "Comments"
201
+ # This is a has_many relationship and editor can add items.
202
+ assert_select 'div#views h2', "Views\n Add new"
203
+
204
+ expected = "/admin/views/new?back_to=%2Fadmin%2Fposts%2Fshow%2F4%23views&amp;post_id=4&amp;resource=post&amp;resource_id=4"
205
+ assert_match expected, @response.body
206
+
207
+ end
208
+
209
+ end