282 lines
8.6 KiB
Python
282 lines
8.6 KiB
Python
import json
|
||
import ssl
|
||
|
||
import pytest
|
||
from constance.test import override_config
|
||
from django.test import Client
|
||
|
||
from address.dadata import (
|
||
DADATA_SUGGEST_URL,
|
||
house_suggest_payload,
|
||
parse_house_suggestions,
|
||
suggest_houses,
|
||
)
|
||
from address.tests.paths import api_url
|
||
|
||
|
||
class _FakeResponse:
|
||
def __init__(self, payload):
|
||
self._raw = json.dumps(payload).encode()
|
||
|
||
def read(self):
|
||
return self._raw
|
||
|
||
def __enter__(self):
|
||
return self
|
||
|
||
def __exit__(self, *args):
|
||
return False
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_dadata_streets_require_region_and_city():
|
||
response = Client().get(api_url("dadata/streets"))
|
||
|
||
assert response.status_code == 422
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_dadata_streets_disabled_by_default():
|
||
response = Client().get(
|
||
api_url("dadata/streets"),
|
||
{"region_code": "28", "city": "Благовещенск"},
|
||
)
|
||
|
||
assert response.status_code == 503
|
||
assert response.json() == {"detail": "Интеграция с DaData отключена"}
|
||
|
||
|
||
@pytest.mark.django_db
|
||
@override_config(DADATA_IS_ENABLED=True)
|
||
def test_dadata_streets_require_token_when_enabled():
|
||
response = Client().get(
|
||
api_url("dadata/streets"),
|
||
{"region_code": "28", "city": "Благовещенск", "query": "ленин"},
|
||
)
|
||
|
||
assert response.status_code == 503
|
||
assert response.json() == {"detail": "Не задан токен DaData"}
|
||
|
||
|
||
@pytest.mark.django_db
|
||
@override_config(DADATA_IS_ENABLED=True, DADATA_TOKEN="test-token")
|
||
def test_dadata_streets_return_empty_without_integration():
|
||
response = Client().get(
|
||
api_url("dadata/streets"),
|
||
{"region_code": "28", "city": "Благовещенск", "query": "ленин"},
|
||
)
|
||
|
||
assert response.status_code == 200
|
||
assert response.json() == {"count": 0, "items": []}
|
||
|
||
|
||
@pytest.mark.django_db
|
||
def test_dadata_houses_require_street():
|
||
response = Client().get(
|
||
api_url("dadata/houses"),
|
||
{"region_code": "28", "city": "Благовещенск"},
|
||
)
|
||
|
||
assert response.status_code == 422
|
||
|
||
|
||
@pytest.mark.django_db
|
||
@override_config(DADATA_IS_ENABLED=True, DADATA_TOKEN="test-token")
|
||
def test_dadata_houses_return_empty_without_integration(monkeypatch):
|
||
def boom(*args, **kwargs):
|
||
raise TimeoutError("offline")
|
||
|
||
monkeypatch.setattr("urllib.request.urlopen", boom)
|
||
|
||
response = Client().get(
|
||
api_url("dadata/houses"),
|
||
{
|
||
"region_code": "28",
|
||
"city": "Благовещенск",
|
||
"street": "Ленина",
|
||
"query": "10",
|
||
},
|
||
)
|
||
|
||
assert response.status_code == 200
|
||
assert response.json() == {"count": 0, "items": []}
|
||
|
||
|
||
@pytest.mark.django_db
|
||
@override_config(DADATA_IS_ENABLED=True, DADATA_TOKEN="test-token")
|
||
def test_suggest_houses_skips_certificate_verification(monkeypatch):
|
||
captured = {}
|
||
|
||
def fake_urlopen(request, timeout=None, context=None):
|
||
captured["context"] = context
|
||
return _FakeResponse({"suggestions": []})
|
||
|
||
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
||
|
||
items = suggest_houses(
|
||
region_code="27",
|
||
city="Хабаровск",
|
||
street="Тихая",
|
||
query="12",
|
||
limit=20,
|
||
street_fias_id="11111111-1111-1111-1111-111111111111",
|
||
)
|
||
|
||
assert items == []
|
||
assert captured["context"] is not None
|
||
assert captured["context"].check_hostname is False
|
||
assert captured["context"].verify_mode == ssl.CERT_NONE
|
||
|
||
|
||
def test_house_suggest_payload_uses_street_fias_id():
|
||
payload = house_suggest_payload(
|
||
query="12",
|
||
limit=20,
|
||
region_code="27",
|
||
city="Хабаровск",
|
||
street="Тихая",
|
||
street_fias_id="11111111-1111-1111-1111-111111111111",
|
||
)
|
||
|
||
assert payload == {
|
||
"query": "12",
|
||
"count": 20,
|
||
"from_bound": {"value": "house"},
|
||
"to_bound": {"value": "house"},
|
||
"locations": [{"street_fias_id": "11111111-1111-1111-1111-111111111111"}],
|
||
}
|
||
|
||
|
||
def test_parse_house_suggestions_skips_without_house_fias_id():
|
||
items = parse_house_suggestions(
|
||
[
|
||
{
|
||
"value": "г Хабаровск, ул Тихая, д 12",
|
||
"data": {
|
||
"house_fias_id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
|
||
"house": "12",
|
||
"house_type": "д",
|
||
"block": "2",
|
||
"block_type": "к",
|
||
"postal_code": "680000",
|
||
"street": "Тихая",
|
||
"city": "Хабаровск",
|
||
"region_kladr_id": "2700000000000",
|
||
},
|
||
},
|
||
{"value": "без фиас", "data": {"house": "13", "house_type": "д"}},
|
||
]
|
||
)
|
||
|
||
assert len(items) == 1
|
||
assert items[0].fias_id == "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
|
||
assert items[0].house == "12"
|
||
assert items[0].block == "2"
|
||
assert items[0].block_type == "к"
|
||
assert items[0].region_code == "27"
|
||
|
||
|
||
@pytest.mark.django_db
|
||
@override_config(DADATA_IS_ENABLED=True, DADATA_TOKEN="test-token")
|
||
def test_suggest_houses_posts_street_fias_id(monkeypatch):
|
||
captured = {}
|
||
|
||
def fake_urlopen(request, timeout=None, context=None):
|
||
captured["url"] = request.full_url
|
||
captured["body"] = json.loads(request.data.decode())
|
||
captured["authorization"] = request.get_header("Authorization")
|
||
return _FakeResponse(
|
||
{
|
||
"suggestions": [
|
||
{
|
||
"value": "г Хабаровск, ул Тихая, д 12",
|
||
"data": {
|
||
"house_fias_id": "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
|
||
"house": "12",
|
||
"house_type": "д",
|
||
"postal_code": "680000",
|
||
"street": "Тихая",
|
||
"city": "Хабаровск",
|
||
"region_kladr_id": "2700000000000",
|
||
},
|
||
}
|
||
]
|
||
}
|
||
)
|
||
|
||
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
||
|
||
items = suggest_houses(
|
||
region_code="27",
|
||
city="Хабаровск",
|
||
street="Тихая",
|
||
query="12",
|
||
limit=20,
|
||
street_fias_id="11111111-1111-1111-1111-111111111111",
|
||
)
|
||
|
||
assert captured["url"] == DADATA_SUGGEST_URL
|
||
assert captured["authorization"] == "Token test-token"
|
||
assert captured["body"]["locations"] == [
|
||
{"street_fias_id": "11111111-1111-1111-1111-111111111111"}
|
||
]
|
||
assert captured["body"]["from_bound"] == {"value": "house"}
|
||
assert captured["body"]["to_bound"] == {"value": "house"}
|
||
assert captured["body"]["query"] == "12"
|
||
assert len(items) == 1
|
||
assert items[0].fias_id == "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
|
||
|
||
|
||
@pytest.mark.django_db
|
||
@override_config(DADATA_IS_ENABLED=True, DADATA_TOKEN="test-token")
|
||
def test_dadata_houses_return_suggestions(monkeypatch):
|
||
def fake_urlopen(request, timeout=None, context=None):
|
||
return _FakeResponse(
|
||
{
|
||
"suggestions": [
|
||
{
|
||
"value": "г Благовещенск, ул Ленина, д 10",
|
||
"data": {
|
||
"house_fias_id": "cccccccc-cccc-cccc-cccc-cccccccccccc",
|
||
"house": "10",
|
||
"house_type": "д",
|
||
"postal_code": "675000",
|
||
"street": "Ленина",
|
||
"city": "Благовещенск",
|
||
"region_kladr_id": "2800000000000",
|
||
},
|
||
}
|
||
]
|
||
}
|
||
)
|
||
|
||
monkeypatch.setattr("urllib.request.urlopen", fake_urlopen)
|
||
|
||
response = Client().get(
|
||
api_url("dadata/houses"),
|
||
{
|
||
"region_code": "28",
|
||
"city": "Благовещенск",
|
||
"street": "Ленина",
|
||
"query": "10",
|
||
},
|
||
)
|
||
|
||
assert response.status_code == 200
|
||
assert response.json() == {
|
||
"count": 1,
|
||
"items": [
|
||
{
|
||
"value": "г Благовещенск, ул Ленина, д 10",
|
||
"fias_id": "cccccccc-cccc-cccc-cccc-cccccccccccc",
|
||
"house": "10",
|
||
"house_type": "д",
|
||
"block": "",
|
||
"postal_code": "675000",
|
||
"street": "Ленина",
|
||
"city": "Благовещенск",
|
||
"region_code": "28",
|
||
}
|
||
],
|
||
}
|