Add unit tests for User model and configure dedicated test database#493
Add unit tests for User model and configure dedicated test database#493tudoramariei merged 4 commits intocode4romania:mainfrom MissTipo:main
Conversation
Implemented tests for creating users, superusers, token management, user activation/deactivation, and group properties in the users app. Signed-off-by: MissTipo <dorine.a.tipo@gmail.com>
- Modified database settings to use a dedicated test database when running tests to avoid conflicts with production data. Signed-off-by: MissTipo <dorine.a.tipo@gmail.com>
tudoramariei
left a comment
There was a problem hiding this comment.
Some changes are still necessary so that all the checks can pass.
| with self.assertRaisesMessage(ValueError, "Superuser must have is_staff=True."): | ||
| User.objects.create_superuser(email="x@example.com", password="pass", is_staff=False) | ||
| # is_superuser must be True | ||
| with self.assertRaisesMessage(ValueError, "Superuser must have is_superuser=True."): | ||
| User.objects.create_superuser(email="y@example.com", password="pass", is_superuser=False) |
There was a problem hiding this comment.
If you're checking these errors, it's good to take into account i18n.
In this case, instead of checking for "Superuser must have is_staff=True.", test for _("Superuser must have is_staff=True."), where _ is from django.utils.translation import gettext as _.
| import uuid | ||
| from django.test import TestCase, override_settings | ||
| from django.urls import reverse | ||
| from django.utils import timezone | ||
| from django.contrib.auth.models import Group | ||
|
|
||
| from users.models import User, CustomUserManager | ||
| from users.groups_management import MAIN_ADMIN, NGO_ADMIN, NGO_MEMBER, RESTRICTED_ADMIN |
There was a problem hiding this comment.
Ruff is going to complain on unused imports, so remove what you didn't make use of.
In the backend/ folder, run ruff check . and it will tell you what issues you have.
| if "test" in sys.argv: | ||
| DATABASES["default"].update({ | ||
| "NAME": env("TEST_DATABASE_NAME", default="redirectioneaza_test"), | ||
| "HOST": env("TEST_DATABASE_HOST", default="localhost"), | ||
| "USER": env("TEST_DATABASE_USER", default=env("DATABASE_USER")), | ||
| "PASSWORD": env("TEST_DATABASE_PASSWORD", default=env("DATABASE_PASSWORD")), | ||
| }) |
There was a problem hiding this comment.
Black is going to complain regarding formatting on this file but also tests.py.
To check it on your own system, either run black ./ --check in the backend/ folder, or black ./backend --check in the root.
You can also run it without the --check flag, but beware that if you're not targeting a folder in the root of which you have a pyproject.toml, it will not apply the correct rules and might end up trying to over-format code.
Here's a sample of the differences with a lot of lines redacted for readability
❯ black ./backend --check
would reformat ~/redirectioneaza/backend/redirectioneaza/settings/database.py
Oh no! 💥 💔 💥
1 file would be reformatted, 141 files would be left unchanged.
❯ black ./ --check
would reformat ~/redirectioneaza/backend/donations/common/models_hashing.py
[...] — 91 more lines
would reformat ~/redirectioneaza/backend/users/models.py
Oh no! 💥 💔 💥
93 files would be reformatted, 108 files would be left unchanged.
… issues. Signed-off-by: MissTipo <dorine.a.tipo@gmail.com>
Replaced hardcoded error message with gettext-aware version to support translation. Signed-off-by: MissTipo <dorine.a.tipo@gmail.com>
This PR introduces a suite of unit tests for the User model (and its custom manager) in the users app, and updates the database settings so that tests run against a dedicated test database instead of the production/Postgres host. These changes improve confidence in user‑related functionality and ensure an isolated environment for running tests.
What’s been changed
users/tests.py
Added tests for
CustomUserManager.create_user andcreate_superuser, including invalid‑flag error cases.Covered token lifecycle methods (
refresh_token,verify_token,clear_token).Verified activate/deactivate behavior.
Tested
create_admin_login_url URL generation.Exercised group‑based properties (
is_admin,is_ngo_admin,is_ngo_member) against all relevant roles.redirectioneaza/settings/database.py
Imported sys and detect "test" in sys.argv.
When running tests, override
DATABASES['default']to point at a localredirectioneaza_testdatabase (hosted on localhost) to avoid conflicts with production data.How to verify
createdb redirectioneaza_test).python manage.py test usersand confirm all 8 tests pass.This lays the groundwork for broader test coverage across the project and ensures our CI/test runs are safe and repeatable.