def test_DELETE_url(testapp: TestApp, db: Session, democontent: None) -> None: """Test DELETE /api/urls/{slug}.""" assert Url.by_slug("foo", db=db) is not None testapp.delete( "/api/urls/foo", headers={"Authorization": f"Token {USER_ONE_JWT}"}, status=200, ) assert Url.by_slug("foo", db=db) is None
def delete(request: Request) -> None: """Delete an URL.""" url = object_or_404( Url.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) request.db.delete(url) return None
def update(request: Request) -> Url: """Update an URL.""" body = request.openapi_validated.body url = object_or_404( Url.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) if getattr(body, "title", None): url.title = body.title if getattr(body, "description", None): url.description = body.description if getattr(body, "href", None): url.href = body.href return url
def test_json_renderer(db: Session, democontent: None) -> None: """Test that Url is correctly rendered for an OpenAPI JSON response.""" user = User.by_username("two", db=db) url = Url.by_slug("foo", db=db) request = DummyRequest() request.user = user renderer = json_renderer() output = renderer(None)(url, {"request": request}) assert json.loads(output) == { "createdAt": "2019-01-01T01:01:01.000Z", "description": "Foö desc", "href": "https://glicksoftware.com", "slug": "foo", "title": "Foö", "updatedAt": "2019-02-02T02:02:02.000Z", }
def redirect(request: Request) -> None: """Redirect to target.""" slug = request.matchdict["slug"] url = object_or_404(Url.by_slug(slug, db=request.db)) raise HTTPFound(url.href)
def url(request: Request) -> Url: """Get an URL.""" url = object_or_404( Url.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) return url
def test_by_shortcuts(db: Session, democontent: None) -> None: """Test that by_* shortcuts work.""" assert Url.by_slug("foo", db) == Url.by_id(URL_FOO_ID, db)