OnlineJudge/contest/tests.py

163 lines
6.6 KiB
Python
Raw Normal View History

2017-04-18 17:37:10 +00:00
import copy
2017-02-27 04:18:22 +00:00
from datetime import datetime, timedelta
from django.utils import timezone
from utils.api.tests import APITestCase
2017-04-18 17:37:10 +00:00
from .models import ContestAnnouncement, ContestRuleType, Contest
2017-02-27 04:18:22 +00:00
DEFAULT_CONTEST_DATA = {"title": "test title", "description": "test description",
"start_time": timezone.localtime(timezone.now()),
"end_time": timezone.localtime(timezone.now()) + timedelta(days=1),
"rule_type": ContestRuleType.ACM,
"password": "123",
"allowed_ip_ranges": [],
2017-02-27 04:18:22 +00:00
"visible": True, "real_time_rank": True}
2017-06-22 06:10:32 +00:00
class ContestAdminAPITest(APITestCase):
2017-02-27 04:18:22 +00:00
def setUp(self):
self.create_super_admin()
2017-06-22 06:10:32 +00:00
self.url = self.reverse("contest_admin_api")
self.data = copy.deepcopy(DEFAULT_CONTEST_DATA)
2017-02-27 04:18:22 +00:00
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"))
2017-02-27 04:18:22 +00:00
def test_update_contest(self):
id = self.test_create_contest().data["data"]["id"]
update_data = {"id": id, "title": "update title",
"description": "update description",
"password": "12345",
"visible": False, "real_time_rank": False}
data = copy.deepcopy(self.data)
data.update(update_data)
response = self.client.put(self.url, data=data)
self.assertSuccess(response)
response_data = response.data["data"]
for k in data.keys():
if isinstance(data[k], datetime):
2017-12-01 09:19:31 +00:00
continue
2017-02-27 04:18:22 +00:00
self.assertEqual(response_data[k], data[k])
def test_get_contests(self):
self.test_create_contest()
response = self.client.get(self.url)
self.assertSuccess(response)
def test_get_one_contest(self):
id = self.test_create_contest().data["data"]["id"]
response = self.client.get("{}?id={}".format(self.url, id))
self.assertSuccess(response)
2017-06-22 06:10:32 +00:00
class ContestAPITest(APITestCase):
def setUp(self):
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)
2017-06-22 06:10:32 +00:00
def test_get_contest_list(self):
2017-10-27 10:36:29 +00:00
url = self.reverse("contest_list_api")
response = self.client.get(url + "?limit=10")
2017-06-22 06:10:32 +00:00
self.assertSuccess(response)
2017-10-27 10:36:29 +00:00
self.assertEqual(len(response.data["data"]["results"]), 1)
2017-06-22 06:10:32 +00:00
def test_get_one_contest(self):
2017-10-27 10:36:29 +00:00
resp = self.client.get(self.url)
self.assertSuccess(resp)
2017-06-22 06:10:32 +00:00
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"})
2017-10-21 02:51:35 +00:00
self.assertDictEqual(resp.data, {"error": "error", "data": "Wrong 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):
2017-07-20 07:52:11 +00:00
self.create_user("test", "test123")
url = self.reverse("contest_access_api")
resp = self.client.get(url + "?contest_id=" + str(self.contest.id))
2017-10-21 02:51:35 +00:00
self.assertFalse(resp.data["data"]["access"])
2017-07-20 07:52:11 +00:00
password_url = self.reverse("contest_password_api")
2017-10-27 10:36:29 +00:00
resp = self.client.post(password_url,
{"contest_id": self.contest.id, "password": DEFAULT_CONTEST_DATA["password"]})
2017-07-20 07:52:11 +00:00
self.assertSuccess(resp)
2017-10-27 10:36:29 +00:00
resp = self.client.get(self.url)
2017-07-20 07:52:11 +00:00
self.assertSuccess(resp)
2017-06-22 06:10:32 +00:00
2017-10-31 08:33:25 +00:00
class ContestAnnouncementAdminAPITest(APITestCase):
2017-02-27 04:18:22 +00:00
def setUp(self):
self.create_super_admin()
self.url = self.reverse("contest_announcement_admin_api")
contest_id = self.create_contest().data["data"]["id"]
self.data = {"title": "test title", "content": "test content", "contest_id": contest_id, "visible": True}
2017-02-27 04:18:22 +00:00
def create_contest(self):
2017-06-22 06:10:32 +00:00
url = self.reverse("contest_admin_api")
2017-02-27 04:18:22 +00:00
data = DEFAULT_CONTEST_DATA
return self.client.post(url, data=data)
def test_create_contest_announcement(self):
response = self.client.post(self.url, data=self.data)
self.assertSuccess(response)
return response
def test_delete_contest_announcement(self):
id = self.test_create_contest_announcement().data["data"]["id"]
response = self.client.delete("{}?id={}".format(self.url, id))
self.assertSuccess(response)
self.assertFalse(ContestAnnouncement.objects.filter(id=id).exists())
def test_get_contest_announcements(self):
self.test_create_contest_announcement()
2017-10-31 08:33:25 +00:00
response = self.client.get(self.url + "?contest_id=" + str(self.data["contest_id"]))
2017-02-27 04:18:22 +00:00
self.assertSuccess(response)
def test_get_one_contest_announcement(self):
id = self.test_create_contest_announcement().data["data"]["id"]
response = self.client.get("{}?id={}".format(self.url, id))
self.assertSuccess(response)
class ContestAnnouncementListAPITest(APITestCase):
def setUp(self):
self.create_super_admin()
2017-06-22 06:10:32 +00:00
self.url = self.reverse("contest_announcement_api")
2017-02-27 04:18:22 +00:00
def create_contest_announcements(self):
2017-06-22 06:10:32 +00:00
contest_id = self.client.post(self.reverse("contest_admin_api"), data=DEFAULT_CONTEST_DATA).data["data"]["id"]
2017-02-27 04:18:22 +00:00
url = self.reverse("contest_announcement_admin_api")
self.client.post(url, data={"title": "test title1", "content": "test content1", "contest_id": contest_id})
self.client.post(url, data={"title": "test title2", "content": "test content2", "contest_id": contest_id})
return contest_id
def test_get_contest_announcement_list(self):
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)