mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-04 08:13:02 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
"""Tests for ``PATCH /api/devices/<id>`` body validation.
|
|
|
|
The validation rejects bad ``name``/``description`` values before any DB
|
|
access, so the 400 cases exercise the handler directly without Postgres.
|
|
A valid PATCH is covered with the repository + session mocked.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import pytest
|
|
from flask import Flask
|
|
|
|
from docsgpt.api.devices import routes as routes_module
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
return Flask(__name__)
|
|
|
|
|
|
def _call_update(app: Flask, body, device_id: str = "dev_abc"):
|
|
with app.test_request_context(
|
|
f"/api/devices/{device_id}", method="PATCH", json=body,
|
|
):
|
|
from flask import request as flask_request
|
|
|
|
flask_request.decoded_token = {"sub": "user_abc"}
|
|
return routes_module.update_device(device_id)
|
|
|
|
|
|
def test_patch_rejects_null_name(app):
|
|
response = _call_update(app, {"name": None})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "invalid_name"
|
|
|
|
|
|
def test_patch_rejects_blank_name(app):
|
|
response = _call_update(app, {"name": " "})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "invalid_name"
|
|
|
|
|
|
def test_patch_rejects_non_string_name(app):
|
|
response = _call_update(app, {"name": 123})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "invalid_name"
|
|
|
|
|
|
def test_patch_rejects_null_description(app):
|
|
response = _call_update(app, {"description": None})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "invalid_description"
|
|
|
|
|
|
def test_patch_rejects_non_string_description(app):
|
|
response = _call_update(app, {"description": ["x"]})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "invalid_description"
|
|
|
|
|
|
def test_patch_still_validates_approval_mode(app):
|
|
response = _call_update(app, {"approval_mode": "bogus"})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "invalid_approval_mode"
|
|
|
|
|
|
def test_patch_empty_body_is_no_fields(app):
|
|
response = _call_update(app, {})
|
|
assert response.status_code == 400
|
|
assert response.get_json()["error"] == "no_fields"
|
|
|
|
|
|
def test_patch_valid_name_trimmed_and_persisted(app, monkeypatch):
|
|
"""A valid PATCH trims the name and writes the stripped value."""
|
|
captured: dict = {}
|
|
|
|
class _Repo:
|
|
def __init__(self, _conn):
|
|
pass
|
|
|
|
def update(self, _device_id, _user_id, fields):
|
|
captured.update(fields)
|
|
return True
|
|
|
|
def get(self, device_id, user_id=None):
|
|
return {"id": device_id, "name": fields_name(), "description": ""}
|
|
|
|
def fields_name():
|
|
return captured.get("name")
|
|
|
|
class _Sess:
|
|
def __enter__(self):
|
|
return None
|
|
|
|
def __exit__(self, *a):
|
|
return False
|
|
|
|
monkeypatch.setattr(routes_module, "DevicesRepository", _Repo)
|
|
monkeypatch.setattr(routes_module, "db_session", _Sess)
|
|
monkeypatch.setattr(
|
|
"docsgpt.api.devices.pairing._upsert_remote_device_user_tool",
|
|
lambda conn, **kwargs: None,
|
|
)
|
|
|
|
response = _call_update(app, {"name": " laptop ", "description": " box "})
|
|
assert response.status_code == 200
|
|
assert captured["name"] == "laptop"
|
|
assert captured["description"] == "box"
|