add delete judge server api

This commit is contained in:
virusdefender 2017-01-24 16:48:39 +08:00
parent e98eb5c3c1
commit b9fe8d4669
4 changed files with 28 additions and 16 deletions

View File

@ -1,5 +1,6 @@
import hashlib
from django.utils import timezone
from utils.api.tests import APITestCase
from .models import JudgeServer, JudgeServerToken, SMTPConfig
@ -77,19 +78,6 @@ class WebsiteConfigAPITest(APITestCase):
self.assertEqual(resp.data["data"]["name_shortcut"], "oj")
class JudgeServerStatusAPITest(APITestCase):
def setUp(self):
self.url = self.reverse("judge_server_api")
self.user = self.create_super_admin()
def test_get_judge_server_status(self):
self.assertFalse(JudgeServerToken.objects.exists())
resp = self.client.get(self.url)
self.assertSuccess(resp)
self.assertListEqual(resp.data["data"]["servers"], [])
self.assertEqual(JudgeServerToken.objects.first().token, resp.data["data"]["token"])
class JudgeServerHeartbeatTest(APITestCase):
def setUp(self):
self.url = self.reverse("judge_server_heartbeat_api")
@ -113,7 +101,6 @@ class JudgeServerHeartbeatTest(APITestCase):
resp = self.client.post(self.url, data=self.data, **{"HTTP_X_JUDGE_SERVER_TOKEN": self.hashed_token})
self.assertSuccess(resp)
server = JudgeServer.objects.first()
self.assertEqual(server.ip, None)
self.assertEqual(server.service_url, service_url)
def test_update_heartbeat(self):
@ -125,6 +112,27 @@ class JudgeServerHeartbeatTest(APITestCase):
self.assertEqual(JudgeServer.objects.get(hostname=self.data["hostname"]).judger_version, data["judger_version"])
class JudgeServerAPITest(APITestCase):
def setUp(self):
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()})
self.url = self.reverse("judge_server_api")
self.create_super_admin()
def test_get_judge_server(self):
self.assertFalse(JudgeServerToken.objects.exists())
resp = self.client.get(self.url)
self.assertSuccess(resp)
self.assertEqual(len(resp.data["data"]["servers"]), 1)
self.assertEqual(JudgeServerToken.objects.first().token, resp.data["data"]["token"])
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())
class LanguageListAPITest(APITestCase):
def test_get_languages(self):
resp = self.client.get(self.reverse("language_list_api"))

View File

@ -85,6 +85,9 @@ class JudgeServerAPI(APIView):
@super_admin_required
def delete(self, request):
hostname = request.GET.get("hostname")
if hostname:
JudgeServer.objects.filter(hostname=hostname).delete()
return self.success()

View File

@ -56,7 +56,7 @@ class APIView(View):
response_class = JSONResponse
def _get_request_data(self, request):
if request.method != "GET":
if request.method not in ["GET", "DELETE"]:
body = request.body
content_type = request.META.get("CONTENT_TYPE")
if not content_type:

View File

@ -27,7 +27,8 @@ class APITestCase(TestCase):
return reverse(url_name)
def assertSuccess(self, response):
self.assertTrue(response.data["error"] is None)
if not response.data["error"] is None:
raise AssertionError("response with errors, response: " + str(response.data))
def assertFailed(self, response):
self.assertTrue(response.data["error"] is not None)