Skip to content

Commit

Permalink
Merge branch 'fix-test-overload' into 'main'
Browse files Browse the repository at this point in the history
Fix test overload

See merge request reportcreator/reportcreator!754
  • Loading branch information
MWedl committed Nov 6, 2024
2 parents d20759d + 372775f commit 9bd76df
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions api/src/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[pytest]
addopts = --dist=loadgroup
asyncio_mode = strict
asyncio_default_fixture_loop_scope = function
DJANGO_SETTINGS_MODULE = reportcreator_api.conf.settings_test
Expand Down
15 changes: 12 additions & 3 deletions api/src/reportcreator_api/tasks/rendering/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@
UserNotebookPage,
)
from reportcreator_api.tasks.rendering import tasks
from reportcreator_api.tasks.rendering.error_messages import MessageLocationInfo, MessageLocationType
from reportcreator_api.tasks.rendering.error_messages import (
ErrorMessage,
MessageLevel,
MessageLocationInfo,
MessageLocationType,
)
from reportcreator_api.tasks.rendering.render_utils import RenderStageResult
from reportcreator_api.users.models import PentestUser
from reportcreator_api.utils.logging import log_timing
Expand Down Expand Up @@ -284,7 +289,7 @@ async def render_project_markdown_fields_to_html(project: PentestProject, reques
output='html',
)
if not res.pdf:
return res
return res.to_dict()

def format_output():
from reportcreator_api.pentests.serializers.project import PentestProjectDetailSerializer
Expand Down Expand Up @@ -317,7 +322,11 @@ def format_output():
return await sync_to_async(format_output)()
except Exception:
log.exception('Error while formatting output')
return res
res.messages.append(ErrorMessage(
level=MessageLevel.ERROR,
message='Error while formatting output',
))
return res.to_dict()


@elasticapm.async_capture_span()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/*
This file is used as a common base style for markdown text rendering.
It is included in the global base styles in PDF rendering and in the markdown preview in the frontend.
This file is not actually a scss file, but a regular css file.
The reason why the file extension is .scss is that the SCSS-preprocessor inlines it in the markdown preview component styles.
*/


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import url("/assets/global/base-text.scss");
@import url("/assets/global/base-text.css");
@import url("/assets/global/base-ref.css");


Expand Down
17 changes: 11 additions & 6 deletions api/src/reportcreator_api/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta
from functools import cached_property
from typing import Optional
from unittest import mock
from uuid import uuid4

import pytest
Expand Down Expand Up @@ -29,6 +30,7 @@
UploadedUserNotebookImage,
)
from reportcreator_api.pentests.models.project import CommentStatus
from reportcreator_api.tasks.rendering.render_utils import RenderStageResult
from reportcreator_api.tests.mock import (
create_archived_project,
create_png_file,
Expand Down Expand Up @@ -61,7 +63,7 @@ def viewset_urls(basename, get_kwargs, create_data=None, list=False, retrieve=Fa

out = []
if list:
out.append((basename + ' list', lambda s, c: c.get(reverse(list_urlname, kwargs=get_kwargs(s, False))), lambda s: get_kwargs(s, True)))
out.append((basename + ' list', lambda s, c: c.get(reverse(list_urlname, kwargs=get_kwargs(s, False))), {'initialize_dependencies': lambda s: get_kwargs(s, True)}))
if retrieve:
out.append((basename + ' retrieve', lambda s, c: c.get(reverse(detail_urlname, kwargs=get_kwargs(s, True)))))
if create:
Expand Down Expand Up @@ -241,7 +243,7 @@ def guest_urls():
*viewset_urls('findingtemplate', get_kwargs=lambda s, detail: {'pk': s.template.pk} if detail else {}, list=True, retrieve=True),
*viewset_urls('findingtemplatetranslation', get_kwargs=lambda s, detail: {'template_pk': s.template.pk} | ({'pk': s.template.main_translation.pk} if detail else {}), list=True, retrieve=True, history_timeline=True),
*file_viewset_urls('uploadedtemplateimage', get_obj=lambda s: s.template.images.first(), get_base_kwargs=lambda s: {'template_pk': s.template.pk}, read=True),
('findingtemplate fielddefinition', lambda s, c: c.get(reverse('findingtemplate-fielddefinition')), lambda s: [s.template, s.project_type]),
('findingtemplate fielddefinition', lambda s, c: c.get(reverse('findingtemplate-fielddefinition')), {'initialize_dependencies': lambda s: [s.template, s.project_type]}),
('findingtemplatehistory template', lambda s, c: c.get(reverse('findingtemplatehistory-detail', kwargs={'template_pk': s.template.pk, 'history_date': s.history_date}))),
('findingtemplatehistory image-by-name', lambda s, c: c.get(reverse('findingtemplatehistory-image-by-name', kwargs={'template_pk': s.template.pk, 'filename': s.template.images.first().name, 'history_date': s.history_date}))),

Expand Down Expand Up @@ -487,8 +489,11 @@ def history_date(self):


@pytest.mark.django_db()
@pytest.mark.parametrize(('username', 'name', 'perform_request', 'initialize_dependencies', 'expected'), sorted(build_test_parameters(), key=lambda t: (t[0], t[1], t[4])))
def test_api_requests(username, name, perform_request, initialize_dependencies, expected):
@pytest.mark.parametrize(('username', 'name', 'perform_request', 'options', 'expected'), sorted(build_test_parameters(), key=lambda t: (t[0], t[1], t[4])))
def test_api_requests(username, name, perform_request, options, expected):
async def mock_render_pdf(*args, output=None, **kwargs):
return RenderStageResult(pdf=b'<html><head></head><body><div></div></body></html>' if output == 'html' else b'%PDF-1.3')

with override_settings(
GUEST_USERS_CAN_IMPORT_PROJECTS=False,
GUEST_USERS_CAN_CREATE_PROJECTS=False,
Expand All @@ -503,7 +508,7 @@ def test_api_requests(username, name, perform_request, initialize_dependencies,
'label': 'Dummy',
},
},
):
), mock.patch('reportcreator_api.tasks.rendering.render.render_pdf_impl', mock_render_pdf):
user_map = {
'public': lambda: None,
'guest': lambda: ApiRequestsAndPermissionsTestData.create_user(is_guest=True),
Expand All @@ -528,7 +533,7 @@ def test_api_requests(username, name, perform_request, initialize_dependencies,
}
session.save()

if initialize_dependencies:
if initialize_dependencies := (options or {}).get('initialize_dependencies'):
initialize_dependencies(data)
res = perform_request(data, client)
info = res.data if not isinstance(res, (FileResponse, StreamingHttpResponse)) else res
Expand Down
1 change: 1 addition & 0 deletions api/src/reportcreator_api/tests/test_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def html_load_script(src):
return f"""<component :is="{{ template: '<div />', mounted() {{ const script = document.createElement('script'); script.type = 'text/javascript'; script.src='{src}'; document.head.appendChild(script); }} }}" />"""


@pytest.mark.xdist_group('rendering')
@pytest.mark.django_db()
class TestHtmlRendering:
@pytest.fixture(autouse=True)
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/assets/rendering/base-text.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Placeholder file that will be replaced in docker builds.
See api/src/reportcreator_api/tasks/rendering/global_assets/base-text.css
*/
2 changes: 0 additions & 2 deletions frontend/src/assets/rendering/base-text.scss

This file was deleted.

4 changes: 3 additions & 1 deletion frontend/src/components/Markdown/DiffFieldContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const { markdownPreviewAttrsHistoric, markdownPreviewAttrsCurrent, markdownToolb
</script>

<style lang="scss" scoped>
@use "sass:meta";
$mde-min-height: 15em;
.mde-mergeview {
Expand All @@ -65,7 +67,7 @@ $mde-min-height: 15em;
}
:deep(.mde-mergeview) {
@import "@/assets/mde-highlight.scss";
@include meta.load-css("@/assets/mde-highlight.scss");
}
.mde-preview {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/Markdown/DiffPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const { markdownPreviewAttrsHistoric, markdownPreviewAttrsCurrent, markdownToolb
</script>

<style lang="scss" scoped>
@use "sass:meta";
:deep(.mde-mergeview) {
.cm-wrap { border: 1px solid silver; }
Expand All @@ -54,7 +56,7 @@ const { markdownPreviewAttrsHistoric, markdownPreviewAttrsCurrent, markdownToolb
}
:deep(.mde-mergeview) {
@import "@/assets/mde-highlight.scss";
@include meta.load-css("@/assets/mde-highlight.scss");
.cm-content {
font-family: monospace;
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/Markdown/FieldContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ defineExpose({
</script>

<style lang="scss" scoped>
@use "sass:meta";
$mde-min-height: 15em;
.mde-editor {
Expand All @@ -74,7 +76,7 @@ $mde-min-height: 15em;
}
:deep(.mde-editor) {
@import "@/assets/mde-highlight.scss";
@include meta.load-css("@/assets/mde-highlight.scss");
}
.mde-preview {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/Markdown/Page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ defineExpose({
</script>

<style lang="scss" scoped>
@use "sass:meta";
.mde-editor-side.cm-editor {
width: 50%;
}
Expand All @@ -79,7 +81,7 @@ defineExpose({
}
:deep(.mde-editor) {
@import "@/assets/mde-highlight.scss";
@include meta.load-css("@/assets/mde-highlight.scss");
.cm-content {
font-family: monospace;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Markdown/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ onBeforeUnmount(() => {
</script>

<style lang="scss" scoped>
@use "sass:meta";
@use "@base/assets/vuetify.scss" as vuetify;
.preview {
Expand All @@ -130,7 +131,7 @@ onBeforeUnmount(() => {
}
.preview :deep() {
@import "@/assets/rendering/base-text.scss";
@include meta.load-css("@/assets/rendering/base-text.css");
.footnotes {
border-top: 1px solid currentColor;
Expand Down

0 comments on commit 9bd76df

Please sign in to comment.