gotta make that site firsts

Oleg committed Oct 15, 2010
commit 1363576caca3d6022e27797f44ea5c8d2757bdd1
Showing 6 changed files with 52 additions and 4 deletions
app/controllers/cms_admin/base_controller.rb +11 -1
@@ @@ -3,8 +3,18 @@ include ComfortableMexicanSofa.config.authentication.to_s.constantize
class CmsAdmin::BaseController < ApplicationController
- before_filter :authenticate
+ before_filter :authenticate,
+ :load_admin_cms_site
layout 'cms_admin'
+ protected
+
+ def load_admin_cms_site
+ @cms_site = CmsSite.find_by_hostname!(request.host.downcase)
+ rescue ActiveRecord::RecordNotFound
+ flash[:error] = 'No Site defined for this hostname. Create it now.'
+ redirect_to new_cms_admin_site_path
+ end
+
end
app/controllers/cms_admin/sites_controller.rb +3 -0
@@ @@ -1,5 +1,7 @@
class CmsAdmin::SitesController < CmsAdmin::BaseController
+ skip_before_filter :load_admin_cms_site
+
before_filter :build_cms_site, :only => [:new, :create]
before_filter :load_cms_site, :only => [:edit, :update, :destroy]
@@ @@ -43,6 +45,7 @@ protected
def build_cms_site
@cms_site = CmsSite.new(params[:cms_site])
+ @cms_site.hostname ||= request.host.downcase
end
def load_cms_site
test/functional/cms_admin/sites_controller_test.rb +1 -0
@@ @@ -13,6 +13,7 @@ class CmsAdmin::SitesControllerTest < ActionController::TestCase
get :new
assert_response :success
assert assigns(:cms_site)
+ assert_equal 'test.host', assigns(:cms_site).hostname
assert_template :new
assert_select 'form[action=/cms-admin/sites]'
end
test/integration/admin_sites_test.rb +23 -0
@@ @@ -0,0 +1,23 @@
+ require File.dirname(__FILE__) + '/../test_helper'
+
+ class AdminSitesTest < ActionDispatch::IntegrationTest
+
+ def test_get_pages_index
+ http_auth :get, cms_admin_pages_path
+ assert_response :success
+ end
+
+ def test_get_pages_index_with_no_site
+ CmsSite.delete_all
+ http_auth :get, cms_admin_pages_path
+ assert_response :redirect
+ assert_redirected_to new_cms_admin_site_path
+ end
+
+ def test_get_sites_index_with_no_site
+ CmsSite.delete_all
+ http_auth :get, cms_admin_sites_path
+ assert_response :success
+ end
+
+ end
\ No newline at end of file
test/integration/authentication_test.rb +3 -3
@@ @@ -11,7 +11,7 @@ class AuthenticationTest < ActionDispatch::IntegrationTest
end
def test_get_with_authorized_access
- get '/cms-admin/pages', {}, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('username:password')}"}
+ http_auth :get, '/cms-admin/pages'
assert_response :success
end
@@ @@ -19,9 +19,9 @@ class AuthenticationTest < ActionDispatch::IntegrationTest
assert_equal 'CmsHttpAuthentication', ComfortableMexicanSofa.config.authentication
CmsHttpAuthentication.username = 'newuser'
CmsHttpAuthentication.password = 'newpass'
- get '/cms-admin/pages', {}, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('username:password')}"}
+ http_auth :get, '/cms-admin/pages'
assert_response :unauthorized
- get '/cms-admin/pages', {}, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64('newuser:newpass')}"}
+ http_auth :get, '/cms-admin/pages', {}, 'newuser', 'newpass'
assert_response :success
end
end
\ No newline at end of file
test/test_helper.rb +11 -0
@@ @@ -40,3 +40,14 @@ class ActionController::TestCase
@request.env['HTTP_AUTHORIZATION'] = "Basic #{Base64.encode64('username:password')}"
end
end
+
+ class ActionDispatch::IntegrationTest
+
+ def setup
+ host! 'test.host'
+ end
+
+ def http_auth(method, path, options = {}, username = 'username', password = 'password')
+ send(method, path, options, {'HTTP_AUTHORIZATION' => "Basic #{Base64.encode64(username + ':' + password)}"})
+ end
+ end