Skip to content

Add unit tests for User model and configure dedicated test database#493

Merged
tudoramariei merged 4 commits intocode4romania:mainfrom
MissTipo:main
Apr 15, 2025
Merged

Add unit tests for User model and configure dedicated test database#493
tudoramariei merged 4 commits intocode4romania:mainfrom
MissTipo:main

Conversation

@MissTipo
Copy link
Copy Markdown
Contributor

@MissTipo MissTipo commented Apr 6, 2025

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 and create_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 local redirectioneaza_test database (hosted on localhost) to avoid conflicts with production data.

How to verify

  1. Make sure the redirectioneaza_test database exists locally (e.g. via createdb redirectioneaza_test).
  2. Run python manage.py test users and 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.

MissTipo added 2 commits April 5, 2025 13:50
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>
Copy link
Copy Markdown
Member

@tudoramariei tudoramariei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes are still necessary so that all the checks can pass.

Comment thread backend/users/tests.py Outdated
Comment on lines +35 to +39
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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _.

Comment thread backend/users/tests.py
Comment on lines +2 to +9
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +23 to +29
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")),
})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Working on it.

MissTipo added 2 commits April 9, 2025 11:11
… 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>
@tudoramariei tudoramariei merged commit 23a1392 into code4romania:main Apr 15, 2025
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants