|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from pathlib import Path |
| 4 | +from utils.jira_confluence_util import JiraConfluenceUtil |
| 5 | + |
| 6 | + |
| 7 | +pytestmark = [pytest.mark.utils] |
| 8 | + |
| 9 | +TEST_RESULTS_DIR = ( |
| 10 | + Path(__file__).parent.joinpath("resources").joinpath("jira-util-test-dir") |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(autouse=True) |
| 15 | +def set_env_vars(monkeypatch) -> None: |
| 16 | + monkeypatch.setattr("dotenv.load_dotenv", lambda *a, **kw: None) |
| 17 | + applicable_vars = [ |
| 18 | + "JIRA_URL", |
| 19 | + "JIRA_PROJECT_KEY", |
| 20 | + "JIRA_API_KEY", |
| 21 | + "CONFLUENCE_URL", |
| 22 | + "CONFLUENCE_API_KEY", |
| 23 | + ] |
| 24 | + for var in applicable_vars: |
| 25 | + os.environ[var] = "test" |
| 26 | + |
| 27 | + |
| 28 | +def test_check_init() -> None: |
| 29 | + """Check initialization works as intended""" |
| 30 | + test_init = JiraConfluenceUtil(TEST_RESULTS_DIR) |
| 31 | + assert isinstance(test_init, JiraConfluenceUtil) |
| 32 | + |
| 33 | + with pytest.raises(ValueError): |
| 34 | + JiraConfluenceUtil("a/fake/directory") |
| 35 | + |
| 36 | + |
| 37 | +def test_can_complete_jira_actions_check(monkeypatch: object) -> None: |
| 38 | + """Test that errors occur when env values not set for Jira""" |
| 39 | + applicable_vars = ["JIRA_URL", "JIRA_PROJECT_KEY", "JIRA_API_KEY"] |
| 40 | + # monkeypatch.setattr("dotenv.load_dotenv", lambda *a, **kw: None) |
| 41 | + |
| 42 | + for expected_var in applicable_vars: |
| 43 | + test_util = JiraConfluenceUtil(TEST_RESULTS_DIR) |
| 44 | + for var in applicable_vars: |
| 45 | + os.environ[var] = "test" |
| 46 | + os.environ.pop(expected_var, None) |
| 47 | + with pytest.raises(ValueError) as e: |
| 48 | + test_util._can_complete_jira_actions_check() |
| 49 | + assert ( |
| 50 | + str(e.value) |
| 51 | + == f"The [{expected_var}] os environment variable is required to complete any Jira actions" |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def test_can_complete_confluence_actions_check(monkeypatch: object) -> None: |
| 56 | + """Test that errors occur when env values not set for Confluence""" |
| 57 | + applicable_vars = ["CONFLUENCE_URL", "CONFLUENCE_API_KEY"] |
| 58 | + # monkeypatch.setattr("dotenv.load_dotenv", lambda *a, **kw: None) |
| 59 | + |
| 60 | + for expected_var in applicable_vars: |
| 61 | + test_util = JiraConfluenceUtil(TEST_RESULTS_DIR) |
| 62 | + for var in applicable_vars: |
| 63 | + os.environ[var] = "test" |
| 64 | + os.environ.pop(expected_var, None) |
| 65 | + with pytest.raises(ValueError) as e: |
| 66 | + test_util._can_complete_confluence_actions_check() |
| 67 | + assert ( |
| 68 | + str(e.value) |
| 69 | + == f"The [{expected_var}] os environment variable is required to complete any Confluence actions" |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_get_files_to_upload_to_jira() -> None: |
| 74 | + """Test that valid Jira reference checks work""" |
| 75 | + test_util = JiraConfluenceUtil(TEST_RESULTS_DIR) |
| 76 | + results_list = test_util._get_files_to_upload_to_jira(True, True, True, True) |
| 77 | + # Get all paths |
| 78 | + path_list = [] |
| 79 | + for item in results_list: |
| 80 | + path_list.append(item["path"]) |
| 81 | + # Get all test files |
| 82 | + test_files = [ |
| 83 | + TEST_RESULTS_DIR.joinpath("report.html"), |
| 84 | + TEST_RESULTS_DIR.joinpath("test_image.png"), |
| 85 | + TEST_RESULTS_DIR.joinpath("csv_test.csv"), |
| 86 | + TEST_RESULTS_DIR.joinpath("screenshot/test_image_b.png"), |
| 87 | + TEST_RESULTS_DIR.joinpath("test-sub-dir/trace.zip"), |
| 88 | + ] |
| 89 | + assert len(path_list) == len(test_files) |
| 90 | + for test_file in test_files: |
| 91 | + assert test_file in path_list |
0 commit comments