def test_displays_only_published_pages(self): user = User.objects.create_user(username="******", password="******") page1 = user.add_page(ContentPage(title="My First Entry")) page2 = user.add_page(ContentPage(title="My Second Entry")) pub_page = page2.publish() self.assertEqual(Page.objects.count(), 3) self.client.login(username="******", password="******") response = self.client.get(reverse("astormain:home")) self.assertNotContains(response, page1.specific.title) self.assertContains(response, page2.specific.title)
def test_for_adding_comment_to_page(self): user = User.objects.create_user(username="******", password="******") page = user.add_page(ContentPage(title="My First Entry")) comment = page.add_comment(body="Very nice entry.") page.refresh_from_db() self.assertEqual(page.comments.count(), 1) self.assertEqual(page.comments.all()[0].body, comment.body)
def test_displays_titles_of_the_latest_analyses(self): user = User.objects.create(username="******", password="******") page = user.add_page(instance=ContentPage(title="My First Entry")) pub_page = page.publish() self.client.login(username="******", password="******") response = self.client.get(reverse("astormain:home")) self.assertContains(response, page.specific.title)
def test_raises_validation_error_when_page_not_saved(self): user = User(username="******") user.id = 5 user.pk = 5 page = ContentPage(title="Page") form = CommentForm(page=page,author=user) with self.assertRaises(ValidationError): form.is_valid()
def test_add_comment_accepts_instance(self): user = User.objects.create_user(username="******", password="******") page = user.add_page(ContentPage(title="My First Entry")) comment = Comment.objects.create(body="Very nice entry.", author=user) page.add_comment(comment) page.refresh_from_db() self.assertEqual(page.comments.count(), 1) self.assertEqual(page.comments.all()[0].body, comment.body)
def test_can_add_tags_to_page(self): user = User.objects.create_user(username="******", password="******") page = user.add_page(ContentPage(title="My First Entry")) page.tags.add("entry", "intro", "astor") page = Page.objects.first().specific self.assertEqual(page.tags.count(), 3) tags = [tag.name for tag in page.tags.all()] self.assertCountEqual(tags, ["entry", "intro", "astor"])
def create_user_with_analyses(self, n=5): user = User.objects.create_user(username="******", password="******") for i in range(n): user.add_page(ContentPage( title="Test Title #{:d}".format(i), abstract="Test Abstract #{:d}".format(i) )) return user
def test_saved_user_and_page_make_validation_successfull(self): user = User(username="******") user.id = 5 user.pk = user.id page = ContentPage(title="Page") page.id = 3 page.pk = page.id form = CommentForm(page=page, author=user) self.assertTrue(form.is_valid())
def create_user_with_page(self, **kwargs): user = User.objects.create_user( username=kwargs.get("username", "Test"), password=kwargs.get("password", "test") ) page = user.add_page( ContentPage(**(kwargs or dict(title="My First Page"))) ) return user, page
def test_save_adds_comment_to_page_and_sets_author(self): user = User.objects.create_user(username="******", password="******") page = user.add_page(ContentPage(title="My First Page")) form = CommentForm(page=page, author=user, data={"body": "My First Comment"}) self.assertTrue(form.is_valid()) form.save() self.assertEqual(page.comments.count(), 1) comment = page.comments.first() self.assertEqual(comment.author, user) self.assertEqual(comment.body, "My First Comment")
def generate_pages_for_user(self, user, n=10): titles = [ ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(20)) for _ in range(n) ] pages = [ user.add_page(instance=ContentPage(title=title)) for title in titles ] return pages
def test_use_tags_names_when_populating_form(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage(title="It's time!!!")) page.tags.add("one", "two", "three") response = self.client.get( reverse("astoraccount:page_edit", kwargs={"pk": page.pk})) self.assertCountEqual( list( map(lambda x: x.strip(), response.context["form"]["tags"].value().split(","))), ["one", "two", "three"])
def test_for_publishing_page(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage()) self.client.post( reverse("astoraccount:page_edit", kwargs={"pk": page.pk}), { "title": "First Edit Ever", "abstract": "Simple Page", "body": "Nice body", "action_type": "publish" }) self.assertEqual(Page.objects.count(), 2) page.refresh_from_db() self.assertIsNotNone(page.published_page)
def test_for_saving_data_from_form(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage()) self.client.post( reverse("astoraccount:page_edit", kwargs={"pk": page.pk}), { "title": "First Edit Ever", "abstract": "Simple Page", "body": "Nice body" }) page = ContentPage.objects.get(id=page.id) self.assertEqual(page.specific.title, "First Edit Ever") self.assertEqual(page.specific.abstract, "Simple Page") self.assertEqual(page.specific.body, "Nice body")
def test_save_adds_reply_to_comment_and_sets_author(self): user = User.objects.create_user(username="******", password="******") page = user.add_page(ContentPage(title="My First Page")) comment = page.add_comment(body="Comment #1", author=user) form = ReplyForm(comment=comment, author=user, data={"body": "Reply #1"}) self.assertTrue(form.is_valid()) form.save() comment.refresh_from_db() self.assertEqual(comment.replies.count(), 1) reply = comment.replies.first() self.assertEqual(reply.body, "Reply #1") self.assertEqual(reply.author, user) self.assertIsNone(reply.page) self.assertEqual(reply.parent, comment)
def test_specific_returns_correct_type(self): page = ContentPage(title="Test") page.save() self.assertIs(type(page.specific), ContentPage)
def test_for_passing_correct_form_to_tempalte2(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage()) response = self.client.get( reverse("astoraccount:page_edit", args=(page.id, ))) self.assertEqual(type(response.context["form"]), ContentPageForm)
def test_redirects_to_404_when_invalid_page_number(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage()) response = self.client.get( reverse("astoraccount:page_edit", kwargs={"pk": page.pk + 2})) self.assertRedirects(response, reverse("astoraccount:404"))
def test_publish_sets_page_as_live(self): user = User.objects.create_user(username="******", password="******") page1 = user.add_page(ContentPage(title="My First Entry")) page2 = user.add_page(ContentPage(title="My Second Entry")) pub_page2 = page2.publish() self.assertTrue(pub_page2.live)
def test_publish_users_page(self): user = User.objects.create_user(username="******", password="******") page1 = user.add_page(instance=ContentPage(title="My First Entry")) page2 = user.add_page(instance=ContentPage(title="F**K")) pub_page = page2.publish() self.assertEqual(ContentPage.objects.count(), 3)
def create_user_and_page(self): user = User.objects.create_user(username="******", password="******") page = user.add_page(ContentPage(title="My First Entry")) return user, page
def test_can_read_entries_of_other_user(self): # Spider told her favourite firend Fly about the new website he had # discovered and his first publication. user = User.objects.create_user(username="******", password="******") page = ContentPage( title="Spiders eat flies", abstract="Short dispute over importance of proper eating.", body="For the generations, our ancestors hunted (...)") user.add_page(page) page.publish() # Fly visits ASTOR webpage. self.browser.get(self.live_server_url) # That is not the prettiest webpage she had ever seen, but it's very # readable and easy to navigate. She spots the section 'Newest Entries'. section = self.find_section_with_the_newest_entries() # To her suprise the first entry has the same title as Spider's article. # 'It has to be it' - she thinks. self.check_for_entry_in_newest_section(page.title) # She clicks the title. entry = self.browser.find_element_by_link_text(page.title) entry.click() # The page updates and she can see a header at the top of page with # the title. title = self.browser.find_element_by_id("id_page_title") self.assertEqual(title.text, page.title) # There is also short abstract abstract = self.browser.find_element_by_id("id_page_abstract") self.assertEqual(abstract.text, page.abstract) # And the content of the article body = self.browser.find_element_by_id("id_page_body") self.assertEqual(body.text, page.body) user = User.objects.create_user(username="******", password="******") page = ContentPage( title="The superiority of flies over spiders", abstract="Who can fly, can reach the sky", body="Flies can fly, Flies can walk, spiders are stupid (...)") user.add_page(page) page.publish() # Fly reads all of the infromation, and becomes a little bit worry of # Spider and his intentation. "Can it be a trap?" - she thinks. She # immedietaly clicks "ASTOR" link which redirects her to main page. self.browser.find_element_by_link_text("ASTOR").click() # Main page change a little bit and there is one more entry in the # newest section. section = self.find_section_with_the_newest_entries() entries = section.find_elements_by_tag_name("li") self.assertEqual(len(entries), 2) # Thre is a new entry at the top with ttitle: "The superiority of flies # over spiders" self.check_for_entry_in_newest_section( "The superiority of flies over spiders") # The entry was published by 'SuperFly'. authors = self.browser.find_elements_by_class_name("entry_author") self.assertIn("SuperFly", [item.text for item in authors])
def test_renders_correct_template(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage()) response = self.client.get( reverse("astoraccount:page_edit", kwargs={"pk": page.pk})) self.assertTemplateUsed(response, "astoraccount/page_edit.html")
def test_passes_recent_edits_to_template(self): user = self.create_and_login_user() page = user.add_page(instance=ContentPage(title="My First Page")) response = self.client.get(reverse("astoraccount:index")) self.assertEqual(response.context["recent_edits"][0], page)
def test_published_new_page_has_the_same_user(self): user = User.objects.create(username="******", password="******") page = user.add_page(instance=ContentPage(title="Test")) new_page = page.publish() self.assertEqual(user, new_page.user)
def test_can_add_tags(self): ''' Fly has just been told by Spider that she can add tags to her analyses what enable to group them and make them easier searchable. ''' user = User.objects.create_user(username="******", password="******") data = dict( title="Flies are awesome!", abstract="The past and future of our species.", body="If not for spiders, flies would dominated the universe " "a long time ago.") page = user.add_page(ContentPage(**data)) page.publish() # Fly visits her latest favourite page ASTOR and logs into her account. self.browser.get(self.live_server_url) self.login_user(username="******", password="******") self.wait_for_page_which_url_ends_with("/account/") # She is already very familier with the structue of the main panel and # immedietaly clicks 'Analyses' from the left sidebar. self.browser.find_element_by_link_text("Analyses").click() self.wait_for_page_which_url_ends_with("/account/analyses/") # Fly sees list of her analyses. There is only one now, but she can # always create new one with big green button 'Add New Analysis' self.browser.find_element_by_link_text("Add New Analysis") self.assertEqual( len(self.browser.find_elements_by_class_name("analysis")), 1) # She can see that her analysis is 'LIVE'. There is a status which # say this. analysis = self.find_analysis_with_title(data["title"]) self.assertIsNotNone(analysis) tds = analysis.find_elements_by_tag_name("td") self.assertTrue(any("LIVE" in td.text for td in tds)) ## Remember befor switching to new window/tab. window_account = self.browser.window_handles[0] # It turns out that 'LIVE' is a button, so she clicks it. self.browser.find_element_by_link_text("LIVE").click() # The new page opens and there is her article. WOW self.assertEqual(len(self.browser.window_handles), 2) self.browser.switch_to_window(self.browser.window_handles[1]) self.wait_for_page_with_text_in_url("da/fly") abstract = self.browser.find_element_by_id("id_abstract") self.assertEqual(data["abstract"], abstract.text) analysis_url = self.browser.current_url # She closes the window and goes back to her account page. self.browser.close() self.browser.switch_to_window(window_account) # Time to add some tags. Fly clicks edit button below the title of # her analysis. analysis.find_element_by_link_text("Edit").click() # The page changes to one with analysis editor. # 'I love this editor' - she thinks. self.wait_for_element_with_id("id_title") # There are three tabs, and she switches to ''. self.browser.find_element_by_link_text("Promote").click() # Fly spots input box for tags. There she has to put tags she # wants to ascribe to this analysis. inputbox_tags = self.browser.find_element_by_id("id_tags") ## Make input box visible (otherwise error: Element is not visible) self.browser.execute_script( "document.getElementById('id_tags').style.display='inline';") import time time.sleep(1) # She enters names of three tags. inputbox_tags.send_keys("flies, spiders, flying") # And then clicks 'Save & Publish' button. self.browser.find_element_by_xpath( "//button[@type='submit' and @value='publish']").click() # Message appears which confirms successful save & publish of analysis. self.check_for_message("The analysis has been saved and published.") # Fly wants to see whether the tags can be see in the 'live' version # of her analysis. She memorized the url of the page and go there # straight away. self.browser.get(analysis_url) self.wait_for_page_with_text_in_url("da/fly") # Now she can see below the title three words. tags = self.find_tags() self.assertEqual(len(tags), 3) self.assert_tags(["flies", "spiders", "flying"])
def test_for_adding_comments_to_analysis(self): ''' Spider has just heard about new analysis prepered by Fly. He goes to her page and adds comment. ''' fly = User.objects.create_user(username="******", password="******") spider = User.objects.create_user(username="******", password="******") data = dict( title="Hard decision!", abstract="What brings the future?", body="Every one from time to time has to make some decision. " "Some of them can be especially hard.") page1 = fly.add_page(ContentPage(**data)).publish() data = dict( title="Flies are awesome!", abstract="The past and future of our species.", body="If not for spiders, flies would dominated the universe " "a long time ago.") page2 = fly.add_page(ContentPage(**data)).publish() # Spider vistis fly profile page. self.browser.get( self.live_server_url + reverse("astormain:profile", kwargs={"slug": fly.slug})) self.wait_for_page_with_text_in_url("da/fly") # He can see big heading containing 'Fly' heading = self.browser.find_element_by_id("id_profile_heading") self.assertIn("Fly", heading.text) # 'It has to be her profile' - he thinks. There are two analyses. analyses = self.browser.find_elements_by_class_name("analysis-wrapper") self.assertEqual(len(analyses), 2) # The title of first analysis is 'Flies are awesome!' title = analyses[0].find_element_by_class_name("analysis-title") self.assertEqual(title.text, page2.title) # Spider thinks it can be very interesting and clicks the title. self.browser.find_element_by_link_text(page2.title).click() # The page updates and he can see the analysis self.wait_for_page_which_url_ends_with("pages/{:d}".format(page2.id)) title = self.browser.find_element_by_tag_name("h3") self.assertEqual(title.text, page2.title) # He didn't like the what he had read. He wants to leave a comment. # There is comment section but he has to log in to add comments. element = self.browser.find_element_by_id("id_comment_form") self.assertIn("Only authenticated users can leave comments. ", element.text) fly_page_url = self.browser.current_url # Spider clicks 'login' and it's being redirect to login page where he # enters his username and password. self.browser.find_element_by_link_text("login").click() self.wait_for_page_with_text_in_url("account/login/") self.login_user(username="******", password="******", open_login_page=False, test_for_login=False) import time time.sleep(3) # After successful login he is automatically redirected to last page. self.assertEqual(self.browser.current_url, fly_page_url) # The previous information disappeared and now he can see form to # enter comment. inputbox_body = self.browser.find_element_by_id("id_body") # Spider writes his comment and clicks 'Send Comment' inputbox_body.send_keys( "If not for spiders, you will destroy the Earth.") self.browser.find_element_by_xpath( "//input[@type='submit' and @value='Send Comment']").click() # The page updates and now he can see his comment below the analysis self.wait_for_element_with_id("id_comments") comments = self.browser.find_elements_by_class_name("comment-body") self.assertTrue(any("Spider" in cmt.text for cmt in comments)) self.assertTrue(any("Earth" in cmt.text for cmt in comments))
def test_add_page_adds_page_to_user_pages_hierarchy(self): user = User.objects.create_user(username="******", password="******") page = ContentPage(title="My First Page") user.add_page(page) self.assertEqual(user.pages.count(), 1)
def test_raises_validation_error_when_invalid_author(self): form = CommentForm(page=ContentPage(title="Test"), author=None) with self.assertRaises(ValidationError): form.is_valid()