benadida / helios-server

Helios server
http://heliosvoting.org
Apache License 2.0
711 stars 341 forks source link

Fixed issue in `create_user` #370

Open crazyscientist opened 1 year ago

crazyscientist commented 1 year ago

The function assumed that User.get_by_type_and_id returns a User instance or None. However, this called method raises a helios_auth.models.User.DoesNotExist exception.

This commit changes the behavior of create_user to match the behavior of User.get_by_type_and_id.

crazyscientist commented 1 year ago

Example

$ ./manage.py shell
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> from helios_auth.models import User
>>> from helios_auth.auth_systems.password import create_user
>>> django.VERSION
(2, 2, 24, 'final', 0)
>>> create_user("andi","very-secret-password", "Andi")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/andi/workspace/helios-server/helios_auth/auth_systems/password.py", line 24, in create_user
    user = User.get_by_type_and_id('password', username)
  File "/home/andi/workspace/helios-server/helios_auth/models.py", line 48, in get_by_type_and_id
    return cls.objects.get(user_type = user_type, user_id = user_id)
  File "/home/andi/virtualenvs/helios/lib/python3.10/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/andi/virtualenvs/helios/lib/python3.10/site-packages/django/db/models/query.py", line 406, in get
    raise self.model.DoesNotExist(
helios_auth.models.User.DoesNotExist: User matching query does not exist.

Note: I only tested this with Python 3.10 and Django 2.2[^1]

[^1]: I'm fairly sure, that more recent Django versions behave identically.