mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2024-12-29 16:41:56 +00:00
增加用户密码修改成功的API测试,以及增加用户邮箱字段并做了相应的API测试,目前的account/view.py测试覆盖率为100%
This commit is contained in:
parent
17b44800ca
commit
117a6d3525
19
account/migrations/0003_user_email.py
Normal file
19
account/migrations/0003_user_email.py
Normal file
@ -0,0 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0002_auto_20150731_2310'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='email',
|
||||
field=models.EmailField(max_length=254, null=True, blank=True),
|
||||
),
|
||||
]
|
@ -19,6 +19,8 @@ class User(AbstractBaseUser):
|
||||
username = models.CharField(max_length=30, unique=True)
|
||||
# 真实姓名
|
||||
real_name = models.CharField(max_length=30, blank=True, null=True)
|
||||
# 用户邮箱
|
||||
email = models.EmailField(max_length=254, blank=True, null=True)
|
||||
admin_group = models.ForeignKey(AdminGroup, null=True, on_delete=models.SET_NULL)
|
||||
|
||||
USERNAME_FIELD = 'username'
|
||||
|
@ -11,10 +11,15 @@ class UsernameCheckSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=30)
|
||||
|
||||
|
||||
class EmailCheckSerializer(serializers.Serializer):
|
||||
email = serializers.EmailField(max_length=254)
|
||||
|
||||
|
||||
class UserRegisterSerializer(serializers.Serializer):
|
||||
username = serializers.CharField(max_length=30)
|
||||
real_name = serializers.CharField(max_length=30)
|
||||
password = serializers.CharField(max_length=30, min_length=6)
|
||||
email = serializers.EmailField(max_length=254)
|
||||
|
||||
|
||||
class UserChangePasswordSerializer(serializers.Serializer):
|
||||
|
@ -1,6 +1,8 @@
|
||||
# coding=utf-8
|
||||
import json
|
||||
|
||||
from django.contrib import auth
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import TestCase, Client
|
||||
from django.http import HttpResponse
|
||||
@ -63,6 +65,26 @@ class UsernameCheckTest(APITestCase):
|
||||
self.assertEqual(response.data, {"code": 0, "data": False})
|
||||
|
||||
|
||||
class EmailCheckTest(APITestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse("email_check_api")
|
||||
User.objects.create(email="11@qq.com")
|
||||
|
||||
def test_invalid_data(self):
|
||||
response = self.client.post(self.url, data={"email000": "11@qq.com"})
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_email_exists(self):
|
||||
response = self.client.post(self.url, data={"email": "11@qq.com"})
|
||||
self.assertEqual(response.data, {"code": 0, "data": True})
|
||||
|
||||
def test_email_does_not_exist(self):
|
||||
response = self.client.post(self.url, data={"email": "33@qq.com"})
|
||||
self.assertEqual(response.data, {"code": 0, "data": False})
|
||||
|
||||
|
||||
class UserRegisterAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
@ -74,22 +96,35 @@ class UserRegisterAPITest(APITestCase):
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_short_password(self):
|
||||
data = {"username": "test", "real_name": "TT", "password": "qq"}
|
||||
data = {"username": "test", "real_name": "TT", "password": "qq", "email": "6060@qq.com"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_same_username(self):
|
||||
User.objects.create(username="aa", real_name="ww")
|
||||
data = {"username": "aa", "real_name": "ww", "password": "zzzzzzz"}
|
||||
User.objects.create(username="aa")
|
||||
data = {"username": "aa", "real_name": "ww", "password": "zzzzzzz", "email": "6060@qq.com"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 1, "data": u"用户名已存在"})
|
||||
|
||||
def test_same_email(self):
|
||||
User.objects.create(username="bb", email="8080@qq.com")
|
||||
data = {"username": "aa", "real_name": "ww", "password": "zzzzzzz", "email": "8080@qq.com"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 1, "data": u"该邮箱已被注册,请换其他邮箱进行注册"})
|
||||
|
||||
def test_success_email(self):
|
||||
data = {"username": "cc", "real_name": "dd", "password": "xxxxxx", "email": "9090@qq.com"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 0, "data": u"注册成功!"})
|
||||
|
||||
|
||||
class UserChangePasswordAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
self.url = reverse("user_change_password_api")
|
||||
User.objects.create(username="test", password="aaabbb")
|
||||
user = User.objects.create(username="test")
|
||||
user.set_password("aaabbb")
|
||||
user.save()
|
||||
|
||||
def test_error_old_password(self):
|
||||
data = {"username": "test", "old_password": "aaaccc", "new_password": "aaaddd"}
|
||||
@ -106,6 +141,11 @@ class UserChangePasswordAPITest(APITestCase):
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data["code"], 1)
|
||||
|
||||
def test_success_change_password(self):
|
||||
data = {"username": "test", "old_password": "aaabbb", "new_password": "aaaccc"}
|
||||
response = self.client.post(self.url, data=data)
|
||||
self.assertEqual(response.data, {"code": 0, "data": u"用户密码修改成功!"})
|
||||
|
||||
|
||||
@login_required
|
||||
def login_required_FBV_test_without_args(request):
|
||||
|
@ -7,7 +7,7 @@ from utils.shortcuts import serializer_invalid_response, error_response, success
|
||||
|
||||
from .models import User
|
||||
from .serializers import UserLoginSerializer, UsernameCheckSerializer, UserRegisterSerializer, \
|
||||
UserChangePasswordSerializer
|
||||
UserChangePasswordSerializer, EmailCheckSerializer
|
||||
|
||||
|
||||
class UserLoginAPIView(APIView):
|
||||
@ -45,7 +45,13 @@ class UserRegisterAPIView(APIView):
|
||||
User.objects.get(username=data["username"])
|
||||
return error_response(u"用户名已存在")
|
||||
except User.DoesNotExist:
|
||||
user = User.objects.create(username=data["username"], real_name=data["real_name"])
|
||||
pass
|
||||
try:
|
||||
User.objects.get(email=data["email"])
|
||||
return error_response(u"该邮箱已被注册,请换其他邮箱进行注册")
|
||||
except User.DoesNotExist:
|
||||
user = User.objects.create(username=data["username"], real_name=data["real_name"],
|
||||
email=data["email"])
|
||||
user.set_password(data["password"])
|
||||
user.save()
|
||||
return success_response(u"注册成功!")
|
||||
@ -89,4 +95,21 @@ class UsernameCheckAPIView(APIView):
|
||||
except User.DoesNotExist:
|
||||
return success_response(False)
|
||||
else:
|
||||
return serializer_invalid_response(serializer)
|
||||
return serializer_invalid_response(serializer)
|
||||
|
||||
class EmailCheckAPIView(APIView):
|
||||
def post(self, request):
|
||||
"""
|
||||
检测邮箱是否存在,存在返回True,不存在返回False
|
||||
---
|
||||
request_serializer: EmailCheckSerializer
|
||||
"""
|
||||
serializer = EmailCheckSerializer(data=request.DATA)
|
||||
if serializer.is_valid():
|
||||
try:
|
||||
User.objects.get(email=serializer.data["email"])
|
||||
return success_response(True)
|
||||
except User.DoesNotExist:
|
||||
return success_response(False)
|
||||
else:
|
||||
return serializer_invalid_response(serializer)
|
||||
|
@ -3,7 +3,8 @@ from django.conf.urls import include, url
|
||||
from django.contrib import admin
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from account.views import UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, UserChangePasswordAPIView
|
||||
from account.views import UserLoginAPIView, UsernameCheckAPIView, UserRegisterAPIView, UserChangePasswordAPIView, \
|
||||
EmailCheckAPIView
|
||||
from announcement.views import AnnouncementAPIView
|
||||
|
||||
urlpatterns = [
|
||||
@ -17,6 +18,7 @@ urlpatterns = [
|
||||
url(r'^api/register/$', UserRegisterAPIView.as_view(), name="user_register_api"),
|
||||
url(r'^api/change_password/$', UserChangePasswordAPIView.as_view(), name="user_change_password_api"),
|
||||
url(r'^api/username_check/$', UsernameCheckAPIView.as_view(), name="username_check_api"),
|
||||
url(r'^api/email_check/$', EmailCheckAPIView.as_view(), name="email_check_api"),
|
||||
url(r'^api/admin/announcement/$', AnnouncementAPIView.as_view(), name="announcement_api"),
|
||||
url(r'^problem/(?P<problem_id>\d+)/$', "problem.views.problem_page", name="problem_page"),
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user