-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* patch: remove view * patch: remove generate special token url * patch: create token manually from test * patch: generate token from commands * patch: remove generate token tests
- Loading branch information
1 parent
cbd9e53
commit 81d69fc
Showing
5 changed files
with
34 additions
and
65 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
django_project/minisass/management/commands/generate_token.py
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,27 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.contrib.auth.models import User | ||
from rest_framework_simplejwt.tokens import AccessToken | ||
from datetime import timedelta | ||
|
||
class Command(BaseCommand): | ||
help = 'Manually generate a special token for a given user email' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('email', type=str, help="The user's email for whom the token should be generated") | ||
|
||
def handle(self, *args, **kwargs): | ||
email = kwargs['email'] | ||
|
||
try: | ||
user = User.objects.get(email=email) | ||
except User.DoesNotExist: | ||
self.stdout.write(self.style.ERROR(f"User with email {email} not found")) | ||
return | ||
|
||
# Generate token for the user | ||
token = AccessToken.for_user(user) | ||
# Set token expiry to 100 years | ||
token.set_exp(lifetime=timedelta(days=365 * 100)) | ||
|
||
# Output the token in the console | ||
self.stdout.write(self.style.SUCCESS(f"Generated token for {email}: {str(token)}")) |
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 |
---|---|---|
|
@@ -14,51 +14,6 @@ | |
from rest_framework import status | ||
|
||
|
||
class GenerateSpecialTokenTest(APITestCase): | ||
def setUp(self): | ||
# Create a test admin user | ||
self.admin_user = User.objects.create_user( | ||
username='adminuser', | ||
email='[email protected]', | ||
password='adminpassword', | ||
is_staff=True | ||
) | ||
|
||
# Create a test regular user | ||
self.regular_user = User.objects.create_user( | ||
username='regularuser', | ||
email='[email protected]', | ||
password='regularpassword' | ||
) | ||
|
||
def test_generate_token_success_for_admin(self): | ||
# Use the admin user's email to generate a token | ||
url = reverse('generate_special_token', args=[self.admin_user.email]) | ||
response = self.client.post(url) | ||
|
||
# Check that the response is successful | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertIn('token', response.json()) | ||
|
||
def test_generate_token_forbidden_for_regular_user(self): | ||
# Use the regular user's email to generate a token | ||
url = reverse('generate_special_token', args=[self.regular_user.email]) | ||
response = self.client.post(url) | ||
|
||
# Check that the response indicates the user is not an admin | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
self.assertEqual(response.json(), {'error': 'User is not an admin'}) | ||
|
||
def test_generate_token_user_not_found(self): | ||
# Use a non-existent email | ||
url = reverse('generate_special_token', args=['[email protected]']) | ||
response = self.client.post(url) | ||
|
||
# Check that the response indicates the user was not found | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
self.assertEqual(response.json(), {'error': 'User not found'}) | ||
|
||
|
||
|
||
class PasswordResetTest(APITestCase): | ||
def setUp(self): | ||
|
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