def test_creation_of_report_layout_full(): report = Report(*_transform('grid_checks/fullgrid.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(table.columns) == 12 assert len(table.rows) == 12 # Check the specific merged cells vtable = [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] assert get_vtable_merged(table) == vtable # Check the page breaks assert len(d.element.xpath('//w:pgSz[@w:orient="landscape"]')) == 0 # Check page size page_sz = d.element.xpath('//w:pgSz[@w:w]') assert len(page_sz) == 1 page_sz = page_sz[0] width, height = int(page_sz.w.mm), int(page_sz.h.mm) expected_width, expected_height = A4_MM_WIDTH.mm, A4_MM_HEIGHT.mm # Conversions aren't exact, so we are looking for a close enough difference. assert abs(expected_width - width) < 2 assert abs(expected_height - height) < 2
def test_items_section_in_report(): report = Report(*_transform('elements/items_section.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) # Check there are enough itemsSections assert len( d.element.xpath('//w:tbl//w:tbl[not(*/w:tblStyle) and .//w:sz[' '@w:val="18"]]')) == 5 # Check values assert len(d.element.xpath('//w:tbl//w:t[contains(text(), "Bot")]')) == 6 assert len(d.element.xpath('//w:tbl//w:t[contains(text(), "2019")]')) == 7 assert len( d.element.xpath('//w:tbl//w:t[contains(text(), "' + str(date.today().year) + '")]')) == 1 assert len(d.element.xpath('//w:tbl//w:t[contains(text(), "week")]')) == 1 assert len( d.element.xpath('//w:tbl//w:t[contains(text(), "Timeline ' 'Information")]')) == 1 assert len( d.element.xpath('//w:tbl//w:t[contains(text(),' ' "columnheader2")]')) == 1
def test_text(): """ To check the xpath: rename the .elements to .zip and open word/document.xml """ report = Report(*_transform('elements/text.json')) report.populate_report() d = report.document # Find 1 fonts, we also have default one which is different assert len(d.element.xpath('//w:rFonts')) == 2 # Check with Arial font too assert len(d.element.xpath('//w:rFonts[@w:ascii="Verdana"]')) == 1 # Find one H1 assert len(d.element.xpath('//w:sz[@w:val="48"]')) == 1 # Find two H2 assert len(d.element.xpath('//w:sz[@w:val="32"]')) == 1 # Find styles assert len(d.element.xpath('//w:i')) == 1 assert len(d.element.xpath('//w:strike')) == 1 assert len(d.element.xpath('//w:u')) == 1 assert len(d.element.xpath('//w:b')) == 1
def test_markdown(): """ To check the xpath: rename the .elements to .zip and open word/document.xml """ report = Report(*_transform('elements/markdown.json')) report.populate_report() d = report.document # Find 6 headings assert len(d.element.xpath("//w:t[contains(text(), 'Heading')]")) == 6 # Find 3 Hrs assert len(d.element.xpath('//w:jc[@w:val="center"]')) == 3 # Find Text stylings # Two bold assert len(d.element.xpath( "//w:r//w:t[contains(text(), 'bold')]/preceding-sibling" + "::w:rPr/w:b")) == 2 # Two italics assert len(d.element.xpath( "//w:r//w:t[contains(text(), 'italic')]/preceding-sibling" + "::w:rPr/w:i")) == 2 # One strikethrough assert len(d.element.xpath( "//w:r//w:t[contains(text(), 'Strike')]/preceding-sibling" + "::w:rPr/w:strike")) == 1 # Find one quote assert len(d.element.xpath( '//w:tbl//w:shd[@w:fill="#fff8dc"]/following::w:t[position() <2]')) == 1 # Check the quote has a bold element inside assert len(d.element.xpath( '//w:tbl//w:shd[@w:fill="#fff8dc"]/following::w:b')) == 1 # Find one code assert len(d.element.xpath( '//w:tbl//w:shd[@w:fill="#f5f5f5"]/following::w:t[position() <2]')) == 1 # Check the quote has no bold element inside assert len(d.element.xpath( '//w:tbl//w:shd[@w:fill="#f5f5f5"]/following::w:b')) == 0 # Find ULs assert len( d.element.xpath('//w:p//w:pStyle[contains(@w:val,"ListBullet")]')) == 4 # Find OLs assert len( d.element.xpath('//w:p//w:pStyle[contains(@w:val,"ListNumber")]')) == 6 # Find one link assert len(d.element.xpath("//w:hyperlink//w:t[text()='link text']")) == 1 # Find one image assert len( d.element.xpath("//w:drawing//pic:cNvPr[@name='image.png']")) == 1 assert len(d.element.xpath('//w:br')) == 0
def test_number_and_trend_in_report(): report = Report(*_transform('elements/number_and_trend.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) if SHOULD_HAVE_12_GRID: assert len(table.columns) == 12 assert len(table.rows) == 4 else: assert len(table.columns) == 9 assert len(table.rows) == 4 # Check that there is indeed a table within a table assert len(d.element.xpath('//w:tbl//w:tbl')) == 3 # Check that it has the right amount of rows assert len(d.element.xpath('//w:tbl//w:tbl//w:t')) == 8 # Check that there is an extra sign assert len( d.element.xpath("//w:tbl//w:tbl//w:t[contains(text(),'%')]")) == 3 # Check that precentage is correct assert len( d.element.xpath( "//w:tbl//w:tbl//w:t[contains(text(),'100.25%')]")) == 1
def test_table_new_json(): report = Report(*_transform('elements/table_new_json.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert int(d.element.xpath('count(//w:tbl)')) == 7 # grid + 5 tables
def test_table_63_cols(): report = Report(*_transform('elements/table_63_cols.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(d.element.xpath('//w:tbl//w:tbl')) == 1 assert int(d.element.xpath('count(//w:t)')) == 64 # 63 + title
def test_markdown_md_button(): report = Report(*_transform('elements/markdown_md_button.json')) report.populate_report() d = report.document # Don't find the %%% sings, but do find the message contents assert len(d.element.xpath("//w:t[contains(text(), '%')]")) == 0 assert len(d.element.xpath("//w:t[contains(text(), 'hi 1')]")) == 1
def test_creation_of_report_with_exception(): # Mock an error generating the json with patch('sane_doc_reports.elements.table.invoke', side_effect=KeyError('mocked error')): report = Report(*_transform('elements/table.json')) report.populate_report() assert len( report.document.element.xpath( '//w:t[contains(text(), "mocked error")]')) == 1
def test_table_empty_in_report(): report = Report(*_transform('elements/table_empty.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(d.element.xpath('//w:tbl//w:tbl')) == 1 # Check that it has the right amount of rows assert len(d.element.xpath('//w:tbl//w:tbl//w:tr')) == 2
def test_markdown_paged_not_breaking(): report = Report(*_transform('elements/markdown_paged_not_working.json')) report.populate_report() d = report.document # Find 1 headings assert len(d.element.xpath("//w:t[contains(text(), 'Heading')]")) == 1 # Page break (none because it is the first and only element) assert len(d.element.xpath('//w:br')) == 0
def test_logo_works_in_regular_report_svg(): report = Report(*_transform('grid_checks/fullgrid.json'), options={ 'customerLogo': XSOAR_LOGO_BASE64, 'demistoLogo': "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iNDY2IiBoZWlnaHQ9IjQ2NiIgdmlld0JveD0iLTQwIC00MCA4MCA4MCI+Cgk8Y2lyY2xlIHI9IjM5Ii8+Cgk8cGF0aCBkPSJNMCwzOGEzOCwzOCAwIDAgMSAwLC03NmExOSwxOSAwIDAgMSAwLDM4YTE5LDE5IDAgMCAwIDAsMzgiIGZpbGw9IiNmZmYiLz4KCTxjaXJjbGUgY3k9IjE5IiByPSI1IiBmaWxsPSIjZmZmIi8+Cgk8Y2lyY2xlIGN5PSItMTkiIHI9IjUiLz4KPC9zdmc+" }) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) # Check headers for 2 images (customer logo) assert len(d.sections[0].header._element.xpath('.//w:drawing')) == 2
def test_logo_works_in_regular_report(): report = Report(*_transform('grid_checks/fullgrid.json'), options={ 'customerLogo': XSOAR_LOGO_BASE64, 'demistoLogo': XSOAR_LOGO_BASE64 }) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) # Check headers for 2 images (customer logo) assert len(d.sections[0].header._element.xpath('.//w:drawing')) == 2
def test_table_in_report_widget(): report = Report(*_transform('elements/table_widget.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(table.columns) == 12 assert len(table.rows) == 3 assert len(d.element.xpath('//w:tbl//w:tbl')) == 1 # Check that it has the right amount of rows assert len(d.element.xpath('//w:t[contains(text(), "Eve listens")]')) == 1
def test_creation_of_report_layout_basic(): report = Report(*_transform('basic.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(table.columns) == 12 assert len(table.rows) == 2 # Check the specific merged cells vtable = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] assert get_vtable_merged(table) == vtable
def test_list_in_report(): report = Report(*_transform('elements/list.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(d.element.xpath('//w:tbl//w:tbl')) == 4 assert len(d.element.xpath('//w:tbl//w:tbl//w:t')) == 16 # Check that we have a string conversion assert len(d.element.xpath('//w:t[contains(text(),\'[]\')]')) == 1 assert len(d.element.xpath('//w:t[contains(text(),\'0\')]')) == 1
def test_list_in_report(): report = Report(*_transform('elements/list.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) # assert len(table.columns) == 9 assert len(table.rows) == 2 # Check that there is indeed an image assert len(d.element.xpath('//w:tbl//w:tbl')) == 1 # Check that it has the right amount of rows assert len(d.element.xpath('//w:tbl//w:tbl//w:t')) == 7
def test_unimplemented_element_generated(): report = Report(*_transform('elements/unimplemented.json')) report.populate_report() d = report.document assert len(d.element.xpath('//w:p')) == 3 # Styles or error assert len(d.element.xpath('//w:i[@w:val="0"]')) == 1 assert len(d.element.xpath('//w:strike[@w:val="0"]')) == 1 assert len(d.element.xpath('//w:color[@w:val="FF0013"]')) == 1 assert len(d.element.xpath('//w:sz[@w:val="20"]')) == 1 assert len(d.element.xpath('//w:u[@w:val="none"]')) == 1
def test_date(): """ To check the xpath: rename the .elements to .zip and open word/document.xml """ report = Report(*_transform('elements/date.json')) report.populate_report() d = report.document # Find 2 dates assert len(d.element.xpath('//w:t')) == 2 # Find the 2 dates assert len(d.element.xpath('//w:t[contains(text(), "18 Dec 2012")]')) == 1
def test_markdown_placeholder_styled(): report = Report(*_transform('elements/markdown_placeholder.json')) report.populate_report() d = report.document base_textval = "//w:t[contains(text(), '1 Incident Summary')]" assert len(d.element.xpath(base_textval)) == 1 style_color = 'w:color[@w:val="FFC421"]' assert len( d.element.xpath(f"{base_textval}/preceding::{style_color}")) == 1 style_fontsize = 'w:sz[@w:val="50"]' assert len( d.element.xpath(f"{base_textval}/preceding::{style_fontsize}")) == 1 style_font = 'w:rFonts[@w:ascii="Arial"]' assert len(d.element.xpath(f"{base_textval}/preceding::{style_font}")) == 1
def test_markdown_paged(): report = Report(*_transform('elements/markdown_paged2.json')) report.populate_report() d = report.document # Find 6 headings assert len(d.element.xpath("//w:t[contains(text(), 'Heading')]")) == 2 # Page break assert len(d.element.xpath('//w:br')) == 1 # Structure sanity check (heading -> break -> heading) assert len(d.element.xpath("//w:t[contains(text(),'page 1')]/following::w:br")) == 1 assert len(d.element.xpath("//w:t[contains(text(),'page 2')]/preceding::w:br")) == 1
def test_hr(): """ To check the xpath: rename the .elements to .zip and open word/document.xml """ report = Report(*_transform('elements/hr.json')) report.populate_report() d = report.document # Find 3 paragraphs assert len(d.element.xpath('//w:p')) == 6 # Find hr assert len(d.element.xpath('//w:jc[@w:val="center"]')) == 3
def test_items_section_in_report(): report = Report(*_transform('elements/items_section.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) # Check there are enough itemsSections assert len( d.element.xpath( '//w:tbl//w:tbl[not(*/w:tblStyle) and .//w:sz[@w:val="18"]]')) == 4 # Check values assert len(d.element.xpath('//w:tbl//w:t[contains(text(), "Bot")]')) == 6 assert len(d.element.xpath('//w:tbl//w:t[contains(text(), "2019")]')) == 8 assert len(d.element.xpath('//w:tbl//w:t[contains(text(), "week")]')) == 1
def test_pie_chart_in_report(): report = Report(*_transform('elements/pie_chart.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) if SHOULD_HAVE_12_GRID: assert len(table.columns) == 12 assert len(table.rows) == 1 else: assert len(table.columns) == 12 assert len(table.rows) == 5 # Check that there is indeed an image assert len(d.element.xpath('//pic:pic')) == 3
def test_number_and_trend_in_report(): report = Report(*_transform('elements/number_and_trend.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) if SHOULD_HAVE_12_GRID: assert len(table.columns) == 12 assert len(table.rows) == 4 else: assert len(table.columns) == 9 assert len(table.rows) == 4 # Check that there is indeed an image assert len(d.element.xpath('//w:tbl//w:tbl')) == 2 # Check that it has the right amount of rows assert len(d.element.xpath('//w:tbl//w:tbl//w:t')) == 5
def test_old_json(): ''' To check the xpath: rename the .elements to .zip and open word/document.xml ''' report = Report(*_transform('old_json.json')) report.populate_report() d = report.document # Find 1 fonts, we also have default one which is different assert len(d.element.xpath('//w:t')) == 107 # One red text assert len( d.element.xpath( "//w:color[@w:val='FF1744']/following::w:t[position() < 2]")) == 1 # Find HRs assert len(d.element.xpath("//w:jc[@w:val='center']")) == 2 # Find tables assert len(d.element.xpath('//w:tbl')) == 5
def test_duration(): report = Report(*_transform('elements/duration.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) if SHOULD_HAVE_12_GRID: assert len(table.columns) == 12 assert len(table.rows) == 1 else: assert len(table.columns) == 12 assert len(table.rows) == 1 # Check that there is indeed a duration table assert len(d.element.xpath('//w:tbl//w:tbl')) == 1 # Check that it has the right amount of rows assert len(d.element.xpath('//w:tbl//w:tbl//w:t')) == 9 # Right title assert len( d.element.xpath( "//w:t[contains(text(), 'Mean Time to Resolution (Occurred)')]") ) == 1 # Check duration value assert len(d.element.xpath("//w:t[contains(text(), '38')]")) == 1 # Check that it has the right values of labels days = DURATION_DAYS_LABEL.strip() hours = DURATION_HOURS_LABEL.strip() mins = DURATION_MINUTES_LABEL.strip() print(days, hours, mins) assert len(d.element.xpath(f"//w:t[contains(text(), '{days}')]")) == 1 assert len(d.element.xpath(f"//w:t[contains(text(), '{hours}')]")) == 1 assert len(d.element.xpath(f"//w:t[contains(text(), '{mins}')]")) == 1
def test_creation_of_report_layout_full_paged(): report = Report(*_transform('grid_checks/fullgridpaged.json')) report.populate_report() d = report.document table = next(utils.iter_block_items(d)) assert isinstance(table, Table) assert len(table.columns) == 12 assert len(table.rows) == 11 # Check the specific merged cells vtable = [ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], ] assert get_vtable_merged(table) == vtable # Check the page breaks assert len(d.element.xpath('//w:br')) == 1
def test_markdown_no_werid_html(): report = Report(*_transform('elements/markdown_bad_html.json')) report.populate_report() d = report.document assert len(d.element.xpath("//w:t[contains(text(), 'asd')]")) == 0
def test_table_in_report_with_missing_header(): report = Report(*_transform('elements/table_missing_header.json')) report.populate_report() d = report.document assert len(d.element.xpath('//w:t')) == 31