Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-sorrentino committed Nov 10, 2024
1 parent be78ce2 commit ac3a9e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
6 changes: 4 additions & 2 deletions hushline/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,10 @@ def logout() -> Response:
@app.route("/directory")
def directory() -> Response | str:
logged_in = "user_id" in session
# Fetch intro text from OrganizationSetting and get the value field
intro_text = OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO).value
# Provide a default intro text if DIRECTORY_INTRO is not set
intro_text_setting = OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO)
intro_text = intro_text_setting.value if intro_text_setting else "Welcome to the directory."

return render_template(
"directory.html",
usernames=get_directory_usernames(),
Expand Down
17 changes: 6 additions & 11 deletions hushline/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ async def index() -> str | Response:
return redirect(url_for("login"))

# Initialize the directory intro text form with existing value
directory_intro_text_value = OrganizationSetting.fetch_one(
OrganizationSetting.DIRECTORY_INTRO
).value if OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO) else ""
directory_intro_text_value = (
OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO).value
if OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO)
else ""
)
update_directory_intro_text_form = UpdateDirectoryIntroTextForm(
directory_intro_text=directory_intro_text_value
)
Expand Down Expand Up @@ -815,18 +817,11 @@ def update_directory_intro_text() -> Response:
form = UpdateDirectoryIntroTextForm()
if form.validate_on_submit():
intro_text = form.directory_intro_text.data
OrganizationSetting.upsert(
key=OrganizationSetting.DIRECTORY_INTRO, value=intro_text
)
OrganizationSetting.upsert(key=OrganizationSetting.DIRECTORY_INTRO, value=intro_text)
db.session.commit()
flash("✅ Directory introduction text updated successfully.", "success")
else:
flash("❌ Failed to update introduction text. Please check your input.", "error")
# Re-render the form with the current input if validation fails
return render_template(
"settings/index.html",
update_directory_intro_text_form=form,
)

return redirect(url_for("settings.index"))

Expand Down
4 changes: 1 addition & 3 deletions hushline/settings/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,5 @@ class DeleteBrandLogoForm(FlaskForm):


class UpdateDirectoryIntroTextForm(FlaskForm):
directory_intro_text = TextAreaField(
"Directory Introduction Text", validators=[DataRequired()]
)
directory_intro_text = TextAreaField("Directory Introduction Text", validators=[DataRequired()])
submit = SubmitField("Update Intro Text")
42 changes: 42 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,3 +666,45 @@ def test_update_brand_logo(client: FlaskClient, admin: User) -> None:
resp = client.get(logo_url, follow_redirects=True)
# yes this check is ridiculous. why? because we redirect not-founds instead of actually 404-ing
assert "That page doesn" in resp.text


@pytest.mark.usefixtures("_authenticated_admin")
def test_update_directory_intro_text(client: FlaskClient, admin: User) -> None:
"""Test updating the directory intro text and ensure data integrity on failure."""

# Step 1: Set initial intro text and confirm it loads correctly
initial_intro_text = "Welcome to our directory!"
OrganizationSetting.upsert(key=OrganizationSetting.DIRECTORY_INTRO, value=initial_intro_text)
db.session.commit()

response = client.get(url_for("settings.index"))
assert response.status_code == 200
assert initial_intro_text in response.text

# Step 2: Update the intro text with valid data
updated_intro_text = "New introductory text for the directory."
response = client.post(
url_for("settings.update_directory_intro_text"),
data={"directory_intro_text": updated_intro_text},
follow_redirects=True,
)
assert response.status_code == 200
assert "✅ Directory introduction text updated successfully." in response.text

# Confirm intro text was successfully updated in the database
setting = OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO)
assert setting.value == updated_intro_text

# Step 3: Test invalid update (e.g., empty intro text), confirm flash message,
# and verify unchanged value
response = client.post(
url_for("settings.update_directory_intro_text"),
data={"directory_intro_text": ""},
follow_redirects=True,
)
assert response.status_code == 200
assert "❌ Failed to update introduction text. Please check your input." in response.text

# Verify intro text remains unchanged in the database after the failed update
retained_setting = OrganizationSetting.fetch_one(OrganizationSetting.DIRECTORY_INTRO)
assert retained_setting.value == updated_intro_text

0 comments on commit ac3a9e5

Please sign in to comment.