comfortable_mexican_sofa 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/Gemfile +1 -1
  2. data/Gemfile.lock +1 -1
  3. data/README.md +83 -1
  4. data/VERSION +1 -1
  5. data/app/controllers/cms_admin/base_controller.rb +15 -0
  6. data/app/controllers/cms_admin/layouts_controller.rb +3 -3
  7. data/app/controllers/cms_admin/pages_controller.rb +8 -8
  8. data/app/controllers/cms_admin/sites_controller.rb +58 -0
  9. data/app/controllers/cms_admin/snippets_controller.rb +18 -14
  10. data/app/controllers/cms_admin/uploads_controller.rb +7 -5
  11. data/app/controllers/cms_content_controller.rb +17 -5
  12. data/app/models/cms_layout.rb +7 -7
  13. data/app/models/cms_page.rb +18 -4
  14. data/app/models/cms_site.rb +23 -0
  15. data/app/models/cms_snippet.rb +17 -12
  16. data/app/models/cms_upload.rb +4 -7
  17. data/app/views/cms_admin/layouts/_form.html.erb +1 -1
  18. data/app/views/cms_admin/layouts/index.html.erb +1 -1
  19. data/app/views/cms_admin/pages/_form.html.erb +3 -2
  20. data/app/views/cms_admin/pages/_form_blocks.html.erb +1 -1
  21. data/app/views/cms_admin/pages/_index_branch.html.erb +1 -1
  22. data/app/views/cms_admin/pages/index.html.erb +1 -1
  23. data/app/views/cms_admin/sites/_form.html.erb +2 -0
  24. data/app/views/cms_admin/sites/edit.html.erb +6 -0
  25. data/app/views/cms_admin/sites/index.html.erb +22 -0
  26. data/app/views/cms_admin/sites/new.html.erb +6 -0
  27. data/app/views/cms_admin/snippets/_form.html.erb +2 -1
  28. data/app/views/cms_admin/snippets/index.html.erb +20 -1
  29. data/app/views/layouts/cms_admin.html.erb +2 -1
  30. data/comfortable_mexican_sofa.gemspec +31 -9
  31. data/config/initializers/comfortable_mexican_sofa.rb +10 -0
  32. data/config/routes.rb +3 -1
  33. data/db/migrate/01_create_cms.rb +18 -4
  34. data/lib/comfortable_mexican_sofa.rb +28 -22
  35. data/lib/comfortable_mexican_sofa/acts_as_tree.rb +97 -0
  36. data/lib/comfortable_mexican_sofa/cms_tag/snippet.rb +4 -0
  37. data/lib/comfortable_mexican_sofa/configuration.rb +19 -0
  38. data/lib/comfortable_mexican_sofa/controller_methods.rb +41 -0
  39. data/lib/comfortable_mexican_sofa/{cms_engine.rb → engine.rb} +1 -1
  40. data/lib/comfortable_mexican_sofa/{cms_form_builder.rb → form_builder.rb} +1 -1
  41. data/lib/comfortable_mexican_sofa/http_auth.rb +17 -0
  42. data/lib/comfortable_mexican_sofa/rails_extensions.rb +11 -0
  43. data/lib/comfortable_mexican_sofa/view_methods.rb +33 -0
  44. data/lib/generators/cms_generator.rb +4 -0
  45. data/public/stylesheets/comfortable_mexican_sofa/structure.css +10 -12
  46. data/test/fixtures/cms_layouts.yml +3 -1
  47. data/test/fixtures/cms_pages.yml +6 -2
  48. data/test/fixtures/cms_sites.yml +3 -0
  49. data/test/fixtures/cms_snippets.yml +3 -1
  50. data/test/fixtures/cms_uploads.yml +1 -0
  51. data/test/functional/cms_admin/layouts_controller_test.rb +3 -1
  52. data/test/functional/cms_admin/pages_controller_test.rb +4 -2
  53. data/test/functional/cms_admin/sites_controller_test.rb +92 -0
  54. data/test/functional/cms_admin/snippets_controller_test.rb +62 -37
  55. data/test/functional/cms_content_controller_test.rb +32 -5
  56. data/test/integration/authentication_test.rb +27 -0
  57. data/test/integration/render_cms_test.rb +57 -0
  58. data/test/integration/sites_test.rb +30 -0
  59. data/test/test_helper.rb +46 -3
  60. data/test/unit/cms_block_test.rb +1 -0
  61. data/test/unit/cms_configuration_test.rb +16 -0
  62. data/test/unit/cms_layout_test.rb +3 -3
  63. data/test/unit/cms_page_test.rb +23 -13
  64. data/test/unit/cms_site_test.rb +41 -0
  65. data/test/unit/cms_snippet_test.rb +1 -1
  66. data/test/unit/cms_tags/snippet_test.rb +1 -1
  67. data/test/unit/cms_upload_test.rb +7 -6
  68. metadata +32 -10
  69. data/lib/comfortable_mexican_sofa/cms_acts_as_tree.rb +0 -101
  70. data/lib/comfortable_mexican_sofa/cms_rails_extensions.rb +0 -32
  71. data/test/functional/cms_admin/base_controller_test.rb +0 -9
@@ -1,4 +1,5 @@
1
1
  default:
2
+ cms_site: default
2
3
  file_file_name: sample.jpg
3
4
  file_content_type: image/jpeg
4
5
  file_file_size: 20099
@@ -40,7 +40,9 @@ class CmsAdmin::LayoutsControllerTest < ActionController::TestCase
40
40
  :content => 'Test Content'
41
41
  }
42
42
  assert_response :redirect
43
- assert_redirected_to :action => :edit, :id => CmsLayout.last
43
+ layout = CmsLayout.last
44
+ assert_equal cms_sites(:default), layout.cms_site
45
+ assert_redirected_to :action => :edit, :id => layout
44
46
  assert_equal 'Layout created', flash[:notice]
45
47
  end
46
48
  end
@@ -113,7 +113,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
113
113
  get :edit, :id => page
114
114
  assert_response :success
115
115
  assert assigns(:cms_page)
116
- assert_equal cms_layouts(:default), assigns(:cms_page).cms_layout
116
+ assert assigns(:cms_page).cms_layout
117
117
  end
118
118
 
119
119
  def test_creation
@@ -137,7 +137,9 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
137
137
  ]
138
138
  }
139
139
  assert_response :redirect
140
- assert_redirected_to :action => :edit, :id => CmsPage.last
140
+ page = CmsPage.last
141
+ assert_equal cms_sites(:default), page.cms_site
142
+ assert_redirected_to :action => :edit, :id => page
141
143
  assert_equal 'Page saved', flash[:notice]
142
144
  end
143
145
  end
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class CmsAdmin::SitesControllerTest < ActionController::TestCase
4
+
5
+ def test_get_index
6
+ get :index
7
+ assert_response :success
8
+ assert assigns(:cms_sites)
9
+ assert_template :index
10
+ end
11
+
12
+ def test_get_new
13
+ get :new
14
+ assert_response :success
15
+ assert assigns(:cms_site)
16
+ assert_equal 'test.host', assigns(:cms_site).hostname
17
+ assert_template :new
18
+ assert_select 'form[action=/cms-admin/sites]'
19
+ end
20
+
21
+ def test_get_edit
22
+ site = cms_sites(:default)
23
+ get :edit, :id => site
24
+ assert_response :success
25
+ assert assigns(:cms_site)
26
+ assert_template :edit
27
+ assert_select "form[action=/cms-admin/sites/#{site.id}]"
28
+ end
29
+
30
+ def test_get_edit_failure
31
+ get :edit, :id => 'not_found'
32
+ assert_response :redirect
33
+ assert_redirected_to :action => :index
34
+ assert_equal 'Site not found', flash[:error]
35
+ end
36
+
37
+ def test_creation
38
+ assert_difference 'CmsSite.count' do
39
+ post :create, :cms_site => {
40
+ :label => 'Test Site',
41
+ :hostname => 'test.site.local'
42
+ }
43
+ assert_response :redirect
44
+ assert_redirected_to :action => :edit, :id => CmsSite.last
45
+ assert_equal 'Site created', flash[:notice]
46
+ end
47
+ end
48
+
49
+ def test_creation_failure
50
+ assert_no_difference 'CmsSite.count' do
51
+ post :create, :cms_site => { }
52
+ assert_response :success
53
+ assert_template :new
54
+ assert_equal 'Failed to create site', flash[:error]
55
+ end
56
+ end
57
+
58
+ def test_update
59
+ site = cms_sites(:default)
60
+ put :update, :id => site, :cms_site => {
61
+ :label => 'New Site',
62
+ :hostname => 'new.site.local'
63
+ }
64
+ assert_response :redirect
65
+ assert_redirected_to :action => :edit, :id => site
66
+ assert_equal 'Site updated', flash[:notice]
67
+ site.reload
68
+ assert_equal 'New Site', site.label
69
+ assert_equal 'new.site.local', site.hostname
70
+ end
71
+
72
+ def test_update_failure
73
+ site = cms_sites(:default)
74
+ put :update, :id => site, :cms_site => {
75
+ :label => ''
76
+ }
77
+ assert_response :success
78
+ assert_template :edit
79
+ site.reload
80
+ assert_not_equal '', site.label
81
+ assert_equal 'Failed to update site', flash[:error]
82
+ end
83
+
84
+ def test_destroy
85
+ assert_difference 'CmsSite.count', -1 do
86
+ delete :destroy, :id => cms_sites(:default)
87
+ assert_response :redirect
88
+ assert_redirected_to :action => :index
89
+ assert_equal 'Site deleted', flash[:notice]
90
+ end
91
+ end
92
+ end
@@ -2,69 +2,94 @@ require File.dirname(__FILE__) + '/../../test_helper'
2
2
 
3
3
  class CmsAdmin::SnippetsControllerTest < ActionController::TestCase
4
4
 
5
- def test_index
5
+ def test_get_index
6
6
  get :index
7
7
  assert_response :success
8
- assert_template 'index'
8
+ assert assigns(:cms_snippets)
9
+ assert_template :index
9
10
  end
10
11
 
11
- def test_new
12
+ def test_get_new
12
13
  get :new
13
14
  assert_response :success
14
- assert_template 'new'
15
+ assert assigns(:cms_snippet)
16
+ assert_template :new
17
+ assert_select 'form[action=/cms-admin/snippets]'
15
18
  end
16
19
 
17
- def test_create
18
- assert_difference 'CmsSnippet.count', 1 do
19
- post :create, :cms_snippet => cms_snippet_params
20
- end
21
- assert_equal 'Snippet saved', flash[:notice]
22
- assert_redirected_to edit_cms_admin_snippet_path(assigns(:cms_snippet))
20
+ def test_get_edit
21
+ snippet = cms_snippets(:default)
22
+ get :edit, :id => snippet
23
+ assert_response :success
24
+ assert assigns(:cms_snippet)
25
+ assert_template :edit
26
+ assert_select "form[action=/cms-admin/snippets/#{snippet.id}]"
23
27
  end
24
28
 
25
- def test_create_fails
26
- assert_no_difference 'CmsSnippet.count' do
27
- post :create, :cms_snippet => cms_snippet_params(:label => '')
29
+ def test_get_edit_failure
30
+ get :edit, :id => 'not_found'
31
+ assert_response :redirect
32
+ assert_redirected_to :action => :index
33
+ assert_equal 'Snippet not found', flash[:error]
34
+ end
35
+
36
+ def test_creation
37
+ assert_difference 'CmsSnippet.count' do
38
+ post :create, :cms_snippet => {
39
+ :label => 'Test Snippet',
40
+ :slug => 'test-snippet',
41
+ :content => 'Test Content'
42
+ }
43
+ assert_response :redirect
44
+ snippet = CmsSnippet.last
45
+ assert_equal cms_sites(:default), snippet.cms_site
46
+ assert_redirected_to :action => :edit, :id => snippet
47
+ assert_equal 'Snippet created', flash[:notice]
28
48
  end
29
- assert_response :success
30
- assert_template 'new'
31
49
  end
32
50
 
33
- def test_edit
34
- get :edit, :id => cms_snippets(:default)
35
- assert_response :success
36
- assert_template 'edit'
51
+ def test_creation_failure
52
+ assert_no_difference 'CmsSnippet.count' do
53
+ post :create, :cms_snippet => { }
54
+ assert_response :success
55
+ assert_template :new
56
+ assert_equal 'Failed to create snippet', flash[:error]
57
+ end
37
58
  end
38
59
 
39
60
  def test_update
40
61
  snippet = cms_snippets(:default)
41
- put :update, :id => snippet, :cms_snippet => {:label => 'new-label'}
42
- assert_equal 'Snippet saved', flash[:notice]
43
- assert_redirected_to edit_cms_admin_snippet_path(assigns(:cms_snippet))
44
- assert_equal 'new-label', assigns(:cms_snippet).label
62
+ put :update, :id => snippet, :cms_snippet => {
63
+ :label => 'New-Snippet',
64
+ :content => 'New Content'
65
+ }
66
+ assert_response :redirect
67
+ assert_redirected_to :action => :edit, :id => snippet
68
+ assert_equal 'Snippet updated', flash[:notice]
69
+ snippet.reload
70
+ assert_equal 'New-Snippet', snippet.label
71
+ assert_equal 'New Content', snippet.content
45
72
  end
46
73
 
47
- def test_update_fails
74
+ def test_update_failure
48
75
  snippet = cms_snippets(:default)
49
- put :update, :id => snippet, :cms_snippet => {:label => ''}
76
+ put :update, :id => snippet, :cms_snippet => {
77
+ :label => ''
78
+ }
50
79
  assert_response :success
51
- assert_template 'edit'
52
- assert_equal 'default', snippet.label
80
+ assert_template :edit
81
+ snippet.reload
82
+ assert_not_equal '', snippet.label
83
+ assert_equal 'Failed to update snippet', flash[:error]
53
84
  end
54
85
 
55
86
  def test_destroy
56
87
  assert_difference 'CmsSnippet.count', -1 do
57
88
  delete :destroy, :id => cms_snippets(:default)
89
+ assert_response :redirect
90
+ assert_redirected_to :action => :index
91
+ assert_equal 'Snippet deleted', flash[:notice]
58
92
  end
59
- assert_equal 'Snippet deleted', flash[:notice]
60
- assert_redirected_to cms_admin_snippets_path
61
- end
62
-
63
- protected
64
- def cms_snippet_params(options = {})
65
- {
66
- :label => 'snippet-label',
67
- :content => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit'
68
- }.merge(options)
69
93
  end
94
+
70
95
  end
@@ -15,7 +15,7 @@ class CmsContentControllerTest < ActionController::TestCase
15
15
  layout_content_b
16
16
  default_snippet_content
17
17
  layout_content_c'
18
- ), @response.body
18
+ ), response.body
19
19
  end
20
20
 
21
21
  def test_render_page_with_app_layout
@@ -28,13 +28,40 @@ class CmsContentControllerTest < ActionController::TestCase
28
28
  def test_render_page_not_found
29
29
  get :render_html, :cms_path => 'doesnotexist'
30
30
  assert_response 404
31
+ assert_equal 'Page Not Found', response.body
32
+ end
33
+
34
+ def test_render_page_not_found_with_custom_404
35
+ cms_sites(:default).cms_pages.create!(
36
+ :label => '404',
37
+ :slug => '404',
38
+ :parent_id => cms_pages(:default).id,
39
+ :cms_layout_id => cms_layouts(:default).id,
40
+ :cms_blocks_attributes => [
41
+ { :label => 'default_page_text',
42
+ :type => 'CmsTag::PageText',
43
+ :content => 'custom 404 page content' }
44
+ ]
45
+ )
46
+ get :render_html, :cms_path => 'doesnotexist'
47
+ assert_response 404
48
+ assert assigns(:cms_page)
49
+ assert_match /custom 404 page content/, response.body
50
+ end
51
+
52
+ def test_render_page_with_redirect
53
+ cms_pages(:child).update_attribute(:target_page, cms_pages(:default))
54
+ assert_equal cms_pages(:default), cms_pages(:child).target_page
55
+ get :render_html, :cms_path => 'child-page'
56
+ assert_response :redirect
57
+ assert_redirected_to '/'
31
58
  end
32
59
 
33
60
  def test_render_css
34
61
  get :render_css, :id => cms_layouts(:default)
35
62
  assert_response :success
36
- assert_match %r{text\/css}, @response.headers["Content-Type"]
37
- assert_equal cms_layouts(:default).css, @response.body
63
+ assert_match %r{text\/css}, response.headers["Content-Type"]
64
+ assert_equal cms_layouts(:default).css, response.body
38
65
  end
39
66
 
40
67
  def test_render_css_not_found
@@ -45,8 +72,8 @@ class CmsContentControllerTest < ActionController::TestCase
45
72
  def test_render_js
46
73
  get :render_js, :id => cms_layouts(:default)
47
74
  assert_response :success
48
- assert_match %r{text\/javascript}, @response.headers["Content-Type"]
49
- assert_equal cms_layouts(:default).js, @response.body
75
+ assert_match %r{text\/javascript}, response.headers["Content-Type"]
76
+ assert_equal cms_layouts(:default).js, response.body
50
77
  end
51
78
 
52
79
  def test_render_js_not_found
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class AuthenticationTest < ActionDispatch::IntegrationTest
4
+
5
+ def test_get_with_unauthorized_access
6
+ assert_equal 'ComfortableMexicanSofa::HttpAuth', ComfortableMexicanSofa.config.authentication
7
+ get '/cms-admin/pages'
8
+ assert_response :unauthorized
9
+ get '/'
10
+ assert_response :success
11
+ end
12
+
13
+ def test_get_with_authorized_access
14
+ http_auth :get, '/cms-admin/pages'
15
+ assert_response :success
16
+ end
17
+
18
+ def test_get_with_changed_default_config
19
+ assert_equal 'ComfortableMexicanSofa::HttpAuth', ComfortableMexicanSofa.config.authentication
20
+ ComfortableMexicanSofa::HttpAuth.username = 'newuser'
21
+ ComfortableMexicanSofa::HttpAuth.password = 'newpass'
22
+ http_auth :get, '/cms-admin/pages'
23
+ assert_response :unauthorized
24
+ http_auth :get, '/cms-admin/pages', {}, 'newuser', 'newpass'
25
+ assert_response :success
26
+ end
27
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RenderCmsTest < ActionDispatch::IntegrationTest
4
+
5
+ def setup
6
+ Rails.application.routes.draw do
7
+ get '/render-implicit' => 'render_test#implicit'
8
+ get '/render-explicit' => 'render_test#explicit'
9
+ end
10
+ super
11
+ end
12
+
13
+ def teardown
14
+ load(File.expand_path('config/routes.rb', Rails.root))
15
+ end
16
+
17
+ class ::RenderTestController < ApplicationController
18
+ def implicit
19
+ render
20
+ end
21
+ def explicit
22
+ render :cms_page => '/render-explicit-page'
23
+ end
24
+ end
25
+
26
+ def test_get_with_no_template
27
+ assert_exception_raised ActionView::MissingTemplate do
28
+ get '/render-implicit'
29
+ end
30
+ end
31
+
32
+ def test_get_with_implicit_cms_template
33
+ page = cms_pages(:child)
34
+ page.slug = 'render-implicit'
35
+ page.save!
36
+ get '/render-implicit'
37
+ assert_response :success
38
+ end
39
+
40
+ def test_get_with_explicit_cms_template
41
+ page = cms_pages(:child)
42
+ page.slug = 'render-explicit-page'
43
+ page.save!
44
+ get '/render-explicit'
45
+ assert_response :success
46
+ end
47
+
48
+ def test_get_with_explicit_cms_template_failure
49
+ page = cms_pages(:child)
50
+ page.slug = 'render-explicit-404'
51
+ page.save!
52
+ assert_exception_raised ActionView::MissingTemplate do
53
+ get '/render-explicit'
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class SitesTest < ActionDispatch::IntegrationTest
4
+
5
+ def test_get_admin_pages_index
6
+ http_auth :get, cms_admin_pages_path
7
+ assert_response :success
8
+ end
9
+
10
+ def test_get_admin_pages_index_with_no_site
11
+ CmsSite.delete_all
12
+ http_auth :get, cms_admin_pages_path
13
+ assert_response :redirect
14
+ assert_redirected_to new_cms_admin_site_path
15
+ end
16
+
17
+ def test_get_admin_sites_index_with_no_site
18
+ CmsSite.delete_all
19
+ http_auth :get, cms_admin_sites_path
20
+ assert_response :success
21
+ end
22
+
23
+ def test_get_public_page_for_non_existent_site
24
+ host! 'bogus.host'
25
+ get '/'
26
+ assert_response 404
27
+ assert_equal 'Site Not Found', response.body
28
+ end
29
+
30
+ end
data/test/test_helper.rb CHANGED
@@ -6,6 +6,16 @@ class ActiveSupport::TestCase
6
6
  fixtures :all
7
7
  include ActionDispatch::TestProcess
8
8
 
9
+ def setup
10
+ # resetting default configuration
11
+ ComfortableMexicanSofa.configure do |config|
12
+ config.cms_title = 'ComfortableMexicanSofa'
13
+ config.authentication = 'ComfortableMexicanSofa::HttpAuth'
14
+ end
15
+ ComfortableMexicanSofa::HttpAuth.username = 'username'
16
+ ComfortableMexicanSofa::HttpAuth.password = 'password'
17
+ end
18
+
9
19
  # Example usage:
10
20
  # assert_has_errors_on( @record, [:field_1, :field_2] )
11
21
  # assert_has_errors_on( @record, {:field_1 => 'Message1', :field_2 => 'Message 2'} )
@@ -21,17 +31,50 @@ class ActiveSupport::TestCase
21
31
  end
22
32
  end
23
33
 
34
+ # Example usage:
35
+ # assert_exception_raised do ... end
36
+ # assert_exception_raised ActiveRecord::RecordInvalid do ... end
37
+ # assert_exception_raised Plugin::Error, 'error_message' do ... end
38
+ def assert_exception_raised(exception_class = nil, error_message = nil, &block)
39
+ exception_raised = nil
40
+ yield
41
+ rescue => exception_raised
42
+ ensure
43
+ if exception_raised
44
+ if exception_class
45
+ assert_equal exception_class, exception_raised.class, exception_raised.to_s
46
+ else
47
+ assert true
48
+ end
49
+ assert_equal error_message, exception_raised.to_s if error_message
50
+ else
51
+ flunk 'Exception was not raised'
52
+ end
53
+ end
54
+
24
55
  # Small method that allows for better formatting in tests
25
56
  def rendered_content_formatter(string)
26
57
  string.gsub(/^[ ]+/, '')
27
58
  end
28
-
29
59
  end
30
60
 
31
61
  class ActionController::TestCase
32
-
33
- def http_auth
62
+ def setup
34
63
  @request.env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64('username:password')}"
35
64
  end
65
+ end
66
+
67
+ class ActionDispatch::IntegrationTest
36
68
 
69
+ def setup
70
+ host! 'test.host'
71
+ ComfortableMexicanSofa::HttpAuth.username = 'username'
72
+ ComfortableMexicanSofa::HttpAuth.password = 'password'
73
+ end
74
+
75
+ # Attaching http_auth stuff with request. Example use:
76
+ # http_auth :get, '/cms-admin/pages'
77
+ def http_auth(method, path, options = {}, username = 'username', password = 'password')
78
+ send(method, path, options, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64(username + ':' + password)}"})
79
+ end
37
80
  end