Skip to content

Commit

Permalink
Merge pull request #198 from anexia-it/SIANXKE-410_userUUID
Browse files Browse the repository at this point in the history
Test cases for issue #99 (UUID as user model primary key field)
  • Loading branch information
nezhar authored Nov 5, 2024
2 parents b68009c + b3eb207 commit 79118b6
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ jobs:
# prepare Django project: link all necessary data from the test project into the root directory
# Hint: Simply changing the directory does not work (leads to missing files in coverage report)
ln -s ./tests/test test
ln -s ./tests/user_id_uuid_testapp user_id_uuid_testapp
ln -s ./tests/manage.py manage.py
ln -s ./tests/settings.py settings.py
ln -s ./tests/urls.py urls.py
Expand Down
6 changes: 5 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
'rest_framework',

# include multi token auth
'django_rest_passwordreset'
'django_rest_passwordreset',

# test app
'user_id_uuid_testapp',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -123,3 +126,4 @@

STATIC_URL = '/static/'

AUTH_USER_MODEL = "user_id_uuid_testapp.User"
4 changes: 3 additions & 1 deletion tests/test/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model

User = get_user_model()

try:
from unittest.mock import patch
Expand Down
4 changes: 3 additions & 1 deletion tests/test/test_auth_test_case.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from datetime import timedelta

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.test import override_settings
from django.utils import timezone
from rest_framework import status
Expand All @@ -11,6 +11,8 @@
from django_rest_passwordreset.views import clear_expired_tokens, generate_token_for_email
from tests.test.helpers import HelperMixin, patch

User = get_user_model()


class AuthTestCase(APITestCase, HelperMixin):
"""
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions tests/user_id_uuid_testapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class TestappConfig(AppConfig):
name = "user_id_uuid_testapp"
45 changes: 45 additions & 0 deletions tests/user_id_uuid_testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 3.2.18 on 2024-10-25 12:00

import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
import uuid


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
Empty file.
8 changes: 8 additions & 0 deletions tests/user_id_uuid_testapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import uuid

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Empty file.
5 changes: 5 additions & 0 deletions tests/user_id_uuid_testapp/tests/test_auth_test_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from test.test_auth_test_case import AuthTestCase


class AuthTestCase(AuthTestCase):
pass
21 changes: 21 additions & 0 deletions tests/user_id_uuid_testapp/tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.apps import apps
from django.conf import settings
from django.db.models import UUIDField
from django.test import SimpleTestCase

from django_rest_passwordreset.models import ResetPasswordToken
from user_id_uuid_testapp.models import User


class TestSetup(SimpleTestCase):
def test_installed_apps(self):
self.assertIn("django_rest_passwordreset", settings.INSTALLED_APPS)

def test_models(self):
self.assertIs(
apps.get_model("django_rest_passwordreset", "ResetPasswordToken"), ResetPasswordToken
)
self.assertIs(apps.get_model("user_id_uuid_testapp", "User"), User)
for field in User._meta.fields:
if field.name == "id":
self.assertTrue(isinstance(field, UUIDField))
16 changes: 16 additions & 0 deletions tests/user_id_uuid_testapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from rest_framework import viewsets

from drf_anonymous_login.views import AnonymousLoginAuthenticationModelViewSet

from .models import PrivateModel, PublicModel
from .serializers import PrivateModelSerializer, PublicModelSerializer


class PublicModelViewSet(viewsets.ModelViewSet):
queryset = PublicModel.objects.all()
serializer_class = PublicModelSerializer


class PrivateModelViewSet(AnonymousLoginAuthenticationModelViewSet):
queryset = PrivateModel.objects.all()
serializer_class = PrivateModelSerializer

0 comments on commit 79118b6

Please sign in to comment.