-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
234 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"python.envFile": "${workspaceFolder}/env/local.env", | ||
"python.testing.pytestArgs": [ | ||
"." | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,72 @@ | ||
import uuid | ||
|
||
from django.db import models | ||
from django.contrib.auth.models import AbstractUser | ||
from django.contrib.auth.hashers import make_password | ||
from django.contrib.auth.models import PermissionsMixin | ||
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager | ||
from django.utils import timezone | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
class User(AbstractUser): | ||
class UserManager(BaseUserManager): | ||
use_in_migrations = True | ||
|
||
def _create_user(self, email, password, **extra_fields): | ||
"""Create and save a user with the given email, and | ||
password. | ||
""" | ||
if not email: | ||
raise ValueError("The given email must be set") | ||
|
||
email = self.normalize_email(email) | ||
user = self.model(email=email, **extra_fields) | ||
user.password = make_password(password) | ||
user.save(using=self._db) | ||
return user | ||
|
||
def create_user(self, email, password=None, **extra_fields): | ||
extra_fields.setdefault("is_staff", False) | ||
extra_fields.setdefault("is_superuser", False) | ||
return self._create_user(email, password, **extra_fields) | ||
|
||
def create_superuser(self, email=None, password=None, **extra_fields): | ||
extra_fields.setdefault("is_staff", True) | ||
extra_fields.setdefault("is_superuser", True) | ||
|
||
if extra_fields.get("is_staff") is not True: | ||
raise ValueError("Superuser must have is_staff=True.") | ||
if extra_fields.get("is_superuser") is not True: | ||
raise ValueError("Superuser must have is_superuser=True.") | ||
|
||
return self._create_user(email, password, **extra_fields) | ||
|
||
def get_by_natural_key(self, username): | ||
return self.get(**{f"{self.model.USERNAME_FIELD}__iexact": username}) | ||
|
||
|
||
class User(AbstractBaseUser, PermissionsMixin): | ||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) | ||
first_name = models.CharField(_("first name"), max_length=150, blank=True) | ||
last_name = models.CharField(_("last name"), max_length=150, blank=True) | ||
email = models.EmailField(_("email address"), unique=True) | ||
is_staff = models.BooleanField( | ||
_("staff status"), | ||
default=False, | ||
help_text=_("Designates whether the user can log into this admin site."), | ||
) | ||
is_active = models.BooleanField( | ||
_("active"), | ||
default=True, | ||
help_text=_( | ||
"Designates whether this user should be treated as active. " | ||
"Unselect this instead of deleting accounts." | ||
), | ||
) | ||
date_joined = models.DateTimeField(_("date joined"), default=timezone.now) | ||
|
||
objects = UserManager() | ||
|
||
EMAIL_FIELD = "email" | ||
USERNAME_FIELD = "email" | ||
|
||
objects = UserManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,69 @@ | ||
# Create your tests here. | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def email(): | ||
return "[email protected]" | ||
|
||
|
||
@pytest.fixture | ||
def password(): | ||
return "safe11password" | ||
|
||
|
||
@pytest.fixture | ||
def user(django_user_model, email, password): | ||
return django_user_model.objects.create_user(email=email, password=password) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_can_register_user(django_user_model, client, email, password): | ||
user_qty = django_user_model.objects.count() | ||
|
||
response = client.post( | ||
"/api/registration/", | ||
{ | ||
"email": email, | ||
"password1": password, | ||
"password2": password, | ||
}, | ||
) | ||
|
||
assert response.status_code == 201 | ||
assert django_user_model.objects.count() == user_qty + 1 | ||
|
||
|
||
def test_can_login_as_user(user, client, email, password): | ||
response = client.post("/api/login/", {"email": email, "password": password}) | ||
|
||
assert response.status_code == 200 | ||
assert response.data["access"] | ||
assert response.data["refresh"] | ||
|
||
|
||
def test_can_login_with_email_in_different_case(user, client, email, password): | ||
assert email != email.upper() | ||
|
||
response = client.post( | ||
"/api/login/", {"email": email.upper(), "password": password} | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert response.data["access"] | ||
assert response.data["refresh"] | ||
|
||
|
||
def test_cant_register_with_email_in_different_case(user, client, password): | ||
assert user.email != user.email.upper() | ||
|
||
response = client.post( | ||
"/api/registration/", | ||
{ | ||
"email": user.email.upper(), | ||
"password1": password, | ||
"password2": password, | ||
}, | ||
) | ||
|
||
assert response.status_code == 400 | ||
assert response.data["email"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.