From d097799b34e5392f577db28920f8fb27b35602f3 Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Mon, 22 Nov 2021 10:51:44 -0800 Subject: [PATCH] change(usermaint): converted to use aurweb.db ORM - Removed usermaint sharness test Signed-off-by: Kevin Morris --- aurweb/scripts/usermaint.py | 34 ++++++++++++------- test/t2700-usermaint.t | 49 --------------------------- test/test_usermaint.py | 67 +++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 61 deletions(-) delete mode 100755 test/t2700-usermaint.t create mode 100644 test/test_usermaint.py diff --git a/aurweb/scripts/usermaint.py b/aurweb/scripts/usermaint.py index 1621d410..aad3e8de 100755 --- a/aurweb/scripts/usermaint.py +++ b/aurweb/scripts/usermaint.py @@ -1,21 +1,31 @@ #!/usr/bin/env python3 -import time +from datetime import datetime -import aurweb.db +from sqlalchemy import update + +from aurweb import db +from aurweb.models import User + + +def _main(): + limit_to = int(datetime.utcnow().timestamp()) - 86400 * 7 + + update_ = update(User).where( + User.LastLogin < limit_to + ).values(LastLoginIPAddress=None) + db.get_session().execute(update_) + + update_ = update(User).where( + User.LastSSHLogin < limit_to + ).values(LastSSHLoginIPAddress=None) + db.get_session().execute(update_) def main(): - conn = aurweb.db.Connection() - - limit_to = int(time.time()) - 86400 * 7 - conn.execute("UPDATE Users SET LastLoginIPAddress = NULL " + - "WHERE LastLogin < ?", [limit_to]) - conn.execute("UPDATE Users SET LastSSHLoginIPAddress = NULL " + - "WHERE LastSSHLogin < ?", [limit_to]) - - conn.commit() - conn.close() + db.get_engine() + with db.begin(): + _main() if __name__ == '__main__': diff --git a/test/t2700-usermaint.t b/test/t2700-usermaint.t deleted file mode 100755 index c119e3f4..00000000 --- a/test/t2700-usermaint.t +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh - -test_description='usermaint tests' - -. "$(dirname "$0")/setup.sh" - -test_expect_success 'Test removal of login IP addresses.' ' - now=$(date -d now +%s) && - threedaysago=$(date -d "3 days ago" +%s) && - tendaysago=$(date -d "10 days ago" +%s) && - cat <<-EOD | sqlite3 aur.db && - UPDATE Users SET LastLogin = $threedaysago, LastLoginIPAddress = "1.2.3.4" WHERE ID = 1; - UPDATE Users SET LastLogin = $tendaysago, LastLoginIPAddress = "2.3.4.5" WHERE ID = 2; - UPDATE Users SET LastLogin = $now, LastLoginIPAddress = "3.4.5.6" WHERE ID = 3; - UPDATE Users SET LastLogin = 0, LastLoginIPAddress = "4.5.6.7" WHERE ID = 4; - UPDATE Users SET LastLogin = 0, LastLoginIPAddress = "5.6.7.8" WHERE ID = 5; - UPDATE Users SET LastLogin = $tendaysago, LastLoginIPAddress = "6.7.8.9" WHERE ID = 6; - EOD - cover "$USERMAINT" && - cat <<-EOD >expected && - 1.2.3.4 - 3.4.5.6 - EOD - echo "SELECT LastLoginIPAddress FROM Users WHERE LastLoginIPAddress IS NOT NULL;" | sqlite3 aur.db >actual && - test_cmp actual expected -' - -test_expect_success 'Test removal of SSH login IP addresses.' ' - now=$(date -d now +%s) && - threedaysago=$(date -d "3 days ago" +%s) && - tendaysago=$(date -d "10 days ago" +%s) && - cat <<-EOD | sqlite3 aur.db && - UPDATE Users SET LastSSHLogin = $now, LastSSHLoginIPAddress = "1.2.3.4" WHERE ID = 1; - UPDATE Users SET LastSSHLogin = $threedaysago, LastSSHLoginIPAddress = "2.3.4.5" WHERE ID = 2; - UPDATE Users SET LastSSHLogin = $tendaysago, LastSSHLoginIPAddress = "3.4.5.6" WHERE ID = 3; - UPDATE Users SET LastSSHLogin = 0, LastSSHLoginIPAddress = "4.5.6.7" WHERE ID = 4; - UPDATE Users SET LastSSHLogin = 0, LastSSHLoginIPAddress = "5.6.7.8" WHERE ID = 5; - UPDATE Users SET LastSSHLogin = $tendaysago, LastSSHLoginIPAddress = "6.7.8.9" WHERE ID = 6; - EOD - cover "$USERMAINT" && - cat <<-EOD >expected && - 1.2.3.4 - 2.3.4.5 - EOD - echo "SELECT LastSSHLoginIPAddress FROM Users WHERE LastSSHLoginIPAddress IS NOT NULL;" | sqlite3 aur.db >actual && - test_cmp actual expected -' - -test_done diff --git a/test/test_usermaint.py b/test/test_usermaint.py new file mode 100644 index 00000000..f1af59e1 --- /dev/null +++ b/test/test_usermaint.py @@ -0,0 +1,67 @@ +from datetime import datetime + +import pytest + +from aurweb import db +from aurweb.models import User +from aurweb.models.account_type import USER_ID +from aurweb.scripts import usermaint + + +@pytest.fixture(autouse=True) +def setup(db_test): + return + + +@pytest.fixture +def user() -> User: + with db.begin(): + user = db.create(User, Username="test", Email="test@example.org", + Passwd="testPassword", AccountTypeID=USER_ID) + yield user + + +def test_usermaint_noop(user: User): + """ Last[SSH]Login isn't expired in this test: usermaint is noop. """ + + now = int(datetime.utcnow().timestamp()) + with db.begin(): + user.LastLoginIPAddress = "127.0.0.1" + user.LastLogin = now - 10 + user.LastSSHLoginIPAddress = "127.0.0.1" + user.LastSSHLogin = now - 10 + + usermaint.main() + + assert user.LastLoginIPAddress == "127.0.0.1" + assert user.LastSSHLoginIPAddress == "127.0.0.1" + + +def test_usermaint(user: User): + """ + In this case, we first test that only the expired record gets + updated, but the non-expired record remains untouched. After, + we update the login time on the non-expired record and exercise + its code path. + """ + + now = int(datetime.utcnow().timestamp()) + limit_to = now - 86400 * 7 + with db.begin(): + user.LastLoginIPAddress = "127.0.0.1" + user.LastLogin = limit_to - 666 + user.LastSSHLoginIPAddress = "127.0.0.1" + user.LastSSHLogin = now - 10 + + usermaint.main() + + assert user.LastLoginIPAddress is None + assert user.LastSSHLoginIPAddress == "127.0.0.1" + + with db.begin(): + user.LastSSHLogin = limit_to - 666 + + usermaint.main() + + assert user.LastLoginIPAddress is None + assert user.LastSSHLoginIPAddress is None