Skip to content

Commit

Permalink
feat: add back button to settings/enable-2fa page to allow user to abort
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Dec 4, 2024
1 parent b805519 commit a5d3157
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ RUN apt-get update && \
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

RUN pip install poetry
RUN pip install poetry pytest-playwright

# Playwright dependencies to run browsers
RUN playwright install
RUN playwright install-deps

WORKDIR /app

Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ ifndef message
endif
poetry run flask db revision -m "$(message)" --autogenerate

TESTS ?= ./tests/
TESTS ?= ./tests
.PHONY: test
test: ## Run the test suite
docker compose run --rm app \
poetry run pytest --cov hushline --cov-report term --cov-report html -vv $(PYTEST_ADDOPTS) $(TESTS)

.PHONY: test/ui
test/ui: # Only run the ui tests
docker compose run --rm app \
poetry run pytest --cov hushline --cov-report term --cov-report html -vv $(PYTEST_ADDOPTS) $(TESTS)/ui
1 change: 1 addition & 0 deletions hushline/templates/enable_2fa.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{% block title %}Settings{% endblock %}

{% block content %}
<a href="{{ url_for('.auth') }}" class="back-button"><span class="icon chevron back"></span> Back to Authentication</a>
<h2>Enable Two-Factor Authentication</h2>
<p>
Scan the QR code with your 2FA app or enter the text code below to enable
Expand Down
117 changes: 116 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ stripe = "^10.9.0"
types-requests = "^2.32.0.20240712"
types-setuptools = "^71.1.0.20240813"
python = "^3.11"
pytest-playwright = "^0.6.2"
tenacity = "^9.0.0"

[tool.poetry.group.dev.dependencies]
beautifulsoup4 = "^4.12.3"
Expand Down
43 changes: 43 additions & 0 deletions tests/ui/test_settings_enable_2fa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import re

from playwright.sync_api import Page, expect
from tenacity import RetryCallState, retry, stop_after_attempt, wait_random_exponential

BASE_URL = "http://127.0.0.1:8080"


def log_retry_error(retry_state: RetryCallState) -> None:
if retry_state is None or retry_state.outcome is None:
return
print(f"Retrying due to error: {retry_state.outcome.exception()}")


@retry(
wait=wait_random_exponential(min=1, max=60),
stop=stop_after_attempt(3),
retry_error_callback=log_retry_error,
)
def test_enable_2fa_has_back_button(page: Page, user_password: str) -> None:
page.goto(f"{BASE_URL}/login", wait_until="domcontentloaded")
page.get_by_label("Username").fill("test")
page.get_by_label("Password").fill(user_password)
page.get_by_role("button", name="Login").click()

page.goto(f"{BASE_URL}/settings/enable-2fa", wait_until="domcontentloaded")
expect(page.get_by_text("Back to Authentication")).to_be_visible()


@retry(
wait=wait_random_exponential(min=1, max=60),
stop=stop_after_attempt(3),
retry_error_callback=log_retry_error,
)
def test_enable_2fa_back_button_returns(page: Page, user_password: str) -> None:
page.goto(f"{BASE_URL}/login", wait_until="domcontentloaded")
page.get_by_label("Username").fill("test")
page.get_by_label("Password").fill(user_password)
page.get_by_role("button", name="Login").click()

page.goto(f"{BASE_URL}/settings/enable-2fa", wait_until="domcontentloaded")
page.get_by_text("Back to Authentication").click()
expect(page).to_have_url(re.compile(".*auth"))

0 comments on commit a5d3157

Please sign in to comment.