Accept Merge Request #8 : (virusdefender-dev -> dev)

Merge Request: 增加 login_required decorator 和对应的测试
Created By: @virusdefender
Accepted By: @virusdefender
URL: https://coding.net/u/virusdefender/p/qduoj/git/merge/8
This commit is contained in:
virusdefender 2015-08-04 13:23:46 +08:00
commit 93e4599789
3 changed files with 114 additions and 1 deletions

24
account/decorators.py Normal file
View File

@ -0,0 +1,24 @@
# coding=utf-8
from django.http import HttpResponse
from django.shortcuts import render
from utils.shortcuts import error_response
from .models import User
def login_required(func):
def check(*args, **kwargs):
# 在class based views 里面args 有两个元素一个是self, 第二个才是request
# 在function based views 里面args 只有request 一个参数
request = args[-1]
if request.user.is_authenticated():
return func(*args, **kwargs)
if request.is_ajax():
return error_response(u"请先登录")
else:
return render(request, "utils/error.html", {"error": u"请先登录"})
return check
def admin_required():
pass

12
account/test_urls.py Normal file
View File

@ -0,0 +1,12 @@
# coding=utf-8
from django.conf.urls import include, url
from .tests import LoginRequiredCBVTestWithArgs, LoginRequiredCBVTestWithoutArgs
urlpatterns = [
url(r'^test/fbv/1/$', "account.tests.login_required_FBV_test_without_args"),
url(r'^test/fbv/(?P<problem_id>\d+)/$', "account.tests.login_required_FBC_test_with_args"),
url(r'^test/cbv/1/$', LoginRequiredCBVTestWithoutArgs.as_view()),
url(r'^test/cbv/(?P<problem_id>\d+)/$', LoginRequiredCBVTestWithArgs.as_view()),
]

View File

@ -1,10 +1,16 @@
# coding=utf-8
import json
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from django.http import HttpResponse
from rest_framework.test import APITestCase, APIClient
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import User
from .decorators import login_required
class UserLoginTest(TestCase):
@ -98,4 +104,75 @@ class UserChangePasswordAPITest(APITestCase):
def test_username_does_not_exist(self):
data = {"username": "test1", "old_password": "aaabbb", "new_password": "aaaddd"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
self.assertEqual(response.data["code"], 1)
@login_required
def login_required_FBV_test_without_args(request):
return HttpResponse("function based view test1")
@login_required
def login_required_FBC_test_with_args(request, problem_id):
return HttpResponse(problem_id)
class LoginRequiredCBVTestWithoutArgs(APIView):
@login_required
def get(self, request):
return HttpResponse("class based view login required test1")
class LoginRequiredCBVTestWithArgs(APIView):
@login_required
def get(self, request, problem_id):
return HttpResponse(problem_id)
class LoginRequiredDecoratorTest(TestCase):
urls = 'account.test_urls'
def setUp(self):
self.client = Client()
user = User.objects.create(username="test")
user.set_password("test")
user.save()
def test_fbv_without_args(self):
# 没登陆
response = self.client.get("/test/fbv/1/")
self.assertTemplateUsed(response, "utils/error.html")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/test/fbv/1/")
self.assertEqual(response.content, "function based view test1")
def test_fbv_with_args(self):
# 没登陆
response = self.client.get("/test/fbv/1024/")
self.assertTemplateUsed(response, "utils/error.html")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/test/fbv/1024/")
self.assertEqual(response.content, "1024")
def test_cbv_without_args(self):
# 没登陆
response = self.client.get("/test/cbv/1/")
self.assertTemplateUsed(response, "utils/error.html")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/test/cbv/1/")
self.assertEqual(response.content, "class based view login required test1")
def test_cbv_with_args(self):
# 没登陆
response = self.client.get("/test/cbv/1024/", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(json.loads(response.content), {"code": 1, "data": u"请先登录"})
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/test/cbv/1024/")
self.assertEqual(response.content, "1024")