2010-05-30 23:57:33 +00:00
|
|
|
require 'spec_helper'
|
2011-07-01 00:15:45 +00:00
|
|
|
require 'ostruct'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
describe 'Locomotive rendering system' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
before(:each) do
|
2010-05-30 23:57:33 +00:00
|
|
|
@controller = Locomotive::TestController.new
|
2011-11-26 06:24:34 +00:00
|
|
|
Locomotive::Site.any_instance.stubs(:create_default_pages!).returns(true)
|
2011-08-25 21:28:56 +00:00
|
|
|
@site = FactoryGirl.build(:site)
|
2011-11-26 06:24:34 +00:00
|
|
|
Locomotive::Site.stubs(:find).returns(@site)
|
2010-05-30 23:57:33 +00:00
|
|
|
@controller.current_site = @site
|
2011-08-25 21:28:56 +00:00
|
|
|
@page = FactoryGirl.build(:page, :site => nil, :published => true)
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
|
|
|
|
2011-07-01 00:15:45 +00:00
|
|
|
context '#liquid_context' do
|
|
|
|
|
2012-05-30 22:41:11 +00:00
|
|
|
before(:each) do
|
2011-07-01 00:15:45 +00:00
|
|
|
@controller.instance_variable_set(:@page, @page)
|
|
|
|
@controller.stubs(:flash).returns({})
|
|
|
|
@controller.stubs(:params).returns({})
|
|
|
|
@controller.stubs(:request).returns(OpenStruct.new(:url => '/'))
|
2012-05-30 22:41:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the current date and time' do
|
2011-07-01 00:15:45 +00:00
|
|
|
context = @controller.send(:locomotive_context)
|
|
|
|
context['now'].should_not be_blank
|
|
|
|
context['today'].should_not be_blank
|
|
|
|
end
|
|
|
|
|
2012-05-30 22:41:11 +00:00
|
|
|
it 'includes the locale' do
|
|
|
|
context = @controller.send(:locomotive_context)
|
|
|
|
context['locale'].should == 'en'
|
|
|
|
end
|
|
|
|
|
2011-07-01 00:15:45 +00:00
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
context 'setting the response' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
before(:each) do
|
2010-07-09 10:38:50 +00:00
|
|
|
@controller.instance_variable_set(:@page, @page)
|
2010-05-30 23:57:33 +00:00
|
|
|
@controller.send(:prepare_and_set_response, 'Hello world !')
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
it 'sets the content type to html' do
|
|
|
|
@controller.response.headers['Content-Type'].should == 'text/html; charset=utf-8'
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2012-03-22 18:20:31 +00:00
|
|
|
it 'sets the content type to the one specified by the page' do
|
|
|
|
@page.response_type = 'application/json'
|
|
|
|
@controller.send(:prepare_and_set_response, 'Hello world !')
|
|
|
|
@controller.response.headers['Content-Type'].should == 'application/json; charset=utf-8'
|
|
|
|
end
|
|
|
|
|
2011-01-09 02:08:32 +00:00
|
|
|
it 'sets the status to 200 OK' do
|
|
|
|
@controller.status.should == :ok
|
|
|
|
end
|
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
it 'displays the output' do
|
2010-05-30 23:57:33 +00:00
|
|
|
@controller.output.should == 'Hello world !'
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-16 14:43:29 +00:00
|
|
|
it 'does not set the cache' do
|
|
|
|
@controller.response.headers['Cache-Control'].should be_nil
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
it 'sets the cache by simply using etag' do
|
|
|
|
@page.cache_strategy = 'simple'
|
|
|
|
@page.stubs(:updated_at).returns(Time.now)
|
|
|
|
@controller.send(:prepare_and_set_response, 'Hello world !')
|
|
|
|
@controller.response.to_a # force to build headers
|
|
|
|
@controller.response.headers['Cache-Control'].should == 'public'
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
it 'sets the cache for Varnish' do
|
|
|
|
@page.cache_strategy = '3600'
|
|
|
|
@page.stubs(:updated_at).returns(Time.now)
|
|
|
|
@controller.send(:prepare_and_set_response, 'Hello world !')
|
|
|
|
@controller.response.to_a # force to build headers
|
|
|
|
@controller.response.headers['Cache-Control'].should == 'max-age=3600, public'
|
2010-06-16 14:43:29 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-01-09 02:08:32 +00:00
|
|
|
it 'sets the status to 404 not found when no page is found' do
|
2011-06-21 00:39:59 +00:00
|
|
|
@page.stubs(:not_found?).returns(true)
|
2011-01-09 02:08:32 +00:00
|
|
|
@controller.send(:prepare_and_set_response, 'Hello world !')
|
|
|
|
@controller.status.should == :not_found
|
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
context 'when retrieving page' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should retrieve the index page /' do
|
|
|
|
@controller.request.fullpath = '/'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
|
2010-06-01 00:06:46 +00:00
|
|
|
@controller.send(:locomotive_page).should_not be_nil
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should also retrieve the index page (index.html)' do
|
|
|
|
@controller.request.fullpath = '/index.html'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
|
2010-06-01 00:06:46 +00:00
|
|
|
@controller.send(:locomotive_page).should_not be_nil
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should retrieve it based on the full path' do
|
|
|
|
@controller.request.fullpath = '/about_us/team.html'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/content_type_template content_type_template/team}).returns([@page])
|
2010-06-01 00:06:46 +00:00
|
|
|
@controller.send(:locomotive_page).should_not be_nil
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-07-28 11:51:59 +00:00
|
|
|
it 'does not include the query string' do
|
|
|
|
@controller.request.fullpath = '/about_us/team.html?some=params&we=use'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{about_us/team about_us/content_type_template content_type_template/team}).returns([@page])
|
2011-07-28 11:51:59 +00:00
|
|
|
@controller.send(:locomotive_page).should_not be_nil
|
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
it 'should return the 404 page if the page does not exist' do
|
|
|
|
@controller.request.fullpath = '/contact'
|
2011-11-26 06:24:34 +00:00
|
|
|
(klass = Locomotive::Page).expects(:published).returns([true])
|
2010-07-17 20:51:52 +00:00
|
|
|
@controller.current_site.pages.expects(:not_found).returns(klass)
|
2010-05-30 23:57:33 +00:00
|
|
|
@controller.send(:locomotive_page).should be_true
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-06-22 15:53:29 +00:00
|
|
|
context 'redirect' do
|
2011-01-06 14:48:41 +00:00
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@page.redirect = true
|
|
|
|
@page.redirect_url = 'http://www.example.com/'
|
|
|
|
@controller.request.fullpath = '/contact'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
|
2011-01-06 14:48:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects to the redirect_url' do
|
|
|
|
@controller.expects(:redirect_to).with('http://www.example.com/').returns(true)
|
|
|
|
@controller.send(:render_locomotive_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
context 'templatized page' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
before(:each) do
|
2011-08-25 21:28:56 +00:00
|
|
|
@content_type = FactoryGirl.build(:content_type, :site => nil)
|
2011-12-22 23:45:32 +00:00
|
|
|
@content_entry = @content_type.entries.build(:_visible => true)
|
2010-07-19 00:09:10 +00:00
|
|
|
@page.templatized = true
|
2012-02-16 01:45:14 +00:00
|
|
|
@page.stubs(:fetch_target_entry).returns(@content_entry)
|
2012-03-26 14:31:28 +00:00
|
|
|
@page.stubs(:fullpath).returns('/projects/content_type_template')
|
2010-07-16 20:36:07 +00:00
|
|
|
@controller.request.fullpath = '/projects/edeneo.html'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{projects/edeneo projects/content_type_template content_type_template/edeneo}).returns([@page])
|
2010-07-16 20:36:07 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-12-22 23:45:32 +00:00
|
|
|
it 'sets the content_entry variable' do
|
2012-03-26 14:31:28 +00:00
|
|
|
page = @controller.send(:locomotive_page)
|
|
|
|
page.should_not be_nil
|
|
|
|
page.content_entry.should == @content_entry
|
2010-07-16 20:36:07 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
it 'returns the 404 page if the instance does not exist' do
|
2012-02-16 01:45:14 +00:00
|
|
|
@page.stubs(:fetch_target_entry).returns(nil)
|
2011-11-26 06:24:34 +00:00
|
|
|
(klass = Locomotive::Page).expects(:published).returns([true])
|
2010-07-17 20:51:52 +00:00
|
|
|
@controller.current_site.pages.expects(:not_found).returns(klass)
|
2010-07-16 20:36:07 +00:00
|
|
|
@controller.send(:locomotive_page).should be_true
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-19 00:09:10 +00:00
|
|
|
it 'returns the 404 page if the instance is not visible' do
|
2011-12-22 23:45:32 +00:00
|
|
|
@content_entry._visible = false
|
2012-02-16 01:45:14 +00:00
|
|
|
@page.stubs(:fetch_target_entry).returns(@content_entry)
|
2011-11-26 06:24:34 +00:00
|
|
|
(klass = Locomotive::Page).expects(:published).returns([true])
|
2010-07-19 00:09:10 +00:00
|
|
|
@controller.current_site.pages.expects(:not_found).returns(klass)
|
|
|
|
@controller.send(:locomotive_page).should be_true
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
context 'non published page' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
before(:each) do
|
|
|
|
@page.published = false
|
2011-11-04 15:55:51 +00:00
|
|
|
@controller.current_locomotive_account = nil
|
2010-06-01 00:06:46 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
it 'should return the 404 page if the page has not been published yet' do
|
2010-07-23 20:09:54 +00:00
|
|
|
@controller.request.fullpath = '/contact'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
|
2011-11-26 06:24:34 +00:00
|
|
|
(klass = Locomotive::Page).expects(:published).returns([true])
|
2010-07-17 20:51:52 +00:00
|
|
|
@controller.current_site.pages.expects(:not_found).returns(klass)
|
2010-06-01 00:06:46 +00:00
|
|
|
@controller.send(:locomotive_page).should be_true
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
it 'should not return the 404 page if the page has not been published yet and admin is logged in' do
|
2011-11-04 15:55:51 +00:00
|
|
|
@controller.current_locomotive_account = true
|
2010-06-01 00:06:46 +00:00
|
|
|
@controller.request.fullpath = '/contact'
|
2012-03-29 14:17:09 +00:00
|
|
|
@controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
|
2010-06-01 00:06:46 +00:00
|
|
|
@controller.send(:locomotive_page).should == @page
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-06-01 00:06:46 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|