Skip to content

Commit

Permalink
add test cases for html in MOTD
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom committed Sep 7, 2023
1 parent 7cffcec commit b1bdd73
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
27 changes: 27 additions & 0 deletions apps/dashboard/test/fixtures/files/motd_md_w_html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Markdown MOTD with HTML

<div>
Here's some html content
</div>

<form action="/my-handling-form-page" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" id="name" name="user_name" />
</li>
<li>
<label for="mail">Email:</label>
<input type="email" id="mail" name="user_email" />
</li>
<li>
<label for="msg">Message:</label>
<textarea id="msg" name="user_message"></textarea>
</li>
</ul>
</form>


<script type='text/javascript'>
window.alert('hello!');
</script>
92 changes: 92 additions & 0 deletions apps/dashboard/test/integration/motd_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require 'test_helper'

# Test various message of the day formats in html
class MotdIntegrationTest < ActionDispatch::IntegrationTest
test 'default RSS MOTD sanitizes HTML' do
env = {
MOTD_FORMAT: 'rss',
MOTD_PATH: Rails.root.join('test/fixtures/files/motd_rss').to_s
}

with_modified_env(env) do
get('/')
end

items = css_select('[data-motd-rss-item]')
assert_equal(10, items.size)

# no scripts or forms
forms = css_select('[data-motd-rss-item] form')
assert_equal(0, forms.size)

scripts = css_select('[data-motd-rss-item] script')
assert_equal(0, scripts.size)
end

test 'RSS MOTD can render HTML when configured' do
env = {
MOTD_FORMAT: 'rss',
MOTD_PATH: Rails.root.join('test/fixtures/files/motd_rss').to_s,
OOD_MOTD_RENDER_HTML: 'yesplz'
}

with_modified_env(env) do
get('/')
end

items = css_select('[data-motd-rss-item]')
assert_equal(10, items.size)

# now with HTML rendering we have forms & scripts
forms = css_select('[data-motd-rss-item] form')
assert_equal(2, forms.size)

scripts = css_select('[data-motd-rss-item] script')
assert_equal(2, scripts.size)
end

test 'default Markdown MOTD sanitizes HTML' do
env = {
MOTD_FORMAT: 'markdown',
MOTD_PATH: Rails.root.join('test/fixtures/files/motd_md_w_html').to_s
}

with_modified_env(env) do
get('/')
end

items = css_select('[data-motd-md]')
assert_equal(1, items.size)

# no scripts or forms
forms = css_select('[data-motd-rss-item] form')
assert_equal(0, forms.size)

scripts = css_select('[data-motd-rss-item] script')
assert_equal(0, scripts.size)
end

test 'Markdown MOTD can render HTML when configured' do
env = {
MOTD_FORMAT: 'markdown',
MOTD_PATH: Rails.root.join('test/fixtures/files/motd_md_w_html').to_s,
OOD_MOTD_RENDER_HTML: 'yesplz'
}

with_modified_env(env) do
get('/')
end

items = css_select('[data-motd-md]')
assert_equal(1, items.size)

# scripts & forms are preserved
forms = css_select('[data-motd-md] form')
assert_equal(1, forms.size)

scripts = css_select('[data-motd-md] script')
assert_equal(1, scripts.size)
end
end

0 comments on commit b1bdd73

Please sign in to comment.