From 31a5ef19de8fd846319fe55ca1728b04ff03b444 Mon Sep 17 00:00:00 2001 From: Sarah Mei Date: Sat, 28 May 2011 19:44:40 -0700 Subject: [PATCH] Switching to settingslogic to manage our configuration. THE FILE FORMAT HAS CHANGED. OLD APP.YML AND APP_CONFIG.YML FILES WILL NOT WORK ANYMORE. Two important differences: 1. The app only looks in one file for settings: config/application.yml. If you want to override something, do it in that file. It is gitignore'd. If config/application.yml does not exist, or exists but is in the old format, you'll get a nice friendly set of instructions on how to convert your old file. 2. script/server settings are no longer in application.yml. They have their own settings file - copy config/script_server_config.yml.example to config/script_server_config.yml. This is also gitignore'd. script/server will print a message and exit if that file doesn't exist. --- .gitignore | 4 +- Gemfile | 1 + Gemfile.lock | 24 +-- app/models/app_config.rb | 94 +++++++++++ .../{app_base.yml => application.yml.example} | 101 +++++++----- config/initializers/_load_app_config.rb | 11 +- config/script_server_config.yml.example | 9 + lib/app_config.rb | 107 ------------ script/get_config.rb | 49 +++--- script/server | 7 +- spec/config/config_spec.rb | 18 -- .../fixtures/config/old_style_app.yml | 3 +- spec/lib/app_config_spec.rb | 79 --------- spec/models/app_config_spec.rb | 155 ++++++++++++++++++ 14 files changed, 364 insertions(+), 298 deletions(-) create mode 100644 app/models/app_config.rb rename config/{app_base.yml => application.yml.example} (54%) create mode 100644 config/script_server_config.yml.example delete mode 100644 lib/app_config.rb delete mode 100644 spec/config/config_spec.rb rename config/app.yml.example => spec/fixtures/config/old_style_app.yml (99%) delete mode 100644 spec/lib/app_config_spec.rb create mode 100644 spec/models/app_config_spec.rb diff --git a/.gitignore b/.gitignore index 8b60753a3..5d89c813c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ public/images/ball.png # Configuration files config/app_config.yml config/app.yml +config/application.yml +config/script_server_config.yml config/fb_config.yml config/oauth_keys.yml config/initializers/secret_token.rb @@ -31,12 +33,12 @@ public/source.tar* tmp/**/* tmp/* db/*.sqlite3 -.redcar # Temporary files of every sort .sass-cache/* .DS_Store .idea +.redcar .rvmrc .stgit* *.swap diff --git a/Gemfile b/Gemfile index 0076b62e5..29666cfea 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem 'chef', '0.9.12', :require => false gem 'ohai', '0.5.8', :require => false #Chef dependency gem 'nokogiri' +gem 'settingslogic', '2.0.6' #Security gem 'devise', '~> 1.3.1' diff --git a/Gemfile.lock b/Gemfile.lock index df6e73be4..3d1f526bf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -129,14 +129,14 @@ GEM erubis extlib highline - json (>= 1.4.4, <= 1.4.6) + json (<= 1.4.6, >= 1.4.4) mixlib-authentication (>= 1.1.0) mixlib-cli (>= 1.1.0) mixlib-config (>= 1.1.2) mixlib-log (>= 1.2.0) moneta ohai (>= 0.5.7) - rest-client (>= 1.0.4, < 1.7.0) + rest-client (< 1.7.0, >= 1.0.4) uuidtools childprocess (0.1.7) ffi (~> 0.6.3) @@ -146,10 +146,10 @@ GEM columnize (0.3.2) configuration (1.2.0) crack (0.1.8) - cucumber (0.10.2) + cucumber (0.10.3) builder (>= 2.1.2) diff-lcs (>= 1.1.2) - gherkin (>= 2.3.5) + gherkin (>= 2.3.8) json (>= 1.4.6) term-ansicolor (>= 1.0.5) cucumber-rails (0.3.2) @@ -163,7 +163,7 @@ GEM warden (~> 1.0.3) devise_invitable (0.5.0) devise (~> 1.3.1) - rails (>= 3.0.0, <= 3.2) + rails (<= 3.2, >= 3.0.0) diff-lcs (1.1.2) erubis (2.6.6) abstract (>= 1.0.0) @@ -177,7 +177,7 @@ GEM faraday (0.5.4) addressable (~> 2.2.2) multipart-post (~> 1.1.0) - rack (>= 1.1.0, < 2) + rack (< 2, >= 1.1.0) faraday_middleware (0.3.2) faraday (~> 0.5.4) fastercsv (1.5.4) @@ -201,7 +201,7 @@ GEM rspec-instafail (~> 0.1.4) ruby-progressbar (~> 0.0.10) gem_plugin (0.2.3) - gherkin (2.3.8) + gherkin (2.3.9) json (>= 1.4.6) haml (3.0.25) hashie (0.4.0) @@ -244,7 +244,7 @@ GEM gem_plugin (>= 0.2.3) multi_json (0.0.5) multi_xml (0.2.2) - multipart-post (1.1.1) + multipart-post (1.1.2) mysql2 (0.2.6) net-ldap (0.1.1) net-scp (1.0.4) @@ -284,7 +284,7 @@ GEM multi_json (~> 0.0.4) ohai (0.5.8) extlib - json (>= 1.4.4, <= 1.4.6) + json (<= 1.4.6, >= 1.4.4) mixlib-cli mixlib-config mixlib-log @@ -299,7 +299,7 @@ GEM orm_adapter (0.0.5) polyglot (0.3.1) pyu-ruby-sasl (0.0.3.2) - rack (1.2.2) + rack (1.2.3) rack-mount (0.6.14) rack (>= 1.0.0) rack-openid (1.2.0) @@ -366,10 +366,11 @@ GEM ffi (~> 0.6.3) json_pure rubyzip + settingslogic (2.0.6) simple_oauth (0.1.5) sinatra (1.2.6) rack (~> 1.1) - tilt (>= 1.2.2, < 2.0) + tilt (< 2.0, >= 1.2.2) subexec (0.0.4) systemu (2.2.0) term-ansicolor (1.0.5) @@ -449,6 +450,7 @@ DEPENDENCIES rspec-rails (>= 2.0.0) ruby-debug selenium-webdriver (= 0.1.3) + settingslogic (= 2.0.6) sod! thin (= 1.2.11) twitter! diff --git a/app/models/app_config.rb b/app/models/app_config.rb new file mode 100644 index 000000000..33b29956d --- /dev/null +++ b/app/models/app_config.rb @@ -0,0 +1,94 @@ +# Copyright (c) 2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. +require 'uri' + +class AppConfig < Settingslogic + + source File.join(Rails.root, "config", "application.yml") + namespace Rails.env + + def self.load! + if no_config_file? && !have_old_config_file? + $stderr.puts <<-HELP +******** You haven't set up your Diaspora settings file. ********** +Please do the following: +1. Copy config/application.yml.example to config/application.yml. +2. Have a look at the settings in that file. It has sensible defaults for development, which (we hope) +work without modification. However, it's always good to know what's available to change later. +3. Restart Diaspora! +******** Thanks for being an alpha tester! ********** +HELP + Process.exit(1) + end + + if (no_config_file? && have_old_config_file?) || config_file_is_old_style? + $stderr.puts <<-HELP +******** The Diaspora configuration file format has changed. ********** +Please do the following: +1. Copy config/application.yml.example to config/application.yml. +2. Make any changes in config/application.yml that you previously made in config/app.yml or config/app_config.yml. +3. Delete config/app.yml and config/app_config.yml. Don't worry if they don't exist, though. +4. Restart Diaspora! +******** Thanks for being an alpha tester! ********** +HELP + Process.exit(1) + end + + super + + normalize_pod_url + normalize_admins + end + + def self.config_file_is_old_style? + !(File.read(@source) =~ /^defaults: &defaults/) + end + + def self.no_config_file? + !File.exists?(@source) + end + + def self.have_old_config_file? + File.exists?(File.join(Rails.root, "config", "app.yml")) || (File.exists?(File.join(Rails.root, "config", "app_config.yml"))) + end + + def self.normalize_pod_url + unless self[:pod_url] =~ /^(https?:\/\/)/ # starts with http:// or https:// + self[:pod_url] = "http://#{self[:pod_url]}" + end + unless self[:pod_url] =~ /\/$/ # ends with slash + self[:pod_url] = "#{self[:pod_url]}/" + end + end + + def self.normalize_admins + self[:admins] ||= [] + self[:admins].collect! { |username| username.downcase } + end + + load! + + def self.[] (key) + return self.pod_uri if key == :pod_uri + super + end + + def self.[]= (key, value) + @@pod_uri = nil if key == :pod_url + super + end + + cattr_accessor :pod_uri + + def self.pod_uri + if @@pod_uri.nil? + begin + @@pod_uri = URI.parse(self.pod_url) + rescue + puts "WARNING: pod url " + self.pod_url + " is not a legal URI" + end + end + return @@pod_uri + end +end \ No newline at end of file diff --git a/config/app_base.yml b/config/application.yml.example similarity index 54% rename from config/app_base.yml rename to config/application.yml.example index 22a641470..b4a92f3b0 100644 --- a/config/app_base.yml +++ b/config/application.yml.example @@ -2,42 +2,64 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -default: +defaults: &defaults - # Hostname of this host, as seen from the internet. + # Hostname of the machine you're running Diaspora on, as seen from the internet. + # For development and testing, you can leave this as is. pod_url: "http://localhost:3000" - # Set this to true in order to close signups. Users will still be - # able to invite other people to join. + # Set this to true to prevent people from signing up for your pod without an invitation. registrations_closed: false + # Set this to true to prevent users from sending invitations. + invites_off: false + + # + # Logging setup + # + # Enable extensive logging to log/{development,test,production}.log debug: false - # Websocket server setup, see script/websocket_server.rb + # If you want normal Rails logs, set this to false in the appropriate environment. + # It is false by default in development and test. + enable_splunk_logging: true + + # + # Websocket server setup - see script/websocket_server.rb + # + # Enable extensive logging to websocket server. socket_debug : false - # Websocket host, leave as 0.0.0.0 unless you know what you are doing + # Websocket host - leave as 0.0.0.0 unless you know what you are doing socket_host: 0.0.0.0 # File containing pid of running script/websocket_server.rb socket_pidfile: "log/diaspora-wsd.pid" - # Websocket port, should normally be 8080 or 8081. + # Websocket port - should normally be 8080 or 8081. socket_port: 8080 socket_collection_name: 'websocket' - # Secure websocket confguration (wss://) - # requires SSL cert and key + # Secure websocket confguration (wss://). + # Requires SSL cert and key socket_secure: false socket_cert_chain_location: '/full/path/to/cert_chain.crt' socket_private_key_location: '/full/path/to/file.key' - # Diaspora is only tested against this default pubsub server. + # + # Federation setup + # + + # Diaspora is only tested against this default pubsub server. You probably don't want to change this. pubsub_server: 'https://pubsubhubbub.appspot.com/' - # Setting this to true enables diaspora's "send email" functionality + # + # Email setup + # + + # Setting this to true enables Diaspora's "send email" functionality, # requiring meaningful smtp_* settings. These are options for RoR's # ActionMailer class. mailer_on: false @@ -46,85 +68,78 @@ default: # connection or 'sendmail' to use the sendmail binary. mailer_method: 'smtp' - # Address/port to smtp server handing outgoing mail. + # Address/port to smtp server handling outgoing mail. smtp_address: 'smtp.example.com' smtp_port: '587' - # Domain administered of smtp server. + # Domain of smtp server. smtp_domain: 'example.com' - # Sender address in diaspora's outgoing mail. + # Sender address in Diaspora's outgoing mail. smtp_sender_address: 'no-reply@joindiaspora.com' - # Authentication required to send mail. Use one of 'one','plain', - # 'login' or 'cram-md5'. Use 'none' if server do not support + # Authentication required to send mail. Use one of 'one', 'plain', + # 'login' or 'cram-md5'. Use 'none' if server does not support # authentication smtp_authentication: 'plain' # Automatically enable TLS? Ignored if smtp_authentication is set to none smtp_starttls_auto: true - # Credentails possibly required to log in to SMTP server if - # smtp_authentication != 'none' + # Credentials to log in to the SMTP server - may be necessary if + # smtp_authentication is not 'none' smtp_username: 'smtp_username' smtp_password: 'secret' # The path to the sendmail binary. sendmail_location: '/usr/sbin/sendmail' - #google analytics key, if false, it won't include the javascript + # + # Web tracking setup + # + + # google analytics key - if false, no javascript included google_a_site: false - #piwik integration if not set, no javascript included + # piwik integration - if not set, no javascript included piwik_id: # the site url in raw format (e.g. pikwik.examplehost.com) piwik_url: + # + # Miscellaneous setup + # - #cloudfiles username and api-key, used for backups + # cloudfiles username and api-key - used for backups cloudfiles_username: 'example' cloudfiles_api_key: 'abc123' - invites_off: false - #list of users who have admin privilages + # List of users who have admin privileges admins: - 'example_user1dsioaioedfhgoiesajdigtoearogjaidofgjo' - #s3 config, if set, carrierwave will store your photos on s3 + # s3 config - if set, carrierwave will store your photos on s3. Otherwise they're on the filesystem. #s3_key: 'key' #s3_secret: 'secret' #s3_bucket: 'my_photos' s3_region: 'us-east-1' - # If you want normal Rails logs, set this to false in the appropriate environment. - # It is false by default in development and test. - enable_splunk_logging: true - - # Process jobs in process? + # Set this to true if you want to do everything synchronously instead of using resque, our redis-backed queue system. single_process_mode: true -# Use this sections to overide settings from default in the specific environments +# Use this section to override default settings in specific environments development: + <<: *defaults enable_splunk_logging: false production: + <<: *defaults single_process_mode: false # Do not touch unless you know what you're doing test: - pod_url: "http://example.org/" + <<: *defaults + pod_url: "http://example.org" socket_port: 8081 enable_splunk_logging: false - -# This section is special, you cannot overide settings from here in the above sections -script_server: - # Port on which thin should listen - thin_port: 3000 - - # customize thins startup - default_thin_args: "-p $THIN_PORT -e $RAILS_ENV" - - # Possibilties are development, production - rails_env: "development" - diff --git a/config/initializers/_load_app_config.rb b/config/initializers/_load_app_config.rb index 3b5aa3d7c..e0639240d 100644 --- a/config/initializers/_load_app_config.rb +++ b/config/initializers/_load_app_config.rb @@ -1,14 +1,5 @@ # Copyright (c) 2010, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -# -# Sets up AppConfig. Unless stated below, each entry is a the string in -# the file app.yml, as applicable for current environment. -# -# Specific items -# * pod_url: As in app.yml, normalized with a trailing /. -# * pod_uri: An uri object derived from pod_url. -require File.join(Rails.root, 'lib', 'app_config') - -AppConfig.configure_for_environment(Rails.env) +require File.join(Rails.root, 'app', 'models', 'app_config') \ No newline at end of file diff --git a/config/script_server_config.yml.example b/config/script_server_config.yml.example new file mode 100644 index 000000000..2a6de73cf --- /dev/null +++ b/config/script_server_config.yml.example @@ -0,0 +1,9 @@ +script_server: + # Port on which thin should listen + thin_port: 3000 + + # Customize thin's startup + default_thin_args: "-p $THIN_PORT -e $RAILS_ENV" + + # Possibilities are development, production + rails_env: "development" \ No newline at end of file diff --git a/lib/app_config.rb b/lib/app_config.rb deleted file mode 100644 index 38da6ba7a..000000000 --- a/lib/app_config.rb +++ /dev/null @@ -1,107 +0,0 @@ -# Copyright (c) 2011, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -class AppConfig - - cattr_accessor :config_vars - cattr_accessor :base_file_path - cattr_accessor :file_path - - def self.base_file_path - @@base_file_path || File.join(Rails.root, "config", "app_base.yml") - end - - def self.file_path - @@file_path || File.join(Rails.root, "config", "app.yml") - end - - def self.[](key) - config_vars[key] - end - - def self.[]=(key, value) - config_vars[key] = value - end - - def self.has_key?(key) - config_vars.has_key?(key) - end - - def self.configure_for_environment(env) - load_config_for_environment(env) - generate_pod_uri - normalize_pod_url - check_pod_uri - downcase_usernames - end - - def self.load_config_for_environment(env) - if File.exist?(base_file_path) - all_envs = load_config_yaml(base_file_path) - else - $stderr.puts "OH NO! Required file #{base_file_path} doesn't exist! Did you move it?" - all_envs = {} - end - if File.exist?(file_path) - all_envs_custom = load_config_yaml(file_path) - all_envs.deep_merge!(all_envs_custom) - elsif File.exist? "#{Rails.root}/config/app_config.yml" - all_envs_custom = load_config_yaml "#{Rails.root}/config/app_config.yml" - all_envs.deep_merge!(all_envs_custom) - $stderr.puts "DEPRECATION WARNING: config/app_config.yml has been renamed to config/app.yml" - else - unless Rails.env == "development" || Rails.env == "test" - $stderr.puts "WARNING: No config/app.yml found! Look at config/app.yml.example for help." - end - end - - # Is there a config at all? - unless all_envs['default'] - $stderr.puts "What did you do? There's no config at all!" - Process.exit(false) - end - - env = env.to_s - if all_envs[env] - self.config_vars = all_envs['default'].merge(all_envs[env]).symbolize_keys - else - self.config_vars = all_envs['default'].symbolize_keys - end - end - - def self.generate_pod_uri - require 'uri' - unless self.config_vars[:pod_url] =~ /^(https?:\/\/)/ - self.config_vars[:pod_url] = "http://#{self.config_vars[:pod_url]}" - end - begin - self.config_vars[:pod_uri] = URI.parse(self.config_vars[:pod_url]) - rescue - puts "WARNING: pod url " + self.config_vars[:pod_url] + " is not a legal URI" - end - end - - def self.normalize_pod_url - self.config_vars[:pod_url] = self.config_vars[:pod_uri].normalize.to_s - end - - def self.check_pod_uri - if self.config_vars[:pod_uri].host == "example.org" && Rails.env != "test" - puts "WARNING: Please modify your app.yml to have a proper pod_url!" - end - end - - - def self.downcase_usernames - [:admins, :auth_tokenable].each do |key| - self.config_vars[key] ||= [] - self.config_vars[key].collect! { |username| username.downcase } - end - end - - def self.load_config_yaml filename - # nil values are bad for merges and have no meaning here, so lets get rid of them - YAML.load(File.read(filename)).delete_if { |k, v| v.nil? } - end -end diff --git a/script/get_config.rb b/script/get_config.rb index 759238f34..8e8eee647 100755 --- a/script/get_config.rb +++ b/script/get_config.rb @@ -4,17 +4,12 @@ # the COPYRIGHT file. require 'rubygems' -require 'yaml' - -require 'active_support/core_ext/class/attribute_accessors' -require 'active_support/core_ext/hash/keys' -require 'active_support/core_ext/hash/deep_merge' class Rails def self.root - File.join(File.dirname(__FILE__), "..") + File.expand_path(File.join(File.dirname(__FILE__), "..")) end - + def self.env env = 'development' env = ENV['RAILS_ENV'] if ENV.has_key?('RAILS_ENV') @@ -23,21 +18,33 @@ class Rails end end -require File.join(Rails.root, 'lib', 'app_config') - - if ARGV.length >= 1 - key = ARGV[0].to_sym - AppConfig.configure_for_environment(Rails.env) - if AppConfig.has_key?(key) - print AppConfig[key] - else - puts "Invalid option #{ARGV[0]}" - exit 2 + setting_name = ARGV[0] + if Rails.env == 'script_server' # load from the special script_server_config.yml file + require 'yaml' + script_server_config_file = File.join(Rails.root, 'config', 'script_server_config.yml') + begin + print YAML.load_file(script_server_config_file)['script_server'][setting_name] + rescue + $stderr.puts "Setting '#{setting_name}' not found in file #{script_server_config_file}." + $stderr.puts "Does that file exist? If not, copy it from #{File.basename(script_server_config_file)}.example in the same directory and run this script again." + Process.exit(1) + end + else # load from the general diaspora settings file + require 'active_support/core_ext/class/attribute_accessors' + require 'settingslogic' + require File.join(Rails.root, 'app', 'models', 'app_config') + setting_name = setting_name.to_sym + if AppConfig[setting_name].nil? + $stderr.puts "Could not find setting #{ARGV[0]} for environment #{Rails.env}." + Process.exit(1) + else + print AppConfig[setting_name] + end end else - puts "Usage: ./script/get_config.rb option [section]" - puts "" - puts "section defaults to development" - exit 1 + $stderr.puts "Usage: ./script/get_config.rb option [section]" + $stderr.puts "" + $stderr.puts "section defaults to development" + Process.exit(1) end diff --git a/script/server b/script/server index 32cf17a87..e51462fb7 100755 --- a/script/server +++ b/script/server @@ -137,12 +137,7 @@ if [ ! -e public/source.tar.gz ]; then fi -# Check if config files exists -if [ ! -e 'config/app.yml' ]; then - echo "FATAL: config/app.yml is missing! Copy over config/app.yml.example to config/app.yml and edit it properly!" >&2 - exit 67 -fi - +# Check if database.yml exists if [ ! -e 'config/database.yml' ]; then echo 'FATAL: config/database.yml is missing! Copy over config/database.yml.example to config/database.yml and edit it properly!' >&2 exit 68 diff --git a/spec/config/config_spec.rb b/spec/config/config_spec.rb deleted file mode 100644 index ae89bdaad..000000000 --- a/spec/config/config_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -require 'spec_helper' -describe 'making sure the config is parsed as should' do - - describe 'pod_url' do - it 'should have a trailing slash' do - AppConfig[:pod_url].should == 'http://example.org/' - end - end - - describe 'terse_pod_url' - it 'should be correctly parsed' do - AppConfig[:pod_uri].host.should == 'example.org' - end -end diff --git a/config/app.yml.example b/spec/fixtures/config/old_style_app.yml similarity index 99% rename from config/app.yml.example rename to spec/fixtures/config/old_style_app.yml index 71ec42c60..529e256c4 100644 --- a/config/app.yml.example +++ b/spec/fixtures/config/old_style_app.yml @@ -126,5 +126,4 @@ script_server: default_thin_args: "-p $THIN_PORT -e $RAILS_ENV" # Possibilties are development, production - rails_env: "development" - + rails_env: "development" \ No newline at end of file diff --git a/spec/lib/app_config_spec.rb b/spec/lib/app_config_spec.rb deleted file mode 100644 index f36643f5c..000000000 --- a/spec/lib/app_config_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -require 'spec_helper' - -describe AppConfig do - before do - @environment_vars = AppConfig.config_vars - AppConfig.config_vars = {} - end - after do - AppConfig.config_vars = @environment_vars - end - describe ".base_file_path" do - it "allows you to set the base file path" do - AppConfig.base_file_path = "foo" - AppConfig.base_file_path.should == "foo" - end - it "defaults to config/app_base.yml" do - AppConfig.base_file_path = nil - AppConfig.base_file_path.should == "#{Rails.root}/config/app_base.yml" - end - end - describe ".load_config_for_environment" do - before do - @original_stderr = $stderr - $stderr = StringIO.new - end - after do - $stderr = @original_stderr - end - it "prints error if base file is missing" do - AppConfig.base_file_path = "/no/such/file" - AppConfig.file_path = File.join(Rails.root, "config", "app_base.yml") - - AppConfig.load_config_for_environment(:test) - $stderr.rewind - $stderr.string.chomp.should_not be_blank - end - it "prints error and exits if there's no config at all" do - AppConfig.base_file_path = "/no/such/file" - AppConfig.file_path = "/no/such/file" - - lambda { - AppConfig.load_config_for_environment(:test) - }.should raise_error SystemExit - - $stderr.rewind - $stderr.string.chomp.should_not be_blank - end - end - describe ".generate_pod_uri" do - describe "when pod_url is prefixed with protocol" do - it "generates a URI with a host for http" do - AppConfig[:pod_url] = "http://oscar.joindiaspora.com" - AppConfig.generate_pod_uri - AppConfig[:pod_uri].host.should == "oscar.joindiaspora.com" - end - it "generates a URI with a host for https" do - AppConfig[:pod_url] = "https://oscar.joindiaspora.com" - AppConfig.generate_pod_uri - AppConfig[:pod_uri].host.should == "oscar.joindiaspora.com" - end - end - describe "when pod_url is not prefixed with protocol" do - it "generates a URI with a host" do - AppConfig[:pod_url] = "oscar.joindiaspora.com" - AppConfig.generate_pod_uri - AppConfig[:pod_uri].host.should == "oscar.joindiaspora.com" - end - it "adds http:// to the front of the pod_url" do - AppConfig[:pod_url] = "oscar.joindiaspora.com" - AppConfig.generate_pod_uri - AppConfig[:pod_url].should == "http://oscar.joindiaspora.com" - end - end - end -end diff --git a/spec/models/app_config_spec.rb b/spec/models/app_config_spec.rb new file mode 100644 index 000000000..2ce2d8a5f --- /dev/null +++ b/spec/models/app_config_spec.rb @@ -0,0 +1,155 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe AppConfig do + + after do + AppConfig.reload! + end + + describe ".load!" do + context "error conditions" do + before do + @original_stderr = $stderr + $stderr = StringIO.new + end + after do + $stderr = @original_stderr + end + + context "with old-style application.yml" do + before do + @original_source = AppConfig.source + AppConfig.source(File.join(Rails.root, "spec", "fixtures", "config", "old_style_app.yml")) + end + after do + AppConfig.source(@original_source) + end + it "prints an error message and exits" do + expect { + AppConfig.load! + }.should raise_error SystemExit + + $stderr.rewind + $stderr.string.chomp.should_not be_blank + end + end + + context "when source config file (i.e. config/application.yml) does not exist" do + before do + application_yml = File.join(Rails.root, "config", "application.yml") + @app_yml = File.join(Rails.root, "config", "app.yml") + @app_config_yml = File.join(Rails.root, "config", "app_config.yml") + File.should_receive(:exists?).with(application_yml).at_least(:once).and_return(false) + end + after do + File.instance_eval { alias :exists? :obfuscated_by_rspec_mocks__exists? } # unmock exists? so that the AppConfig.reload! in the top-level after block can run + end + context "and there are no old-style config files around" do + it "prints an error message with instructions for setting up application.yml and exits" do + File.should_receive(:exists?).with(@app_yml).at_least(:once).and_return(false) + File.should_receive(:exists?).with(@app_config_yml).at_least(:once).and_return(false) + + expect { + AppConfig.load! + }.should raise_error SystemExit + + $stderr.rewind + $stderr.string.should include("haven't set up") + end + end + context "and there is an old-style app.yml" do + it "prints an error message with instructions for converting an old-style file and exits" do + File.should_receive(:exists?).with(@app_yml).at_least(:once).and_return(true) + + expect { + AppConfig.load! + }.should raise_error SystemExit + + $stderr.rewind + $stderr.string.should include("file format has changed") + end + end + context "and there is an old-style app_config.yml" do + it "prints an error message with instructions for converting an old-style file and exits" do + File.should_receive(:exists?).with(@app_yml).at_least(:once).and_return(false) + File.should_receive(:exists?).with(@app_config_yml).at_least(:once).and_return(true) + + expect { + AppConfig.load! + }.should raise_error SystemExit + + $stderr.rewind + $stderr.string.should include("file format has changed") + end + end + end + + end + it "calls normalize_pod_url" do + AppConfig.should_receive(:normalize_pod_url).twice # apparently should_receive counts stuff in after blocks...WTF? + AppConfig.load! + end + it "calls normalize_admins" do + AppConfig.should_receive(:normalize_admins).twice + AppConfig.load! + end + end + + describe ".normalize_admins" do + it "downcases the user names that are set as admins" do + AppConfig[:admins] = ["UPPERCASE", "MiXeDCaSe", "lowercase"] + AppConfig.normalize_admins + AppConfig[:admins].should == ["uppercase", "mixedcase", "lowercase"] + end + it "sets admins to an empty array if no admins were specified" do + AppConfig[:admins] = nil + AppConfig.normalize_admins + AppConfig[:admins].should == [] + end + end + + describe ".normalize_pod_url" do + it "adds a trailing slash if there isn't one" do + AppConfig[:pod_url] = "http://example.org" + AppConfig.normalize_pod_url + AppConfig[:pod_url].should == "http://example.org/" + end + it "does not add an extra trailing slash" do + AppConfig[:pod_url] = "http://example.org/" + AppConfig.normalize_pod_url + AppConfig[:pod_url].should == "http://example.org/" + end + it "adds http:// on the front if it's missing" do + AppConfig[:pod_url] = "example.org/" + AppConfig.normalize_pod_url + AppConfig[:pod_url].should == "http://example.org/" + end + it "does not add a prefix if there already is http:// on the front" do + AppConfig[:pod_url] = "http://example.org/" + AppConfig.normalize_pod_url + AppConfig[:pod_url].should == "http://example.org/" + end + it "does not add a prefix if there already is https:// on the front" do + AppConfig[:pod_url] = "https://example.org/" + AppConfig.normalize_pod_url + AppConfig[:pod_url].should == "https://example.org/" + end + end + + describe ".pod_uri" do + it "properly parses the pod_url" do + pod_uri = AppConfig[:pod_uri] + pod_uri.scheme.should == "http" + pod_uri.host.should == "example.org" + end + it "clears the cached pod_uri when you set pod_url" do + AppConfig[:pod_uri].host.should == "example.org" + AppConfig[:pod_url] = "http://joindiaspora.com" + AppConfig[:pod_uri].host.should == "joindiaspora.com" + end + end +end \ No newline at end of file