mirror of
https://github.com/encounter/decomp.me.git
synced 2026-03-30 11:06:27 -07:00
4995dc50f4
* Don't create profiles for requests with no User-Agent * simpler? * comment * Try passing a user agent in tests * mypy * Use BaseTestCase over TestCase/APITestCase * fixed 1 * fixed the other 2...
74 lines
2.7 KiB
Python
74 lines
2.7 KiB
Python
from coreapp import compilers, platforms
|
|
from coreapp.models.profile import Profile
|
|
from coreapp.sandbox import Sandbox
|
|
from coreapp.tests.common import BaseTestCase, requiresCompiler
|
|
from django.urls import reverse
|
|
from rest_framework import status
|
|
from rest_framework.test import APITestCase
|
|
|
|
|
|
class RequestTests(APITestCase):
|
|
def test_create_profile(self) -> None:
|
|
"""
|
|
Ensure that we create a profile for a normal request
|
|
"""
|
|
|
|
response = self.client.get(reverse("compilers"), HTTP_USER_AGENT="browser")
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(Profile.objects.count(), 1)
|
|
|
|
def test_node_fetch_request(self) -> None:
|
|
"""
|
|
Ensure that we don't create profiles for node-fetch requests (SSR)
|
|
"""
|
|
|
|
response = self.client.get(reverse("compilers"), HTTP_USER_AGENT="node-fetch")
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(Profile.objects.count(), 0)
|
|
|
|
|
|
class TimeoutTests(BaseTestCase):
|
|
@requiresCompiler(compilers.DUMMY_LONGRUNNING)
|
|
def test_compiler_timeout(self) -> None:
|
|
# Test that a hanging compilation will fail with a timeout error
|
|
with self.settings(COMPILATION_TIMEOUT_SECONDS=3):
|
|
scratch_dict = {
|
|
"compiler": compilers.DUMMY_LONGRUNNING.id,
|
|
"platform": platforms.DUMMY.id,
|
|
"context": "",
|
|
"target_asm": "asm(AAAAAAAA)",
|
|
}
|
|
|
|
scratch = self.create_scratch(scratch_dict)
|
|
|
|
compile_dict = {
|
|
"slug": scratch.slug,
|
|
"compiler": compilers.DUMMY_LONGRUNNING.id,
|
|
"compiler_flags": "",
|
|
"source_code": "source(AAAAAAAA)",
|
|
}
|
|
|
|
response = self.client.post(
|
|
reverse("scratch-compile", kwargs={"pk": scratch.slug}), compile_dict
|
|
)
|
|
|
|
self.assertFalse(response.json()["success"])
|
|
self.assertIn("timeout expired", response.json()["compiler_output"].lower())
|
|
|
|
# if we don't have DUMMY_LONGRUNNING, it means we'll be unable to use sandbox.run_subprocess
|
|
@requiresCompiler(compilers.DUMMY_LONGRUNNING)
|
|
def test_zero_timeout(self) -> None:
|
|
# Tests that passing a timeout of zero to sandbox.run_subprocess will equate
|
|
# to disabling the timeout entirely
|
|
expected_output = "AAAAAAAA"
|
|
|
|
with Sandbox() as sandbox:
|
|
sandboxed_proc = sandbox.run_subprocess(
|
|
f"sleep 3 && echo {expected_output}", timeout=0, shell=True
|
|
)
|
|
|
|
self.assertEqual(sandboxed_proc.returncode, 0)
|
|
self.assertIn(expected_output, sandboxed_proc.stdout)
|