Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Custom User model #47

Merged
merged 1 commit into from
Apr 12, 2024
Merged

Introduce Custom User model #47

merged 1 commit into from
Apr 12, 2024

Conversation

ZhangChen199102
Copy link
Contributor

Created an accounts app with custom User model.

Issue: #24

Copy link
Contributor

@a-musing-moose a-musing-moose left a comment

Choose a reason for hiding this comment

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

A great start. I think your CI issues are because of the app name missing the top level package. A few other minor issues that should be resolved before we can merge this. Details inline.


class AccountsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "accounts"
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: This should be {{ project_name }}.accounts

Because our Django application is inside a top level package, that top level package name needs to form part of the app config name. Simeon came across the same issue and reported it in #25

And because this is a template we do not know the packahe/project name ahead of time. Which is fine because startproject will do this for us it we use the {{ project_name }} template tag.

I suspect that this will fix your CI issue.

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

issue:pass is not needed here

pass

def __str__(self):
return self.username
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: Please ensure that your IDE it set to ensure there is a trailing line ending. The little red symbol above represents that there is not line end on the file line.‏

@@ -0,0 +1,7 @@
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: We could just call this User.

I'm not sure calling it CustomUser is helpful. It implies there is a non-custom User which is true in the context of Django as a whole, but not really in the context of just the project in which this model exists. ‏

Comment on lines 17 to 25
def test_create_superuser(self):
User = get_user_model()
admin_user = User.objects.create_superuser(username="superuser", password="123")

self.assertEqual(admin_user.username, "superuser")
self.assertTrue(admin_user.is_active)
self.assertTrue(admin_user.is_staff)
self.assertTrue(admin_user.is_superuser)
self.assertEqual(admin_user.email, '')
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: Thanks for covering the super user as well as the basic user.‏

class UsersManagersTests(TestCase):

def test_create_user(self):
User = get_user_model()
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Assert that it return the correct class

If the settings are incorrect, then get_user_model will return the standard, default user model. Since our custom model is functionally exactly the same as the default one - this test would still pass.‏

It would be a good idea to create a separate test to ensure it actually returns the expected class.

from django.test import TestCase


class UsersManagersTests(TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: ‏You do not need to extend TestCase

We are using Pytest for our test suite - not Django's built in unit test framework. You should not extend the Django TestCase. You don't even need to wrap your tests in a class. As long as the test function begins def test_ it will be picked up.

See: https://github.com/ackama/django-template/blob/main/template/src/tests/system/main/test_views.py for an example.

@ZhangChen199102 ZhangChen199102 force-pushed the add-custom-user-model branch 13 times, most recently from 86cbebb to 0e6ee6b Compare April 11, 2024 04:39
Copy link
Contributor

@a-musing-moose a-musing-moose left a comment

Choose a reason for hiding this comment

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

This looks great but we need to get to the bottom of why you are getting S101 linting errors for tests. That rule should already be disabled for the tests folder.

Comment on lines 6 to 29
def test_create_user() -> None:
User = get_user_model()
user = User.objects.create_user(username="test", password="123") # noqa: S106

assert isinstance(User, UserModel) # noqa: S101
assert user.username == "test" # noqa: S101
assert user.is_active # noqa: S101
assert not user.is_staff # noqa: S101
assert not user.is_superuser # noqa: S101
assert user.email == "" # noqa: S101


def test_create_superuser() -> None:
User = get_user_model()
admin_user = User.objects.create_superuser( # noqa: S106
username="superuser", password="123"
)

assert isinstance(User, UserModel) # noqa: S101
assert admin_user.username == "superuser" # noqa: S101
assert admin_user.is_active # noqa: S101
assert admin_user.is_staff # noqa: S101
assert admin_user.is_superuser # noqa: S101
assert admin_user.email == "" # noqa: S101
Copy link
Contributor

Choose a reason for hiding this comment

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

chore: The S101 noqa markers should not be necessary

That error code is about using asserts - and only applies to production code because running code with the python optimise flag on will remove asserts. It doesn't apply to tests where we need to use them all the time. This linting error should be disabled for the whole src/tests directory. See: https://github.com/ackama/django-template/blob/main/template/setup.cfg

Are you seeing this linting errors when you run inv lint in your projects created with this template?

The S106 is fine.

@ZhangChen199102 ZhangChen199102 force-pushed the add-custom-user-model branch 3 times, most recently from 7670fc8 to e29f8ed Compare April 12, 2024 03:52
Copy link
Contributor

@a-musing-moose a-musing-moose left a comment

Choose a reason for hiding this comment

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

Perfect! Thank you.

@ZhangChen199102 ZhangChen199102 merged commit 5bca363 into main Apr 12, 2024
1 check passed
@ZhangChen199102 ZhangChen199102 deleted the add-custom-user-model branch April 12, 2024 04:23
ZhangChen199102 added a commit that referenced this pull request Apr 15, 2024
Created ADR for the custom User model introduced by #47
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