engine/spec/models/site_spec.rb

138 lines
4.0 KiB
Ruby
Raw Normal View History

2010-04-09 09:23:41 +00:00
require 'spec_helper'
2010-04-09 09:23:41 +00:00
describe Site do
2010-06-14 13:04:01 +00:00
before(:each) do
Locomotive.stubs(:add_heroku_domain).returns(true)
end
2010-04-09 09:23:41 +00:00
it 'should have a valid factory' do
Factory.build(:site).should be_valid
end
2010-04-09 09:23:41 +00:00
## Validations ##
2010-04-09 09:23:41 +00:00
it 'should validate presence of name' do
site = Factory.build(:site, :name => nil)
site.should_not be_valid
site.errors[:name].should == ["can't be blank"]
end
2010-04-10 15:25:07 +00:00
it 'should validate presence of subdomain' do
2010-04-09 09:23:41 +00:00
site = Factory.build(:site, :subdomain => nil)
site.should_not be_valid
2010-04-10 15:25:07 +00:00
site.errors[:subdomain].should == ["can't be blank"]
end
%w{test test42}.each do |subdomain|
2010-04-10 15:25:07 +00:00
it "should accept subdomain like '#{subdomain}'" do
Factory.build(:site, :subdomain => subdomain).should be_valid
end
end
['-', '_test', 'test_', 't est', '42', '42test', 'foo_bar'].each do |subdomain|
2010-04-10 15:25:07 +00:00
it "should not accept subdomain like '#{subdomain}'" do
(site = Factory.build(:site, :subdomain => subdomain)).should_not be_valid
site.errors[:subdomain].should == ['is invalid']
end
end
2010-04-10 15:25:07 +00:00
it "should not use reserved keywords as subdomain" do
%w{www admin email blog webmail mail support help site sites}.each do |subdomain|
(site = Factory.build(:site, :subdomain => subdomain)).should_not be_valid
site.errors[:subdomain].should == ['is reserved']
end
end
2010-04-10 15:25:07 +00:00
it 'should validate uniqueness of subdomain' do
Factory(:site)
2010-04-10 15:25:07 +00:00
(site = Factory.build(:site)).should_not be_valid
site.errors[:subdomain].should == ["is already taken"]
end
2010-04-10 15:25:07 +00:00
it 'should validate uniqueness of domains' do
Factory(:site, :domains => %w{www.acme.net www.acme.com})
2010-04-10 15:25:07 +00:00
(site = Factory.build(:site, :domains => %w{www.acme.com})).should_not be_valid
site.errors[:domains].should == ["www.acme.com is already taken"]
(site = Factory.build(:site, :subdomain => 'foo', :domains => %w{acme.example.com})).should_not be_valid
site.errors[:domains].should == ["acme.example.com is already taken"]
2010-04-09 09:23:41 +00:00
end
2010-04-10 15:25:07 +00:00
it 'should validate format of domains' do
site = Factory.build(:site, :domains => ['barformat.superlongextension', '-foo.net'])
site.should_not be_valid
site.errors[:domains].should == ['barformat.superlongextension is invalid', '-foo.net is invalid']
end
2010-04-10 15:25:07 +00:00
## Named scopes ##
2010-04-10 15:25:07 +00:00
it 'should retrieve sites by domain' do
site_1 = Factory(:site, :domains => %w{www.acme.net})
site_2 = Factory(:site, :subdomain => 'test', :domains => %w{www.example.com})
2010-04-10 15:25:07 +00:00
sites = Site.match_domain('www.acme.net')
sites.size.should == 1
sites.first.should == site_1
2010-04-10 15:25:07 +00:00
sites = Site.match_domain('www.example.com')
sites.size.should == 1
sites.first.should == site_2
2010-04-10 15:25:07 +00:00
sites = Site.match_domain('test.example.com')
sites.size.should == 1
sites.first.should == site_2
2010-04-10 15:25:07 +00:00
sites = Site.match_domain('www.unknown.com')
sites.should be_empty
end
## Associations ##
it 'should have many accounts' do
site = Factory.build(:site)
account_1, account_2 = Factory(:account), Factory(:account, :name => 'homer', :email => 'homer@simpson.net')
site.memberships.build(:account => account_1, :admin => true)
site.memberships.build(:account => account_2)
site.memberships.size.should == 2
site.accounts.should == [account_1, account_2]
end
2010-04-10 15:25:07 +00:00
## Methods ##
2010-04-10 15:25:07 +00:00
it 'should return domains without subdomain' do
site = Factory(:site, :domains => %w{www.acme.net www.acme.com})
site.domains.should == %w{www.acme.net www.acme.com acme.example.com}
site.domains_without_subdomain.should == %w{www.acme.net www.acme.com}
end
describe 'once created' do
before(:each) do
@site = Factory(:site)
end
it 'should create index and 404 pages' do
@site.pages.size.should == 2
@site.pages.map(&:fullpath).sort.should == %w{404 index}
end
end
describe 'deleting in cascade' do
before(:each) do
@site = Factory(:site)
end
it 'should also destroy pages' do
lambda {
@site.destroy
}.should change(Page, :count).by(-2)
end
end
end