完善contest和announcement单元测试

This commit is contained in:
zema1 2017-11-28 16:20:29 +08:00
parent 0c52704a72
commit 5cac51007c
6 changed files with 63 additions and 13 deletions

View File

@ -98,7 +98,7 @@ def check_contest_permission(check_type="details"):
if self.contest.status == ContestStatus.CONTEST_NOT_START and check_type != "details":
return self.error("Contest has not started yet.")
# check does user have permission to get ranks, submissions OI Contest
# check does user have permission to get ranks, submissions in OI Contest
if self.contest.status == ContestStatus.CONTEST_UNDERWAY and self.contest.rule_type == ContestRuleType.OI:
if not self.contest.real_time_rank and (check_type == "ranks" or check_type == "submissions"):
return self.error(f"No permission to get {check_type}")

View File

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-11-25 15:14
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('account', '0008_auto_20171011_1214'),
]
operations = [
migrations.AlterField(
model_name='userprofile',
name='avatar',
field=models.CharField(default='/public/avatar/default.png', max_length=256),
),
]

View File

@ -22,7 +22,7 @@ class UserAdminAPI(APIView):
@super_admin_required
def post(self, request):
"""
Generate user
Import User
"""
data = request.data["users"]
@ -166,6 +166,9 @@ class GenerateUserAPI(APIView):
@validate_serializer(GenerateUserSerializer)
@super_admin_required
def post(self, request):
"""
Generate User
"""
data = request.data
number_max_length = max(len(str(data["number_from"])), len(str(data["number_to"])))
if number_max_length + len(data["prefix"]) + len(data["suffix"]) > 32:

View File

@ -35,3 +35,14 @@ class AnnouncementAdminTest(APITestCase):
resp = self.client.delete(self.url + "?id=" + str(id))
self.assertSuccess(resp)
self.assertFalse(Announcement.objects.filter(id=id).exists())
class AnnouncementAPITest(APITestCase):
def setUp(self):
self.user = self.create_super_admin()
Announcement.objects.create(title="title", content="content", visible=True, created_by=self.user)
self.url = self.reverse("announcement_api")
def test_get_announcement_list(self):
resp = self.client.get(self.url)
self.assertSuccess(resp)

View File

@ -3,5 +3,5 @@ from django.conf.urls import url
from ..views.oj import AnnouncementAPI
urlpatterns = [
url(r"^announcement/?$", AnnouncementAPI.as_view(), name="announcement_admin_api"),
url(r"^announcement/?$", AnnouncementAPI.as_view(), name="announcement_api"),
]

View File

@ -6,7 +6,7 @@ from django.utils import timezone
from utils.api._serializers import DateTimeTZField
from utils.api.tests import APITestCase
from .models import ContestAnnouncement, ContestRuleType
from .models import ContestAnnouncement, ContestRuleType, Contest
DEFAULT_CONTEST_DATA = {"title": "test title", "description": "test description",
"start_time": timezone.localtime(timezone.now()),
@ -21,13 +21,18 @@ class ContestAdminAPITest(APITestCase):
def setUp(self):
self.create_super_admin()
self.url = self.reverse("contest_admin_api")
self.data = DEFAULT_CONTEST_DATA
self.data = copy.deepcopy(DEFAULT_CONTEST_DATA)
def test_create_contest(self):
response = self.client.post(self.url, data=self.data)
self.assertSuccess(response)
return response
def test_create_contest_with_invalid_cidr(self):
self.data["allowed_ip_ranges"] = ["127.0.0"]
resp = self.client.post(self.url, data=self.data)
self.assertTrue(resp.data["data"].endswith("is not a valid cidr network"))
def test_update_contest(self):
id = self.test_create_contest().data["data"]["id"]
update_data = {"id": id, "title": "update title",
@ -58,10 +63,9 @@ class ContestAdminAPITest(APITestCase):
class ContestAPITest(APITestCase):
def setUp(self):
self.create_admin()
url = self.reverse("contest_admin_api")
self.contest = self.client.post(url, data=DEFAULT_CONTEST_DATA).data["data"]
self.url = self.reverse("contest_api") + "?id=" + str(self.contest["id"])
user = self.create_admin()
self.contest = Contest.objects.create(created_by=user, **DEFAULT_CONTEST_DATA)
self.url = self.reverse("contest_api") + "?id=" + str(self.contest.id)
def test_get_contest_list(self):
url = self.reverse("contest_list_api")
@ -76,21 +80,21 @@ class ContestAPITest(APITestCase):
def test_regular_user_validate_contest_password(self):
self.create_user("test", "test123")
url = self.reverse("contest_password_api")
resp = self.client.post(url, {"contest_id": self.contest["id"], "password": "error_password"})
resp = self.client.post(url, {"contest_id": self.contest.id, "password": "error_password"})
self.assertDictEqual(resp.data, {"error": "error", "data": "Wrong password"})
resp = self.client.post(url, {"contest_id": self.contest["id"], "password": DEFAULT_CONTEST_DATA["password"]})
resp = self.client.post(url, {"contest_id": self.contest.id, "password": DEFAULT_CONTEST_DATA["password"]})
self.assertSuccess(resp)
def test_regular_user_access_contest(self):
self.create_user("test", "test123")
url = self.reverse("contest_access_api")
resp = self.client.get(url + "?contest_id=" + str(self.contest["id"]))
resp = self.client.get(url + "?contest_id=" + str(self.contest.id))
self.assertFalse(resp.data["data"]["access"])
password_url = self.reverse("contest_password_api")
resp = self.client.post(password_url,
{"contest_id": self.contest["id"], "password": DEFAULT_CONTEST_DATA["password"]})
{"contest_id": self.contest.id, "password": DEFAULT_CONTEST_DATA["password"]})
self.assertSuccess(resp)
resp = self.client.get(self.url)
self.assertSuccess(resp)
@ -146,3 +150,15 @@ class ContestAnnouncementListAPITest(APITestCase):
contest_id = self.create_contest_announcements()
response = self.client.get(self.url, data={"contest_id": contest_id})
self.assertSuccess(response)
class ContestRankAPITest(APITestCase):
def setUp(self):
user = self.create_admin()
self.acm_contest = Contest.objects.create(created_by=user, **DEFAULT_CONTEST_DATA)
self.create_user("test", "test123")
self.url = self.reverse("contest_rank_api")
def get_contest_rank(self):
resp = self.client.get(self.url + "?contest_id=" + self.acm_contest.id)
self.assertSuccess(resp)