write tests for the last cells menu + write tests for the bushi.do events + refactor the locomotive logger + add the cas_extra_attributes hook

This commit is contained in:
did 2011-07-26 21:20:03 +02:00
parent 5e3a4ffa0f
commit e05586d387
22 changed files with 218 additions and 147 deletions

View File

@ -56,7 +56,7 @@ group :test, :development do
gem 'ruby-debug', :platforms => :mri_18 gem 'ruby-debug', :platforms => :mri_18
gem 'ruby-debug19', :platforms => :mri_19 gem 'ruby-debug19', :platforms => :mri_19
gem 'bushido_stub', :git => 'git://github.com/did/bushido_stub.git' gem 'bushido_stub', '0.0.3'
gem 'cucumber-rails', '1.0.2' gem 'cucumber-rails', '1.0.2'
end end

View File

@ -17,13 +17,6 @@ GIT
devise (>= 1.0.6) devise (>= 1.0.6)
rubycas-client (>= 2.2.1) rubycas-client (>= 2.2.1)
GIT
remote: git://github.com/did/bushido_stub.git
revision: 87f64541b4444dd3ff9fd1c1c44ac20c549ce2c6
specs:
bushido_stub (0.0.1)
activesupport (>= 3.0.7)
GIT GIT
remote: git://github.com/floehopper/mocha.git remote: git://github.com/floehopper/mocha.git
revision: d03843e763f4e5ceadb66bcb4c530d6c5d0cb293 revision: d03843e763f4e5ceadb66bcb4c530d6c5d0cb293
@ -77,6 +70,8 @@ GEM
bson (1.3.1) bson (1.3.1)
bson_ext (1.3.1) bson_ext (1.3.1)
builder (2.1.2) builder (2.1.2)
bushido_stub (0.0.3)
activesupport (>= 3.0.7)
cancan (1.6.5) cancan (1.6.5)
capybara (1.0.0) capybara (1.0.0)
mime-types (>= 1.16) mime-types (>= 1.16)
@ -303,7 +298,7 @@ DEPENDENCIES
autotest autotest
bson_ext (~> 1.3.0) bson_ext (~> 1.3.0)
bushido! bushido!
bushido_stub! bushido_stub (= 0.0.3)
cancan cancan
capybara capybara
carrierwave (~> 0.5.5) carrierwave (~> 0.5.5)

View File

@ -29,7 +29,7 @@ module Admin
respond_to :html respond_to :html
rescue_from CanCan::AccessDenied do |exception| rescue_from CanCan::AccessDenied do |exception|
::Locomotive::Logger.info "[CanCan::AccessDenied] #{exception.inspect}" ::Locomotive.log "[CanCan::AccessDenied] #{exception.inspect}"
if request.xhr? if request.xhr?
render :json => { :error => exception.message } render :json => { :error => exception.message }

View File

@ -4,7 +4,7 @@ Locomotive::Application.configure do
# In the development environment your application's code is reloaded on # In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development # every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes. # since you don't have to restart the webserver when you make code changes.
config.cache_classes = true config.cache_classes = false
# Log error messages when you accidentally call methods on nil. # Log error messages when you accidentally call methods on nil.
config.whiny_nils = true config.whiny_nils = true
@ -30,12 +30,12 @@ Locomotive::Application.configure do
# set up the bushido stub (uncomment it) # set up the bushido stub (uncomment it)
# config.bushido_stub_env = false # config.bushido_stub_env = false
config.bushido_stub_env = { # config.bushido_stub_env = {
'APP_TLD' => 'bushi.do', # 'APP_TLD' => 'bushi.do',
'BUSHIDO_APP' => 'san_francisco', # 'BUSHIDO_APP' => 'san_francisco',
'BUSHIDO_HOST' => 'bushi.do', # 'BUSHIDO_HOST' => 'bushi.do',
'LOCOMOTIVE_SITE_NAME' => 'Locomotive TEST', # 'LOCOMOTIVE_SITE_NAME' => 'Locomotive TEST',
'BUSHIDO_CLAIMED' => 'true', # 'BUSHIDO_CLAIMED' => 'true',
'BUSHIDO_METRICS_TOKEN' => 'foobar' # 'BUSHIDO_METRICS_TOKEN' => 'foobar'
} # }
end end

View File

@ -126,10 +126,11 @@ module Locomotive
end end
end end
def self.logger(message) def self.log(*args)
if self.config.enable_logs == true level = args.size == 1 ? 'info' : args.first
Rails.logger.info(message) message = args.size == 1 ? args.first : args.last
end
::Locomotive::Logger.send(level.to_sym, message)
end end
# rack_cache: needed by default # rack_cache: needed by default

View File

@ -21,7 +21,7 @@ module Locomotive
end end
else else
Locomotive.logger.warning "Unable to resize on the fly: #{source.inspect}" Locomotive.log :error, "Unable to resize on the fly: #{source.inspect}"
return return
end end

View File

@ -321,7 +321,7 @@ module Locomotive
def log(message, domain = '') def log(message, domain = '')
head = "[export_template][#{@site.name}]" head = "[export_template][#{@site.name}]"
head += "[#{domain}]" unless domain.blank? head += "[#{domain}]" unless domain.blank?
::Locomotive::Logger.info "\t#{head} #{message}" ::Locomotive.log "\t#{head} #{message}"
end end
end end

View File

@ -12,6 +12,21 @@ module Locomotive
end end
module InstanceMethods
def cas_extra_attributes=(extra_attributes)
return if extra_attributes.blank?
self.update_attributes({
:email => extra_attributes['email'],
:name => "#{extra_attributes['first_name']} #{extra_attributes['last_name']}",
:bushido_user_id => extra_attributes['ido_id'],
:locale => extra_attributes['locale'],
})
end
end
end end
end end

View File

@ -1,4 +1,5 @@
require 'bushido' require 'bushido'
require 'locomotive/hosting/bushido/hooks'
require 'locomotive/hosting/bushido/custom_domain' require 'locomotive/hosting/bushido/custom_domain'
require 'locomotive/hosting/bushido/first_installation' require 'locomotive/hosting/bushido/first_installation'
require 'locomotive/hosting/bushido/account_ext' require 'locomotive/hosting/bushido/account_ext'
@ -26,8 +27,6 @@ module Locomotive
end end
def enable_bushido! def enable_bushido!
require './lib/locomotive/hosting/bushido/hooks'
self.config.domain = ENV['APP_TLD'] unless self.config.multi_sites? self.config.domain = ENV['APP_TLD'] unless self.config.multi_sites?
self.config.devise_modules = [:cas_authenticatable, :rememberable, :trackable] self.config.devise_modules = [:cas_authenticatable, :rememberable, :trackable]
@ -38,6 +37,8 @@ module Locomotive
self.setup_cas_client self.setup_cas_client
self.subscribe_to_events
self.setup_smtp_settings self.setup_smtp_settings
self.add_middlewares self.add_middlewares
@ -74,9 +75,10 @@ module Locomotive
def setup_cas_client def setup_cas_client
::Devise.setup do |config| ::Devise.setup do |config|
config.cas_base_url = 'https://auth.bushi.do/cas' config.cas_base_url = 'https://auth.bushi.do/cas'
config.cas_logout_url = 'https://auth.bushi.do/cas/logout' config.cas_logout_url = 'https://auth.bushi.do/cas/logout'
config.cas_create_user = false config.cas_create_user = false
config.cas_username_column = :email
end end
Admin::SessionsController.class_eval do Admin::SessionsController.class_eval do
@ -106,7 +108,7 @@ module Locomotive
# manage domains # manage domains
def add_bushido_domain(name) def add_bushido_domain(name)
Locomotive.logger "[add bushido domain] #{name}" Locomotive.log "[add bushido domain] #{name}"
::Bushido::App.add_domain(name) ::Bushido::App.add_domain(name)
if ::Bushido::Command.last_command_successful? if ::Bushido::Command.last_command_successful?
@ -115,7 +117,7 @@ module Locomotive
end end
def remove_bushido_domain(name) def remove_bushido_domain(name)
Locomotive.logger "[remove bushido domain] #{name}" Locomotive.log "[remove bushido domain] #{name}"
::Bushido::App.remove_domain(name) ::Bushido::App.remove_domain(name)
if ::Bushido::Command.last_command_successful? if ::Bushido::Command.last_command_successful?
@ -124,7 +126,7 @@ module Locomotive
end end
def set_bushido_subdomain(name) def set_bushido_subdomain(name)
Locomotive.logger "[set bushido subdomain] #{name}.bushi.do" Locomotive.log "[set bushido subdomain] #{name}.bushi.do"
::Bushido::App.set_subdomain(name) ::Bushido::App.set_subdomain(name)
if ::Bushido::Command.last_command_successful? if ::Bushido::Command.last_command_successful?

View File

@ -1,8 +1,25 @@
Bushido::Data.listen("app.claimed") do |event| require 'bushido'
puts "Saving #{Account.first.inspect} with incoming data #{event.inspect}"
account = Account.first module Locomotive
account.email = event["data"].try(:[], "email") module Hosting
account.username = account.email module Bushido
account.save module Enabler
module ClassMethods
def subscribe_to_events
::Bushido::Data.listen('app.claimed') do |event|
Locomotive.log "Saving #{Account.first.inspect} with incoming data #{event.inspect}"
account = Account.first
account.email = event['data'].try(:[], 'email')
account.name = account.email.split('@').first
account.save
end
end
end
end
end
end
end end

View File

@ -60,13 +60,13 @@ module Locomotive
# manage domains # manage domains
def add_heroku_domain(name) def add_heroku_domain(name)
Locomotive.logger "[add heroku domain] #{name}" Locomotive.log "[add heroku domain] #{name}"
self.heroku_connection.add_domain(self.config.heroku[:name], name) self.heroku_connection.add_domain(self.config.heroku[:name], name)
self.heroku_domains << name self.heroku_domains << name
end end
def remove_heroku_domain(name) def remove_heroku_domain(name)
Locomotive.logger "[remove heroku domain] #{name}" Locomotive.log "[remove heroku domain] #{name}"
self.heroku_connection.remove_domain(self.config.heroku[:name], name) self.heroku_connection.remove_domain(self.config.heroku[:name], name)
self.heroku_domains.delete(name) self.heroku_domains.delete(name)
end end

View File

@ -5,7 +5,7 @@ module Locomotive
def log(message, domain = '') def log(message, domain = '')
head = "[import_template]" head = "[import_template]"
head += "[#{domain}]" unless domain.blank? head += "[#{domain}]" unless domain.blank?
::Locomotive::Logger.info "\t#{head} #{message}" ::Locomotive.log "\t#{head} #{message}"
end end
end end

View File

@ -46,7 +46,7 @@ module Locomotive
end end
render_element(context, element) render_element(context, element)
else else
Locomotive.logger "[editable element] missing element `#{context['block'].try(:name)}` / #{@slug} on #{current_page.fullpath}" Locomotive.log :error, "[editable element] missing element `#{context['block'].try(:name)}` / #{@slug} on #{current_page.fullpath}"
'' ''
end end
end end

View File

@ -1,9 +1,9 @@
module Locomotive module Locomotive
module Logger module Logger
def self.method_missing(meth, args, &block) def self.method_missing(meth, message, &block)
if Locomotive.config.enable_logs == true if Locomotive.config.enable_logs == true
Rails.logger.send(meth, args) Rails.logger.send(meth, "[LocomotiveCMS] #{message}")
end end
end end

View File

@ -17,7 +17,7 @@ module Locomotive
protected protected
def fetch_site def fetch_site
Locomotive.logger "[fetch site] host = #{request.host} / #{request.env['HTTP_HOST']}" Locomotive.log "[fetch site] host = #{request.host} / #{request.env['HTTP_HOST']}"
if Locomotive.config.multi_sites? if Locomotive.config.multi_sites?
@current_site ||= Site.match_domain(request.host).first @current_site ||= Site.match_domain(request.host).first

View File

@ -1,101 +1,101 @@
# require 'spec_helper' require 'spec_helper'
#
# module Resetter module Resetter
#
# @@original_global_actions_cell_klass = Admin::GlobalActionsCell @@original_global_actions_cell_klass = Admin::GlobalActionsCell
#
# def self.original_global_actions_cell_klass def self.original_global_actions_cell_klass
# @@original_global_actions_cell_klass @@original_global_actions_cell_klass
# end end
#
# def self.reset_global_actions_cell_klass def self.reset_global_actions_cell_klass
# ::Admin.send(:remove_const, 'GlobalActionsCell') ::Admin.send(:remove_const, 'GlobalActionsCell')
# ::Admin.const_set('GlobalActionsCell', self.original_global_actions_cell_klass.clone) ::Admin.const_set('GlobalActionsCell', self.original_global_actions_cell_klass.clone)
# ::Admin::GlobalActionsCell.any_instance.stubs(:sections).returns({ :main => 'settings', :sub => 'site' }) ::Admin::GlobalActionsCell.any_instance.stubs(:sections).returns({ :main => 'settings', :sub => 'site' })
# end end
#
# end end
#
# describe Admin::GlobalActionsCell do describe Admin::GlobalActionsCell do
#
# render_views render_views
#
# let(:menu) { render_cell('admin/global_actions', :show) } let(:menu) { render_cell('admin/global_actions', :show, :current_admin => Factory.build('admin user'), :current_site_url => 'http://www.yahoo.fr') }
#
# describe 'show menu' do describe 'show menu' do
#
# before(:each) do before(:each) do
# Resetter.reset_global_actions_cell_klass Resetter.reset_global_actions_cell_klass
# end end
#
# it 'has 3 links' do it 'has 3 links' do
# menu.should have_selector('li', :count => 3) menu.should have_selector('a', :count => 4)
# end end
#
# it 'has a link to edit my account' do it 'has a link to edit my account' do
# menu.should have_link('Site') menu.should have_link('Admin')
# end end
#
# it 'has a link to edit the template files' do it 'has a link to see my website' do
# menu.should have_link('Theme files') menu.should have_link('See website')
# end end
#
# it 'has a link to edit my account' do it 'has a link to log out' do
# menu.should have_link('My account') menu.should have_link('Log out')
# end end
#
# end end
#
# describe 'add a new menu item' do describe 'add a new menu item' do
#
# before(:each) do before(:each) do
# Resetter.reset_global_actions_cell_klass Resetter.reset_global_actions_cell_klass
# Admin::GlobalActionsCell.update_for(:testing_add) { |m| m.add(:my_link, :label => 'My link', :url => 'http://www.locomotivecms.com') } Admin::GlobalActionsCell.update_for(:testing_add) { |m| m.add(:my_link, :label => 'My link', :url => 'http://www.locomotivecms.com') }
# end end
#
# it 'has 4 items' do it 'has 4 items' do
# menu.should have_selector('li', :count => 4) menu.should have_selector('a', :count => 5)
# end end
#
# it 'has a new link' do it 'has a new link' do
# menu.should have_link('My link') menu.should have_link('My link')
# end end
#
# end end
#
# describe 'remove a new menu item' do describe 'remove a new menu item' do
#
# before(:each) do before(:each) do
# Resetter.reset_global_actions_cell_klass Resetter.reset_global_actions_cell_klass
# Admin::GlobalActionsCell.update_for(:testing_remove) { |m| m.remove(:theme_assets) } Admin::GlobalActionsCell.update_for(:testing_remove) { |m| m.remove(:see) }
# end end
#
# it 'has 2 items' do it 'has 2 items' do
# menu.should have_selector('li', :count => 2) menu.should have_selector('a', :count => 3)
# end end
#
# it 'does not have the link to edit the template files' do it 'does not have the link to see my website' do
# menu.should_not have_link('Theme files') menu.should_not have_link('See website')
# end end
#
# end end
#
# describe 'modify an existing menu item' do describe 'modify an existing menu item' do
#
# before(:each) do before(:each) do
# Resetter.reset_global_actions_cell_klass Resetter.reset_global_actions_cell_klass
# Admin::GlobalActionsCell.update_for(:testing_update) { |m| m.modify(:theme_assets, { :label => 'Modified !' }) } Admin::GlobalActionsCell.update_for(:testing_update) { |m| m.modify(:see, { :label => 'Modified !' }) }
# end end
#
# it 'still has 3 items' do it 'still has 3 items' do
# menu.should have_selector('li', :count => 3) menu.should have_selector('a', :count => 4)
# end end
#
# it 'has a modified menu item' do it 'has a modified menu item' do
# menu.should_not have_link('Theme files') menu.should_not have_link('See website')
# menu.should have_link('Modified !') menu.should have_link('Modified !')
# end end
#
# end end
#
# end end

View File

@ -62,6 +62,23 @@ describe 'Bushido support' do
Locomotive.bushido?.should be_true Locomotive.bushido?.should be_true
end end
describe 'events' do
before(:each) do
configure_locomotive_with_bushido
@site = Factory.build('test site')
@account = @site.memberships.first.account
Account.stubs(:first).returns(@account)
end
it 'responds to the app.claimed event' do
::Bushido::Data.call('app.claimed')
@account.name.should == 'san_francisco'
@account.email.should == 'san_francisco@bushi.do'
end
end
context 'enhancing site' do context 'enhancing site' do
before(:each) do before(:each) do

View File

@ -177,4 +177,9 @@ describe 'Locomotive rendering system' do
end end
after(:all) do
ENV['APP_TLD'] = nil
Locomotive.configure_for_test(true)
end
end end

View File

@ -88,7 +88,11 @@ describe Account do
end end
end
after(:all) do
ENV['APP_TLD'] = nil
Locomotive.configure_for_test(true)
end end
end end

View File

@ -149,6 +149,11 @@ describe ContentInstance do
end end
end end
after(:all) do
ENV['APP_TLD'] = nil
Locomotive.configure_for_test(true)
end
def build_content(options = {}) def build_content(options = {})
@content_type.contents.build({ :title => 'Locomotive', :description => 'Lorem ipsum....' }.merge(options)) @content_type.contents.build({ :title => 'Locomotive', :description => 'Lorem ipsum....' }.merge(options))
end end

View File

@ -260,6 +260,11 @@ describe ContentType do
end end
after(:all) do
ENV['APP_TLD'] = nil
Locomotive.configure_for_test(true)
end
def build_content(content_type) def build_content(content_type)
content_type.contents.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true) content_type.contents.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true)
end end

View File

@ -231,4 +231,9 @@ describe Page do
@page.errors[:redirect_url].should == ["is invalid"] @page.errors[:redirect_url].should == ["is invalid"]
end end
end end
after(:all) do
ENV['APP_TLD'] = nil
Locomotive.configure_for_test(true)
end
end end