bd1cc61434
카페24 디자인 FTP 는 허용 확장자만 받는다. 임시 이름 `product-swiper.js.cf24tmp-<tag>` 는 확장자가 바뀌어 STOR 가 `550 Operation not permitted` 로 거부돼 모바일 스와이프·PC/모바일 상품상세 적용이 모두 실패했다. 원래 확장자를 끝에 두도록 `<stem>_cf24tmp_<tag><ext>` 로 바꾼다(원본 비킴 이름도 동일). 테스트 FTP 대역에 확장자 제한을 흉내내 회귀를 막는다. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
538 lines
24 KiB
Python
538 lines
24 KiB
Python
"""카페24 모듈 단위 테스트용 대역 — 메모리 저장소 · 가짜 카페24 · 가짜 FTP.
|
|
|
|
실제 DB 의 동작(잠금 만료, SKIP LOCKED, JSONB 병합 등)은 이 대역으로 증명되지 않는다.
|
|
그 부분은 test_cafe24_db.py(격리된 로컬 PostgreSQL)가 검증한다. 여기서는 쓰기 순서와
|
|
분기(백업·충돌·결과 분류·복구 판정)를 빠르게 확인한다.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import copy
|
|
import ftplib
|
|
import itertools
|
|
import uuid
|
|
from contextlib import contextmanager
|
|
from datetime import datetime, timedelta
|
|
from typing import Any
|
|
|
|
from app.integrations.cafe24.errors import Cafe24ApiError
|
|
from app.modules.cafe24 import store
|
|
from app.timezone import KST, now_kst
|
|
|
|
|
|
class MemoryStore:
|
|
"""Cafe24Store 가 writes/worker/라우터에 주는 메서드를 메모리로 흉내낸다."""
|
|
|
|
def __init__(self) -> None:
|
|
self._ids = itertools.count(1)
|
|
self.revisions: dict[int, dict[str, Any]] = {}
|
|
self.attempts: dict[int, dict[str, Any]] = {}
|
|
self.locks: dict[str, dict[str, Any]] = {}
|
|
self.schedules: dict[int, dict[str, Any]] = {}
|
|
self.snapshots: dict[int, dict[str, Any]] = {}
|
|
self.audits: list[dict[str, Any]] = []
|
|
self.products: dict[int, dict[str, Any]] = {}
|
|
self.fail_on: set[str] = set() # 이 메서드 이름이 들어 있으면 예외(기록 실패 재현)
|
|
self.schema_ok = True
|
|
|
|
def _maybe_fail(self, name: str) -> None:
|
|
if name in self.fail_on:
|
|
raise RuntimeError(f"DB 장애({name})")
|
|
|
|
# ── 스키마 ──
|
|
def write_schema_ready(self) -> bool:
|
|
return self.schema_ok
|
|
|
|
# ── 잠금 ──
|
|
def acquire_lock(self, key, *, owner, purpose, ttl_seconds):
|
|
now = now_kst()
|
|
held = self.locks.get(key)
|
|
if held and held["expires_at"] > now:
|
|
return None
|
|
token = uuid.uuid4().hex
|
|
self.locks[key] = {"owner": owner, "purpose": purpose, "token": token,
|
|
"expires_at": now + timedelta(seconds=ttl_seconds)}
|
|
return token
|
|
|
|
def renew_lock(self, key, token, *, ttl_seconds):
|
|
held = self.locks.get(key)
|
|
if not held or held["token"] != token or held["expires_at"] <= now_kst():
|
|
return False
|
|
held["expires_at"] = now_kst() + timedelta(seconds=ttl_seconds)
|
|
return True
|
|
|
|
def release_lock(self, key, token):
|
|
if self.locks.get(key, {}).get("token") == token:
|
|
del self.locks[key]
|
|
|
|
def lock_holder(self, key):
|
|
held = self.locks.get(key)
|
|
return dict(held) if held and held["expires_at"] > now_kst() else {}
|
|
|
|
def expire_lock(self, key):
|
|
if key in self.locks:
|
|
self.locks[key]["expires_at"] = now_kst() - timedelta(seconds=1)
|
|
|
|
# ── 버전 ──
|
|
def add_revision(self, *, product_no, html_content, revision_type, memo="", created_by="",
|
|
mall_id="", shop_no=1, source="", source_revision_id=None):
|
|
self._maybe_fail("add_revision")
|
|
rid = next(self._ids)
|
|
self.revisions[rid] = {
|
|
"id": rid, "product_no": int(product_no), "html_content": html_content or "",
|
|
"revision_type": store.normalize_revision_type(revision_type), "memo": memo,
|
|
"created_by": created_by, "mall_id": mall_id, "shop_no": shop_no, "source": source,
|
|
"source_revision_id": source_revision_id, "created_at": now_kst().isoformat(),
|
|
}
|
|
return rid
|
|
|
|
def get_revision(self, revision_id):
|
|
return dict(self.revisions.get(int(revision_id), {}))
|
|
|
|
def list_revisions(self, product_no, *, limit=20):
|
|
rows = [dict(r, html_length=len(r["html_content"])) for r in self.revisions.values()
|
|
if r["product_no"] == int(product_no)]
|
|
return sorted(rows, key=lambda r: -r["id"])[:limit]
|
|
|
|
def of_type(self, revision_type):
|
|
return [r for r in self.revisions.values() if r["revision_type"] == revision_type]
|
|
|
|
# ── 쓰기 시도 ──
|
|
def start_attempt(self, **fields):
|
|
self._maybe_fail("start_attempt")
|
|
aid = next(self._ids)
|
|
row = {"id": aid, "status": store.ATTEMPT_STARTED, "error": "", "remote_updated_at": "",
|
|
"remote_at": None, "resolved_by": "", "applied_revision_id": None,
|
|
"created_at": now_kst(), "target_html": "", "target_revision_id": None,
|
|
"observed_digest": "", "base_digest": "", "target_digest": "", "schedule_id": None,
|
|
"set_display": None, "set_selling": None, "memo": "", "actor": "", "origin": "MANUAL"}
|
|
row.update(fields)
|
|
self.attempts[aid] = row
|
|
return aid
|
|
|
|
def update_attempt(self, attempt_id, *, status=None, error=None, remote_updated_at=None,
|
|
remote_ok=False, resolved_by=None, only_from=None):
|
|
self._maybe_fail("update_attempt")
|
|
row = self.attempts.get(int(attempt_id))
|
|
if not row or (only_from and row["status"] not in only_from):
|
|
return False
|
|
if status is not None:
|
|
row["status"] = status
|
|
if error is not None:
|
|
row["error"] = error
|
|
if remote_updated_at is not None:
|
|
row["remote_updated_at"] = remote_updated_at
|
|
if remote_ok and row["remote_at"] is None:
|
|
row["remote_at"] = now_kst()
|
|
if resolved_by is not None:
|
|
row["resolved_by"] = resolved_by
|
|
return True
|
|
|
|
def record_attempt_revision(self, attempt_id, *, revision_type, memo, created_by, source="",
|
|
source_revision_id=None):
|
|
self._maybe_fail("record_attempt_revision")
|
|
row = self.attempts[int(attempt_id)]
|
|
if row.get("applied_revision_id"):
|
|
return row["applied_revision_id"]
|
|
body = row.get("target_html") or self.revisions.get(row.get("target_revision_id") or 0, {}).get("html_content", "")
|
|
rid = self.add_revision(product_no=row["product_no"], html_content=body, revision_type=revision_type,
|
|
memo=memo, created_by=created_by, mall_id=row.get("mall_id", ""),
|
|
source=source, source_revision_id=source_revision_id)
|
|
row.update(status=store.ATTEMPT_RECORDED, applied_revision_id=rid, target_html="")
|
|
if row["remote_at"] is None:
|
|
row["remote_at"] = now_kst()
|
|
return rid
|
|
|
|
def get_attempt(self, attempt_id):
|
|
return dict(self.attempts.get(int(attempt_id), {}))
|
|
|
|
def latest_attempt_for_schedule(self, schedule_id):
|
|
rows = [a for a in self.attempts.values() if a.get("schedule_id") == int(schedule_id)]
|
|
return dict(max(rows, key=lambda a: a["id"])) if rows else {}
|
|
|
|
def open_attempts(self, product_no, *, since):
|
|
return [dict(a) for a in sorted(self.attempts.values(), key=lambda a: a["id"])
|
|
if a["product_no"] == int(product_no) and a["status"] in store.ATTEMPT_OPEN
|
|
and a["created_at"] >= since]
|
|
|
|
def attempts_needing_record(self, *, since, limit=20):
|
|
return [dict(a) for a in self.attempts.values()
|
|
if a["status"] == store.ATTEMPT_REMOTE_OK and a["created_at"] >= since][:limit]
|
|
|
|
def write_lineage(self, product_no, *, since, limit=30):
|
|
rows = [a for a in self.attempts.values()
|
|
if a["product_no"] == int(product_no) and a["status"] in store.ATTEMPT_CONFIRMED
|
|
and a["target_digest"] and a["remote_at"] and a["remote_at"] >= since]
|
|
rows.sort(key=lambda a: (a["remote_at"], a["id"]), reverse=True)
|
|
out = []
|
|
for i, a in enumerate(rows[:limit]):
|
|
body = ""
|
|
if i == 0:
|
|
body = (a.get("target_html")
|
|
or self.revisions.get(a.get("applied_revision_id") or 0, {}).get("html_content")
|
|
or self.revisions.get(a.get("target_revision_id") or 0, {}).get("html_content") or "")
|
|
out.append({**a, "created_at": a["remote_at"], "created_by": a.get("actor", ""), "html_content": body})
|
|
return out
|
|
|
|
# ── 상품 캐시 · 스냅샷 ──
|
|
def upsert_products(self, rows):
|
|
self._maybe_fail("upsert_products")
|
|
for r in rows:
|
|
self.products[int(r["product_no"])] = dict(r)
|
|
return len(rows)
|
|
|
|
def save_write_snapshot(self, product_no, section, data, *, replace=False):
|
|
self._maybe_fail("save_write_snapshot")
|
|
at = now_kst().isoformat(timespec="seconds")
|
|
current = self.snapshots.setdefault(int(product_no), {})
|
|
if section == "variants":
|
|
previous = (current.get("variants") or {}).get("data") or {}
|
|
data = store.merge_variant_snapshot(previous, data, at=at, replace=replace)
|
|
current[section] = {"data": data, "written_at": at}
|
|
|
|
def get_write_snapshot(self, product_no):
|
|
return dict(self.snapshots.get(int(product_no), {}))
|
|
|
|
def get_recent_product_snapshots(self, grace_minutes):
|
|
return {no: dict(snap["product"]) for no, snap in self.snapshots.items() if snap.get("product")}
|
|
|
|
# ── 예약 ──
|
|
def add_schedule(self, **fields):
|
|
sid = next(self._ids)
|
|
row = {"id": sid, "product_no": 131, "revision_id": None, "set_display": None, "set_selling": None,
|
|
"status": store.STATUS_PENDING, "retry_count": 0, "next_retry_at": None, "last_error": "",
|
|
"scheduled_at": now_kst() - timedelta(minutes=1), "lease_token": "", "lease_owner": "",
|
|
"lease_expires_at": None, "started_at": None}
|
|
row.update(fields)
|
|
self.schedules[sid] = row
|
|
return sid
|
|
|
|
def claim_due_schedule(self, *, now, owner, ttl_seconds):
|
|
due = [s for s in sorted(self.schedules.values(), key=lambda s: (s["scheduled_at"], s["id"]))
|
|
if s["status"] == store.STATUS_PENDING and s["scheduled_at"] <= now
|
|
and (s["next_retry_at"] is None or s["next_retry_at"] <= now)]
|
|
if not due:
|
|
return None
|
|
row = due[0]
|
|
row.update(status=store.STATUS_PROCESSING, lease_owner=owner, lease_token=uuid.uuid4().hex,
|
|
lease_expires_at=now_kst() + timedelta(seconds=ttl_seconds), started_at=now_kst(),
|
|
last_error="")
|
|
return dict(row)
|
|
|
|
def claim_expired_schedules(self, *, owner, ttl_seconds, legacy_minutes, limit=20):
|
|
out = []
|
|
for row in self.schedules.values():
|
|
if row["status"] != store.STATUS_PROCESSING:
|
|
continue
|
|
expired = row["lease_expires_at"] is not None and row["lease_expires_at"] < now_kst()
|
|
legacy = row["lease_expires_at"] is None and (row["started_at"] or now_kst()) < now_kst() - timedelta(minutes=legacy_minutes)
|
|
if expired or legacy:
|
|
row.update(lease_owner=owner, lease_token=uuid.uuid4().hex,
|
|
lease_expires_at=now_kst() + timedelta(seconds=ttl_seconds))
|
|
out.append(dict(row))
|
|
return out[:limit]
|
|
|
|
def renew_schedule_lease(self, schedule_id, token, *, ttl_seconds):
|
|
row = self.schedules.get(int(schedule_id))
|
|
if (not row or row["status"] != store.STATUS_PROCESSING or row["lease_token"] != token
|
|
or row["lease_expires_at"] <= now_kst()):
|
|
return False
|
|
row["lease_expires_at"] = now_kst() + timedelta(seconds=ttl_seconds)
|
|
return True
|
|
|
|
def finish_schedule(self, schedule_id, *, token, status, error="", next_retry_at=None, retry_count=None):
|
|
self._maybe_fail("finish_schedule")
|
|
row = self.schedules.get(int(schedule_id))
|
|
if not row or row["status"] != store.STATUS_PROCESSING or row["lease_token"] != token:
|
|
return False
|
|
row.update(status=store.normalize_schedule_status(status), last_error=error, next_retry_at=next_retry_at,
|
|
lease_token="", lease_owner="", lease_expires_at=None)
|
|
if retry_count is not None:
|
|
row["retry_count"] = retry_count
|
|
return True
|
|
|
|
def expire_schedule_lease(self, schedule_id):
|
|
self.schedules[int(schedule_id)]["lease_expires_at"] = now_kst() - timedelta(seconds=1)
|
|
|
|
def get_schedule(self, schedule_id):
|
|
return dict(self.schedules.get(int(schedule_id), {}))
|
|
|
|
def review_schedule(self, schedule_id, *, status, note, actor, action):
|
|
row = self.schedules.get(int(schedule_id))
|
|
if not row or row["status"] != store.STATUS_NEEDS_REVIEW:
|
|
return {}
|
|
row.update(status=status, last_error=note,
|
|
next_retry_at=now_kst() if status == store.STATUS_PENDING else None)
|
|
if status != store.STATUS_SUCCESS:
|
|
for a in self.attempts.values():
|
|
if a.get("schedule_id") == int(schedule_id) and a["status"] in (store.ATTEMPT_STARTED, store.ATTEMPT_UNKNOWN):
|
|
a.update(status=store.ATTEMPT_ABANDONED, resolved_by=actor)
|
|
return dict(row)
|
|
|
|
# ── 감사 ──
|
|
def log_audit(self, **fields):
|
|
self._maybe_fail("log_audit")
|
|
self.audits.append(fields)
|
|
|
|
def log_api_call(self, **_fields):
|
|
pass
|
|
|
|
|
|
class FakeConfig:
|
|
mall_id = "testmall"
|
|
shop_base = "https://testmall.cafe24.com"
|
|
|
|
def product_url(self, no):
|
|
return f"https://testmall.cafe24.com/product/detail.html?product_no={no}"
|
|
|
|
|
|
class FakeCafe24:
|
|
"""카페24 상품 API 대역 — 상세설명·진열/판매·updated_date 를 들고 있고,
|
|
읽기 지연(lag)·PUT 실패·응답 유실을 흉내낸다."""
|
|
|
|
def __init__(self, description: str = "<p>A</p>", *, product_no: int = 131):
|
|
self.product_no = product_no
|
|
self.state = {"product_no": product_no, "product_name": "상품", "product_code": "P000000B",
|
|
"description": description, "mobile_description": description,
|
|
"separated_mobile_description": "F", "display": "T", "selling": "T",
|
|
"updated_date": "2026-09-30T10:00:00+09:00"}
|
|
self.stale: dict | None = None # 설정하면 GET 이 이 값을 돌려준다(읽기 지연)
|
|
self.put_error: Exception | None = None
|
|
self.put_error_after_apply = False # True 면 반영한 뒤 오류(응답 유실)
|
|
self.calls: list[tuple] = []
|
|
self._clock = 0
|
|
self.option: dict = {"has_option": "F", "options": []}
|
|
self.variants: list = []
|
|
self.stale_option: dict | None = None # 설정하면 GET options 가 이 값을 돌려준다
|
|
self.stale_variants: list | None = None # 설정하면 GET variants 가 이 값을 돌려준다
|
|
self.hidden_variants = 0 # 새로 생긴 품목이 GET 에 늦게 나타나는 횟수
|
|
self.fail_variant_chunk = 0 # n번째 PUT variants 묶음을 실패시킨다
|
|
self.variant_error: Exception | None = None
|
|
self._variant_puts = 0
|
|
self._code_seq = 0
|
|
|
|
def external_edit(self, description: str) -> None:
|
|
"""카페24 관리자에서 사람이 직접 고친 것."""
|
|
self._tick()
|
|
self.state.update(description=description, mobile_description=description)
|
|
|
|
def _tick(self) -> None:
|
|
self._clock += 1
|
|
self.state["updated_date"] = (datetime(2026, 9, 30, 10, 0, tzinfo=KST)
|
|
+ timedelta(minutes=self._clock)).isoformat()
|
|
|
|
# Cafe24Client 흉내 — products.* 래퍼가 부르는 get/put
|
|
def get(self, path, **kw):
|
|
self.calls.append(("GET", path))
|
|
if path == f"/admin/products/{self.product_no}":
|
|
return {"product": dict(self.stale if self.stale is not None else self.state)}
|
|
out = self._options_call("GET", path, None)
|
|
return copy.deepcopy(out) if out is not None else {}
|
|
|
|
# ── 옵션 / 품목 ── (상태는 __init__ 에서 만든다)
|
|
|
|
def _new_code(self) -> str:
|
|
self._code_seq += 1
|
|
return f"P000000B{self._code_seq:04X}"
|
|
|
|
def _options_call(self, method, path, json):
|
|
no = self.product_no
|
|
req = (json or {}).get("request") or {}
|
|
if path == f"/admin/products/{no}/options":
|
|
if method == "GET":
|
|
return {"option": self.stale_option if self.stale_option is not None else self.option}
|
|
if method == "POST":
|
|
opt = req["options"][0]
|
|
values = [{"option_text": v["option_text"], "value_no": i + 1} for i, v in enumerate(opt["option_value"])]
|
|
self.option = {"has_option": "T", "option_type": "T", "option_list_type": "S",
|
|
"options": [{"option_name": opt["option_name"], "option_code": "O1",
|
|
"option_display_type": opt.get("option_display_type", "S"),
|
|
"option_value": values}]}
|
|
self.variants = [{"variant_code": self._new_code(), "options": [{"name": opt["option_name"], "value": v["option_text"]}],
|
|
"custom_variant_code": "", "additional_amount": "0.00", "display": "T", "selling": "T"}
|
|
for v in values]
|
|
return {"option": self.option}
|
|
if method == "PUT":
|
|
new = req["options"]
|
|
for gi, group in enumerate(new):
|
|
cur = self.option["options"][gi]
|
|
old_name = cur["option_name"]
|
|
old_values = [v["option_text"] for v in cur["option_value"]]
|
|
cur["option_name"] = group["option_name"]
|
|
vals = []
|
|
for vi, v in enumerate(group["option_value"]):
|
|
vals.append({"option_text": v["option_text"], "value_no": vi + 1,
|
|
"option_image_file": v.get("option_image_file", "")})
|
|
if vi < len(old_values):
|
|
for var in self.variants:
|
|
for o in var["options"]:
|
|
if o["name"] == old_name and o["value"] == old_values[vi]:
|
|
o["value"] = v["option_text"]
|
|
else:
|
|
self.variants.append({"variant_code": self._new_code(),
|
|
"options": [{"name": group["option_name"], "value": v["option_text"]}],
|
|
"custom_variant_code": "", "additional_amount": "0.00",
|
|
"display": "T", "selling": "T", "_new": True})
|
|
for var in self.variants:
|
|
for o in var["options"]:
|
|
if o["name"] == old_name:
|
|
o["name"] = group["option_name"]
|
|
cur["option_value"] = vals
|
|
return {"option": self.option}
|
|
if method == "DELETE":
|
|
self.option = {"has_option": "F", "options": []}
|
|
self.variants = []
|
|
return {"option": {"product_no": no}}
|
|
if path == f"/admin/products/{no}/variants":
|
|
if method == "GET":
|
|
if self.stale_variants is not None:
|
|
return {"variants": [dict(v) for v in self.stale_variants]}
|
|
shown = [dict(v) for v in self.variants]
|
|
if self.hidden_variants > 0:
|
|
self.hidden_variants -= 1
|
|
shown = [v for v in shown if not v.get("_new")]
|
|
return {"variants": shown}
|
|
if method == "PUT":
|
|
self._variant_puts += 1
|
|
if self.fail_variant_chunk and self._variant_puts == self.fail_variant_chunk:
|
|
raise self.variant_error or Cafe24ApiError("품목 수정 거절", status=422)
|
|
out = []
|
|
for r in (json or {}).get("requests") or []:
|
|
for var in self.variants:
|
|
if var["variant_code"] == r["variant_code"]:
|
|
var.update({k: v for k, v in r.items() if k != "variant_code"})
|
|
out.append(dict(var))
|
|
return {"variants": out}
|
|
if path.startswith(f"/admin/products/{no}/variants/") and method == "DELETE":
|
|
code = path.rsplit("/", 1)[1]
|
|
self.variants = [v for v in self.variants if v["variant_code"] != code]
|
|
return {"variant": {"variant_code": code}}
|
|
return None
|
|
|
|
def post(self, path, *, json=None, **kw):
|
|
self.calls.append(("POST", path, json))
|
|
out = self._options_call("POST", path, json)
|
|
return copy.deepcopy(out) if out is not None else {}
|
|
|
|
def delete(self, path, **kw):
|
|
self.calls.append(("DELETE", path))
|
|
out = self._options_call("DELETE", path, None)
|
|
return out if out is not None else {}
|
|
|
|
def put(self, path, *, json=None, **kw):
|
|
self.calls.append(("PUT", path, json))
|
|
if path != f"/admin/products/{self.product_no}":
|
|
out = self._options_call("PUT", path, json)
|
|
return copy.deepcopy(out) if out is not None else {}
|
|
if self.put_error is not None and not self.put_error_after_apply:
|
|
raise self.put_error
|
|
req = (json or {}).get("request", {})
|
|
self._tick()
|
|
for key in ("description", "display", "selling", "product_name", "separated_mobile_description"):
|
|
if key in req:
|
|
self.state[key] = req[key]
|
|
if "description" in req:
|
|
self.state["mobile_description"] = req["description"]
|
|
if self.put_error is not None:
|
|
raise self.put_error
|
|
return {"product": dict(self.state)}
|
|
|
|
def puts(self):
|
|
return [c for c in self.calls if c[0] == "PUT"]
|
|
|
|
|
|
class FakeApi:
|
|
def __init__(self, client):
|
|
self.client = client
|
|
self.config = FakeConfig()
|
|
|
|
|
|
def network_error(maybe_applied: bool = True) -> Cafe24ApiError:
|
|
return Cafe24ApiError("네트워크 오류 (ReadTimeout)", status=0, maybe_applied=maybe_applied)
|
|
|
|
|
|
class FakeFTP:
|
|
"""ftplib.FTP 대역 — RETR/STOR/RNFR·RNTO/DELE 를 메모리 파일로. 실패를 주입할 수 있다."""
|
|
|
|
files: dict[str, bytes] = {}
|
|
rename_mode = "overwrite" # overwrite | no_overwrite | unsupported
|
|
truncate_store = False # STOR 가 앞부분만 쓰고 끝난 것처럼
|
|
on_before_rename = None # 교체 직전 끼어들기(남이 원본을 바꿈 등)
|
|
log: list[tuple] = []
|
|
|
|
def __init__(self):
|
|
pass
|
|
|
|
def connect(self, host, port, timeout=None):
|
|
FakeFTP.log.append(("connect",))
|
|
|
|
def login(self, user, password):
|
|
pass
|
|
|
|
def set_pasv(self, value):
|
|
pass
|
|
|
|
def retrbinary(self, cmd, callback):
|
|
path = cmd.split(" ", 1)[1]
|
|
if path not in self.files:
|
|
raise ftplib.error_perm("550 No such file")
|
|
callback(self.files[path])
|
|
|
|
# 카페24 디자인 FTP 는 허용 확장자만 받는다(실물: `x.js.cf24tmp-…` STOR → 550).
|
|
ALLOWED_EXT = (".js", ".html", ".htm", ".css", ".json", ".txt", ".xml",
|
|
".jpg", ".jpeg", ".gif", ".png", ".svg", ".webp")
|
|
|
|
@classmethod
|
|
def _check_ext(cls, path):
|
|
if not path.lower().endswith(cls.ALLOWED_EXT):
|
|
raise ftplib.error_perm(f"550 {path}: Operation not permitted")
|
|
|
|
def storbinary(self, cmd, fp):
|
|
path = cmd.split(" ", 1)[1]
|
|
data = fp.read()
|
|
FakeFTP.log.append(("STOR", path))
|
|
self._check_ext(path)
|
|
self.files[path] = data[: len(data) // 2] if FakeFTP.truncate_store else data
|
|
|
|
def rename(self, src, dst):
|
|
FakeFTP.log.append(("RENAME", src, dst))
|
|
if FakeFTP.on_before_rename is not None:
|
|
hook, FakeFTP.on_before_rename = FakeFTP.on_before_rename, None
|
|
hook(self.files)
|
|
if FakeFTP.rename_mode == "unsupported":
|
|
raise ftplib.error_perm("502 RNFR not implemented")
|
|
if src not in self.files:
|
|
raise ftplib.error_perm("550 No such file")
|
|
self._check_ext(dst)
|
|
if dst in self.files and FakeFTP.rename_mode == "no_overwrite":
|
|
raise ftplib.error_perm("553 File exists")
|
|
self.files[dst] = self.files.pop(src)
|
|
|
|
def delete(self, path):
|
|
FakeFTP.log.append(("DELE", path))
|
|
self.files.pop(path, None)
|
|
|
|
def quit(self):
|
|
pass
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
@classmethod
|
|
def reset(cls, files):
|
|
cls.files = dict(files)
|
|
cls.rename_mode = "overwrite"
|
|
cls.truncate_store = False
|
|
cls.on_before_rename = None
|
|
cls.log = []
|
|
|
|
|
|
@contextmanager
|
|
def patched_ftp():
|
|
saved = ftplib.FTP
|
|
ftplib.FTP = FakeFTP
|
|
try:
|
|
yield FakeFTP
|
|
finally:
|
|
ftplib.FTP = saved
|