Skip to content

Commit

Permalink
Adding mvp ignore file uploads feature (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogenesAnalytics committed Mar 16, 2024
1 parent 7b25861 commit 5d6a429
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"subject": "Your Form Subject",
"title": "Your Custom Title",
"form_backend_url": null,
"ignore_file_upload": true,
"email": "[email protected]",
"questions": [
{
Expand Down
5 changes: 4 additions & 1 deletion scripts/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,12 @@ fetch('config.json')
// Set form backend URL and enctype if available and not null
form.setAttribute('action', formBackendUrl);

// Check for ignore file upload
const ignoreFileUpload = data.ignore_file_upload

// Check if any question has type="file" in config.json
const hasFileUpload = data.questions.some(question => question.type === 'file');
if (hasFileUpload) {
if (hasFileUpload && (!ignoreFileUpload)) {
// If any question involves file upload use multipart encoding
form.setAttribute('enctype', 'multipart/form-data');
} else {
Expand Down
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def prepare_default_config(config: Dict[str, Any], src_path: Path) -> None:
# update form backend
config["form_backend_url"] = f"http://localhost:{port}{submit}"

# update ignore file uploads
config["ignore_file_upload"] = False

# update input[type=file] accept attr
for question in config["questions"]:
# check type
Expand Down Expand Up @@ -323,3 +326,30 @@ def multiple_select_options_config() -> Dict[str, Any]:

# updated
return config


@pytest.fixture(scope="function")
def ignore_upload_config() -> Dict[str, Any]:
"""Custom config file fixture for testing ignore file uploads."""
# get base config
config = base_custom_config()

# set ignore
config["ignore_file_upload"] = True

# update questions
config["questions"] = [
{
"label": "Upload funny memes",
"name": "meme_imgs",
"type": "file",
"required": True,
"custom": {
"multiple": True,
"accept": "*",
},
}
]

# updated
return config
1 change: 1 addition & 0 deletions tests/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Config(Schema):
subject: str
questions: list
form_backend_url: Optional[str] = None
ignore_file_upload: Optional[bool] = None

def __post_init__(self):
"""Post initialization method to validate questions."""
Expand Down
46 changes: 46 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ def test_multi_options_config_schema(
), "Error in multi options config fixture."


@pytest.mark.fixture
def test_multi_options_config_input_type(
multiple_select_options_config: Dict[str, Any]
) -> None:
"""Check that input[type=selectbox]."""
assert (
multiple_select_options_config["questions"][0]["type"] == "selectbox"
), "Input type is not selectbox."


@pytest.mark.fixture
def test_multi_opts_config_multiple(
multiple_select_options_config: Dict[str, Any]
Expand Down Expand Up @@ -332,3 +342,39 @@ def test_multi_opts_config_defaults(

# now check results
assert any(results)


@pytest.mark.fixture
def test_ignore_upload_config_schema(ignore_upload_config: Dict[str, Any]) -> None:
"""Check that the given config.json schema for ignore uploads is correct."""
assert check_config_schema(
ignore_upload_config
), "Error in ignore file upload config fixture."


@pytest.mark.fixture
def test_ignore_upload_config_input_type(ignore_upload_config: Dict[str, Any]) -> None:
"""Check that input[type=file]."""
assert (
ignore_upload_config["questions"][0]["type"] == "file"
), "Input type is not file."


@pytest.mark.fixture
def test_ignore_upload_attr_set(ignore_upload_config: Dict[str, Any]) -> None:
"""Check that the fixture has the correct attribute set."""
assert ignore_upload_config[
"ignore_file_upload"
], "Not configured to ignore file uploads."


@pytest.mark.fixture
def test_ignore_upload_custom_attrs(ignore_upload_config: Dict[str, Any]) -> None:
"""Check that the cusom attributes are properly set."""
# get custom attrs
custom = ignore_upload_config["questions"][0]["custom"]

# now check
assert (
custom["multiple"] and custom["accept"] == "*"
), "Custom attributes not properly set."
62 changes: 60 additions & 2 deletions tests/test_website.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,31 @@ def test_email_in_instructions(
assert default_site_config["email"] in instruct_text


@pytest.mark.website
def test_file_uploads_enabled(
sb: BaseCase,
live_session_web_app_url: str,
default_site_config: Dict[str, Any],
) -> None:
"""Test that the file uploads are enabled on the website."""
# check config for file upload attr
assert not default_site_config[
"ignore_file_upload"
], "Default site config should not ignore file uploads."

# open initial site
sb.open(live_session_web_app_url)

# get form
form_element = sb.get_element("form")

# get the enctype attribute
enctype_value = form_element.get_attribute("enctype")

# make sure it's multipart
assert enctype_value == "multipart/form-data"


@pytest.mark.website
def test_custom_title_works(
sb: BaseCase, live_session_web_app_url: str, default_site_config: Dict[str, Any]
Expand Down Expand Up @@ -528,7 +553,6 @@ def test_form_download_required_constraint(
sb.save_screenshot_to_logs()


@pytest.mark.debug
@pytest.mark.feature
def test_select_multiple_options(
sb: BaseCase,
Expand Down Expand Up @@ -614,7 +638,6 @@ def test_select_multiple_options(
sb.save_screenshot_to_logs()


@pytest.mark.debug
@pytest.mark.feature
def test_select_default_submission_rejected(
sb: BaseCase,
Expand Down Expand Up @@ -669,3 +692,38 @@ def test_select_default_submission_rejected(

# get screenshot
sb.save_screenshot_to_logs()


@pytest.mark.feature
def test_ignore_file_uploads(
sb: BaseCase,
live_session_web_app_url: str,
ignore_upload_config: Dict[str, Any],
) -> None:
"""Confirm that setting ignore file upload attrs works."""
# update config
response = requests.post(
live_session_web_app_url + "/update_config", json=ignore_upload_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 new site
sb.open(site_url)

# get form
updated_form_element = sb.get_element("form")

# get the enctype attribute
updated_enctype_value = updated_form_element.get_attribute("enctype")

# make sure it's multipart
assert updated_enctype_value == "application/x-www-form-urlencoded"

0 comments on commit 5d6a429

Please sign in to comment.