diff --git a/tests/conftest.py b/tests/conftest.py index f1a99f9..373b02f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -173,11 +173,11 @@ def dummy_form_post_data(dummy_txt_file_stream) -> Dict[str, Any]: } -@pytest.fixture(scope="session") -def dummy_jpg_file_path(session_tmp_dir: Path) -> Path: +@pytest.fixture(scope="function") +def dummy_jpg_file_path(tmp_path: Path) -> Path: """Create a dummy JPEG image.""" # create image dir - img_dir = session_tmp_dir / "images" + img_dir = tmp_path / "images" img_dir.mkdir() # create a dummy image @@ -188,7 +188,7 @@ def dummy_jpg_file_path(session_tmp_dir: Path) -> Path: return img_path -@pytest.fixture(scope="session") +@pytest.fixture(scope="function") def dummy_jpg_data_url(dummy_jpg_file_path) -> str: """Create a data URL for the dummy JPEG file.""" # read the content of the file @@ -202,8 +202,10 @@ def dummy_jpg_data_url(dummy_jpg_file_path) -> str: return f"data:image/jpeg;base64,{base64_content}" -@pytest.fixture(scope="session") -def form_inputs(dummy_jpg_file_path: Path, dummy_jpg_data_url: str) -> Dict[str, Any]: +@pytest.fixture(scope="function") +def dummy_form_inputs( + dummy_jpg_file_path: Path, dummy_jpg_data_url: str +) -> Dict[str, Any]: """Defines the values to be submitted for each input type during form tests.""" return { "date": {"date": "01012000"}, @@ -295,6 +297,46 @@ def live_session_web_app_url(session_web_app: Flask) -> str: return f"http://localhost:{port}" +@pytest.fixture(scope="function") +def all_inputs_config(dummy_form_inputs: Dict[str, Any]) -> Dict[str, Any]: + """Create config file fixture for testing all supported input types.""" + # get base config + config = base_custom_config() + + # now create questions based on supported input types + questions = [] + for idx, input_type in enumerate(dummy_form_inputs.keys()): + # basic question attrs + q = { + "label": f"Question{idx+1}", + "name": f"testing_{input_type}_input_type", + "type": input_type, + "required": True, + } + + # check for selectbox type + if input_type == "selectbox": + # setup options + options = [ + {"label": "Option1", "value": "Opt1"}, + {"label": "Option2", "value": "Opt2"}, + {"label": "Option3", "value": "Opt3"}, + {"label": "Option4", "value": "Opt4"}, + ] + + # add them + q["options"] = options + + # update questions + questions.append(q) + + # now update questions + config["questions"] = questions + + # done + return config + + @pytest.fixture(scope="function") def multiple_select_options_config() -> Dict[str, Any]: """Custom config file fixture for testing multiple select options.""" diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py index 66cad39..189ef05 100644 --- a/tests/test_fixtures.py +++ b/tests/test_fixtures.py @@ -43,14 +43,14 @@ def test_websrc_in_temp_dir( @pytest.mark.fixture def test_config_keys_in_form_inputs( - default_site_config: Dict[str, Any], form_inputs: Dict[str, Any] + default_site_config: Dict[str, Any], dummy_form_inputs: Dict[str, Any] ) -> None: """Check that keys from config.json are present in form input testing fixture.""" # get types from questions section of config.json question_types = [q["type"] for q in default_site_config["questions"]] # check config question types missing form inputs (if any) - missing_keys = set(question_types) - set(form_inputs) + missing_keys = set(question_types) - set(dummy_form_inputs) # no missing keys assert ( @@ -289,6 +289,12 @@ def test_session_config_form_backend_updated( assert config[key] == json_data[key] +@pytest.mark.fixture +def test_all_inputs_config_schema(all_inputs_config: Dict[str, Any]) -> None: + """Check that the config.json schema for all inputs config is correct.""" + assert check_config_schema(all_inputs_config), "Error in all inputs config fixture." + + @pytest.mark.fixture def test_multi_options_config_schema( multiple_select_options_config: Dict[str, Any] diff --git a/tests/test_website.py b/tests/test_website.py index 5744fbc..f9acd8a 100644 --- a/tests/test_website.py +++ b/tests/test_website.py @@ -151,7 +151,7 @@ def fill_out_form( test_value = convert_to_isoformat(**test_value) # check if file tuple - if isinstance(test_value, tuple): + elif isinstance(test_value, tuple): # unpack file_path, data_url = test_value @@ -351,7 +351,7 @@ def test_form_backend_updated(sb: BaseCase, live_session_web_app_url: str) -> No def test_form_submission( sb: BaseCase, live_session_web_app_url: str, - form_inputs: Dict[str, Any], + dummy_form_inputs: Dict[str, Any], session_web_app: Flask, ) -> None: """Check that the given form upon completion can be succesfully submitted.""" @@ -370,9 +370,12 @@ def test_form_submission( # fill out form submitted_input = { - k: v for k, v in fill_out_form(form_element, config, form_inputs) + k: v for k, v in fill_out_form(form_element, config, dummy_form_inputs) } + # save screeshot for comfirmation of form entries + sb.save_screenshot_to_logs() + # get send button ... send_button = form_element.find_element(By.ID, "send_button") @@ -403,7 +406,7 @@ def test_form_submission( value1 == value2 ), f"Submitted input: {value1} differs from received: {value2}" - # save screenshot for confirmation + # save screenshot for confirmation of response sb.save_screenshot_to_logs() @@ -456,7 +459,7 @@ def test_form_download( sb: BaseCase, live_session_web_app_url: str, session_web_app: Flask, - form_inputs: Dict[str, Any], + dummy_form_inputs: Dict[str, Any], ) -> None: """Check that the given form upon completion can be succesfully downloaded.""" # get config file @@ -474,9 +477,12 @@ def test_form_download( # fill out form submitted_input = { - k: v for k, v in fill_out_form(form_element, config, form_inputs) + k: v for k, v in fill_out_form(form_element, config, dummy_form_inputs) } + # save screeshot for comfirmation of form entries + sb.save_screenshot_to_logs() + # get download button ... download_button = form_element.find_element(By.ID, "download_button") @@ -564,6 +570,78 @@ def test_form_download_required_constraint( sb.save_screenshot_to_logs() +@pytest.mark.feature +def test_all_supported_inputs( + sb: BaseCase, + live_session_web_app_url: str, + all_inputs_config: Dict[str, Any], + dummy_form_inputs: Dict[str, Any], +) -> None: + """Testing all supported inputs pass correctly.""" + # update config + response = requests.post( + live_session_web_app_url + "/update_config", json=all_inputs_config + ) + + # check response + assert response.status_code == 200 + + # get token + token = response.json().get("token") + assert token is not None + + # update site URL + site_url = f"{live_session_web_app_url}?token={token}" + + # open site + sb.open(site_url) + + # find the form element + form_element = sb.get_element("form") + + # fill out form + submitted_input = { + k: v + for k, v in fill_out_form(form_element, all_inputs_config, dummy_form_inputs) + } + + # save screeshot for comfirmation of form entries + sb.save_screenshot_to_logs() + + # get send button ... + send_button = form_element.find_element(By.ID, "send_button") + + # ... now click it + send_button.click() + + # check that the form was submitted + sb.assert_text("Contact Form Response") + + # get the HTML content of the response + response_html = sb.get_page_source() + + # get received input from Flask response html + received_input = {k: v for k, v in extract_received_form_input(response_html)} + + # check keys are same + missing_keys = set(submitted_input) - set(received_input) + assert not missing_keys, f"Keys are not the same: {missing_keys}" + + # now check values + for key in submitted_input.keys(): + # get values + value1 = submitted_input[key] + value2 = received_input[key] + + # check + assert ( + value1 == value2 + ), f"Submitted input: {value1} differs from received: {value2}" + + # save screenshot for confirmation of response + sb.save_screenshot_to_logs() + + @pytest.mark.feature def test_select_multiple_options( sb: BaseCase,