Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MWedl committed Nov 4, 2024
1 parent 07b1ecd commit 9fae5b3
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions api/src/reportcreator_api/conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,9 @@
# Execute tasks locally, if no broker is configured
CELERY_TASK_ALWAYS_EAGER = not CELERY_BROKER_URL

# Time limits are only enforced if a broker is configured and an external worker is used (but not in eager mode).
# Self-hosted SysReptor instances use the eager mode by default, resulting in no PDF rendering time limits being applied.
PDF_RENDERING_TIME_LIMIT = config('PDF_RENDERING_TIME_LIMIT', cast=int, default=60)
# Maximum time a PDF rendering task is allowed to run. If a task takes longer, it gets cancelled.
# Set to 0 to disable the time limit
PDF_RENDERING_TIME_LIMIT = config('PDF_RENDERING_TIME_LIMIT', cast=int, default=5 * 60)


# History
Expand Down
15 changes: 8 additions & 7 deletions api/src/reportcreator_api/tasks/rendering/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ async def get_celery_result_async(task, timeout=None):


async def _render_pdf_task_async(timeout=None, **kwargs):
timeout = timeout or timedelta(seconds=settings.PDF_RENDERING_TIME_LIMIT + 5)
if not timeout and settings.PDF_RENDERING_TIME_LIMIT:
timeout = timedelta(seconds=settings.PDF_RENDERING_TIME_LIMIT + 5)

try:
if settings.CELERY_TASK_ALWAYS_EAGER:
Expand Down Expand Up @@ -224,7 +225,7 @@ def format_resources():
resources |= {'/images/name/' + i.name: b64encode(i.file.read()).decode() for i in project.images.all() if project.is_file_referenced(i, sections=True, findings=True, notes=False)}
return resources

with res.add_timing('collect data'):
with res.add_timing('collect_data'):
resources = await format_resources()

timing_total_before = sum(res.timings.values())
Expand Down Expand Up @@ -325,7 +326,7 @@ async def render_note_to_pdf(note: Union[ProjectNotebookPage, UserNotebookPage],
parent_obj = note.project if is_project_note else note.user

res = RenderStageResult()
with res.add_timing('collect data'):
with res.add_timing('collect_data'):
# Prevent sending unreferenced images to rendering task to reduce memory consumption
resources = {}
async for i in parent_obj.images.all():
Expand All @@ -343,7 +344,7 @@ async def render_note_to_pdf(note: Union[ProjectNotebookPage, UserNotebookPage],
absolute_file_url = request.build_absolute_uri(reverse('uploadedusernotebookfile-retrieve-by-name', kwargs={'pentestuser_pk': note.user.id, 'filename': f.name}))
note_text = note_text.replace(f'/files/name/{f.name}', absolute_file_url)

return await _render_pdf_task_async(
res |= await _render_pdf_task_async(
template="""<h1>{{ data.note.title }}</h1><markdown :text="data.note.text" />""",
styles="""@import "/assets/global/base.css";""",
data={
Expand All @@ -355,8 +356,8 @@ async def render_note_to_pdf(note: Union[ProjectNotebookPage, UserNotebookPage],
},
language=note.project.language if is_project_note else Language.ENGLISH_US,
resources=resources,
timings=res.timings,
)
return res


async def render_pdf(
Expand All @@ -373,7 +374,7 @@ async def render_pdf(


res = RenderStageResult()
with res.add_timing('collect data'):
with res.add_timing('collect_data'):
data = await format_project_template_data(project=project, project_type=project_type)
return await render_pdf_task(
project=project,
Expand All @@ -389,7 +390,7 @@ async def render_pdf(

async def render_pdf_preview(project_type: ProjectType, report_template: str, report_styles: str, report_preview_data: dict) -> RenderStageResult:
res = RenderStageResult()
with res.add_timing('collect data'):
with res.add_timing('collect_data'):
preview_data = report_preview_data.copy()
data = await sync_to_async(format_template_data)(data=preview_data, project_type=project_type)

Expand Down
2 changes: 1 addition & 1 deletion api/src/reportcreator_api/tasks/rendering/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def encrypt_pdf(pdf_data: bytes, password: Optional[str]) -> RenderStageResult:
if not password:
return out

with out.add_timing('compress_pdf'), \
with out.add_timing('encrypt_pdf'), \
Pdf.open(BytesIO(pdf_data)) as pdf:
out_data = BytesIO()
# Encrypt PDF with AES-256
Expand Down
4 changes: 2 additions & 2 deletions api/src/reportcreator_api/tasks/rendering/render_chromium.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ async def chromium_request_handler(route):
await route.abort()

try:
chromium_startup_timer = out.add_timing('chromium startup')
chromium_startup_timer = out.add_timing('chromium_startup')
chromium_startup_timer.__enter__()
async with get_page() as page:
chromium_startup_timer.__exit__(None, None, None)

with out.add_timing('chromium render'):
with out.add_timing('chromium_render'):
console_output = []
page.on('console', lambda l: console_output.append(l))
page.on('pageerror', lambda exc: out.messages.append(ErrorMessage(
Expand Down
6 changes: 3 additions & 3 deletions api/src/reportcreator_api/tasks/rendering/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

@shared_task(
name='reportcreator.render_pdf',
soft_time_limit=settings.PDF_RENDERING_TIME_LIMIT,
time_limit=settings.PDF_RENDERING_TIME_LIMIT + 5,
expires=settings.PDF_RENDERING_TIME_LIMIT + 5,
soft_time_limit=settings.PDF_RENDERING_TIME_LIMIT or None,
time_limit=settings.PDF_RENDERING_TIME_LIMIT + 5 if settings.PDF_RENDERING_TIME_LIMIT else None,
expires=settings.PDF_RENDERING_TIME_LIMIT + 5 if settings.PDF_RENDERING_TIME_LIMIT else None,
)
def render_pdf_task_celery(*args, **kwargs) -> dict:
return asyncio.run(render_pdf_task_async(*args, **kwargs))
Expand Down
7 changes: 3 additions & 4 deletions api/src/reportcreator_api/tests/test_rendering.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import io
import re
from base64 import b64decode
from unittest import mock

import pikepdf
Expand Down Expand Up @@ -121,8 +120,8 @@ def test_variables_rendering(self, template, html):
def test_error_messages(self, template, expected):
self.project_type.report_template = template
res = async_to_sync(render_pdf)(project=self.project)
assert len(res['messages']) >= 1
assert expected in [copy_keys(m, expected.keys()) for m in res['messages']]
assert len(res.messages) >= 1
assert expected in [copy_keys(m.to_dict(), expected.keys()) for m in res.messages]

def test_markdown_rendering(self):
assertHTMLEqual(
Expand Down Expand Up @@ -362,7 +361,7 @@ def test_mermaid_rendering(self):
('', False),
])
def test_pdf_encryption(self, password, encrypted):
pdf_data = b64decode(async_to_sync(render_pdf)(project=self.project, password=password)['pdf'])
pdf_data = async_to_sync(render_pdf)(project=self.project, password=password).pdf
with pikepdf.Pdf.open(io.BytesIO(pdf_data), password=password) as pdf:
assert pdf.is_encrypted == encrypted

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/PdfPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<v-menu activator="parent" location="top right" :close-on-content-click="false">
<v-table class="pa-2">
<tr v-for="[key, timing] in Object.entries(timings).filter(([k]) => k !== 'total')" :key="key">
<td class="timing-table-key">{{ key }}</td>
<td class="timing-table-key">{{ key.replaceAll('_', ' ') }}</td>
<td class="timing-value">{{ formatTiming(timing) }}</td>
</tr>
<tr>
Expand Down

0 comments on commit 9fae5b3

Please sign in to comment.