From 5cac51007c33876aa19d134577cad1afb13d73a1 Mon Sep 17 00:00:00 2001 From: zema1 Date: Tue, 28 Nov 2017 16:20:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84contest=E5=92=8Cannouncement?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/decorators.py | 2 +- account/migrations/0009_auto_20171125_1514.py | 20 +++++++++++ account/views/admin.py | 5 ++- announcement/tests.py | 11 ++++++ announcement/urls/oj.py | 2 +- contest/tests.py | 36 +++++++++++++------ 6 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 account/migrations/0009_auto_20171125_1514.py diff --git a/account/decorators.py b/account/decorators.py index 93710592..4cbb15a1 100644 --- a/account/decorators.py +++ b/account/decorators.py @@ -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}") diff --git a/account/migrations/0009_auto_20171125_1514.py b/account/migrations/0009_auto_20171125_1514.py new file mode 100644 index 00000000..b476b789 --- /dev/null +++ b/account/migrations/0009_auto_20171125_1514.py @@ -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), + ), + ] diff --git a/account/views/admin.py b/account/views/admin.py index b1ef6889..171fa37d 100644 --- a/account/views/admin.py +++ b/account/views/admin.py @@ -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: diff --git a/announcement/tests.py b/announcement/tests.py index dd702bad..98caa1c8 100644 --- a/announcement/tests.py +++ b/announcement/tests.py @@ -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) diff --git a/announcement/urls/oj.py b/announcement/urls/oj.py index 71bcf3ae..67178b01 100644 --- a/announcement/urls/oj.py +++ b/announcement/urls/oj.py @@ -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"), ] diff --git a/contest/tests.py b/contest/tests.py index 3b7b1e20..965ee8d2 100644 --- a/contest/tests.py +++ b/contest/tests.py @@ -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)