def test_DELETE_article(testapp: TestApp, db: Session, democontent: None) -> None: """Test DELETE /api/articles/{slug}.""" assert Article.by_slug("foo", db=db) is not None testapp.delete( "/api/articles/foo", headers={"Authorization": f"Token {USER_ONE_JWT}"}, status=200, ) assert Article.by_slug("foo", db=db) is None
def add_articles(db: Session) -> None: """Add demo articles to db.""" foo = Article( id=ARTICLE_FOO_ID, slug="foo", title="Foö", description="Foö desc", body="Foö body", author=User.by_username("one", db=db), created=datetime(2019, 1, 1, 1, 1, 1), updated=datetime(2019, 2, 2, 2, 2, 2), tags=[Tag(name="dogs"), Tag(name="cats")], comments=[ Comment( id=99, body="I like this!", author=User.by_username("two", db=db), created=datetime(2019, 7, 7, 7, 7, 7), updated=datetime(2019, 8, 8, 8, 8, 8), ) ], ) db.add(foo) logger.info("Article 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 unfavorite(request: Request) -> SingleArticleResponse: """Unfavorite an article.""" article = object_or_404( Article.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) request.user.unfavorite(article) return {"article": article}
def delete(request: Request) -> None: """Delete an article.""" article = object_or_404( Article.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) request.db.delete(article) return None
def test_json_renderer(db: Session, democontent: None) -> None: """Test that Article is correctly rendered for an OpenAPI JSON response.""" user = User.by_username("two", db=db) article = Article.by_slug("foo", db=db) request = DummyRequest() request.user = user renderer = json_renderer() output = renderer(None)(article, {"request": request}) assert json.loads(output) == { "author": { "bio": None, "following": True, "image": None, "username": "******" }, "body": "Foö body", "createdAt": "2019-01-01T01:01:01.000Z", "description": "Foö desc", "favorited": False, "favoritesCount": 0, "slug": "foo", "tagList": ["dogs", "cats"], "title": "Foö", "updatedAt": "2019-02-02T02:02:02.000Z", }
def comments(request: Request) -> MultipleCommentsResponse: """Get recent comments on the given article.""" article = object_or_404( Article.by_slug( request.openapi_validated.parameters["path"]["slug"], db=request.db ) ) return {"comments": article.comments}
def create(request: Request) -> SingleCommentResponse: """Get an article.""" article = object_or_404( Article.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) body = request.openapi_validated.body comment = Comment(body=body.comment.body, article=article, author=request.user) article.comments.append(comment) return {"comment": comment}
def delete(request: Request) -> None: """Remove a comment.""" object_or_404( Article.by_slug( request.openapi_validated.parameters["path"]["slug"], db=request.db ) ) comment = object_or_404( Comment.by_id(request.openapi_validated.parameters["path"]["id"], db=request.db) ) request.db.delete(comment) return None
def create(request: Request) -> SingleArticleResponse: """Get an article.""" body = request.openapi_validated.body article = Article( slug=slugify(body.article.title), title=body.article.title, description=body.article.description, body=body.article.body, author=request.user, tags=[Tag(name=t) for t in getattr(body.article, "tagList", [])], ) request.db.add(article) request.db.flush() request.response.status_code = 201 return {"article": article}
def update(request: Request) -> SingleArticleResponse: """Update an article.""" body = request.openapi_validated.body article = object_or_404( Article.by_slug(request.openapi_validated.parameters["path"]["slug"], db=request.db)) if getattr(body.article, "title", None): article.title = body.article.title if getattr(body.article, "description", None): article.description = body.article.description if getattr(body.article, "body", None): article.body = body.article.body return {"article": article}
def update(request: Request) -> SingleArticleResponse: """Update an article.""" body = request.openapi_validated.body article = object_or_404( Article.by_slug( request.openapi_validated.parameters["path"]["slug"], db=request.db ) ) if body["article"].get("title"): article.title = body["article"]["title"] if body["article"].get("description"): article.description = body["article"]["description"] if body["article"].get("body"): article.body = body["article"]["body"] return {"article": article}
def test_favorite(db: Session, democontent: None) -> None: """Test favoriting an article.""" user = User.by_username("one", db) article = Article.by_slug("foo", db) assert article not in user.favorites # type: ignore user.favorite(article) # type: ignore assert article in user.favorites # type: ignore user.favorite(article) # type: ignore # again, to test idempotence assert article in user.favorites # type: ignore user.unfavorite(article) # type: ignore assert article not in user.favorites # type: ignore user.unfavorite(article) # type: ignore # again, to test idempotence assert article not in user.favorites # type: ignore
def test_json_renderer(db: Session, democontent: None) -> None: """Test that Comment is correctly rendered for an OpenAPI JSON response.""" user = User.by_username("two", db=db) article = Article.by_slug("foo", db=db) comment = article.comments[0] # type: ignore request = DummyRequest() request.user = user renderer = json_renderer() output = renderer(None)(comment, {"request": request}) assert json.loads(output) == { "id": 99, "body": "I like this!", "createdAt": "2019-07-07T07:07:07.000Z", "updatedAt": "2019-08-08T08:08:08.000Z", "author": {"username": "******", "bio": None, "image": None, "following": False}, }
def test_by_shortcuts(db: Session, democontent: None) -> None: """Test that by_* shortcuts work.""" assert Article.by_slug("foo", db) == Article.by_id(ARTICLE_FOO_ID, db)