diff --git a/util/adduser.py b/aurweb/scripts/adduser.py similarity index 85% rename from util/adduser.py rename to aurweb/scripts/adduser.py index 1853869a..4cc059d1 100644 --- a/util/adduser.py +++ b/aurweb/scripts/adduser.py @@ -1,7 +1,17 @@ +""" +Add a user to the configured aurweb database. + +See `aurweb-adduser --help` for documentation. + +Copyright (C) 2022 aurweb Development Team +All Rights Reserved +""" import argparse import sys import traceback +import aurweb.models.account_type as at + from aurweb import db from aurweb.models.account_type import AccountType from aurweb.models.ssh_pub_key import SSHPubKey, get_fingerprint @@ -19,13 +29,9 @@ def parse_args(): parser.add_argument("--pgp-key", help="PGP Key Fingerprint") parser.add_argument("--ssh-pubkey", help="SSH PubKey") + choices = at.ACCOUNT_TYPE_NAME.values() parser.add_argument("-t", "--type", help="Account Type", - choices=[ - "User", - "Trusted User", - "Developer", - "Trusted User & Developer" - ], default="User") + choices=choices, default=at.USER) return parser.parse_args() diff --git a/pyproject.toml b/pyproject.toml index e14fad1e..b64ba64b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -111,3 +111,4 @@ aurweb-rendercomment = "aurweb.scripts.rendercomment:main" aurweb-tuvotereminder = "aurweb.scripts.tuvotereminder:main" aurweb-usermaint = "aurweb.scripts.usermaint:main" aurweb-config = "aurweb.scripts.config:main" +aurweb-adduser = "aurweb.scripts.adduser:main" diff --git a/test/test_adduser.py b/test/test_adduser.py new file mode 100644 index 00000000..6c71a519 --- /dev/null +++ b/test/test_adduser.py @@ -0,0 +1,56 @@ +from typing import List +from unittest import mock + +import pytest + +import aurweb.models.account_type as at + +from aurweb import db +from aurweb.models import User +from aurweb.scripts import adduser +from aurweb.testing.requests import Request + +TEST_SSH_PUBKEY = ("ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAI" + "bmlzdHAyNTYAAABBBEURnkiY6JoLyqDE8Li1XuAW+LHmkmLDMW/GL5wY" + "7k4/A+Ta7bjA3MOKrF9j4EuUTvCuNXULxvpfSqheTFWZc+g= " + "kevr@volcano") + + +@pytest.fixture(autouse=True) +def setup(db_test): + return + + +def run_main(args: List[str] = []): + with mock.patch("sys.argv", ["aurweb-adduser"] + args): + adduser.main() + + +def test_adduser_no_args(): + with pytest.raises(SystemExit): + run_main() + + +def test_adduser(): + run_main(["-u", "test", "-e", "test@example.org", "-p", "abcd1234"]) + test = db.query(User).filter(User.Username == "test").first() + assert test is not None + assert test.login(Request(), "abcd1234") + + +def test_adduser_tu(): + run_main([ + "-u", "test", "-e", "test@example.org", "-p", "abcd1234", + "-t", at.TRUSTED_USER + ]) + test = db.query(User).filter(User.Username == "test").first() + assert test is not None + assert test.AccountTypeID == at.TRUSTED_USER_ID + + +def test_adduser_ssh_pk(): + run_main(["-u", "test", "-e", "test@example.org", "-p", "abcd1234", + "--ssh-pubkey", TEST_SSH_PUBKEY]) + test = db.query(User).filter(User.Username == "test").first() + assert test is not None + assert TEST_SSH_PUBKEY.startswith(test.ssh_pub_key.PubKey)