From e05586d387a8d4b7e71572f2c0da096bdc78c26f Mon Sep 17 00:00:00 2001 From: did Date: Tue, 26 Jul 2011 21:20:03 +0200 Subject: [PATCH] write tests for the last cells menu + write tests for the bushi.do events + refactor the locomotive logger + add the cas_extra_attributes hook --- Gemfile | 2 +- Gemfile.lock | 11 +- app/controllers/admin/base_controller.rb | 2 +- config/environments/development.rb | 18 +- lib/locomotive.rb | 9 +- lib/locomotive/dragonfly.rb | 2 +- lib/locomotive/export.rb | 2 +- lib/locomotive/hosting/bushido/account_ext.rb | 15 ++ lib/locomotive/hosting/bushido/enabler.rb | 18 +- lib/locomotive/hosting/bushido/hooks.rb | 29 ++- lib/locomotive/hosting/heroku/enabler.rb | 4 +- lib/locomotive/import/logger.rb | 2 +- lib/locomotive/liquid/tags/editable/base.rb | 2 +- lib/locomotive/logger.rb | 4 +- lib/locomotive/routing/site_dispatcher.rb | 2 +- spec/cells/admin/global_actions_spec.rb | 202 +++++++++--------- spec/lib/locomotive/bushido_spec.rb | 17 ++ spec/lib/locomotive/render_spec.rb | 5 + spec/models/account_spec.rb | 4 + spec/models/content_instance_spec.rb | 5 + spec/models/content_type_spec.rb | 5 + spec/models/page_spec.rb | 5 + 22 files changed, 218 insertions(+), 147 deletions(-) diff --git a/Gemfile b/Gemfile index e0af41e6..62a99858 100644 --- a/Gemfile +++ b/Gemfile @@ -56,7 +56,7 @@ group :test, :development do gem 'ruby-debug', :platforms => :mri_18 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' end diff --git a/Gemfile.lock b/Gemfile.lock index 38f00731..921859b7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,13 +17,6 @@ GIT devise (>= 1.0.6) 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 remote: git://github.com/floehopper/mocha.git revision: d03843e763f4e5ceadb66bcb4c530d6c5d0cb293 @@ -77,6 +70,8 @@ GEM bson (1.3.1) bson_ext (1.3.1) builder (2.1.2) + bushido_stub (0.0.3) + activesupport (>= 3.0.7) cancan (1.6.5) capybara (1.0.0) mime-types (>= 1.16) @@ -303,7 +298,7 @@ DEPENDENCIES autotest bson_ext (~> 1.3.0) bushido! - bushido_stub! + bushido_stub (= 0.0.3) cancan capybara carrierwave (~> 0.5.5) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 210aaf02..d2ab310c 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -29,7 +29,7 @@ module Admin respond_to :html rescue_from CanCan::AccessDenied do |exception| - ::Locomotive::Logger.info "[CanCan::AccessDenied] #{exception.inspect}" + ::Locomotive.log "[CanCan::AccessDenied] #{exception.inspect}" if request.xhr? render :json => { :error => exception.message } diff --git a/config/environments/development.rb b/config/environments/development.rb index 723693f4..02694487 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -4,7 +4,7 @@ Locomotive::Application.configure do # In the development environment your application's code is reloaded on # 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. - config.cache_classes = true + config.cache_classes = false # Log error messages when you accidentally call methods on nil. config.whiny_nils = true @@ -30,12 +30,12 @@ Locomotive::Application.configure do # set up the bushido stub (uncomment it) # config.bushido_stub_env = false - config.bushido_stub_env = { - 'APP_TLD' => 'bushi.do', - 'BUSHIDO_APP' => 'san_francisco', - 'BUSHIDO_HOST' => 'bushi.do', - 'LOCOMOTIVE_SITE_NAME' => 'Locomotive TEST', - 'BUSHIDO_CLAIMED' => 'true', - 'BUSHIDO_METRICS_TOKEN' => 'foobar' - } + # config.bushido_stub_env = { + # 'APP_TLD' => 'bushi.do', + # 'BUSHIDO_APP' => 'san_francisco', + # 'BUSHIDO_HOST' => 'bushi.do', + # 'LOCOMOTIVE_SITE_NAME' => 'Locomotive TEST', + # 'BUSHIDO_CLAIMED' => 'true', + # 'BUSHIDO_METRICS_TOKEN' => 'foobar' + # } end diff --git a/lib/locomotive.rb b/lib/locomotive.rb index e441ca57..7d5c812b 100644 --- a/lib/locomotive.rb +++ b/lib/locomotive.rb @@ -126,10 +126,11 @@ module Locomotive end end - def self.logger(message) - if self.config.enable_logs == true - Rails.logger.info(message) - end + def self.log(*args) + level = args.size == 1 ? 'info' : args.first + message = args.size == 1 ? args.first : args.last + + ::Locomotive::Logger.send(level.to_sym, message) end # rack_cache: needed by default diff --git a/lib/locomotive/dragonfly.rb b/lib/locomotive/dragonfly.rb index 6f6b29c5..743cdba5 100644 --- a/lib/locomotive/dragonfly.rb +++ b/lib/locomotive/dragonfly.rb @@ -21,7 +21,7 @@ module Locomotive end else - Locomotive.logger.warning "Unable to resize on the fly: #{source.inspect}" + Locomotive.log :error, "Unable to resize on the fly: #{source.inspect}" return end diff --git a/lib/locomotive/export.rb b/lib/locomotive/export.rb index 7bfd9abd..2d78f845 100644 --- a/lib/locomotive/export.rb +++ b/lib/locomotive/export.rb @@ -321,7 +321,7 @@ module Locomotive def log(message, domain = '') head = "[export_template][#{@site.name}]" head += "[#{domain}]" unless domain.blank? - ::Locomotive::Logger.info "\t#{head} #{message}" + ::Locomotive.log "\t#{head} #{message}" end end diff --git a/lib/locomotive/hosting/bushido/account_ext.rb b/lib/locomotive/hosting/bushido/account_ext.rb index 8b257959..46e42a77 100644 --- a/lib/locomotive/hosting/bushido/account_ext.rb +++ b/lib/locomotive/hosting/bushido/account_ext.rb @@ -12,6 +12,21 @@ module Locomotive 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 diff --git a/lib/locomotive/hosting/bushido/enabler.rb b/lib/locomotive/hosting/bushido/enabler.rb index 008795a4..6ac86d36 100644 --- a/lib/locomotive/hosting/bushido/enabler.rb +++ b/lib/locomotive/hosting/bushido/enabler.rb @@ -1,4 +1,5 @@ require 'bushido' +require 'locomotive/hosting/bushido/hooks' require 'locomotive/hosting/bushido/custom_domain' require 'locomotive/hosting/bushido/first_installation' require 'locomotive/hosting/bushido/account_ext' @@ -26,8 +27,6 @@ module Locomotive end def enable_bushido! - require './lib/locomotive/hosting/bushido/hooks' - self.config.domain = ENV['APP_TLD'] unless self.config.multi_sites? self.config.devise_modules = [:cas_authenticatable, :rememberable, :trackable] @@ -38,6 +37,8 @@ module Locomotive self.setup_cas_client + self.subscribe_to_events + self.setup_smtp_settings self.add_middlewares @@ -74,9 +75,10 @@ module Locomotive def setup_cas_client ::Devise.setup do |config| - config.cas_base_url = 'https://auth.bushi.do/cas' - config.cas_logout_url = 'https://auth.bushi.do/cas/logout' - config.cas_create_user = false + config.cas_base_url = 'https://auth.bushi.do/cas' + config.cas_logout_url = 'https://auth.bushi.do/cas/logout' + config.cas_create_user = false + config.cas_username_column = :email end Admin::SessionsController.class_eval do @@ -106,7 +108,7 @@ module Locomotive # manage domains def add_bushido_domain(name) - Locomotive.logger "[add bushido domain] #{name}" + Locomotive.log "[add bushido domain] #{name}" ::Bushido::App.add_domain(name) if ::Bushido::Command.last_command_successful? @@ -115,7 +117,7 @@ module Locomotive end def remove_bushido_domain(name) - Locomotive.logger "[remove bushido domain] #{name}" + Locomotive.log "[remove bushido domain] #{name}" ::Bushido::App.remove_domain(name) if ::Bushido::Command.last_command_successful? @@ -124,7 +126,7 @@ module Locomotive end 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) if ::Bushido::Command.last_command_successful? diff --git a/lib/locomotive/hosting/bushido/hooks.rb b/lib/locomotive/hosting/bushido/hooks.rb index db74fe61..466c23b1 100644 --- a/lib/locomotive/hosting/bushido/hooks.rb +++ b/lib/locomotive/hosting/bushido/hooks.rb @@ -1,8 +1,25 @@ -Bushido::Data.listen("app.claimed") do |event| - puts "Saving #{Account.first.inspect} with incoming data #{event.inspect}" +require 'bushido' - account = Account.first - account.email = event["data"].try(:[], "email") - account.username = account.email - account.save +module Locomotive + module Hosting + module Bushido + 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 diff --git a/lib/locomotive/hosting/heroku/enabler.rb b/lib/locomotive/hosting/heroku/enabler.rb index b13a9df8..dd9ae9e1 100644 --- a/lib/locomotive/hosting/heroku/enabler.rb +++ b/lib/locomotive/hosting/heroku/enabler.rb @@ -60,13 +60,13 @@ module Locomotive # manage domains 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_domains << name end 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_domains.delete(name) end diff --git a/lib/locomotive/import/logger.rb b/lib/locomotive/import/logger.rb index 81fded77..d219ed67 100644 --- a/lib/locomotive/import/logger.rb +++ b/lib/locomotive/import/logger.rb @@ -5,7 +5,7 @@ module Locomotive def log(message, domain = '') head = "[import_template]" head += "[#{domain}]" unless domain.blank? - ::Locomotive::Logger.info "\t#{head} #{message}" + ::Locomotive.log "\t#{head} #{message}" end end diff --git a/lib/locomotive/liquid/tags/editable/base.rb b/lib/locomotive/liquid/tags/editable/base.rb index cc6781f7..a2a8e65b 100644 --- a/lib/locomotive/liquid/tags/editable/base.rb +++ b/lib/locomotive/liquid/tags/editable/base.rb @@ -46,7 +46,7 @@ module Locomotive end render_element(context, element) 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 diff --git a/lib/locomotive/logger.rb b/lib/locomotive/logger.rb index 3a01f63e..6ce0269c 100644 --- a/lib/locomotive/logger.rb +++ b/lib/locomotive/logger.rb @@ -1,9 +1,9 @@ module Locomotive module Logger - def self.method_missing(meth, args, &block) + def self.method_missing(meth, message, &block) if Locomotive.config.enable_logs == true - Rails.logger.send(meth, args) + Rails.logger.send(meth, "[LocomotiveCMS] #{message}") end end diff --git a/lib/locomotive/routing/site_dispatcher.rb b/lib/locomotive/routing/site_dispatcher.rb index ba4e8ef8..7405be1d 100644 --- a/lib/locomotive/routing/site_dispatcher.rb +++ b/lib/locomotive/routing/site_dispatcher.rb @@ -17,7 +17,7 @@ module Locomotive protected 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? @current_site ||= Site.match_domain(request.host).first diff --git a/spec/cells/admin/global_actions_spec.rb b/spec/cells/admin/global_actions_spec.rb index ac72b006..b4c145ef 100644 --- a/spec/cells/admin/global_actions_spec.rb +++ b/spec/cells/admin/global_actions_spec.rb @@ -1,101 +1,101 @@ -# require 'spec_helper' -# -# module Resetter -# -# @@original_global_actions_cell_klass = Admin::GlobalActionsCell -# -# def self.original_global_actions_cell_klass -# @@original_global_actions_cell_klass -# end -# -# def self.reset_global_actions_cell_klass -# ::Admin.send(:remove_const, 'GlobalActionsCell') -# ::Admin.const_set('GlobalActionsCell', self.original_global_actions_cell_klass.clone) -# ::Admin::GlobalActionsCell.any_instance.stubs(:sections).returns({ :main => 'settings', :sub => 'site' }) -# end -# -# end -# -# describe Admin::GlobalActionsCell do -# -# render_views -# -# let(:menu) { render_cell('admin/global_actions', :show) } -# -# describe 'show menu' do -# -# before(:each) do -# Resetter.reset_global_actions_cell_klass -# end -# -# it 'has 3 links' do -# menu.should have_selector('li', :count => 3) -# end -# -# it 'has a link to edit my account' do -# menu.should have_link('Site') -# end -# -# it 'has a link to edit the template files' do -# menu.should have_link('Theme files') -# end -# -# it 'has a link to edit my account' do -# menu.should have_link('My account') -# end -# -# end -# -# describe 'add a new menu item' do -# -# before(:each) do -# 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') } -# end -# -# it 'has 4 items' do -# menu.should have_selector('li', :count => 4) -# end -# -# it 'has a new link' do -# menu.should have_link('My link') -# end -# -# end -# -# describe 'remove a new menu item' do -# -# before(:each) do -# Resetter.reset_global_actions_cell_klass -# Admin::GlobalActionsCell.update_for(:testing_remove) { |m| m.remove(:theme_assets) } -# end -# -# it 'has 2 items' do -# menu.should have_selector('li', :count => 2) -# end -# -# it 'does not have the link to edit the template files' do -# menu.should_not have_link('Theme files') -# end -# -# end -# -# describe 'modify an existing menu item' do -# -# before(:each) do -# Resetter.reset_global_actions_cell_klass -# Admin::GlobalActionsCell.update_for(:testing_update) { |m| m.modify(:theme_assets, { :label => 'Modified !' }) } -# end -# -# it 'still has 3 items' do -# menu.should have_selector('li', :count => 3) -# end -# -# it 'has a modified menu item' do -# menu.should_not have_link('Theme files') -# menu.should have_link('Modified !') -# end -# -# end -# -# end \ No newline at end of file +require 'spec_helper' + +module Resetter + + @@original_global_actions_cell_klass = Admin::GlobalActionsCell + + def self.original_global_actions_cell_klass + @@original_global_actions_cell_klass + end + + def self.reset_global_actions_cell_klass + ::Admin.send(:remove_const, 'GlobalActionsCell') + ::Admin.const_set('GlobalActionsCell', self.original_global_actions_cell_klass.clone) + ::Admin::GlobalActionsCell.any_instance.stubs(:sections).returns({ :main => 'settings', :sub => 'site' }) + end + +end + +describe Admin::GlobalActionsCell do + + render_views + + 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 + + before(:each) do + Resetter.reset_global_actions_cell_klass + end + + it 'has 3 links' do + menu.should have_selector('a', :count => 4) + end + + it 'has a link to edit my account' do + menu.should have_link('Admin') + end + + it 'has a link to see my website' do + menu.should have_link('See website') + end + + it 'has a link to log out' do + menu.should have_link('Log out') + end + + end + + describe 'add a new menu item' do + + before(:each) do + 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') } + end + + it 'has 4 items' do + menu.should have_selector('a', :count => 5) + end + + it 'has a new link' do + menu.should have_link('My link') + end + + end + + describe 'remove a new menu item' do + + before(:each) do + Resetter.reset_global_actions_cell_klass + Admin::GlobalActionsCell.update_for(:testing_remove) { |m| m.remove(:see) } + end + + it 'has 2 items' do + menu.should have_selector('a', :count => 3) + end + + it 'does not have the link to see my website' do + menu.should_not have_link('See website') + end + + end + + describe 'modify an existing menu item' do + + before(:each) do + Resetter.reset_global_actions_cell_klass + Admin::GlobalActionsCell.update_for(:testing_update) { |m| m.modify(:see, { :label => 'Modified !' }) } + end + + it 'still has 3 items' do + menu.should have_selector('a', :count => 4) + end + + it 'has a modified menu item' do + menu.should_not have_link('See website') + menu.should have_link('Modified !') + end + + end + +end \ No newline at end of file diff --git a/spec/lib/locomotive/bushido_spec.rb b/spec/lib/locomotive/bushido_spec.rb index be3f0deb..9c78f75f 100644 --- a/spec/lib/locomotive/bushido_spec.rb +++ b/spec/lib/locomotive/bushido_spec.rb @@ -62,6 +62,23 @@ describe 'Bushido support' do Locomotive.bushido?.should be_true 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 before(:each) do diff --git a/spec/lib/locomotive/render_spec.rb b/spec/lib/locomotive/render_spec.rb index a21cc13f..e1b73cc6 100644 --- a/spec/lib/locomotive/render_spec.rb +++ b/spec/lib/locomotive/render_spec.rb @@ -177,4 +177,9 @@ describe 'Locomotive rendering system' do end + after(:all) do + ENV['APP_TLD'] = nil + Locomotive.configure_for_test(true) + end + end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 680427ad..6c1c8065 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -88,7 +88,11 @@ describe Account do end + end + after(:all) do + ENV['APP_TLD'] = nil + Locomotive.configure_for_test(true) end end diff --git a/spec/models/content_instance_spec.rb b/spec/models/content_instance_spec.rb index d390772c..383bc548 100644 --- a/spec/models/content_instance_spec.rb +++ b/spec/models/content_instance_spec.rb @@ -149,6 +149,11 @@ describe ContentInstance do end end + after(:all) do + ENV['APP_TLD'] = nil + Locomotive.configure_for_test(true) + end + def build_content(options = {}) @content_type.contents.build({ :title => 'Locomotive', :description => 'Lorem ipsum....' }.merge(options)) end diff --git a/spec/models/content_type_spec.rb b/spec/models/content_type_spec.rb index 9152d124..cc7a54c3 100644 --- a/spec/models/content_type_spec.rb +++ b/spec/models/content_type_spec.rb @@ -260,6 +260,11 @@ describe ContentType do end + after(:all) do + ENV['APP_TLD'] = nil + Locomotive.configure_for_test(true) + end + def build_content(content_type) content_type.contents.build(:name => 'Asset on steroids', :description => 'Lorem ipsum', :active => true) end diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index 19996adc..3a25e291 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -231,4 +231,9 @@ describe Page do @page.errors[:redirect_url].should == ["is invalid"] end end + + after(:all) do + ENV['APP_TLD'] = nil + Locomotive.configure_for_test(true) + end end