OnlineJudge/conf/tests.py

185 lines
7.0 KiB
Python
Raw Normal View History

2016-11-20 11:18:25 +00:00
import hashlib
2017-12-23 06:16:39 +00:00
from unittest import mock
2016-09-25 06:07:45 +00:00
2017-01-24 08:48:39 +00:00
from django.utils import timezone
2017-01-25 15:01:33 +00:00
2017-10-01 19:54:34 +00:00
from options.options import SysOptions
2016-11-20 11:18:25 +00:00
from utils.api.tests import APITestCase
2017-10-01 19:54:34 +00:00
from .models import JudgeServer
class SMTPConfigTest(APITestCase):
def setUp(self):
self.user = self.create_super_admin()
self.url = self.reverse("smtp_admin_api")
self.password = "testtest"
def test_create_smtp_config(self):
data = {"server": "smtp.test.com", "email": "test@test.com", "port": 465,
"tls": True, "password": self.password}
resp = self.client.post(self.url, data=data)
self.assertSuccess(resp)
self.assertTrue("password" not in resp.data)
return resp
def test_edit_without_password(self):
self.test_create_smtp_config()
data = {"server": "smtp1.test.com", "email": "test2@test.com", "port": 465,
"tls": True}
resp = self.client.put(self.url, data=data)
self.assertSuccess(resp)
def test_edit_without_password1(self):
self.test_create_smtp_config()
data = {"server": "smtp.test.com", "email": "test@test.com", "port": 465,
"tls": True, "password": ""}
resp = self.client.put(self.url, data=data)
self.assertSuccess(resp)
def test_edit_with_password(self):
self.test_create_smtp_config()
data = {"server": "smtp1.test.com", "email": "test2@test.com", "port": 465,
"tls": True, "password": "newpassword"}
resp = self.client.put(self.url, data=data)
self.assertSuccess(resp)
2017-12-24 03:34:40 +00:00
@mock.patch("conf.views.send_email")
def test_test_smtp(self, mocked_send_email):
url = self.reverse("smtp_test_api")
self.test_create_smtp_config()
resp = self.client.post(url, data={"email": "test@test.com"})
self.assertSuccess(resp)
mocked_send_email.assert_called_once()
class WebsiteConfigAPITest(APITestCase):
def test_create_website_config(self):
2017-01-23 08:25:14 +00:00
self.create_super_admin()
url = self.reverse("website_config_api")
2017-10-01 19:54:34 +00:00
data = {"website_base_url": "http://test.com", "website_name": "test name",
"website_name_shortcut": "test oj", "website_footer": "<a>test</a>",
"allow_register": True, "submission_list_show_all": False}
resp = self.client.post(url, data=data)
self.assertSuccess(resp)
def test_edit_website_config(self):
2017-01-23 08:25:14 +00:00
self.create_super_admin()
url = self.reverse("website_config_api")
2017-10-01 19:54:34 +00:00
data = {"website_base_url": "http://test.com", "website_name": "test name",
2017-12-24 07:34:22 +00:00
"website_name_shortcut": "test oj", "website_footer": "<img onerror=alert(1) src=#>",
"allow_register": True, "submission_list_show_all": False}
resp = self.client.post(url, data=data)
self.assertSuccess(resp)
2017-12-24 07:34:22 +00:00
self.assertEqual(SysOptions.website_footer, "<img src=\"#\" />")
def test_get_website_config(self):
# do not need to login
url = self.reverse("website_info_api")
resp = self.client.get(url)
self.assertSuccess(resp)
2017-09-29 13:58:20 +00:00
2016-11-20 11:18:25 +00:00
class JudgeServerHeartbeatTest(APITestCase):
2016-11-20 11:18:25 +00:00
def setUp(self):
self.url = self.reverse("judge_server_heartbeat_api")
self.data = {"hostname": "testhostname", "judger_version": "1.0.4", "cpu_core": 4,
2017-12-24 04:08:56 +00:00
"cpu": 90.5, "memory": 80.3, "action": "heartbeat", "service_url": "http://127.0.0.1"}
2016-11-20 11:18:25 +00:00
self.token = "test"
self.hashed_token = hashlib.sha256(self.token.encode("utf-8")).hexdigest()
2017-10-01 19:54:34 +00:00
SysOptions.judge_server_token = self.token
2017-12-09 14:42:54 +00:00
self.headers = {"HTTP_X_JUDGE_SERVER_TOKEN": self.hashed_token, "HTTP_X_REAL_IP": "1.2.3.4"}
2016-11-20 11:18:25 +00:00
def test_new_heartbeat(self):
2017-12-09 14:42:54 +00:00
resp = self.client.post(self.url, data=self.data, **self.headers)
2016-11-20 11:18:25 +00:00
self.assertSuccess(resp)
server = JudgeServer.objects.first()
self.assertEqual(server.ip, "127.0.0.1")
def test_update_heartbeat(self):
self.test_new_heartbeat()
data = self.data
data["judger_version"] = "2.0.0"
2017-12-09 14:42:54 +00:00
resp = self.client.post(self.url, data=data, **self.headers)
2016-11-20 11:18:25 +00:00
self.assertSuccess(resp)
self.assertEqual(JudgeServer.objects.get(hostname=self.data["hostname"]).judger_version, data["judger_version"])
2017-01-24 08:48:39 +00:00
class JudgeServerAPITest(APITestCase):
def setUp(self):
2017-12-24 04:10:02 +00:00
self.server = JudgeServer.objects.create(**{"hostname": "testhostname", "judger_version": "1.0.4",
"cpu_core": 4, "cpu_usage": 90.5, "memory_usage": 80.3,
"last_heartbeat": timezone.now()})
2017-01-24 08:48:39 +00:00
self.url = self.reverse("judge_server_api")
self.create_super_admin()
def test_get_judge_server(self):
resp = self.client.get(self.url)
self.assertSuccess(resp)
self.assertEqual(len(resp.data["data"]["servers"]), 1)
def test_delete_judge_server(self):
resp = self.client.delete(self.url + "?hostname=testhostname")
self.assertSuccess(resp)
self.assertFalse(JudgeServer.objects.filter(hostname="testhostname").exists())
2017-12-24 04:10:02 +00:00
def test_disabled_judge_server(self):
resp = self.client.put(self.url, data={"is_disabled": True, "id": self.server.id})
self.assertSuccess(resp)
self.assertTrue(JudgeServer.objects.get(id=self.server.id).is_disabled)
2017-01-24 08:48:39 +00:00
class LanguageListAPITest(APITestCase):
def test_get_languages(self):
resp = self.client.get(self.reverse("language_list_api"))
self.assertSuccess(resp)
2017-12-23 06:16:39 +00:00
class TestCasePruneAPITest(APITestCase):
def setUp(self):
self.url = self.reverse("prune_test_case_api")
self.create_super_admin()
def test_get_isolated_test_case(self):
resp = self.client.get(self.url)
self.assertSuccess(resp)
@mock.patch("conf.views.TestCasePruneAPI.delete_one")
@mock.patch("conf.views.os.listdir")
@mock.patch("conf.views.Problem")
def test_delete_test_case(self, mocked_problem, mocked_listdir, mocked_delete_one):
valid_id = "1172980672983b2b49820be3a741b109"
mocked_problem.return_value = [valid_id, ]
mocked_listdir.return_value = [valid_id, ".test", "aaa"]
resp = self.client.delete(self.url)
self.assertSuccess(resp)
mocked_delete_one.assert_called_once_with(valid_id)
2018-01-02 12:05:33 +00:00
2018-01-04 03:42:20 +00:00
class ReleaseNoteAPITest(APITestCase):
2018-01-02 12:05:33 +00:00
def setUp(self):
2018-01-04 03:42:20 +00:00
self.url = self.reverse("get_release_notes_api")
2018-01-02 12:05:33 +00:00
self.create_super_admin()
self.latest_data = {"update": [
{
"version": "2099-12-25",
"level": 1,
"title": "Update at 2099-12-25",
"details": ["test get", ]
}
]}
2018-01-04 03:42:20 +00:00
def test_get_versions(self):
2018-01-02 12:05:33 +00:00
resp = self.client.get(self.url)
self.assertSuccess(resp)
2018-01-04 03:42:20 +00:00
class DashboardInfoAPITest(APITestCase):
def setUp(self):
self.url = self.reverse("dashboard_info_api")
self.create_admin()
def test_get_info(self):
resp = self.client.get(self.url)
self.assertSuccess(resp)
self.assertEqual(resp.data["data"]["user_count"], 1)