mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
change(rendercomment): converted to use aurweb.db ORM
- Added aurweb.util.git_search. - Decoupled away from rendercomment for easier testability. - Added aurweb.testing.git.GitRepository. - Added templates/testing/{PKGBUILD,SRCINFO}.j2. - Added aurweb.testing.git.GitRepository + `git` pytest fixture Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
4b0cb0721d
commit
2d0e09cd63
9 changed files with 398 additions and 195 deletions
|
@ -58,6 +58,7 @@ import aurweb.db
|
|||
from aurweb import initdb, logging, testing
|
||||
from aurweb.testing.email import Email
|
||||
from aurweb.testing.filelock import FileLock
|
||||
from aurweb.testing.git import GitRepository
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
|
||||
|
@ -211,3 +212,8 @@ def db_test(db_session: scoped_session) -> None:
|
|||
session via aurweb.db.get_session().
|
||||
"""
|
||||
testing.setup_test_db()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def git(tmpdir: py.path.local) -> GitRepository:
|
||||
yield GitRepository(tmpdir)
|
||||
|
|
|
@ -1,160 +0,0 @@
|
|||
#!/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
|
||||
cover "$RENDERCOMMENT" 1 &&
|
||||
cat <<-EOD >expected &&
|
||||
<p>Hello world!
|
||||
This is a comment.</p>
|
||||
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
|
||||
cover "$RENDERCOMMENT" 2 &&
|
||||
cat <<-EOD >expected &&
|
||||
<p><em>Hello</em> <a href="https://www.archlinux.org/">world</a>!</p>
|
||||
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, "<script>alert(""XSS!"");</script>", "");
|
||||
EOD
|
||||
cover "$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 <https://www.archlinux.org/>.
|
||||
Visit \`https://www.archlinux.org/\`.
|
||||
Visit [Arch Linux](https://www.archlinux.org/).
|
||||
Visit [Arch Linux][arch].
|
||||
[arch]: https://www.archlinux.org/
|
||||
", "");
|
||||
EOD
|
||||
cover "$RENDERCOMMENT" 4 &&
|
||||
cat <<-EOD >expected &&
|
||||
<p>Visit <a href="https://www.archlinux.org/#_test_">https://www.archlinux.org/#_test_</a>.
|
||||
Visit <em><a href="https://www.archlinux.org/">https://www.archlinux.org/</a></em>.
|
||||
Visit <a href="https://www.archlinux.org/">https://www.archlinux.org/</a>.
|
||||
Visit <code>https://www.archlinux.org/</code>.
|
||||
Visit <a href="https://www.archlinux.org/">Arch Linux</a>.
|
||||
Visit <a href="https://www.archlinux.org/">Arch Linux</a>.</p>
|
||||
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
|
||||
cover "$RENDERCOMMENT" 5 &&
|
||||
cat <<-EOD >expected &&
|
||||
<p><a href="https://aur.archlinux.org/cgit/aur.git/log/?h=foobar&id=${oid:0:12}">${oid:0:12}</a>
|
||||
<a href="https://aur.archlinux.org/cgit/aur.git/log/?h=foobar&id=${oid:0:7}">${oid:0:7}</a>
|
||||
x.<a href="https://aur.archlinux.org/cgit/aur.git/log/?h=foobar&id=${oid:0:12}">${oid:0:12}</a>.x
|
||||
${oid}x
|
||||
0123456789abcdef
|
||||
<code>$oid</code>
|
||||
<a href="http://example.com/$oid">http://example.com/$oid</a></p>
|
||||
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
|
||||
cover "$RENDERCOMMENT" 6 &&
|
||||
cat <<-EOD >expected &&
|
||||
<p><a href="https://bugs.archlinux.org/task/1234567">FS#1234567</a>.
|
||||
<em><a href="https://bugs.archlinux.org/task/1234">FS#1234</a></em>
|
||||
FS#
|
||||
XFS#1
|
||||
<code>FS#1234</code>
|
||||
<a href="https://archlinux.org/?test=FS#1234">https://archlinux.org/?test=FS#1234</a></p>
|
||||
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
|
||||
cover "$RENDERCOMMENT" 7 &&
|
||||
cat <<-EOD >expected &&
|
||||
<h5>One</h5>
|
||||
<h6>Two</h6>
|
||||
<h6>Three</h6>
|
||||
<h6>Four</h6>
|
||||
<h6>Five</h6>
|
||||
<h6>Six</h6>
|
||||
EOD
|
||||
sqlite3 aur.db <<-EOD >actual &&
|
||||
SELECT RenderedComment FROM PackageComments WHERE ID = 7;
|
||||
EOD
|
||||
test_cmp actual expected
|
||||
'
|
||||
|
||||
test_done
|
202
test/test_rendercomment.py
Normal file
202
test/test_rendercomment.py
Normal file
|
@ -0,0 +1,202 @@
|
|||
from datetime import datetime
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from aurweb import config, db, logging
|
||||
from aurweb.models import Package, PackageBase, PackageComment, User
|
||||
from aurweb.models.account_type import USER_ID
|
||||
from aurweb.scripts import rendercomment
|
||||
from aurweb.scripts.rendercomment import update_comment_render
|
||||
from aurweb.testing.git import GitRepository
|
||||
|
||||
logger = logging.get_logger(__name__)
|
||||
aur_location = config.get("options", "aur_location")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup(db_test, git: GitRepository):
|
||||
config_get = config.get
|
||||
|
||||
def mock_config_get(section: str, key: str) -> str:
|
||||
if section == "serve" and key == "repo-path":
|
||||
return git.file_lock.path
|
||||
elif section == "options" and key == "commit_uri":
|
||||
return "/cgit/aur.git/log/?h=%s&id=%s"
|
||||
return config_get(section, key)
|
||||
|
||||
with mock.patch("aurweb.config.get", side_effect=mock_config_get):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user() -> User:
|
||||
with db.begin():
|
||||
user = db.create(User, Username="test", Email="test@example.org",
|
||||
Passwd=str(), AccountTypeID=USER_ID)
|
||||
yield user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def pkgbase(user: User) -> PackageBase:
|
||||
now = int(datetime.utcnow().timestamp())
|
||||
with db.begin():
|
||||
pkgbase = db.create(PackageBase, Packager=user, Name="pkgbase_0",
|
||||
SubmittedTS=now, ModifiedTS=now)
|
||||
yield pkgbase
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def package(pkgbase: PackageBase) -> Package:
|
||||
with db.begin():
|
||||
package = db.create(Package, PackageBase=pkgbase,
|
||||
Name=pkgbase.Name, Version="1.0")
|
||||
yield package
|
||||
|
||||
|
||||
def create_comment(user: User, pkgbase: PackageBase, comments: str,
|
||||
render: bool = True):
|
||||
with db.begin():
|
||||
comment = db.create(PackageComment, User=user,
|
||||
PackageBase=pkgbase, Comments=comments)
|
||||
if render:
|
||||
update_comment_render(comment)
|
||||
return comment
|
||||
|
||||
|
||||
def test_comment_rendering(user: User, pkgbase: PackageBase):
|
||||
text = "Hello world! This is a comment."
|
||||
comment = create_comment(user, pkgbase, text)
|
||||
expected = f"<p>{text}</p>"
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_rendercomment_main(user: User, pkgbase: PackageBase):
|
||||
text = "Hello world! This is a comment."
|
||||
comment = create_comment(user, pkgbase, text, False)
|
||||
|
||||
args = ["aurweb-rendercomment", str(comment.ID)]
|
||||
with mock.patch("sys.argv", args):
|
||||
rendercomment.main()
|
||||
db.refresh(comment)
|
||||
|
||||
expected = f"<p>{text}</p>"
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_markdown_conversion(user: User, pkgbase: PackageBase):
|
||||
text = "*Hello* [world](https://aur.archlinux.org)!"
|
||||
comment = create_comment(user, pkgbase, text)
|
||||
expected = ('<p><em>Hello</em> '
|
||||
'<a href="https://aur.archlinux.org">world</a>!</p>')
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_html_sanitization(user: User, pkgbase: PackageBase):
|
||||
text = '<script>alert("XSS!")</script>'
|
||||
comment = create_comment(user, pkgbase, text)
|
||||
expected = '<script>alert("XSS!")</script>'
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_link_conversion(user: User, pkgbase: PackageBase):
|
||||
text = """\
|
||||
Visit https://www.archlinux.org/#_test_.
|
||||
Visit *https://www.archlinux.org/*.
|
||||
Visit <https://www.archlinux.org/>.
|
||||
Visit `https://www.archlinux.org/`.
|
||||
Visit [Arch Linux](https://www.archlinux.org/).
|
||||
Visit [Arch Linux][arch].
|
||||
[arch]: https://www.archlinux.org/\
|
||||
"""
|
||||
comment = create_comment(user, pkgbase, text)
|
||||
expected = '''\
|
||||
<p>Visit <a href="https://www.archlinux.org/#_test_">\
|
||||
https://www.archlinux.org/#_test_</a>.
|
||||
Visit <em><a href="https://www.archlinux.org/">https://www.archlinux.org/</a></em>.
|
||||
Visit <a href="https://www.archlinux.org/">https://www.archlinux.org/</a>.
|
||||
Visit <code>https://www.archlinux.org/</code>.
|
||||
Visit <a href="https://www.archlinux.org/">Arch Linux</a>.
|
||||
Visit <a href="https://www.archlinux.org/">Arch Linux</a>.</p>\
|
||||
'''
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_git_commit_link(git: GitRepository, user: User, package: Package):
|
||||
commit_hash = git.commit(package, "Initial commit.")
|
||||
logger.info(f"Created commit: {commit_hash}")
|
||||
logger.info(f"Short hash: {commit_hash[:7]}")
|
||||
|
||||
text = f"""\
|
||||
{commit_hash}
|
||||
{commit_hash[:7]}
|
||||
x.{commit_hash}.x
|
||||
{commit_hash}x
|
||||
0123456789abcdef
|
||||
`{commit_hash}`
|
||||
http://example.com/{commit_hash}\
|
||||
"""
|
||||
comment = create_comment(user, package.PackageBase, text)
|
||||
|
||||
pkgname = package.PackageBase.Name
|
||||
cgit_path = f"/cgit/aur.git/log/?h={pkgname}&"
|
||||
expected = f"""\
|
||||
<p><a href="{cgit_path}id={commit_hash[:12]}">{commit_hash[:12]}</a>
|
||||
<a href="{cgit_path}id={commit_hash[:7]}">{commit_hash[:7]}</a>
|
||||
x.<a href="{cgit_path}id={commit_hash[:12]}">{commit_hash[:12]}</a>.x
|
||||
{commit_hash}x
|
||||
0123456789abcdef
|
||||
<code>{commit_hash}</code>
|
||||
<a href="http://example.com/{commit_hash}">\
|
||||
http://example.com/{commit_hash}\
|
||||
</a>\
|
||||
</p>\
|
||||
"""
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_flyspray_issue_link(user: User, pkgbase: PackageBase):
|
||||
text = """\
|
||||
FS#1234567.
|
||||
*FS#1234*
|
||||
FS#
|
||||
XFS#1
|
||||
`FS#1234`
|
||||
https://archlinux.org/?test=FS#1234\
|
||||
"""
|
||||
comment = create_comment(user, pkgbase, text)
|
||||
|
||||
expected = """\
|
||||
<p><a href="https://bugs.archlinux.org/task/1234567">FS#1234567</a>.
|
||||
<em><a href="https://bugs.archlinux.org/task/1234">FS#1234</a></em>
|
||||
FS#
|
||||
XFS#1
|
||||
<code>FS#1234</code>
|
||||
<a href="https://archlinux.org/?test=FS#1234">\
|
||||
https://archlinux.org/?test=FS#1234\
|
||||
</a>\
|
||||
</p>\
|
||||
"""
|
||||
assert comment.RenderedComment == expected
|
||||
|
||||
|
||||
def test_lower_headings(user: User, pkgbase: PackageBase):
|
||||
text = """\
|
||||
# One
|
||||
## Two
|
||||
### Three
|
||||
#### Four
|
||||
##### Five
|
||||
###### Six\
|
||||
"""
|
||||
comment = create_comment(user, pkgbase, text)
|
||||
|
||||
expected = """\
|
||||
<h5>One</h5>
|
||||
<h6>Two</h6>
|
||||
<h6>Three</h6>
|
||||
<h6>Four</h6>
|
||||
<h6>Five</h6>
|
||||
<h6>Six</h6>\
|
||||
"""
|
||||
assert comment.RenderedComment == expected
|
|
@ -40,3 +40,20 @@ def test_round():
|
|||
assert filters.do_round(1.3) == 1
|
||||
assert filters.do_round(1.5) == 2
|
||||
assert filters.do_round(2.0) == 2
|
||||
|
||||
|
||||
def test_git_search():
|
||||
""" Test that git_search matches the full commit if necessary. """
|
||||
commit_hash = "0123456789abcdef"
|
||||
repo = {commit_hash}
|
||||
prefixlen = util.git_search(repo, commit_hash)
|
||||
assert prefixlen == 16
|
||||
|
||||
|
||||
def test_git_search_double_commit():
|
||||
""" Test that git_search matches a shorter prefix length. """
|
||||
commit_hash = "0123456789abcdef"
|
||||
repo = {commit_hash[:13]}
|
||||
# Locate the shortest prefix length that matches commit_hash.
|
||||
prefixlen = util.git_search(repo, commit_hash)
|
||||
assert prefixlen == 13
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue