def test_article_view_detail_content(db, admin_client): """ Detail view should contain all expected content and relations. Note we are requesting with admin mode to be able to see a draft article to check for the "draft" CSS class. Also, this does not care about textual content (title, lead, content, etc..). """ picsou = AuthorFactory(first_name="Picsou", last_name="McDuck") AuthorFactory(first_name="Flairsou", last_name="Cresus") cat_1 = CategoryFactory(title="cat_1") CategoryFactory(title="cat_2") CategoryFactory(title="cat_3") ArticleFactory(title="Foo") article_2 = ArticleFactory(title="Bar") article_3 = ArticleFactory( title="Ping", fill_categories=[cat_1], fill_related=[article_2], fill_authors=[picsou], status=STATUS_DRAFT, pinned=True, featured=True, private=True, ) # Get detail HTML page response = admin_client.get(article_3.get_absolute_url(), {'admin': 1}) assert response.status_code == 200 # Parse HTML response to get content and relations dom = html_pyquery(response) container = dom.find("#lotus-content .article-detail")[0] categories = [ item.text for item in dom.find("#lotus-content .categories li a") ] authors = [item.text for item in dom.find("#lotus-content .authors li")] relateds = [ item.text for item in dom.find("#lotus-content .relateds li a") ] cover = dom.find("#lotus-content .cover img")[0].get("src") large_img = dom.find("#lotus-content .image img")[0].get("src") classes = sorted( [v for v in container.get("class").split() if v != "article-detail"]) assert categories == ["cat_1"] assert authors == ["Picsou McDuck"] assert relateds == ["Bar"] assert classes == ["draft", "featured", "pinned", "private"] assert cover == article_3.cover.url assert large_img == article_3.image.url
def test_article_view_detail_draft(db, admin_client, client): """ Draft article is only reachable for admin in 'admin mode'. """ user = AuthorFactory() instance = ArticleFactory(status=STATUS_DRAFT) # With default behavior a draft is not available no matter it's for an admin or not response = client.get(instance.get_absolute_url()) assert response.status_code == 404 response = admin_client.get(instance.get_absolute_url()) assert response.status_code == 404 # Admin mode behavior do not work for non admin users response = client.get(instance.get_absolute_url(), {'admin': 1}) assert response.status_code == 404 client.force_login(user) response = client.get(instance.get_absolute_url(), {'admin': 1}) assert response.status_code == 404 # Admin mode behavior only work for admin users response = admin_client.get(instance.get_absolute_url(), {'admin': 1}) assert response.status_code == 200
def create_authors(self): """ Create Author objects required length from factory. """ created = [] self.stdout.write( self.style.SUCCESS("* Creating {} authors".format(self.author_length)) ) for i in range(1, self.author_length+1): first_name = self.faker.unique.first_name() last_name = self.faker.unique.last_name() username = slugify("{} {}".format(first_name, last_name)) obj = AuthorFactory( first_name=first_name, last_name=last_name, username=username, flag_is_admin=True, ) self.stdout.write(" - Author: {}".format(obj.username)) created.append(obj) return created
def test_article_view_detail_private(db, client): """ Private article is reachable only for authenticated users. """ user = AuthorFactory() instance = ArticleFactory(private=True) response = client.get(instance.get_absolute_url()) assert response.status_code == 404 client.force_login(user) response = client.get(instance.get_absolute_url()) assert response.status_code == 200
def test_article_view_list_publication(db, admin_client, client, user_kind, client_kwargs, expected): """ View list should respect publication criterias (dates and state, private article and order. Tested again profiles: * non authenticated; * non authenticated trying to user admin mode; * authenticated user lambda; * authenticated user lambda trying to user admin mode; * admin without admin mode; * admin with admin mode; """ # Available Django clients as a dict to be able to switch on client_for = { "anonymous": client, "user": client, "admin": admin_client, } # Date references now = timezone.now() yesterday = now - datetime.timedelta(days=1) past_hour = now - datetime.timedelta(hours=1) tomorrow = now + datetime.timedelta(days=1) next_hour = now + datetime.timedelta(hours=1) # Create 10 articles (according to pagination limit) with different publication # parameters # Numerate titles to enforce ordering since articles share the exact same datetimes # which would lead to arbitrary order from a session to another ArticleFactory( title="01. draft yesterday", publish_date=yesterday.date(), publish_time=yesterday.time(), status=STATUS_DRAFT, ) ArticleFactory( title="02. published yesterday", publish_date=yesterday.date(), publish_time=yesterday.time(), ) ArticleFactory( title="03. published yesterday, ended one hour ago", publish_date=yesterday.date(), publish_time=yesterday.time(), publish_end=past_hour, ) ArticleFactory( title="04. published past hour", publish_date=past_hour.date(), publish_time=past_hour.time(), ) ArticleFactory( title="05. pinned, published past hour", publish_date=past_hour.date(), publish_time=past_hour.time(), pinned=True, ) ArticleFactory( title="06. featured, published past hour", publish_date=past_hour.date(), publish_time=past_hour.time(), featured=True, ) ArticleFactory( title="07. private, published past hour", publish_date=past_hour.date(), publish_time=past_hour.time(), private=True, ) ArticleFactory( title="08. published past hour, end next hour", publish_date=past_hour.date(), publish_time=past_hour.time(), publish_end=next_hour, ) ArticleFactory( title="09. publish next hour", publish_date=next_hour.date(), publish_time=next_hour.time(), ) ArticleFactory( title="10. publish next hour, end tomorrow", publish_date=next_hour.date(), publish_time=next_hour.time(), publish_end=tomorrow, ) # Select the right client to use for user kind enabled_client = client_for[user_kind] # We have to force authenticated user (non admin) if user_kind == "user": user = AuthorFactory() client.force_login(user) # Get all available items from HTML page urlname = "lotus:article-index" response = enabled_client.get(reverse(urlname), client_kwargs) assert response.status_code == 200 # Parse HTML dom = html_pyquery(response) items = dom.find("#lotus-content .article-list-container .list .item") # Get useful content from list items content = [] for item in items: title = item.cssselect("h3 > a")[0].text # Drop item class since it's useless for test classes = [v for v in item.get("class").split() if v != "item"] content.append([title, classes]) assert content == expected
def test_author_manager(db): """ Author manager should be able to get all author which have published articles and Author method "published_articles" should return all published articles for an Author object. """ now = timezone.now() tomorrow = now + datetime.timedelta(days=1) # Today 5min sooner to avoid shifting with pytest and factory delays today = now - datetime.timedelta(minutes=5) # Some authors picsou = AuthorFactory(username="******") donald = AuthorFactory(username="******") flairsou = AuthorFactory(username="******") # Some articles ArticleFactory( slug="Klondike", publish_date=today.date(), publish_time=today.time(), fill_authors=[picsou], ) ArticleFactory( slug="DuckCity", publish_date=today.date(), publish_time=today.time(), fill_authors=[picsou, donald], ) ArticleFactory( slug="Tomorrow", publish_date=tomorrow.date(), publish_time=tomorrow.time(), fill_authors=[donald], ) # Check for author which have published articles q_authors_published = Author.lotus_objects.get_published() data = queryset_values(q_authors_published, names=["username"], orders=["username"]) assert data == [{"username": "******"}, {"username": "******"}] # Check for published articles for each author assert queryset_values(flairsou.articles.get_published()) == [] assert queryset_values(donald.articles.get_published()) == [ { "language": "en", "slug": "DuckCity" }, ] assert queryset_values(picsou.articles.get_published()) == [ { "language": "en", "slug": "DuckCity" }, { "language": "en", "slug": "Klondike" }, ]
def test_author_creation(db): """ Factory should correctly create a new object without any errors. """ author = AuthorFactory(username="******") assert author.username == "foobar"