/spec/lib/locomotive/render_spec.rb

https://github.com/Sharagoz/engine · Ruby · 209 lines · 168 code · 41 blank · 0 comment · 10 complexity · 3d879fde472585f6513efaad15036383 MD5 · raw file

  1. require 'spec_helper'
  2. require 'ostruct'
  3. describe 'Locomotive rendering system' do
  4. before(:each) do
  5. @controller = Locomotive::TestController.new
  6. Locomotive::Site.any_instance.stubs(:create_default_pages!).returns(true)
  7. @site = FactoryGirl.build(:site)
  8. Locomotive::Site.stubs(:find).returns(@site)
  9. @controller.current_site = @site
  10. @page = FactoryGirl.build(:page, :site => nil, :published => true)
  11. end
  12. context '#liquid_context' do
  13. before(:each) do
  14. @controller.instance_variable_set(:@page, @page)
  15. @controller.stubs(:flash).returns({})
  16. @controller.stubs(:params).returns({})
  17. @controller.stubs(:request).returns(OpenStruct.new(:url => '/'))
  18. end
  19. it 'includes the current date and time' do
  20. context = @controller.send(:locomotive_context)
  21. context['now'].should_not be_blank
  22. context['today'].should_not be_blank
  23. end
  24. it 'includes the locale' do
  25. context = @controller.send(:locomotive_context)
  26. context['locale'].should == 'en'
  27. end
  28. end
  29. context 'setting the response' do
  30. before(:each) do
  31. @controller.instance_variable_set(:@page, @page)
  32. @controller.send(:prepare_and_set_response, 'Hello world !')
  33. end
  34. it 'sets the content type to html' do
  35. @controller.response.headers['Content-Type'].should == 'text/html; charset=utf-8'
  36. end
  37. it 'sets the content type to the one specified by the page' do
  38. @page.response_type = 'application/json'
  39. @controller.send(:prepare_and_set_response, 'Hello world !')
  40. @controller.response.headers['Content-Type'].should == 'application/json; charset=utf-8'
  41. end
  42. it 'sets the status to 200 OK' do
  43. @controller.status.should == :ok
  44. end
  45. it 'displays the output' do
  46. @controller.output.should == 'Hello world !'
  47. end
  48. it 'does not set the cache' do
  49. @controller.response.headers['Cache-Control'].should be_nil
  50. end
  51. it 'sets the cache by simply using etag' do
  52. @page.cache_strategy = 'simple'
  53. @page.stubs(:updated_at).returns(Time.now)
  54. @controller.send(:prepare_and_set_response, 'Hello world !')
  55. @controller.response.to_a # force to build headers
  56. @controller.response.headers['Cache-Control'].should == 'public'
  57. end
  58. it 'sets the cache for Varnish' do
  59. @page.cache_strategy = '3600'
  60. @page.stubs(:updated_at).returns(Time.now)
  61. @controller.send(:prepare_and_set_response, 'Hello world !')
  62. @controller.response.to_a # force to build headers
  63. @controller.response.headers['Cache-Control'].should == 'max-age=3600, public'
  64. end
  65. it 'sets the status to 404 not found when no page is found' do
  66. @page.stubs(:not_found?).returns(true)
  67. @controller.send(:prepare_and_set_response, 'Hello world !')
  68. @controller.status.should == :not_found
  69. end
  70. end
  71. context 'when retrieving page' do
  72. it 'should retrieve the index page /' do
  73. @controller.request.fullpath = '/'
  74. @controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
  75. @controller.send(:locomotive_page).should_not be_nil
  76. end
  77. it 'should also retrieve the index page (index.html)' do
  78. @controller.request.fullpath = '/index.html'
  79. @controller.current_site.pages.expects(:where).with(:depth => 0, :fullpath.in => %w{index}).returns([@page])
  80. @controller.send(:locomotive_page).should_not be_nil
  81. end
  82. it 'should retrieve it based on the full path' do
  83. @controller.request.fullpath = '/about_us/team.html'
  84. @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])
  85. @controller.send(:locomotive_page).should_not be_nil
  86. end
  87. it 'does not include the query string' do
  88. @controller.request.fullpath = '/about_us/team.html?some=params&we=use'
  89. @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])
  90. @controller.send(:locomotive_page).should_not be_nil
  91. end
  92. it 'should return the 404 page if the page does not exist' do
  93. @controller.request.fullpath = '/contact'
  94. (klass = Locomotive::Page).expects(:published).returns([true])
  95. @controller.current_site.pages.expects(:not_found).returns(klass)
  96. @controller.send(:locomotive_page).should be_true
  97. end
  98. context 'redirect' do
  99. before(:each) do
  100. @page.redirect = true
  101. @page.redirect_url = 'http://www.example.com/'
  102. @controller.request.fullpath = '/contact'
  103. @controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
  104. end
  105. (301..302).each do |status|
  106. it "redirects to the redirect_url with a #{status} status" do
  107. @page.redirect_type = status
  108. @controller.expects(:redirect_to).with('http://www.example.com/', { :status => status }).returns(true)
  109. @controller.send(:render_locomotive_page)
  110. end
  111. end
  112. it 'redirects to the redirect_url in the editing version if specified' do
  113. @page.redirect_url = '/another_page'
  114. @controller.stubs(:editing_page?).returns(true)
  115. @controller.expects(:redirect_to).with('/another_page/_edit', { :status => 301 }).returns(true)
  116. @controller.send(:render_locomotive_page)
  117. end
  118. end
  119. context 'templatized page' do
  120. before(:each) do
  121. @content_type = FactoryGirl.build(:content_type, :site => nil)
  122. @content_entry = @content_type.entries.build(:_visible => true)
  123. @page.templatized = true
  124. @page.stubs(:fetch_target_entry).returns(@content_entry)
  125. @page.stubs(:fullpath).returns('/projects/content_type_template')
  126. @controller.request.fullpath = '/projects/edeneo.html'
  127. @controller.current_site.pages.expects(:where).with(:depth => 2, :fullpath.in => %w{projects/edeneo projects/content_type_template content_type_template/edeneo}).returns([@page])
  128. end
  129. it 'sets the content_entry variable' do
  130. page = @controller.send(:locomotive_page)
  131. page.should_not be_nil
  132. page.content_entry.should == @content_entry
  133. end
  134. it 'returns the 404 page if the instance does not exist' do
  135. @page.stubs(:fetch_target_entry).returns(nil)
  136. (klass = Locomotive::Page).expects(:published).returns([true])
  137. @controller.current_site.pages.expects(:not_found).returns(klass)
  138. @controller.send(:locomotive_page).should be_true
  139. end
  140. it 'returns the 404 page if the instance is not visible' do
  141. @content_entry._visible = false
  142. @page.stubs(:fetch_target_entry).returns(@content_entry)
  143. (klass = Locomotive::Page).expects(:published).returns([true])
  144. @controller.current_site.pages.expects(:not_found).returns(klass)
  145. @controller.send(:locomotive_page).should be_true
  146. end
  147. end
  148. context 'non published page' do
  149. before(:each) do
  150. @page.published = false
  151. @controller.current_locomotive_account = nil
  152. end
  153. it 'should return the 404 page if the page has not been published yet' do
  154. @controller.request.fullpath = '/contact'
  155. @controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
  156. (klass = Locomotive::Page).expects(:published).returns([true])
  157. @controller.current_site.pages.expects(:not_found).returns(klass)
  158. @controller.send(:locomotive_page).should be_true
  159. end
  160. it 'should not return the 404 page if the page has not been published yet and admin is logged in' do
  161. @controller.current_locomotive_account = true
  162. @controller.request.fullpath = '/contact'
  163. @controller.current_site.pages.expects(:where).with(:depth => 1, :fullpath.in => %w{contact content_type_template}).returns([@page])
  164. @controller.send(:locomotive_page).should == @page
  165. end
  166. end
  167. end
  168. end