Skip to content

Commit

Permalink
Merge pull request #7 from lihuacai168/feat/jwt-auth
Browse files Browse the repository at this point in the history
fix(middleware): Improve middleware's JsonResponse and add GitHub Actions
  • Loading branch information
lihuacai168 authored Nov 18, 2023
2 parents 4d29b3f + 7602fdb commit e1374b6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/django-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Django Test

on: [push, pull_request]


jobs:
build:

runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.8, 3.9, '3.10', 3.11]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Tests
run: |
python manage.py test
4 changes: 3 additions & 1 deletion core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def __call__(self, request):
try:
data: dict = json.loads(response.content)
_, data["trace_id"] = response.headers._store.get("trace_id")
response = JsonResponse(data)
_response = JsonResponse(data)
_response.status_code = response.status_code
response = _response

except Exception:
...
Expand Down
45 changes: 45 additions & 0 deletions core/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.test import TestCase, Client
from django.contrib.auth.models import User


class AuthTokenTestCase(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user(username="testuser", password="12345")

def test_auth_token_success(self):
"""Test obtain pair token
"""
response = self.client.post(
"/api/token/pair",
{"username": "testuser", "password": "12345"},
content_type="application/json",
)

self.assertEqual(response.status_code, 200)
response_data = response.json()

self.assertIn("access", response_data["data"])
self.assertIn("refresh", response_data["data"])

user_data = response_data["data"]["user"]
self.assertEqual(user_data["email"], self.user.email)
self.assertEqual(user_data["first_name"], self.user.first_name)

self.assertEqual(response_data["success"], True)

def test_auth_token_failure(self):
"""Test the failure of authentication token generation.
"""
response = self.client.post(
"/api/token/pair",
{"username": "wronguser", "password": "wrongpassword"},
content_type="application/json",
)
self.assertEqual(response.status_code, 401)
self.assertEqual(response.json()["success"], False)
self.assertIn("message", response.json())
self.assertIn(
"No active account found with the given credentials",
response.json()["message"],
)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ PyMySQL~=1.0.3
django-redis
gevent==22.10.2
gunicorn==20.1.0
django-ninja-jwt==5.2.7

0 comments on commit e1374b6

Please sign in to comment.