-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test(login,mockapi): Add tests, modify database models and update dependencies In this commit, unit tests for mockapi serializers and login views are added to ensure proper functionality. Database model changes include addition of new columns and alteration of existing columns in 'MockAPI' model. Project dependencies are also updated with the addition of pytest. Last but not the least, login views have been updated for returning appropriate HTTP status codes and URL configurations updated. * ci: Add GitHub Actions workflow and alter Django model A GitHub Actions workflow for running pytest and coverage has been added to the project. In addition, a modification to the Django model has been made to alter the "first_name" field for the "MyUser" model. --------- Co-authored-by: rikasai233 <[email protected]>
- Loading branch information
1 parent
8d26f03
commit 70f15e5
Showing
11 changed files
with
336 additions
and
11 deletions.
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,38 @@ | ||
name: Python application test with pytest and coverage | ||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
types: [opened, synchronize] | ||
paths: | ||
- '**.py' | ||
- 'requirements.txt' | ||
- '!./web/**' | ||
- 'Dockerfile' | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
||
- name: Check out code | ||
uses: actions/checkout@v2 | ||
- name: Set up Python 3.11 | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.11 | ||
|
||
- name: Install poetry | ||
run: | | ||
curl -sSL https://install.python-poetry.org | python3 - | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
poetry install | ||
- name: Test with pytest and coverage | ||
run: | | ||
pytest tests/ --cov=tests/ | ||
- name: Upload coverage to Codecove | ||
uses: codecov/codecov-action@v2 | ||
with: | ||
fail_ci_if_error: true |
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,18 @@ | ||
# Generated by Django 4.1.13 on 2024-03-15 01:11 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("fastrunner", "0023_auto_20210910_1155"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="project", | ||
name="is_deleted", | ||
field=models.IntegerField(default=0, null=True, verbose_name="是否删除"), | ||
), | ||
] |
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,20 @@ | ||
# Generated by Django 4.1.13 on 2024-03-15 01:10 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("fastuser", "0006_myuser_show_hosts"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="myuser", | ||
name="first_name", | ||
field=models.CharField( | ||
blank=True, max_length=150, verbose_name="first name" | ||
), | ||
), | ||
] |
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
61 changes: 61 additions & 0 deletions
61
mock/migrations/0006_mockapi_request_body_mockapi_version_and_more.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,61 @@ | ||
# Generated by Django 4.1.13 on 2024-03-15 01:18 | ||
|
||
from django.db import migrations, models | ||
import mock.models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("mock", "0005_mockapilog_request_id_alter_mockapi_api_id_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="mockapi", | ||
name="request_body", | ||
field=models.JSONField(blank=True, default=dict, null=True), | ||
), | ||
migrations.AddField( | ||
model_name="mockapi", | ||
name="version", | ||
field=models.IntegerField(default=1), | ||
), | ||
migrations.AlterField( | ||
model_name="mockapi", | ||
name="api_id", | ||
field=models.CharField( | ||
default=mock.models.generate_uuid, max_length=32, unique=True | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="mockapi", | ||
name="api_name", | ||
field=models.CharField(max_length=100), | ||
), | ||
migrations.AlterField( | ||
model_name="mockapi", | ||
name="response_text", | ||
field=models.TextField( | ||
default='\ndef execute(req, resp):\n import requests\n\n url = "http://localhost:8000/api/mock/mock_api/"\n\n payload = {}\n headers = {\n "accept": "application/json",\n "X-CSRFToken": "fk5wQDlKC6ufRjk7r38pfbqyq7mTtyc5NUUqkFN5lbZf6nyHVSbAUVoqbwaGcQHT",\n }\n\n response = requests.request("GET", url, headers=headers, data=payload)\n resp.data = response.json()\n' | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="mockapilog", | ||
name="request_id", | ||
field=models.CharField( | ||
blank=True, | ||
db_index=True, | ||
default=mock.models.generate_uuid, | ||
max_length=100, | ||
null=True, | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="mockproject", | ||
name="project_id", | ||
field=models.CharField( | ||
default=mock.models.generate_uuid, max_length=100, unique=True | ||
), | ||
), | ||
] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,53 @@ | ||
import pytest | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
from django.contrib.auth import get_user_model | ||
|
||
# 假设LoginView用的Url名字是'login' | ||
login_url = reverse("login") | ||
|
||
|
||
@pytest.mark.django_db # 如果你的测试需要数据库操作 | ||
class TestLoginView: | ||
def setup_method(self): | ||
User = get_user_model() | ||
User.objects.create_user('validUser', '[email protected]', 'validPassword') | ||
|
||
def test_login_with_correct_credentials(self): | ||
client = APIClient() | ||
user_data = {"username": "validUser", "password": "validPassword"} | ||
response = client.post(login_url, user_data, format="json") | ||
assert response.status_code == status.HTTP_200_OK | ||
assert ( | ||
"token" in response.data | ||
) # assuming that generate_token_and_respond returns a token in response | ||
|
||
def test_login_with_incorrect_credentials(self): | ||
client = APIClient() | ||
user_data = {"username": "invalidUser", "password": "invalidPassword"} | ||
response = client.post(login_url, user_data, format="json") | ||
# body | ||
assert response.json() == {'code': "0103", 'success': False, 'msg': "用户名或密码错误"} | ||
assert ( | ||
response.status_code == status.HTTP_200_OK | ||
) # assuming LOGIN_FAILED responds with status 401 | ||
|
||
def test_login_with_no_credentials(self): | ||
client = APIClient() | ||
user_data = {} | ||
response = client.post(login_url, user_data, format="json") | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
||
def test_login_with_partial_credentials(self): | ||
client = APIClient() | ||
|
||
# Missing username | ||
user_data = {"password": "validPassword"} | ||
response = client.post(login_url, user_data, format="json") | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST | ||
|
||
# Missing password | ||
user_data = {"username": "validUser"} | ||
response = client.post(login_url, user_data, format="json") | ||
assert response.status_code == status.HTTP_400_BAD_REQUEST |
Oops, something went wrong.