PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/copy/test/server_test.rb

https://bitbucket.org/samirahmed/foodtruck
Ruby | 183 lines | 147 code | 36 blank | 0 comment | 1 complexity | f412fb2304df78fce0e3ab5f358e5a37 MD5 | raw file
  1. require 'test_helper'
  2. require 'rack/test'
  3. class ServerTest < Test::Unit::TestCase
  4. include CopyAppSetup
  5. include Rack::Test::Methods
  6. test "GET index" do
  7. get '/'
  8. assert last_response.ok?
  9. assert_equal 'text/html;charset=utf-8', last_response.headers['Content-Type']
  10. assert_match "<title>I'm the layout!</title>", last_response.body
  11. assert_match "<p>I'm the index!</p>", last_response.body
  12. end
  13. test "GET path with index in folder" do
  14. %w(/about /about/).each do |path|
  15. get path
  16. assert last_response.ok?
  17. assert_match "<p>About!</p>", last_response.body
  18. end
  19. end
  20. test "GET path to template in folder" do
  21. %w(/about/us /about/us/).each do |path|
  22. get path
  23. assert last_response.ok?
  24. assert_match "<p>About us!</p>", last_response.body
  25. end
  26. end
  27. test "GET non-existent path" do
  28. get '/nope'
  29. assert last_response.status == 404
  30. end
  31. test "cache_time setting sets a Cache-Control header" do
  32. app.config { set :cache_time, 456 }
  33. get '/'
  34. assert_equal 'public, max-age=456', last_response.headers['Cache-Control']
  35. [0, nil, false].each do |time|
  36. app.config { set :cache_time, time }
  37. get '/'
  38. assert_equal 'no-cache', last_response.headers['Cache-Control']
  39. end
  40. end
  41. test "GET csv" do
  42. get 'data/people.csv'
  43. assert last_response.ok?
  44. assert_equal 'text/csv;charset=utf-8', last_response.headers['Content-Type']
  45. assert_equal File.read(app.settings.views + '/data/people.csv.erb'), last_response.body
  46. end
  47. test "GET xml" do
  48. get 'data/people.xml'
  49. assert last_response.ok?
  50. assert_equal 'application/xml;charset=utf-8', last_response.headers['Content-Type']
  51. assert_equal File.read(app.settings.views + '/data/people.xml.erb'), last_response.body
  52. end
  53. test "connects to storage when setting present" do
  54. connection_url = 'redis://localhost:1234'
  55. app.config { set :storage, connection_url }
  56. Copy::Storage.expects(:connect!).with(connection_url).once.returns(true)
  57. get '/'
  58. assert last_response.ok?
  59. end
  60. end
  61. class ServerCopyHelperTest < Test::Unit::TestCase
  62. include CopyAppSetup
  63. include Rack::Test::Methods
  64. test "copy helper displays content from storage" do
  65. Copy::Storage.stubs(:connected?).returns(true)
  66. Copy::Storage.expects(:get).with(:facts).returns("truth")
  67. get 'with_copy_helper'
  68. assert last_response.ok?
  69. assert_match "truth", last_response.body
  70. end
  71. test "copy helper saves defaults text when content is not in storage and renders it" do
  72. Copy::Storage.stubs(:connected?).returns(true)
  73. Copy::Storage.expects(:get).with(:facts).returns(nil)
  74. Copy::Storage.expects(:set).with(:facts, "_Default Text_\n").returns(true)
  75. get 'with_copy_helper'
  76. assert last_response.ok?, last_response.errors
  77. assert_match %Q(<div class="_copy_editable" data-name="facts"><p><em>Default Text</em></p></div>), last_response.body
  78. end
  79. test "copy helper shows default text when not connected" do
  80. Copy::Storage.expects(:connected?).twice.returns(false)
  81. get 'with_copy_helper'
  82. assert last_response.ok?
  83. assert_match %Q(<div class="_copy_editable" data-name="facts"><p><em>Default Text</em></p></div>), last_response.body
  84. end
  85. test "copy helper renders single line content correctly" do
  86. Copy::Storage.expects(:connected?).twice.returns(false)
  87. get 'with_copy_helper_one_line'
  88. assert last_response.ok?
  89. assert_match %Q(<span class="_copy_editable" data-name="headline">Important!</span>), last_response.body
  90. end
  91. test "copy helper indented in view" do
  92. Copy::Storage.stubs(:connected?).returns(true)
  93. Copy::Storage.expects(:get).with(:three).returns(nil)
  94. Copy::Storage.expects(:set).with(:three, "three\n").returns(true)
  95. get 'indented'
  96. assert last_response.ok?, last_response.errors
  97. assert_match %Q(<div class="_copy_editable" data-name="three"><p>three</p></div>), last_response.body
  98. end
  99. test "partial rendering" do
  100. get 'renders_partials'
  101. assert last_response.ok?, last_response.errors
  102. assert_match "before\none\ntwo\nthree\nafter", last_response.body
  103. end
  104. end
  105. class ServerAdminTest < Test::Unit::TestCase
  106. include CopyAppSetup
  107. include Rack::Test::Methods
  108. test "GET /_copy is protected when no user/pass are set" do
  109. get '/_copy'
  110. assert_equal 401, last_response.status
  111. end
  112. test "GET /_copy protected when user/pass are set, but supplied incorrectly" do
  113. setup_auth 'good', 'girl'
  114. authorize 'bad', 'boy'
  115. get '/_copy'
  116. assert_equal 401, last_response.status
  117. end
  118. test "GET /_copy with valid credentials" do
  119. authorize!
  120. get '/_copy'
  121. assert last_response.ok?
  122. assert_match 'Edit Copy', last_response.body
  123. end
  124. test "GET /_copy.js" do
  125. authorize!
  126. get '/_copy.js'
  127. assert last_response.ok?, last_response.errors
  128. assert_match 'jQuery JavaScript Library', last_response.body
  129. end
  130. test "GET /_copy/:name" do
  131. Copy::Storage.stubs(:connected?).returns(true)
  132. Copy::Storage.expects(:get).with('fun').returns("<b>party\n")
  133. authorize!
  134. get '/_copy/fun'
  135. assert last_response.ok?, last_response.errors
  136. assert_match "&lt;b&gt;party\n</textarea>", last_response.body
  137. end
  138. test "PUT /_copy/:name" do
  139. Copy::Storage.stubs(:connected?).returns(true)
  140. Copy::Storage.expects(:set).with('fun', '_party_').returns(true)
  141. Copy::Storage.expects(:get).with('fun').returns('_party_')
  142. authorize!
  143. put '/_copy/fun', :content => '_party_', :wrap_tag => 'article'
  144. assert last_response.ok?, last_response.errors
  145. assert_match %Q(<article class="_copy_editable" data-name="fun"><em>party</em></article>), last_response.body
  146. end
  147. end