-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from lihuacai168/feat/jwt-auth
fix(middleware): Improve middleware's JsonResponse and add GitHub Actions
- Loading branch information
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
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 @@ | ||
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 |
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
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"], | ||
) |
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 |
---|---|---|
|
@@ -13,3 +13,4 @@ PyMySQL~=1.0.3 | |
django-redis | ||
gevent==22.10.2 | ||
gunicorn==20.1.0 | ||
django-ninja-jwt==5.2.7 |