#!/bin/sh test_description='rendercomment tests' . "$(dirname "$0")/setup.sh" test_expect_success 'Test comment rendering.' ' cat <<-EOD | sqlite3 aur.db && INSERT INTO PackageBases (ID, Name, PackagerUID, SubmittedTS, ModifiedTS, FlaggerComment) VALUES (1, "foobar", 1, 0, 0, ""); INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (1, 1, "Hello world! This is a comment.", ""); EOD "$RENDERCOMMENT" 1 && cat <<-EOD >expected &&

Hello world! This is a comment.

EOD cat <<-EOD | sqlite3 aur.db >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 1; EOD test_cmp actual expected ' test_expect_success 'Test Markdown conversion.' ' cat <<-EOD | sqlite3 aur.db && INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (2, 1, "*Hello* [world](https://www.archlinux.org/)!", ""); EOD "$RENDERCOMMENT" 2 && cat <<-EOD >expected &&

Hello world!

EOD cat <<-EOD | sqlite3 aur.db >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 2; EOD test_cmp actual expected ' test_expect_success 'Test HTML sanitizing.' ' cat <<-EOD | sqlite3 aur.db && INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (3, 1, "", ""); EOD "$RENDERCOMMENT" 3 && cat <<-EOD >expected && <script>alert("XSS!");</script> EOD cat <<-EOD | sqlite3 aur.db >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 3; EOD test_cmp actual expected ' test_expect_success 'Test link conversion.' ' cat <<-EOD | sqlite3 aur.db && INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (4, 1, " Visit https://www.archlinux.org/#_test_. Visit *https://www.archlinux.org/*. Visit . Visit \`https://www.archlinux.org/\`. Visit [Arch Linux](https://www.archlinux.org/). Visit [Arch Linux][arch]. [arch]: https://www.archlinux.org/ ", ""); EOD "$RENDERCOMMENT" 4 && cat <<-EOD >expected &&

Visit https://www.archlinux.org/#_test_. Visit https://www.archlinux.org/. Visit https://www.archlinux.org/. Visit https://www.archlinux.org/. Visit Arch Linux. Visit Arch Linux.

EOD cat <<-EOD | sqlite3 aur.db >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 4; EOD test_cmp actual expected ' test_expect_success 'Test Git commit linkification.' ' local oid=`git -C aur.git rev-parse --verify HEAD` cat <<-EOD | sqlite3 aur.db && INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (5, 1, " $oid ${oid:0:7} x.$oid.x ${oid}x 0123456789abcdef \`$oid\` http://example.com/$oid ", ""); EOD "$RENDERCOMMENT" 5 && cat <<-EOD >expected &&

${oid:0:12} ${oid:0:7} x.${oid:0:12}.x ${oid}x 0123456789abcdef $oid http://example.com/$oid

EOD cat <<-EOD | sqlite3 aur.db >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 5; EOD test_cmp actual expected ' test_expect_success 'Test Flyspray issue linkification.' ' sqlite3 aur.db <<-EOD && INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (6, 1, " FS#1234567. *FS#1234* FS# XFS#1 \`FS#1234\` https://archlinux.org/?test=FS#1234 ", ""); EOD "$RENDERCOMMENT" 6 && cat <<-EOD >expected &&

FS#1234567. FS#1234 FS# XFS#1 FS#1234 https://archlinux.org/?test=FS#1234

EOD sqlite3 aur.db <<-EOD >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 6; EOD test_cmp actual expected ' test_expect_success 'Test headings lowering.' ' sqlite3 aur.db <<-EOD && INSERT INTO PackageComments (ID, PackageBaseID, Comments, RenderedComment) VALUES (7, 1, " # One ## Two ### Three #### Four ##### Five ###### Six ", ""); EOD "$RENDERCOMMENT" 7 && cat <<-EOD >expected &&
One
Two
Three
Four
Five
Six
EOD sqlite3 aur.db <<-EOD >actual && SELECT RenderedComment FROM PackageComments WHERE ID = 7; EOD test_cmp actual expected ' test_done