mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-30 13:10:47 +00:00
Playwright tmp dirs are cleaned up after browser close 🟥
This commit is contained in:
parent
ea4c58bef7
commit
bd1fa09dc2
2 changed files with 52 additions and 0 deletions
50
backend/pashub_fetcher/tests/test_token_getter.py
Normal file
50
backend/pashub_fetcher/tests/test_token_getter.py
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from backend.pashub_fetcher.token_getter import get_token_from_local_storage
|
||||||
|
|
||||||
|
|
||||||
|
def _configure_playwright_mock(mock_sync_playwright: MagicMock) -> None:
|
||||||
|
mock_page = MagicMock()
|
||||||
|
mock_page.url = "https://pashub.net/dashboard"
|
||||||
|
mock_page.evaluate.return_value = "fake-token"
|
||||||
|
|
||||||
|
mock_context = MagicMock()
|
||||||
|
mock_context.new_page.return_value = mock_page
|
||||||
|
|
||||||
|
mock_browser = MagicMock()
|
||||||
|
mock_browser.new_context.return_value = mock_context
|
||||||
|
|
||||||
|
mock_p = MagicMock()
|
||||||
|
mock_p.chromium.launch.return_value = mock_browser
|
||||||
|
|
||||||
|
mock_sync_playwright.return_value.__enter__.return_value = mock_p
|
||||||
|
|
||||||
|
|
||||||
|
@patch("backend.pashub_fetcher.token_getter.shutil.rmtree")
|
||||||
|
@patch("backend.pashub_fetcher.token_getter.glob.glob")
|
||||||
|
@patch("backend.pashub_fetcher.token_getter.sync_playwright")
|
||||||
|
def test_playwright_tmp_dirs_are_cleaned_up_after_browser_close(
|
||||||
|
mock_sync_playwright: MagicMock,
|
||||||
|
mock_glob: MagicMock,
|
||||||
|
mock_rmtree: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
# Arrange
|
||||||
|
fake_artifacts = ["/tmp/playwright-artifacts-abc12"]
|
||||||
|
fake_profiles = ["/tmp/playwright_chromiumdev_profile-xyz99"]
|
||||||
|
|
||||||
|
def glob_side_effect(pattern: str) -> list[str]:
|
||||||
|
if "playwright-artifacts-*" in pattern:
|
||||||
|
return fake_artifacts
|
||||||
|
if "playwright_chromiumdev_profile-*" in pattern:
|
||||||
|
return fake_profiles
|
||||||
|
return []
|
||||||
|
|
||||||
|
mock_glob.side_effect = glob_side_effect
|
||||||
|
_configure_playwright_mock(mock_sync_playwright)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
get_token_from_local_storage("user@example.com", "secret")
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
mock_rmtree.assert_any_call("/tmp/playwright-artifacts-abc12", ignore_errors=True)
|
||||||
|
mock_rmtree.assert_any_call("/tmp/playwright_chromiumdev_profile-xyz99", ignore_errors=True)
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
|
import glob
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
|
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue