Replace APP_CONFIG with lib/app_config.rb so that it's testable.

This commit is contained in:
Sarah Mei 2010-12-24 12:31:09 -08:00
parent aeaabd29f1
commit 1f67cc2485
29 changed files with 144 additions and 109 deletions

View file

@ -25,7 +25,7 @@ class RegistrationsController < Devise::RegistrationsController
private
def check_registrations_open!
if APP_CONFIG[:registrations_closed]
if AppConfig[:registrations_closed]
flash[:error] = t('registrations.closed')
redirect_to new_user_session_path
end

View file

@ -1,6 +1,6 @@
class Notifier < ActionMailer::Base
default :from => APP_CONFIG[:smtp_sender_address]
default :from => AppConfig[:smtp_sender_address]
ATTACHMENT = File.read("#{Rails.root}/public/images/white_on_grey.png")
@ -18,7 +18,7 @@ class Notifier < ActionMailer::Base
@string = string.html_safe
attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT
mail(:to => @recipient.email,
:subject => I18n.t('notifier.single_admin.subject'), :host => APP_CONFIG[:pod_uri].host)
:subject => I18n.t('notifier.single_admin.subject'), :host => AppConfig[:pod_uri].host)
end
def new_request(recipient_id, sender_id)
@ -30,7 +30,7 @@ class Notifier < ActionMailer::Base
attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT
mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => I18n.t('notifier.new_request.subject', :from => @sender.name), :host => APP_CONFIG[:pod_uri].host)
:subject => I18n.t('notifier.new_request.subject', :from => @sender.name), :host => AppConfig[:pod_uri].host)
end
def request_accepted(recipient_id, sender_id)
@ -42,7 +42,7 @@ class Notifier < ActionMailer::Base
attachments.inline['diaspora_white_on_grey.png'] = ATTACHMENT
mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => I18n.t('notifier.request_accepted.subject', :name => @sender.name), :host => APP_CONFIG[:pod_uri].host)
:subject => I18n.t('notifier.request_accepted.subject', :name => @sender.name), :host => AppConfig[:pod_uri].host)
end
private

View file

@ -81,8 +81,8 @@ class Photo < Post
end
def absolute_url *args
pod_url = APP_CONFIG[:pod_url].dup
pod_url.chop! if APP_CONFIG[:pod_url][-1,1] == '/'
pod_url = AppConfig[:pod_url].dup
pod_url.chop! if AppConfig[:pod_url][-1,1] == '/'
"#{pod_url}#{url(*args)}"
end

View file

@ -106,8 +106,8 @@ class Profile
private
def absolutify_local_url url
pod_url = APP_CONFIG[:pod_url].dup
pod_url.chop! if APP_CONFIG[:pod_url][-1,1] == '/'
pod_url = AppConfig[:pod_url].dup
pod_url.chop! if AppConfig[:pod_url][-1,1] == '/'
"#{pod_url}#{url}"
end
end

View file

@ -153,7 +153,7 @@ class User
def post_to_hub(post)
Rails.logger.debug("event=post_to_service type=pubsub sender_handle=#{self.diaspora_handle}")
EventMachine::PubSubHubbub.new(APP_CONFIG[:pubsub_server]).publish self.public_url
EventMachine::PubSubHubbub.new(AppConfig[:pubsub_server]).publish self.public_url
end
def update_post(post, post_hash = {})
@ -355,8 +355,8 @@ class User
opts[:person][:profile] ||= Profile.new
self.person = Person.new(opts[:person])
self.person.diaspora_handle = "#{opts[:username]}@#{APP_CONFIG[:pod_uri].host}"
self.person.url = APP_CONFIG[:pod_url]
self.person.diaspora_handle = "#{opts[:username]}@#{AppConfig[:pod_uri].host}"
self.person.url = AppConfig[:pod_url]
self.serialized_private_key ||= User.generate_key

View file

@ -5,7 +5,7 @@
= render :partial => 'notifier/notifier_css'
%body
%header
= image_tag APP_CONFIG[:pod_url] + 'images/diaspora_white_on_grey.png'
= image_tag AppConfig[:pod_url] + 'images/diaspora_white_on_grey.png'
#container
- @invs = @resource.invitations_to_me
%p

View file

@ -12,7 +12,7 @@
= f.label :username , t('username')
= f.text_field :username
%p.user_network
="@#{APP_CONFIG[:pod_uri].host}"
="@#{AppConfig[:pod_uri].host}"
%p
= f.label :password , t('password')

View file

@ -11,7 +11,7 @@
= f.label :username , t('username')
= f.text_field :username
%p.user_network
="@#{APP_CONFIG[:pod_uri].host}/"
="@#{AppConfig[:pod_uri].host}/"
%p
= f.label :password , t('password')

View file

@ -1,7 +1,7 @@
- if controller_name != 'sessions'
= link_to t('.sign_in'), new_session_path(resource_name)
%br/
- if !APP_CONFIG[:registrations_closed] && devise_mapping.registerable? && controller_name != 'registrations'
- if !AppConfig[:registrations_closed] && devise_mapping.registerable? && controller_name != 'registrations'
= link_to t('.sign_up'), new_registration_path(resource_name)
%br/
- else

View file

@ -5,6 +5,6 @@
:javascript
WebSocket.__swfLocation = "#{javascript_path 'vendor/WebSocketMain.swf'}";
$(document).ready(function(){
WSR.initialize("#{(APP_CONFIG[:socket_secure])?'wss':'ws'}://#{request.host}:#{APP_CONFIG[:socket_port]}/");
WSR.initialize("#{(AppConfig[:socket_secure])?'wss':'ws'}://#{request.host}:#{AppConfig[:socket_port]}/");
});

View file

@ -42,10 +42,10 @@
= yield(:head)
= csrf_meta_tag
-if APP_CONFIG[:google_a_site]
-if AppConfig[:google_a_site]
:javascript
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '#{APP_CONFIG[:google_a_site]}']);
_gaq.push(['_setAccount', '#{AppConfig[:google_a_site]}']);
_gaq.push(['_trackPageview']);
(function() {
@ -54,12 +54,12 @@
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
-if APP_CONFIG[:piwik_id]
-if AppConfig[:piwik_id]
:javascript
var pkBaseURL = (("https:" == document.location.protocol) ? "https://#{APP_CONFIG[:piwik_url]}/" : "http://#{APP_CONFIG[:piwik_url]}/");
var pkBaseURL = (("https:" == document.location.protocol) ? "https://#{AppConfig[:piwik_url]}/" : "http://#{AppConfig[:piwik_url]}/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", #{APP_CONFIG[:piwik_id]});
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", #{AppConfig[:piwik_id]});
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}

View file

@ -1,9 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'
xmlns:hm='http://host-meta.net/xrd/1.0'>
<hm:Host><%= APP_CONFIG[:pod_uri].host %></hm:Host>
<hm:Host><%= AppConfig[:pod_uri].host %></hm:Host>
<Link rel='lrdd'
template='<%= APP_CONFIG[:pod_url] %>webfinger?q={uri}'>
template='<%= AppConfig[:pod_url] %>webfinger?q={uri}'>
<Title>Resource Descriptor</Title>
</Link>
</XRD>

View file

@ -1,4 +1,4 @@
- if APP_CONFIG[:invites_off]
- if AppConfig[:invites_off]
= t('.invites_closed')
-else
-if invites > 0

View file

@ -1,42 +1,14 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
# 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 APP_CONFIG. Unless stated below, each entry is a the string in
# the file app_config.yml, as applicable for current environment.
# Sets up AppConfig. Unless stated below, each entry is a the string in
# the file app_config.yml, as applicable for current environment.
#
# Specific items
# * pod_url: As in app_config.yml, normalized with a trailing /.
# * pod_uri: An uri object derived from pod_url.
# Specific items
# * pod_url: As in app_config.yml, normalized with a trailing /.
# * pod_uri: An uri object derived from pod_url.
require 'uri'
require File.join(Rails.root, 'lib', 'app_config')
def load_config_yaml filename
YAML.load(File.read(filename))
end
if File.exist? "#{Rails.root}/config/app_config.yml"
all_envs = load_config_yaml "#{Rails.root}/config/app_config.yml"
all_envs = load_config_yaml "#{Rails.root}/config/app_config.yml.example" unless all_envs
else
puts "WARNING: No config/app_config.yml found! Look at config/app_config.yml.example for help."
all_envs = load_config_yaml "#{Rails.root}/config/app_config.yml.example"
end
if all_envs[Rails.env.to_s]
APP_CONFIG = all_envs['default'].merge(all_envs[Rails.env.to_s]).symbolize_keys
else
APP_CONFIG = all_envs['default'].symbolize_keys
end
begin
APP_CONFIG[:pod_uri] = URI.parse( APP_CONFIG[:pod_url])
rescue
puts "WARNING: pod url " + APP_CONFIG[:pod_url] + " is not a legal URI"
end
APP_CONFIG[:pod_url] = APP_CONFIG[:pod_uri].normalize.to_s
if APP_CONFIG[:pod_uri].host == "example.org" && Rails.env != "test"
puts "WARNING: Please modify your app_config.yml to have a proper pod_url!"
end
AppConfig.configure_for_environment(Rails.env)

View file

@ -2,7 +2,7 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
ENV['MONGODB_URL'] = ENV['MONGOHQ_URL'] || URI::Generic.build(:scheme => 'mongodb', :host => APP_CONFIG[:mongo_host], :port => APP_CONFIG[:mongo_port], :path => "/diaspora-#{Rails.env}").to_s
ENV['MONGODB_URL'] = ENV['MONGOHQ_URL'] || URI::Generic.build(:scheme => 'mongodb', :host => AppConfig[:mongo_host], :port => AppConfig[:mongo_port], :path => "/diaspora-#{Rails.env}").to_s
MongoMapper.config = {::Rails.env => {'uri' => ENV['MONGODB_URL']}}
MongoMapper.connect ::Rails.env

View file

@ -6,8 +6,8 @@
# four configuration values can also be set straight in your models.
Devise.setup do |config|
# Configure the e-mail address which will be shown in DeviseMailer.
if APP_CONFIG[:smtp_sender_address]
config.mailer_sender = APP_CONFIG[:smtp_sender_address]
if AppConfig[:smtp_sender_address]
config.mailer_sender = AppConfig[:smtp_sender_address]
else
unless Rails.env == 'test'
Rails.logger.warn("No smtp sender address set, mail may fail.")

View file

@ -3,23 +3,23 @@
# the COPYRIGHT file.
Diaspora::Application.configure do
config.action_mailer.default_url_options = {:host => APP_CONFIG[:pod_uri].host}
unless Rails.env == 'test' || APP_CONFIG[:mailer_on] != true
config.action_mailer.default_url_options = {:host => AppConfig[:pod_uri].host}
unless Rails.env == 'test' || AppConfig[:mailer_on] != true
config.action_mailer.delivery_method = :smtp
if APP_CONFIG[:smtp_authentication] == "none"
if AppConfig[:smtp_authentication] == "none"
config.action_mailer.smtp_settings = {
:address => APP_CONFIG[:smtp_address],
:port => APP_CONFIG[:smtp_port],
:domain => APP_CONFIG[:smtp_domain]
:address => AppConfig[:smtp_address],
:port => AppConfig[:smtp_port],
:domain => AppConfig[:smtp_domain]
}
else
config.action_mailer.smtp_settings = {
:address => APP_CONFIG[:smtp_address],
:port => APP_CONFIG[:smtp_port],
:domain => APP_CONFIG[:smtp_domain],
:authentication => APP_CONFIG[:smtp_authentication],
:user_name => APP_CONFIG[:smtp_username],
:password => APP_CONFIG[:smtp_password],
:address => AppConfig[:smtp_address],
:port => AppConfig[:smtp_port],
:domain => AppConfig[:smtp_domain],
:authentication => AppConfig[:smtp_authentication],
:user_name => AppConfig[:smtp_username],
:password => AppConfig[:smtp_password],
:enable_starttls_auto => true
}
end

63
lib/app_config.rb Normal file
View file

@ -0,0 +1,63 @@
# Copyright (c) 2010, 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
def self.[](key)
config_vars[key]
end
def self.[]=(key, value)
config_vars[key] = value
end
def self.configure_for_environment(env)
load_config_for_environment(env)
generate_pod_uri
normalize_pod_url
check_pod_uri
end
def self.load_config_for_environment(env)
if File.exist? "#{Rails.root}/config/app_config.yml"
all_envs = load_config_yaml "#{Rails.root}/config/app_config.yml"
all_envs = load_config_yaml "#{Rails.root}/config/app_config.yml.example" unless all_envs
else
puts "WARNING: No config/app_config.yml found! Look at config/app_config.yml.example for help."
all_envs = load_config_yaml "#{Rails.root}/config/app_config.yml.example"
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'
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_config.yml to have a proper pod_url!"
end
end
def self.load_config_yaml filename
YAML.load(File.read(filename))
end
end

View file

@ -43,7 +43,7 @@ module Diaspora
def create_endpoints
<<-XML
<link href="#{APP_CONFIG[:pubsub_server]}" rel="hub"/>
<link href="#{AppConfig[:pubsub_server]}" rel="hub"/>
<link href="#{@user.public_url}.atom" rel="self" type="application/atom+xml"/>
XML
end

View file

@ -31,13 +31,13 @@ module RakeHelpers
def fix_diaspora_handle_spaces(test = true)
offenders = {}
space_people = Person.all(:diaspora_handle => / /, :url => APP_CONFIG[:pod_url]) # this is every person with a space....
space_people = Person.all(:diaspora_handle => / /, :url => AppConfig[:pod_url]) # this is every person with a space....
#these people dont even have users.... they are totally messed up
totally_messed_up_people = space_people.find_all{|x| x.owner.nil?}
totally_messed_up_people.each{|x| x.delete}
space_people = Person.all(:diaspora_handle => / /, :owner_id.ne => nil, :url => APP_CONFIG[:pod_url]) # this is every person with a space....
space_people = Person.all(:diaspora_handle => / /, :owner_id.ne => nil, :url => AppConfig[:pod_url]) # this is every person with a space....
space_people.each do |person|
user = person.owner
@ -62,7 +62,7 @@ mail
end
def new_diaspora_handle(user)
"#{user.username}@#{APP_CONFIG[:pod_uri].host}"
"#{user.username}@#{AppConfig[:pod_uri].host}"
end
def update_my_posts_with_new_diaspora_handle(user, new_diaspora_handle, test)

View file

@ -6,10 +6,10 @@ namespace :backup do
task :mongo do
Rails.logger.info("event=backup status=start type=mongo")
if APP_CONFIG[:cloudfiles_username] && APP_CONFIG[:cloudfiles_api_key]
if AppConfig[:cloudfiles_username] && AppConfig[:cloudfiles_api_key]
puts "Logging into Cloud Files"
cf = CloudFiles::Connection.new(:username => APP_CONFIG[:cloudfiles_username], :api_key => APP_CONFIG[:cloudfiles_api_key])
cf = CloudFiles::Connection.new(:username => AppConfig[:cloudfiles_username], :api_key => AppConfig[:cloudfiles_api_key])
mongo_container = cf.container("Mongo Backup")
puts "Dumping Mongo"
@ -35,10 +35,10 @@ namespace :backup do
task :photos do
Rails.logger.info("event=backup status=start type=photos")
if APP_CONFIG[:cloudfiles_username] && APP_CONFIG[:cloudfiles_api_key]
if AppConfig[:cloudfiles_username] && AppConfig[:cloudfiles_api_key]
puts "Logging into Cloud Files"
cf = CloudFiles::Connection.new(:username => APP_CONFIG[:cloudfiles_username], :api_key => APP_CONFIG[:cloudfiles_api_key])
cf = CloudFiles::Connection.new(:username => AppConfig[:cloudfiles_username], :api_key => AppConfig[:cloudfiles_api_key])
photo_container = cf.container("Photo Backup")
tar_name = "photos_#{Time.now.to_i}.tar"

View file

@ -73,7 +73,7 @@ namespace :db do
require File.dirname(__FILE__) + '/../../config/environment'
Person.where(:url => 'example.org').all.each{|person|
if person.owner
person.url = APP_CONFIG[:pod_url]
person.url = AppConfig[:pod_url]
person.diaspora_handle = person.owner.diaspora_handle
person.save
end

View file

@ -25,7 +25,7 @@ def write_pidfile
end
def debug_pp thing
pp thing if APP_CONFIG[:socket_debug] || ENV['SOCKET_DEBUG']
pp thing if AppConfig[:socket_debug] || ENV['SOCKET_DEBUG']
end
def process_message
@ -44,15 +44,15 @@ begin
EM.run {
Diaspora::WebSocket.initialize_channels
socket_params = { :host => APP_CONFIG[:socket_host],
:port => APP_CONFIG[:socket_port],
:debug =>APP_CONFIG[:socket_debug] }
socket_params = { :host => AppConfig[:socket_host],
:port => AppConfig[:socket_port],
:debug =>AppConfig[:socket_debug] }
if APP_CONFIG[:socket_secure] && APP_CONFIG[:socket_private_key_location] && APP_CONFIG[:socket_cert_chain_location]
if AppConfig[:socket_secure] && AppConfig[:socket_private_key_location] && AppConfig[:socket_cert_chain_location]
socket_params[:secure] = true;
socket_params[:tls_options] = {
:private_key_file => APP_CONFIG[:socket_private_key_location],
:cert_chain_file => APP_CONFIG[:socket_cert_chain_location]
:private_key_file => AppConfig[:socket_private_key_location],
:cert_chain_file => AppConfig[:socket_cert_chain_location]
}
end
@ -91,7 +91,7 @@ begin
end
}
end
PID_FILE = (APP_CONFIG[:socket_pidfile] ? APP_CONFIG[:socket_pidfile] : 'tmp/diaspora-ws.pid')
PID_FILE = (AppConfig[:socket_pidfile] ? AppConfig[:socket_pidfile] : 'tmp/diaspora-ws.pid')
write_pidfile
puts "Websocket server started."
process_message

View file

@ -6,12 +6,12 @@ describe 'making sure the config is parsed as should' do
describe 'pod_url' do
it 'should have a trailing slash' do
APP_CONFIG[:pod_url].should == 'http://example.org/'
AppConfig[:pod_url].should == 'http://example.org/'
end
end
describe 'terse_pod_url'
it 'should be correctly parsed' do
APP_CONFIG[:pod_uri].host.should == 'example.org'
AppConfig[:pod_uri].host.should == 'example.org'
end
end

View file

@ -22,10 +22,10 @@ describe RegistrationsController do
describe '#check_registrations_open!' do
before do
APP_CONFIG[:registrations_closed] = true
AppConfig[:registrations_closed] = true
end
after do
APP_CONFIG[:registrations_closed] = false
AppConfig[:registrations_closed] = false
end
it 'redirects #new to the login page' do
get :new

View file

@ -35,7 +35,7 @@ Factory.define :user do |u|
user.person = Factory.build(:person, :profile => Factory.create(:profile),
:owner_id => user._id,
:serialized_public_key => user.encryption_key.public_key.export,
:diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}")
:diaspora_handle => "#{user.username}@#{AppConfig[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}")
end
end

View file

@ -43,13 +43,13 @@ describe RakeHelpers do
Factory(:person)
5.times do |number|
f = Factory.create(:user)
f.person.diaspora_handle = "#{f.username} #{APP_CONFIG[:pod_uri].host}"
f.person.url = APP_CONFIG[:pod_url]
f.person.diaspora_handle = "#{f.username} #{AppConfig[:pod_uri].host}"
f.person.url = AppConfig[:pod_url]
f.person.save(:validate => false)
end
p = Factory(:person)
p.diaspora_handle = "bubblegoose @#{APP_CONFIG[:pod_uri].host}"
p.url = APP_CONFIG[:pod_url]
p.diaspora_handle = "bubblegoose @#{AppConfig[:pod_uri].host}"
p.url = AppConfig[:pod_url]
p.save(:validate => false)
end
@ -73,7 +73,7 @@ describe RakeHelpers do
it 'should update a users username, his persons diaspora hande, and posts' do
billy = Factory.create(:user)
billy.username = "ma.x"
billy.person.diaspora_handle = "ma.x@#{APP_CONFIG[:pod_uri].host}"
billy.person.diaspora_handle = "ma.x@#{AppConfig[:pod_uri].host}"
billy.person.save(:validate => false)
billy.save(:validate => false)
@ -82,7 +82,7 @@ describe RakeHelpers do
RakeHelpers::fix_periods_in_username(false)
new_d_handle = "max@#{APP_CONFIG[:pod_uri].host}"
new_d_handle = "max@#{AppConfig[:pod_uri].host}"
User.first.username.should == 'max'
User.first.person.diaspora_handle.should == new_d_handle

View file

@ -33,13 +33,13 @@ describe Person do
context 'local people' do
it 'uses the pod config url to set the diaspora_handle' do
new_user = Factory.create(:user)
new_user.person.diaspora_handle.should == new_user.username + "@" + APP_CONFIG[:pod_uri].host
new_user.person.diaspora_handle.should == new_user.username + "@" + AppConfig[:pod_uri].host
end
end
context 'remote people' do
it 'stores the diaspora_handle in the database' do
@person.diaspora_handle.include?(APP_CONFIG[:pod_uri].host).should be false
@person.diaspora_handle.include?(AppConfig[:pod_uri].host).should be false
end
end

View file

@ -49,7 +49,7 @@ describe Profile do
fixture_name = File.dirname(__FILE__) + '/../fixtures/button.png'
@photo = @user.post(:photo, :user_file => File.open(fixture_name), :to => 'all')
@profile.image_url = "http://tom.joindiaspora.com/images/user/tom.jpg"
@pod_url = (APP_CONFIG[:pod_url][-1,1] == '/' ? APP_CONFIG[:pod_url].chop : APP_CONFIG[:pod_url])
@pod_url = (AppConfig[:pod_url][-1,1] == '/' ? AppConfig[:pod_url].chop : AppConfig[:pod_url])
end
it 'ignores an empty string' do
lambda {@profile.image_url = ""}.should_not change(@profile, :image_url)