Skip to content

Commit

Permalink
Enhance the site host validations so it requires a valid domain name.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@1600 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Aug 13, 2006
1 parent 63ad2be commit e22c8b3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
* SVN *

* Enhance the site host validations so it requires a valid domain name.

* Add basic asset support [Justin, Rick]

* fix user fixtures [Cristi Balan]

* Add windows binary file support to acts as attachment [Cristi Balan]
Expand Down
6 changes: 6 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def home

serialize :filters, Array

before_validation :downcase_host
before_validation :set_default_timezone
before_validation_on_create :set_default_comment_options
validates_format_of :host, :with => /^([a-z0-9]([-a-z0-9]*[a-z0-9])?\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/
validates_uniqueness_of :host

with_options :order => 'contents.created_at', :class_name => 'Comment' do |comment|
Expand Down Expand Up @@ -46,6 +48,10 @@ def timezone=(name)
end

protected
def downcase_host
self.host = host.to_s.downcase
end

def set_default_timezone
self.timezone = 'UTC' if read_attribute(:timezone).blank?
true
Expand Down
2 changes: 1 addition & 1 deletion db/bootstrap/sites.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
default:
id: 1
host: unusedfornow
host: unusedfornow.com
title: Mephisto
subtitle: Publish With Impunity
email: email@domain.com
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/042_rename_default_site_hosts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RenameDefaultSiteHosts < ActiveRecord::Migration
class Site < ActiveRecord::Base; end
def self.up
Site.update_all "host = 'unusedfornow.com'", "host = 'unusedfornow'"
end

def self.down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.

ActiveRecord::Schema.define(:version => 41) do
ActiveRecord::Schema.define(:version => 42) do

create_table "assets", :force => true do |t|
t.column "content_type", :string
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/sites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
first:
id: 1
title: Mephisto
host: test.host
host: test.com
filters: "--- \n- :foo\n- :bar\n"
approve_comments: false
comment_age: 30
Expand All @@ -18,14 +18,14 @@ hostess:
title: Cupcake
subtitle: Your hostess
filters: "--- \n- :foo\n- :bar\n"
host: cupcake.host
host: cupcake.com
timezone: UTC
approve_comments: false
comment_age: 0
articles_per_page: 10
garden:
id: 3
title: walled garden
host: garden.host
host: garden.com
timezone: UTC
comment_age: -1
4 changes: 2 additions & 2 deletions test/functional/comments_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_should_reject_comment_on_expired_article
end

def test_should_add_comment_in_site
@request.host = 'cupcake.host'
host! 'cupcake.com'
assert_difference Comment, :count do
assert_difference contents(:cupcake_welcome), :comments_count do
post :create, contents(:cupcake_welcome).hash_for_permalink.merge(:comment => {
Expand All @@ -64,7 +64,7 @@ def test_should_add_comment_in_site
end

def test_should_not_add_comment_across_site
@request.host = 'cupcake.host'
host! 'cupcake.com'
assert_no_difference Comment, :count do
assert_no_difference contents(:welcome), :comments_count do
assert_no_difference contents(:cupcake_welcome), :comments_count do
Expand Down
10 changes: 5 additions & 5 deletions test/functional/feed_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def test_feed_assigns
end

def test_feed_comes_from_site
@request.host = 'cupcake.host'
host! 'cupcake.com'
get :feed, :sections => ['about']
assert_equal sections(:cupcake_about), assigns(:section)
assert_equal [contents(:cupcake_welcome)], assigns(:articles)
end

def test_site_in_feed_links
@request.host = 'cupcake.host'
host! 'cupcake.com'
get :feed, :sections => []
assert_equal sections(:cupcake_home), assigns(:section)
assert_equal [contents(:cupcake_welcome)], assigns(:articles)
assert_tag :tag => 'link', :attributes => {:href => 'http://cupcake.host/'}
assert_models_equal [sections(:cupcake_home)], [assigns(:section)]
assert_models_equal [contents(:cupcake_welcome)], assigns(:articles)
assert_tag 'link', :attributes => {:href => 'http://cupcake.com/'}
end
end
10 changes: 5 additions & 5 deletions test/functional/mephisto_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setup
@controller = MephistoController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
host! 'test.host'
host! 'test.com'
end

def test_routing
Expand Down Expand Up @@ -43,7 +43,7 @@ def test_should_list_on_home
end

def test_should_show_paged_home
host! 'cupcake.host'
host! 'cupcake.com'
get_mephisto
assert_equal sites(:hostess), assigns(:site)
assert_equal sections(:cupcake_home), assigns(:section)
Expand All @@ -61,7 +61,7 @@ def test_should_show_error_on_bad_blog_url
end

def test_should_show_error_on_bad_paged_url
host! 'cupcake.host'
host! 'cupcake.com'
{'foobar/basd' => sections(:cupcake_home), 'about/foo' => sections(:cupcake_about)}.each do |path, section|
get_mephisto path
assert_equal sites(:hostess), assigns(:site)
Expand All @@ -83,7 +83,7 @@ def test_list_by_sections
end

def test_list_by_site_sections
host! 'cupcake.host'
host! 'cupcake.com'
get_mephisto 'about'
assert_equal sites(:hostess), assigns(:site)
assert_equal sections(:cupcake_about), assigns(:section)
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_should_show_entry
end

def test_should_show_site_entry
host! 'cupcake.host'
host! 'cupcake.com'
date = 3.days.ago
get :show, :year => date.year, :month => date.month, :day => date.day, :permalink => 'welcome-to-cupcake'
assert_equal contents(:cupcake_welcome).to_liquid['id'], assigns(:article)['id']
Expand Down
31 changes: 21 additions & 10 deletions test/unit/site_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@ class SiteTest < Test::Unit::TestCase

def test_should_validate_host
assert_valid sites(:first)
assert_equal true, Site.create(:host => sites(:first).host, :title => 'Copy').new_record?
end
assert_no_difference Site, :count do
assert_equal true, Site.create(:host => sites(:first).host.upcase, :title => 'Copy').new_record?
end
end

def test_should_require_valid_host_name
s = Site.new
['foo', '-34.com', 'A!'].each do |host|
s.host = host
s.valid?
assert s.errors.on(:host), "host valid with #{host}"
end
end

def test_should_set_default_comment_behavior
site = Site.new :host => 'foo'
assert site.valid?, site.errors.full_messages.to_sentence
site = Site.new :host => 'foo.com'
assert_valid site
assert site.accept_comments?
assert !site.approve_comments?
end

def test_should_create_section_without_accepting_comments
site = Site.new :host => 'foo', :comment_age => -1
assert site.valid?, site.errors.full_messages.to_sentence
site = Site.new :host => 'foo.com', :comment_age => -1
assert_valid site
assert !site.accept_comments?
assert !site.approve_comments?
end

def test_should_create_section_with_approving_comments
site = Site.new :host => 'foo', :approve_comments => true
assert site.valid?, site.errors.full_messages.to_sentence
site = Site.new :host => 'foo.com', :approve_comments => true
assert_valid site
assert site.accept_comments?
assert site.approve_comments?
end
Expand All @@ -35,8 +46,8 @@ def test_should_find_valid_articles
end

def test_should_find_host
assert_equal sites(:first), Site.find_by_host('test.host')
assert_equal sites(:hostess), Site.find_by_host('cupcake.host')
assert_equal sites(:first), Site.find_by_host('test.com')
assert_equal sites(:hostess), Site.find_by_host('cupcake.com')
end

def test_should_allow_empty_filter
Expand Down

0 comments on commit e22c8b3

Please sign in to comment.