From 8a3893f33d294d68905a08e512f3d4ab67d00a78 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 6 May 2021 00:09:00 -0700 Subject: [PATCH] Add duplicate username check --- amanuensis/backend/user.py | 4 ++++ tests/test_db.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/amanuensis/backend/user.py b/amanuensis/backend/user.py index 9a28e67..98c4c8b 100644 --- a/amanuensis/backend/user.py +++ b/amanuensis/backend/user.py @@ -33,6 +33,10 @@ def create_user( if not display_name.strip(): display_name = username + # Query the db to make sure the username isn't taken + if db.session.query(User.username == username).count() > 0: + raise ArgumentError('Username is already taken') + new_user = User( username=username, password=password, diff --git a/tests/test_db.py b/tests/test_db.py index 9e551b0..eb05f44 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -46,3 +46,6 @@ def test_create_user(db): assert new_user assert new_user.id is not None assert new_user.created is not None + + with pytest.raises(ArgumentError): + duplicate = userq.create_user(db, **kwargs)