Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
134 lines (108 loc) · 3.44 KB
/
Copy pathconftest.py
File metadata and controls
134 lines (108 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework.test import APIClient
from rest_framework_simplejwt.tokens import AccessToken
from brands.models import Brand
from categories.models import Category
from products.models import Product
User = get_user_model()
# ---------------------------------
# Common Fixtures
# ---------------------------------
@pytest.fixture
def api_client():
"""Provides an instance of APIClient."""
return APIClient()
@pytest.fixture
def user(db):
"""Provides a regular user."""
return User.objects.create_user(
email="user@example.com",
username="username1",
password="testpassword",
is_active=True,
)
@pytest.fixture
def admin_user(db):
"""Provides an admin user."""
return User.objects.create_superuser(
email="admin@example.com",
username="admin",
password="adminpassword",
is_active=True,
)
@pytest.fixture
def authenticated_admin_client(api_client, admin_user):
"""Provides an authenticated client with admin user."""
token = AccessToken.for_user(admin_user)
api_client.credentials(HTTP_AUTHORIZATION=f"Bearer {str(token)}")
return api_client
@pytest.fixture
def authenticated_user_client(api_client, user):
"""Provides an authenticated client with regular user."""
api_client.force_authenticate(user=user)
return api_client
@pytest.fixture
def admin_api_client(admin_user, api_client):
"""Provides an API client authenticated as admin using force_authenticate."""
api_client.force_authenticate(user=admin_user)
return api_client
@pytest.fixture
def verify_email_setup(db):
"""Setup user and URL for verify email tests."""
user = User.objects.create(
username="testuser",
email="test@example.com",
is_active=False,
)
verify_email_url = reverse("verify_email")
return user, verify_email_url
# ---------------------------------
# Brand Fixtures
# ---------------------------------
@pytest.fixture
def brand(db):
"""Create a brand for products."""
return Brand.objects.create(name="Test Brand", slug="test-brand")
# ---------------------------------
# Product Fixtures
# ---------------------------------
@pytest.fixture
def product(db, brand, category_active):
"""Create a product for testing."""
return Product.objects.create(
web_id="12345",
slug="test-product",
name="Test Product",
description="A test product",
brand=brand,
category=category_active,
is_active=True,
)
# ---------------------------------
# Categories Fixtures
# ---------------------------------
@pytest.fixture
def category_active(db):
"""Creates an active category."""
return Category.objects.create(
name="Active Category", slug="active-category", is_active=True
)
@pytest.fixture
def category_inactive(db):
"""Creates an inactive category."""
return Category.objects.create(
name="Inactive Category", slug="inactive-category", is_active=False
)
# ---------------------------------
# Test Settings Overrides
# ---------------------------------
@pytest.fixture(autouse=True)
def configure_rest_framework_settings(settings):
"""Overrides REST framework settings for testing."""
settings.REST_FRAMEWORK = {
**settings.REST_FRAMEWORK,
"DEFAULT_THROTTLE_CLASSES": [],
"DEFAULT_THROTTLE_RATES": {},
}