From 798c27a1f136837aa9f7836c6f47636773525b29 Mon Sep 17 00:00:00 2001 From: lihuacai Date: Sat, 18 Nov 2023 13:13:59 +0800 Subject: [PATCH 1/3] fix(middleware): Improve middleware's JsonResponse and add GitHub Actions Upgraded middleware to preserve original response's status code when creating JsonResponse. This is needed to properly and transparently convey statuses from the underlying services. Moreover, added GitHub Actions to automatically run Django tests on push and pull_request events for more robust code quality assurance. This will offer automated testing with various Python versions and alert on possible issues earlier. --- .github/workflows/django-test.yml | 27 +++++++++++++++++++++++++++ core/middleware.py | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/django-test.yml diff --git a/.github/workflows/django-test.yml b/.github/workflows/django-test.yml new file mode 100644 index 0000000..8ec47eb --- /dev/null +++ b/.github/workflows/django-test.yml @@ -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 \ No newline at end of file diff --git a/core/middleware.py b/core/middleware.py index ceecacc..bc28817 100644 --- a/core/middleware.py +++ b/core/middleware.py @@ -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: ... From c0caf1084b4b5a0d328d81267e251a423771aa7e Mon Sep 17 00:00:00 2001 From: lihuacai Date: Sat, 18 Nov 2023 13:15:05 +0800 Subject: [PATCH 2/3] feat(test): Add authentication token tests This commit introduces two new tests in the file core/tests.py. These tests are designed to verify the functionality of the authentication token generation process. The first test, 'test_auth_token_success', checks whether a token is correctly generated when valid user credentials are provided. The second test, 'test_auth_token_failure', confirms the system's response when invalid credentials are provided. These tests will help ensure the reliability of our authentication system. --- core/tests.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 core/tests.py diff --git a/core/tests.py b/core/tests.py new file mode 100644 index 0000000..33ce912 --- /dev/null +++ b/core/tests.py @@ -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"], + ) From 7602fdb459dc693a6a4e67b8520316170e93bc6b Mon Sep 17 00:00:00 2001 From: lihuacai Date: Sat, 18 Nov 2023 13:18:23 +0800 Subject: [PATCH 3/3] chore: Add django-ninja-jwt to requirements Added django-ninja-jwt==5.2.7 to requirements.txt. This package is necessary to introduce new tests for the authentication token process. Making sure the token generation and validation is robust enough helping us maintain the security and reliability of our authentication system. --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index c543705..62e1940 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ PyMySQL~=1.0.3 django-redis gevent==22.10.2 gunicorn==20.1.0 +django-ninja-jwt==5.2.7