增加 login_required decorator 和对应的测试
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user