engine/spec/models/locomotive/site_spec.rb

177 lines
5.4 KiB
Ruby
Raw Normal View History

# coding: utf-8
2010-04-09 09:23:41 +00:00
require 'spec_helper'
describe Locomotive::Site do
2010-04-09 09:23:41 +00:00
it 'should have a valid factory' do
2011-08-25 21:28:56 +00:00
FactoryGirl.build(:site).should be_valid
2010-04-09 09:23:41 +00:00
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
2011-08-25 21:28:56 +00:00
site = FactoryGirl.build(:site, :name => nil)
2010-04-09 09:23:41 +00:00
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
2011-08-25 21:28:56 +00:00
site = FactoryGirl.build(:site, :subdomain => nil)
2010-04-09 09:23:41 +00:00
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 foo_bar}.each do |subdomain|
2010-04-10 15:25:07 +00:00
it "should accept subdomain like '#{subdomain}'" do
2011-08-25 21:28:56 +00:00
FactoryGirl.build(:site, :subdomain => subdomain).should be_valid
2010-04-10 15:25:07 +00:00
end
end
['-', '_test', 'test_', 't est', '42', '42test'].each do |subdomain|
2010-04-10 15:25:07 +00:00
it "should not accept subdomain like '#{subdomain}'" do
2011-08-25 21:28:56 +00:00
(site = FactoryGirl.build(:site, :subdomain => subdomain)).should_not be_valid
2010-04-10 15:25:07 +00:00
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|
2011-08-25 21:28:56 +00:00
(site = FactoryGirl.build(:site, :subdomain => subdomain)).should_not be_valid
2010-04-10 15:25:07 +00:00
site.errors[:subdomain].should == ['is reserved']
end
end
2010-04-10 15:25:07 +00:00
it 'should validate uniqueness of subdomain' do
2011-08-25 21:28:56 +00:00
FactoryGirl.create(:site)
(site = FactoryGirl.build(:site)).should_not be_valid
2010-04-10 15:25:07 +00:00
site.errors[:subdomain].should == ["is already taken"]
end
2010-04-10 15:25:07 +00:00
it 'should validate uniqueness of domains' do
2011-08-25 21:28:56 +00:00
FactoryGirl.create(:site, :domains => %w{www.acme.net www.acme.com})
2011-08-25 21:28:56 +00:00
(site = FactoryGirl.build(:site, :domains => %w{www.acme.com})).should_not be_valid
2010-04-10 15:25:07 +00:00
site.errors[:domains].should == ["www.acme.com is already taken"]
2011-08-25 21:28:56 +00:00
(site = FactoryGirl.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
2011-08-25 21:28:56 +00:00
site = FactoryGirl.build(:site, :domains => ['barformat.superlongextension', '-foo.net'])
2010-04-10 15:25:07 +00:00
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
2011-08-25 21:28:56 +00:00
site_1 = FactoryGirl.create(:site, :domains => %w{www.acme.net})
site_2 = FactoryGirl.create(:site, :subdomain => 'test', :domains => %w{www.example.com})
2011-11-24 13:37:17 +00:00
sites = Locomotive::Site.match_domain('www.acme.net')
2010-04-10 15:25:07 +00:00
sites.size.should == 1
sites.first.should == site_1
2011-11-24 13:37:17 +00:00
sites = Locomotive::Site.match_domain('www.example.com')
2010-04-10 15:25:07 +00:00
sites.size.should == 1
sites.first.should == site_2
2011-11-24 13:37:17 +00:00
sites = Locomotive::Site.match_domain('test.example.com')
2010-04-10 15:25:07 +00:00
sites.size.should == 1
sites.first.should == site_2
2011-11-24 13:37:17 +00:00
sites = Locomotive::Site.match_domain('www.unknown.com')
2010-04-10 15:25:07 +00:00
sites.should be_empty
end
## Associations ##
it 'should have many accounts' do
2011-08-25 21:28:56 +00:00
site = FactoryGirl.build(:site)
account_1, account_2 = FactoryGirl.create(:account), FactoryGirl.create(: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
2011-08-25 21:28:56 +00:00
site = FactoryGirl.create(:site, :domains => %w{www.acme.net www.acme.com})
2010-04-10 15:25:07 +00:00
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
2011-08-25 21:28:56 +00:00
@site = FactoryGirl.create(: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
it 'translates the index/404 pages if a new locale is added' do
@site.update_attributes :locales => %w(en fr)
@site.errors.should be_empty
::Mongoid::Fields::I18n.with_locale('fr') do
@site.pages.root.first.tap do |page|
page.title.should == "Page d'accueil"
page.slug.should == 'index'
end
@site.pages.not_found.first.tap do |page|
page.title.should == 'Page non trouvée'
page.slug.should == '404'
end
end
end
it 'translates the index/404 pages if the default locale changes' do
@site.update_attributes :locales => %w(fr en)
@site.errors.should be_empty
::Mongoid::Fields::I18n.with_locale('fr') do
@site.pages.root.first.tap do |page|
page.title.should == "Page d'accueil"
page.slug.should == 'index'
end
@site.pages.not_found.first.tap do |page|
page.title.should == 'Page non trouvée'
page.slug.should == '404'
end
end
end
it 'does not allow to remove the default locale' do
@site.update_attributes :locales => %w(fr)
@site.errors[:locales].should == ['The previous default locale can not be removed right away.']
end
end
describe 'deleting in cascade' do
before(:each) do
2011-08-25 21:28:56 +00:00
@site = FactoryGirl.create(:site)
end
it 'should also destroy pages' do
lambda {
@site.destroy
2011-11-24 13:37:17 +00:00
}.should change(Locomotive::Page, :count).by(-2)
end
end
end