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 create(request: Request) -> Url: """Get an URL.""" body = request.openapi_validated.body url = Url( title=body.title, description=body.description, href=body.href, slug=getattr(body, "slug", None) or slugify(body.title), author=request.user, ) request.db.add(url) request.db.flush() request.response.status_code = 201 return url
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 add_urls(db: Session) -> None: """Add demo urls to db.""" foo = Url( id=URL_FOO_ID, slug="foo", title="Foö", description="Foö desc", href="https://glicksoftware.com", author=User.by_username("one", db=db), created=datetime(2019, 1, 1, 1, 1, 1), updated=datetime(2019, 2, 2, 2, 2, 2), ) db.add(foo) logger.info("Url added", slug=foo.slug) # bar = Article( # id=ARTICLE_BAR_ID, # slug="bar", # title="Bär", # description="Bär desc", # body="Bär body", # author=User.by_username("one", db=db), # created=datetime(2019, 3, 3, 3, 3, 3), # updated=datetime(2019, 4, 4, 4, 4, 4), # ) # db.add(bar) # logger.info("Article added", slug=bar.slug) # # Postman tests require this user to have at least one article # johnjacob = Article( # id=ARTICLE_JOHNJACOB_ID, # slug="i-am-johnjacob", # title="I am John Jacob", # description="johnjacob desc", # body="johnjacob body", # author=User.by_username("johnjacob", db=db), # created=datetime(2019, 5, 5, 5, 5, 5), # updated=datetime(2019, 6, 6, 6, 6, 6), # ) # db.add(johnjacob) # logger.info("Article added", slug=johnjacob.slug) db.flush()
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)