mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-29 16:41:56 +00:00
增加检测用户名是否存在的 api 接口和相应测试
This commit is contained in:
parent
798e68509b
commit
8b0ad78b1a
@ -5,3 +5,8 @@ from rest_framework import serializers
|
||||
class UserLoginSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=30)
|
||||
password = serializers.CharField(max_length=30)
|
||||
|
||||
|
||||
class UsernameCheckSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=30)
|
||||
|
||||
|
@ -36,3 +36,22 @@ class UserLoginAPITest(APITestCase):
|
||||
data = {"username": "test", "password": "test"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 0, "data": u"登录成功"})
|
||||
|
||||
|
||||
class UsernameCheckTest(APITestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse("username_check_api")
|
||||
User.objects.create(username="testtest")
|
||||
|
||||
def test_invalid_data(self):
|
||||
response = self.client.post(self.url, data={"username111": "testtest"})
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_username_exists(self):
|
||||
response = self.client.post(self.url, data={"username": "testtest"})
|
||||
self.assertEqual(response.data, {"code": 0, "data": True})
|
||||
|
||||
def test_username_does_not_exist(self):
|
||||
response = self.client.post(self.url, data={"username": "testtest123"})
|
||||
self.assertEqual(response.data, {"code": 0, "data": False})
|
||||
|
@ -6,7 +6,7 @@ from rest_framework.views import APIView
|
||||
from utils.shortcuts import serializer_invalid_response, error_response, success_response
|
||||
|
||||
from .models import User
|
||||
from .serializers import UserLoginSerializer
|
||||
from .serializers import UserLoginSerializer, UsernameCheckSerializer
|
||||
|
||||
|
||||
class UserLoginAPIView(APIView):
|
||||
@ -43,4 +43,22 @@ class UserChangePasswordView(APIView):
|
||||
pass
|
||||
|
||||
def post(self, request):
|
||||
pass
|
||||
pass
|
||||
|
||||
|
||||
class UsernameCheckAPIView(APIView):
|
||||
def post(self, request):
|
||||
"""
|
||||
检测用户名是否存在,存在返回True,不存在返回False
|
||||
---
|
||||
request_serializer: UsernameCheckSerializer
|
||||
"""
|
||||
serializer = UsernameCheckSerializer(data=request.DATA)
|
||||
if serializer.is_valid():
|
||||
try:
|
||||
User.objects.get(username=serializer.data["username"])
|
||||
return success_response(True)
|
||||
except User.DoesNotExist:
|
||||
return success_response(False)
|
||||
else:
|
||||
return serializer_invalid_response(serializer)
|
@ -3,7 +3,7 @@ from django.conf.urls import include, url
|
||||
from django.contrib import admin
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from account.views import UserLoginAPIView
|
||||
from account.views import UserLoginAPIView, UsernameCheckAPIView
|
||||
|
||||
urlpatterns = [
|
||||
url("^$", TemplateView.as_view(template_name="oj/index.html"), name="index_page"),
|
||||
@ -12,6 +12,7 @@ urlpatterns = [
|
||||
url(r'^login/$', TemplateView.as_view(template_name="oj/account/login.html"), name="user_login_page"),
|
||||
url(r'^api/login/$', UserLoginAPIView.as_view(), name="login_api"),
|
||||
url(r'^api/login/$', UserLoginAPIView.as_view(), name="user_login_api"),
|
||||
url(r'^api/username_check/$', UsernameCheckAPIView.as_view(), name="username_check_api"),
|
||||
url(r'^problem/(?P<problem_id>\d+)/$', "problem.views.problem_page", name="problem_page"),
|
||||
|
||||
url(r'^admin/contest/$', TemplateView.as_view(template_name="admin/contest/add_contest.html"), name="add_contest_page"),
|
||||
|
Loading…
Reference in New Issue
Block a user