Skip to content

Commit

Permalink
test that pastes are expired pastes are reaped upon review
Browse files Browse the repository at this point in the history
  • Loading branch information
shtlrs committed Jan 14, 2024
1 parent a3471b3 commit ee0f3e7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 11 deletions.
6 changes: 5 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def pytest_runtest_makereport(item, call):
make_screenshot(item, page)
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
extras.append(pytest_html.extras.png(re.sub(r"test\W*e2e\W*report\W*", "", screen_file)))
extras.append(
pytest_html.extras.png(
re.sub(r"test\W*e2e\W*report\W*", "", screen_file)
)
)
report.extras = extras


Expand Down
3 changes: 3 additions & 0 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def database() -> Generator[None, None, None]:
with tempfile.NamedTemporaryFile(suffix="", delete=False) as temp:
props = load(config_path)
props["database_uri"] = f"sqlite:///{temp.name}"
props[
"expiries.1hour"
] = 4 # It expires in 4 seconds instead of an hour
with open(config_path, "w") as config_file:
toml.dump(props, config_file)
yield
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/pageobjects/base_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ def should_have_theme(self, theme):
self.root,
f"{self.page_name} had incorrect background color for theme {theme.name}",
).to_have_css("background-color", theme.value["background"])

def refresh(self):
self.page.reload()
1 change: 0 additions & 1 deletion test/e2e/pageobjects/error_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ def should_have_error_text(self, code, description):

def should_be_opened(self):
expect(self.page).to_have_title("error")

54 changes: 54 additions & 0 deletions test/e2e/testscenarios/test_reaping_expired_pastes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from test.e2e.conftest import create_paste_page
from test.e2e.pageobjects.create_paste_page import CreatePastePage
from test.e2e.pageobjects.view_paste_page import ViewPastePage
from test.e2e.pageobjects.error_page import ErrorPage
from urllib3.util.url import parse_url

import pytest
from playwright.sync_api import Page, Playwright


@pytest.mark.e2e
def test_view_expired_paste(
page: Page, playwright: Playwright, create_paste_page: CreatePastePage
):
create_paste_page.should_have_title("Create new paste")

pasted_text = create_paste_page.paste_random_text()
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()
view_paste_page.should_have_pasted_text(pasted_text)

page.wait_for_timeout(5000)
view_paste_page.refresh()
error_page = ErrorPage(page)
error_page.should_be_opened()
error_page.should_have_error_text("404", "That page does not exist")


@pytest.mark.e2e
def test_view_expired_paste_on_redirect(
page: Page, playwright: Playwright, create_paste_page: CreatePastePage
):
create_paste_page.should_have_title("Create new paste")

pasted_text = create_paste_page.paste_random_text()
create_paste_page.click_submit()

view_paste_page = ViewPastePage(page)
view_paste_page.should_be_opened()
view_paste_page.should_have_pasted_text(pasted_text)

paste_url = view_paste_page.current_url()
parsed_url = parse_url(paste_url)
parsed_url = parsed_url._replace(path=f"/show{parsed_url.path}")

show_url = parsed_url.url
page.wait_for_timeout(5000)
page.goto(show_url)

error_page = ErrorPage(page)
error_page.should_be_opened()
error_page.should_have_error_text("404", "That page does not exist")
1 change: 0 additions & 1 deletion test/integration/test_http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,6 @@ def test_api_new_wrong_method(self) -> None:
response = self.fetch("/api/v1/paste")
assert response.code == 405


def test_api_detail_many_files(self) -> None:
response = self.fetch(
"/api/v1/paste",
Expand Down
16 changes: 8 additions & 8 deletions test/integration/test_reaping_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def temp_database_url() -> str:

@patch("pinnwand.utility.reap")
@pytest.mark.asyncio
async def test_reaping_tasks_running_periodicity(reap_utility_mock: MagicMock, temp_database_url):
async def test_reaping_tasks_running_periodicity(
reap_utility_mock: MagicMock, temp_database_url
):
"""Test that the reaping task gets executed every period T."""
runner = CliRunner()

runner.invoke(
command.main, ["--configuration-path", config_path, "http"]
)
runner.invoke(command.main, ["--configuration-path", config_path, "http"])

await asyncio.sleep(5)

Expand All @@ -48,7 +48,9 @@ async def test_reaping_tasks_running_periodicity(reap_utility_mock: MagicMock, t

@patch("pinnwand.database.manager.DatabaseManager.get_engine")
@pytest.mark.asyncio
async def test_pastes_reaped_on_startup(get_engine_patch: MagicMock, temp_database_url):
async def test_pastes_reaped_on_startup(
get_engine_patch: MagicMock, temp_database_url
):
"""Ensures that pastes are reaped correctly upon startup."""

engine = create_engine(temp_database_url)
Expand All @@ -74,9 +76,7 @@ async def test_pastes_reaped_on_startup(get_engine_patch: MagicMock, temp_databa
await asyncio.sleep(2)

runner = CliRunner()
runner.invoke(
command.main, ["--configuration-path", config_path, "http"]
)
runner.invoke(command.main, ["--configuration-path", config_path, "http"])

with manager.DatabaseManager.get_session() as session:
for slug in slugs:
Expand Down

0 comments on commit ee0f3e7

Please sign in to comment.