@@ -0,0 +1,111 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.test import override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
import pytest
|
||||
|
||||
from apps.sources.services.fetcher import FetchedDocument
|
||||
from apps.sources.services.manual_import import ManualImportError
|
||||
|
||||
|
||||
def test_manual_import_view_prefills_source_url(client, user):
|
||||
client.force_login(user)
|
||||
response = client.get(
|
||||
reverse("sources:manual_import"), {"source_url": "https://jobs.example.org/vacature"}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert 'value="https://jobs.example.org/vacature"' in response.content.decode("utf-8")
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_manual_import_view_posts_url_and_shows_result(client, user, monkeypatch):
|
||||
def fake_fetch(*_args, **_kwargs):
|
||||
return FetchedDocument(
|
||||
requested_url="https://jobs.example.org/engineering",
|
||||
final_url="https://jobs.example.org/engineering",
|
||||
status_code=200,
|
||||
headers={"content-type": "text/html; charset=utf-8"},
|
||||
content=b"<html><body>vacature</body></html>",
|
||||
)
|
||||
|
||||
def fake_process(_document):
|
||||
return {
|
||||
"extracted": 1,
|
||||
"created": 1,
|
||||
"updated": 0,
|
||||
"duplicates": 0,
|
||||
"parser": "manual-test",
|
||||
"warnings": [],
|
||||
}
|
||||
|
||||
monkeypatch.setattr("apps.sources.services.manual_import.fetch_url", fake_fetch)
|
||||
monkeypatch.setattr("apps.sources.services.manual_import.process_raw_document", fake_process)
|
||||
|
||||
client.force_login(user)
|
||||
response = client.post(
|
||||
reverse("sources:manual_import"),
|
||||
{"source_url": "https://jobs.example.org/engineering"},
|
||||
)
|
||||
html = response.content.decode("utf-8")
|
||||
assert response.status_code == 200
|
||||
assert "Importresultaat" in html
|
||||
assert "Herkenbare items: 1" in html
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_manual_import_view_paste_prefers_post_flow_and_does_not_fetch(client, user, monkeypatch):
|
||||
def fail_fetch(*_args, **_kwargs):
|
||||
raise AssertionError("Fetcher should not run for paste imports.")
|
||||
|
||||
def fake_process(_document):
|
||||
return {
|
||||
"extracted": 1,
|
||||
"created": 0,
|
||||
"updated": 0,
|
||||
"duplicates": 1,
|
||||
"parser": "manual-test",
|
||||
"warnings": [],
|
||||
}
|
||||
|
||||
monkeypatch.setattr("apps.sources.services.manual_import.fetch_url", fail_fetch)
|
||||
monkeypatch.setattr("apps.sources.services.manual_import.process_raw_document", fake_process)
|
||||
|
||||
client.force_login(user)
|
||||
response = client.post(
|
||||
reverse("sources:manual_import"),
|
||||
{"pasted_text": "Eerste regel\nTweede regel"},
|
||||
)
|
||||
html = response.content.decode("utf-8")
|
||||
assert response.status_code == 200
|
||||
assert "Importresultaat" in html
|
||||
assert "Modus: paste" in html
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@override_settings(
|
||||
MANUAL_IMPORT_RATE_LIMIT_MAX_ATTEMPTS=1,
|
||||
MANUAL_IMPORT_RATE_LIMIT_WINDOW_SECONDS=120,
|
||||
MANUAL_IMPORT_RATE_LIMIT_BLOCK_SECONDS=60,
|
||||
)
|
||||
def test_manual_import_blocks_after_failed_attempts(client, user, monkeypatch):
|
||||
cache.clear()
|
||||
|
||||
def fail_import(*_args, **_kwargs):
|
||||
raise ManualImportError("Handmatige import faalde in test")
|
||||
|
||||
client.force_login(user)
|
||||
monkeypatch.setattr("apps.sources.views.import_manual_source", fail_import)
|
||||
|
||||
response = client.post(
|
||||
reverse("sources:manual_import"),
|
||||
{"source_url": "https://jobs.example.org/engineering"},
|
||||
)
|
||||
assert "Handmatige import faalde in test" in response.content.decode("utf-8")
|
||||
|
||||
response = client.post(
|
||||
reverse("sources:manual_import"),
|
||||
{"source_url": "https://jobs.example.org/engineering"},
|
||||
)
|
||||
assert "Te veel handmatige importverzoeken" in response.content.decode("utf-8")
|
||||
Reference in New Issue
Block a user