修改account和announcement中的测试用例,改为当前为管理员用户的登录状态

This commit is contained in:
hohoTT 2015-08-09 19:19:59 +08:00
parent 77a83f7085
commit b089455bba
2 changed files with 16 additions and 10 deletions

View File

@ -10,7 +10,7 @@ from rest_framework.test import APITestCase, APIClient
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import User
from .models import User, SUPER_ADMIN
from .decorators import login_required, admin_required
@ -178,29 +178,32 @@ class UserAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_admin_api")
user = User.objects.create(username="test")
user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
user.set_password("testaa")
user.save()
def test_put_invalid_data(self):
self.client.login(username="test", password="test")
self.client.login(username="test", password="testaa")
data = {"username": "test", "password": "testaa", "email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_user_does_not_exist(self):
self.client.login(username="test", password="testaa")
data = {"id": 2, "username": "test0", "real_name": "test00",
"password": "testaa","email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该用户不存在!"})
def test_success_user_edit_not_password(self):
self.client.login(username="test", password="testaa")
data = {"id": 1, "username": "test0", "real_name": "test00",
"email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_success_user_edit_change_password(self):
self.client.login(username="test", password="testaa")
data = {"id": 1, "username": "test0", "real_name": "test00", "password": "111111",
"email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)

View File

@ -5,43 +5,45 @@ from rest_framework.test import APITestCase, APIClient
from account.models import User
from announcement.models import Announcement
from account.models import REGULAR_USER, ADMIN, SUPER_ADMIN
class AnnouncementAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("announcement_admin_api")
user = User.objects.create(username="test")
user.set_password("test")
user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
user.set_password("testaa")
user.save()
# 以下是发布公告的测试
def test_invalid_format(self):
self.client.login(username="test", password="test")
self.client.login(username="test", password="testaa")
data = {"title": "test1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_success_announcement(self):
self.client.login(username="test", password="test")
self.client.login(username="test", password="testaa")
data = {"title": "title0", "content": "content0"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"公告发布成功!"})
def test_post_invalid_data(self):
self.client.login(username="test", password="test")
self.client.login(username="test", password="testaa")
data = {"title": "test"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
# 以下是编辑公告的测试
def test_put_invalid_data(self):
self.client.login(username="test", password="test")
self.client.login(username="test", password="testaa")
data = {"title": "test0", "content": "test0", "visible": "True"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_announcement_does_not_exist(self):
self.client.login(username="test", password="testaa")
announcement = Announcement.objects.create(title="aa",
content="AA",
created_by=User.objects.get(username="test"))
@ -50,6 +52,7 @@ class AnnouncementAdminAPITest(APITestCase):
self.assertEqual(response.data, {"code": 1, "data": u"该公告不存在!"})
def test_success_edit_announcement(self):
self.client.login(username="test", password="testaa")
announcement = Announcement.objects.create(title="bb",
content="BB",
created_by=User.objects.get(username="test"))
@ -63,7 +66,7 @@ class AnnouncementAPITest(APITestCase):
self.client = APIClient()
self.url = reverse("announcement_list_api")
user = User.objects.create(username="test")
user.set_password("test")
user.set_password("testaa")
user.save()
def test_success_get_data(self):