change(usermaint): converted to use aurweb.db ORM

- Removed usermaint sharness test

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-11-22 10:51:44 -08:00
parent 8d5683d3f1
commit d097799b34
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
3 changed files with 89 additions and 61 deletions

View file

@ -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__':

View file

@ -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

67
test/test_usermaint.py Normal file
View file

@ -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