1
0

application_helper_spec.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ApplicationHelper do
  4. describe 'body_classes' do
  5. context 'with a body class string from a controller' do
  6. before { helper.extend controller_helpers }
  7. it 'uses the controller body classes in the result' do
  8. expect(helper.body_classes)
  9. .to match(/modal-layout compose-standalone/)
  10. .and match(/theme-default/)
  11. end
  12. it 'includes values set via content_for' do
  13. helper.content_for(:body_classes) { 'admin' }
  14. expect(helper.body_classes)
  15. .to match(/admin/)
  16. end
  17. private
  18. def controller_helpers
  19. Module.new do
  20. def body_class_string = 'modal-layout compose-standalone'
  21. def current_account
  22. @current_account ||= Fabricate(:account)
  23. end
  24. def current_theme = 'default'
  25. end
  26. end
  27. end
  28. end
  29. describe 'locale_direction' do
  30. it 'adds rtl body class if locale is Arabic' do
  31. I18n.with_locale(:ar) do
  32. expect(helper.locale_direction).to eq 'rtl'
  33. end
  34. end
  35. it 'adds rtl body class if locale is Farsi' do
  36. I18n.with_locale(:fa) do
  37. expect(helper.locale_direction).to eq 'rtl'
  38. end
  39. end
  40. it 'adds rtl if locale is Hebrew' do
  41. I18n.with_locale(:he) do
  42. expect(helper.locale_direction).to eq 'rtl'
  43. end
  44. end
  45. it 'does not add rtl if locale is Thai' do
  46. I18n.with_locale(:th) do
  47. expect(helper.locale_direction).to_not eq 'rtl'
  48. end
  49. end
  50. end
  51. describe '#material_symbol' do
  52. it 'returns an svg with the icon and options' do
  53. expect(helper.material_symbol('lock', class: :test, data: { hidden: true }))
  54. .to match('<svg.*/svg>')
  55. .and match('class="icon material-lock test"')
  56. .and match('data-hidden="true"')
  57. end
  58. end
  59. describe 'open_registrations?' do
  60. it 'returns true when open for registrations' do
  61. allow(Setting).to receive(:[]).with('registrations_mode').and_return('open')
  62. expect(helper.open_registrations?).to be true
  63. expect(Setting).to have_received(:[]).with('registrations_mode')
  64. end
  65. it 'returns false when closed for registrations' do
  66. allow(Setting).to receive(:[]).with('registrations_mode').and_return('none')
  67. expect(helper.open_registrations?).to be false
  68. expect(Setting).to have_received(:[]).with('registrations_mode')
  69. end
  70. end
  71. describe 'available_sign_up_path' do
  72. context 'when registrations are closed' do
  73. before do
  74. allow(Setting).to receive(:[]).with('registrations_mode').and_return 'none'
  75. end
  76. it 'redirects to joinmastodon site' do
  77. expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
  78. end
  79. end
  80. context 'when in omniauth only mode' do
  81. around do |example|
  82. ClimateControl.modify OMNIAUTH_ONLY: 'true' do
  83. example.run
  84. end
  85. end
  86. it 'redirects to joinmastodon site' do
  87. expect(helper.available_sign_up_path).to match(/joinmastodon.org/)
  88. end
  89. end
  90. context 'when registrations are allowed' do
  91. it 'returns a link to the registration page' do
  92. expect(helper.available_sign_up_path).to eq(new_user_registration_path)
  93. end
  94. end
  95. end
  96. describe 'omniauth_only?' do
  97. context 'when env var is set to true' do
  98. around do |example|
  99. ClimateControl.modify OMNIAUTH_ONLY: 'true' do
  100. example.run
  101. end
  102. end
  103. it 'returns true' do
  104. expect(helper).to be_omniauth_only
  105. end
  106. end
  107. context 'when env var is not set' do
  108. around do |example|
  109. ClimateControl.modify OMNIAUTH_ONLY: nil do
  110. example.run
  111. end
  112. end
  113. it 'returns false' do
  114. expect(helper).to_not be_omniauth_only
  115. end
  116. end
  117. end
  118. describe 'quote_wrap' do
  119. it 'indents and quote wraps text' do
  120. text = <<~TEXT
  121. Hello this is a nice message for you to quote.
  122. Be careful because it has two lines.
  123. TEXT
  124. expect(helper.quote_wrap(text)).to eq <<~EXPECTED.strip
  125. > Hello this is a nice message for you to quote.
  126. > Be careful because it has two lines.
  127. EXPECTED
  128. end
  129. end
  130. describe 'storage_host' do
  131. context 'when S3 alias is present' do
  132. around do |example|
  133. ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
  134. example.run
  135. end
  136. end
  137. it 'returns true' do
  138. expect(helper.storage_host).to eq('https://s3.alias')
  139. end
  140. end
  141. context 'when S3 alias includes a path component' do
  142. around do |example|
  143. ClimateControl.modify S3_ALIAS_HOST: 's3.alias/path' do
  144. example.run
  145. end
  146. end
  147. it 'returns a correct URL' do
  148. expect(helper.storage_host).to eq('https://s3.alias/path')
  149. end
  150. end
  151. context 'when S3 cloudfront is present' do
  152. around do |example|
  153. ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
  154. example.run
  155. end
  156. end
  157. it 'returns true' do
  158. expect(helper.storage_host).to eq('https://s3.cloudfront')
  159. end
  160. end
  161. end
  162. describe 'storage_host?' do
  163. context 'when S3 alias is present' do
  164. around do |example|
  165. ClimateControl.modify S3_ALIAS_HOST: 's3.alias' do
  166. example.run
  167. end
  168. end
  169. it 'returns true' do
  170. expect(helper.storage_host?).to be true
  171. end
  172. end
  173. context 'when S3 cloudfront is present' do
  174. around do |example|
  175. ClimateControl.modify S3_CLOUDFRONT_HOST: 's3.cloudfront' do
  176. example.run
  177. end
  178. end
  179. it 'returns true' do
  180. expect(helper.storage_host?).to be true
  181. end
  182. end
  183. context 'when neither env value is present' do
  184. it 'returns false' do
  185. expect(helper.storage_host?).to be false
  186. end
  187. end
  188. end
  189. describe 'title' do
  190. it 'returns site title on production environment' do
  191. Setting.site_title = 'site title'
  192. allow(Rails.env).to receive(:production?).and_return(true)
  193. expect(helper.title).to eq 'site title'
  194. expect(Rails.env).to have_received(:production?)
  195. end
  196. it 'returns site title with note on non-production environment' do
  197. Setting.site_title = 'site title'
  198. allow(Rails.env).to receive(:production?).and_return(false)
  199. expect(helper.title).to eq 'site title (Dev)'
  200. expect(Rails.env).to have_received(:production?)
  201. end
  202. end
  203. describe 'html_title' do
  204. before do
  205. allow(Rails.env).to receive(:production?).and_return(true)
  206. end
  207. context 'with a page_title content_for value' do
  208. it 'uses the value in the html title' do
  209. Setting.site_title = 'Site Title'
  210. helper.content_for(:page_title, 'Test Value')
  211. expect(helper.html_title).to eq 'Test Value - Site Title'
  212. expect(helper.html_title).to be_html_safe
  213. end
  214. it 'does not escape twice' do
  215. Setting.site_title = 'Site Title'
  216. helper.content_for(:page_title, '&quot;Test Value&quot;'.html_safe)
  217. expect(helper.html_title).to eq '&quot;Test Value&quot; - Site Title'
  218. expect(helper.html_title).to be_html_safe
  219. end
  220. end
  221. context 'without any page_title content_for value' do
  222. it 'returns the site title' do
  223. Setting.site_title = 'Site Title'
  224. expect(helper.html_title).to eq 'Site Title'
  225. expect(helper.html_title).to be_html_safe
  226. end
  227. end
  228. end
  229. end