Skip to content

Commit

Permalink
Add gui error on duplicate email register
Browse files Browse the repository at this point in the history
  • Loading branch information
rbaltrusch committed Nov 12, 2023
1 parent 2b494a1 commit a3dc53e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
13 changes: 12 additions & 1 deletion desktop_shop/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
"""

import argparse
import sqlite3
from typing import Any, List

from desktop_shop import crypto
from desktop_shop.database import _statements


class DuplicateUserError(Exception):
"""Exception for duplicate user"""

def __init__(self):
super().__init__("User email is already in use.")


def query_user_id_from_user_email(cursor, user_email):
"""Queries for the user id of the user specified by the user_email passed (unique)"""
command = _statements.QUERY_USER_ID_BY_EMAIL
Expand Down Expand Up @@ -122,7 +130,10 @@ def add_user(cursor, user_data, password, pepper="", iterations=100_000):
hashed_password = hash_function.hash(password, salt + pepper)
user_data = list(user_data) + [salt, hashed_password, str(hash_function)]

cursor.execute(_statements.INSERT_USER, user_data)
try:
cursor.execute(_statements.INSERT_USER, user_data)
except sqlite3.IntegrityError:
raise DuplicateUserError() from None


def update_user(cursor, user_data, user_id):
Expand Down
6 changes: 5 additions & 1 deletion desktop_shop/gui/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ def register():
return

with gui.db_conn as cursor:
session_id = server.add_user(cursor, user_data, password)
try:
session_id = server.add_user(cursor, user_data, password)
except server.DuplicateUserError:
show_error_message("You cannot use the supplied email.")
return

gui.app.data["session_id"] = session_id
if session_id is not None:
Expand Down
4 changes: 3 additions & 1 deletion desktop_shop/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from desktop_shop import crypto, util
from desktop_shop.database import database
from desktop_shop.database.database import DuplicateUserError # pylint: disable=unused-import

# combined with every salt for extra security in pw hashing
PEPPER = "secret"
Expand Down Expand Up @@ -98,7 +99,8 @@ def query_product_data_from_product_table_by_product_ids(cursor, product_ids):


def add_user(cursor, user_data, password):
"""Adds a new user to the users table, with the user_data specified"""
"""Adds a new user to the users table, with the user_data specified.
Raises a DuplicateUserError if the specified email has already been used."""
database.add_user(cursor, list(user_data), password, PEPPER)
new_session_id = _add_new_session(cursor, user_data.email)
return new_session_id
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/gui/callbacks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

# pylint: disable=wrong-import-position
from desktop_shop.gui import callbacks, init
from desktop_shop import gui
from desktop_shop import gui, server
from desktop_shop.user import UserSignUpData
from desktop_shop.datagen import generate_data

Expand Down Expand Up @@ -104,6 +104,13 @@ def test_register(monkeypatch: pytest.MonkeyPatch):
assert_no_error_message()


@pytest.mark.slow
def test_register_twice(monkeypatch: pytest.MonkeyPatch):
test_register(monkeypatch)
with pytest.raises(server.DuplicateUserError):
test_register(monkeypatch)


@pytest.mark.slow
def test_sign_out(monkeypatch: pytest.MonkeyPatch):
init_gui(monkeypatch)
Expand Down

0 comments on commit a3dc53e

Please sign in to comment.