0
# User Management
1
2
User creation and authentication fixtures for testing Django user functionality. These fixtures provide admin users, user model access, and authenticated clients for comprehensive user testing.
3
4
## Capabilities
5
6
### Admin User Creation
7
8
Create an admin user for testing administrative functionality.
9
10
```python { .api }
11
def admin_user(db: None, django_user_model, django_username_field):
12
"""
13
Create an admin/superuser for testing.
14
15
Creates a superuser with username 'admin', email 'admin@example.com',
16
and password 'password'. The user has is_staff=True and is_superuser=True.
17
18
Args:
19
db: Database fixture dependency
20
django_user_model: Django user model class
21
django_username_field: Username field name
22
23
Returns:
24
User: Django user model instance with admin privileges
25
"""
26
```
27
28
Usage example:
29
```python
30
def test_admin_functionality(admin_user):
31
assert admin_user.is_superuser
32
assert admin_user.is_staff
33
assert admin_user.username == "admin"
34
assert admin_user.email == "admin@example.com"
35
```
36
37
### Admin Client
38
39
Authenticated test client logged in as admin user.
40
41
```python { .api }
42
def admin_client(admin_user) -> django.test.Client:
43
"""
44
Django test client authenticated as admin user.
45
46
Returns a test client that is already logged in as the admin user,
47
allowing testing of admin-only views and functionality without
48
manual authentication.
49
50
Args:
51
admin_user: Admin user fixture dependency
52
53
Returns:
54
django.test.Client: Test client authenticated as admin
55
"""
56
```
57
58
Usage example:
59
```python
60
def test_admin_view_access(admin_client):
61
response = admin_client.get("/admin/")
62
assert response.status_code == 200
63
64
response = admin_client.get("/admin/myapp/mymodel/")
65
assert response.status_code == 200
66
```
67
68
### Django User Model Access
69
70
Access to the configured Django user model class.
71
72
```python { .api }
73
def django_user_model(db: None):
74
"""
75
Get the configured Django user model class.
76
77
Returns the user model class as configured in Django settings
78
(AUTH_USER_MODEL). Useful for creating users or accessing
79
user model methods in tests.
80
81
Args:
82
db: Database fixture dependency
83
84
Returns:
85
Type[AbstractUser]: Django user model class
86
"""
87
```
88
89
Usage example:
90
```python
91
def test_user_creation(django_user_model):
92
User = django_user_model
93
user = User.objects.create_user(
94
username="testuser",
95
email="test@example.com",
96
password="testpass123"
97
)
98
assert user.username == "testuser"
99
assert user.is_active
100
```
101
102
### Django Username Field
103
104
Get the username field name for the configured user model.
105
106
```python { .api }
107
def django_username_field(django_user_model) -> str:
108
"""
109
Get the username field name for the Django user model.
110
111
Returns the field name used as the username field in the
112
configured Django user model (usually 'username' but can
113
be customized in custom user models).
114
115
Args:
116
django_user_model: Django user model fixture dependency
117
118
Returns:
119
str: Username field name (e.g., 'username', 'email')
120
"""
121
```
122
123
Usage example:
124
```python
125
def test_username_field(django_user_model, django_username_field):
126
User = django_user_model
127
username_field = django_username_field
128
129
user_data = {username_field: "testuser", "password": "testpass123"}
130
user = User.objects.create_user(**user_data)
131
132
assert getattr(user, username_field) == "testuser"
133
```
134
135
## User Management Types
136
137
```python { .api }
138
from django.contrib.auth.models import AbstractUser, AbstractBaseUser
139
from django.contrib.auth import get_user_model
140
from typing import Type, Any
141
142
# User model types
143
UserModel = Type[AbstractUser]
144
UsernameField = str
145
146
# User instance type (actual user object)
147
User = AbstractUser
148
149
# Common user fields
150
class UserFields:
151
username: str
152
email: str
153
first_name: str
154
last_name: str
155
is_staff: bool
156
is_active: bool
157
is_superuser: bool
158
date_joined: datetime
159
last_login: datetime
160
161
# User manager methods available on user model
162
class UserManager:
163
def create_user(self, username: str, email: str = None, password: str = None, **extra_fields) -> AbstractUser: ...
164
def create_superuser(self, username: str, email: str = None, password: str = None, **extra_fields) -> AbstractUser: ...
165
def get_by_natural_key(self, username: str) -> AbstractUser: ...
166
```