def testAuthorCanDeleteOwnPost(self): # Sign in as "Valid User" and navigate to the post's page self.testapp.set_cookie('user', create_user_cookie('Valid User')) response = self.testapp.request('/posts/%d' % self.initial_post_key.integer_id()) # Check that a delete button is available and points to the # delete endpoint delete_button = response.html.select('.post-delete') self.assertEqual(len(delete_button), 1) delete_response = response.click(description="Delete", href="/posts/\d+/delete") # Follow the delete redirect to main page and check that deleted # post isn't listed main_page = delete_response.follow() posts = main_page.html.select('.post') for post in posts: self.assertNotEqual(post['id'], self.initial_post_key.integer_id()) # Check that the deleted post's page is unavailable deleted_post = self.testapp.request('/posts/%d' % self.initial_post_key.integer_id(), expect_errors=404) self.assertEqual(deleted_post.status_int, 404)
def testAuthorCanEditOwnPost(self): # Sign in as "Valid User" and navigate to the post's page self.testapp.set_cookie('user', create_user_cookie('Valid User')) response = self.testapp.request('/posts/%d' % self.initial_post_key.integer_id()) # Check that an edit button is available and redirects to the edit page edit_button = response.html.select('.post-edit') self.assertEqual(len(edit_button), 1) edit_response = response.click(description="Edit", href="/posts/\d+/edit") # Perform an edit edit_response.form['title'] = "EDITED TITLE" edit_response.form['content'] = "EDITED CONTENT" form_redirect = edit_response.form.submit() edited_post_page = form_redirect.follow() # Check that the edit has updated title and content, but not # submission info edited_title = edited_post_page.html.select( '.post-title')[0].get_text() edited_submission_info = edited_post_page.html.select( '.post-submitter')[0].get_text() unedited_submission_info = response.html.select( '.post-submitter')[0].get_text() edited_content = edited_post_page.html.select( '.post-content')[0].get_text() self.assertEqual(edited_title, "EDITED TITLE") self.assertEqual(edited_content, "EDITED CONTENT") self.assertEqual(unedited_submission_info, edited_submission_info)
def testUserCanDeleteTheirOwnComment(self): self.testapp.set_cookie('user', create_user_cookie('Test_User_01')) user_01_comment = "/posts/%d/comments/%d" % (self.test_post_id, self.comment_ids[0]) delete_response = self.testapp.request(user_01_comment + "/delete") for comment in delete_response.html.select('.comment'): self.assertNotEqual(int(comment['id']), self.comment_ids[0])
def testUnlikedPostCanBeLiked(self): # Ensure that post has been liked post = Post.get_by_id(self.initial_post_to_like_key.integer_id()) post.liked_by.append("Valid Liker") post.put() # Ensure that the Unlike button appears self.testapp.set_cookie('user', create_user_cookie('Valid Liker')) response = self.testapp.request( '/posts/%d' % self.initial_post_to_like_key.integer_id()) like_button = response.html.select('.post-like')[0] self.assertEqual(like_button.get_text(), 'Unlike') # Click the Unlike button an ensure the like counter decrements unlike_response = response.click(description="Unlike").follow() like_counter = unlike_response.html.select('.post-likes')[0].get_text() self.assertEqual(int(like_counter), 1) # Now ensure that there is a Like button and that re-liking # increments the counter re_like_response = unlike_response.click(description="Like").follow() re_like_counter = re_like_response.html.select( '.post-likes')[0].get_text() self.assertTrue(int(re_like_counter) == int(like_counter) + 1)
def testNonAuthorCannotDeletePostViaUrl(self): # Sign in as "Invalid User" and navigate to the post's /delete page self.testapp.set_cookie('user', create_user_cookie('Invalid User')) response = self.testapp.request('/posts/%d/delete' % self.initial_post_key.integer_id()) # Test that the user is redirected to the post page self.assertEqual(response.status_int, 302)
def testSignedInUserCanAddComment(self): self.testapp.set_cookie('user', create_user_cookie('Test_User_01')) response = self.testapp.request("/posts/%d" % self.test_post_id) response.form['content'] = "New content" form_response = response.form.submit() redirect_response = form_response.follow() latest_comment = redirect_response.html.select(".comment")[-1] latest_comment_content = latest_comment.select( '.comment-content')[0].get_text() self.assertEqual(latest_comment_content, "New content")
def testNonAuthorHasNoDeleteOption(self): # Sign in as "Invalid User" and navigate to the post's page self.testapp.set_cookie('user', create_user_cookie('Invalid User')) response = self.testapp.request('/posts/%d' % self.initial_post_key.integer_id()) # Check that the delete button is available and redirects # to the delete endpoint delete_button = response.html.select('.post-delete') self.assertEqual(len(delete_button), 0)
def testNonAuthorHasNoEditOption(self): # Sign in as "Invalid User" and navigate to the post's page self.testapp.set_cookie('user', create_user_cookie('Invalid User')) response = self.testapp.request('/posts/%d' % self.initial_post_key.integer_id()) # Test that the edit button is not available edit_button = response.html.select('.post-edit') self.assertEqual(len(edit_button), 0)
def testAuthorCannotLikePostViaUI(self): # Sign in as author and navigate to an authored post self.testapp.set_cookie('user', create_user_cookie('Invalid Liker')) response = self.testapp.request( '/posts/%d' % self.initial_post_to_like_key.integer_id()) # Assert that the like button is unavailable like_button = response.html.select('.post-like') self.assertEqual(len(like_button), 0)
def testUserCannotDeleteOtherComments(self): self.testapp.set_cookie('user', create_user_cookie('Test_User_01')) user_02_comment = "/posts/%d/comments/%d" % (self.test_post_id, self.comment_ids[1]) delete_response = self.testapp.request(user_02_comment + "/delete") delete_redirect = delete_response.follow() self.assertEqual(delete_redirect.request.path, '/posts/%d' % self.test_post_id) comments = delete_redirect.html.select('.comment') self.assertEqual(len(comments), 3)
def testEditCommentViaPOSTIsIneffectiveForNonAuthor(self): self.testapp.set_cookie('user', create_user_cookie('Test_User_01')) # Attempt to POST directly to comment URL to edit the second comment url = "/posts/%d/comments/%d" % (self.test_post_id, self.comment_ids[1]) _ = self.testapp.post(url, {'content': 'Test content 02 (edited)'}) # Check that the second comment is unaltered post_page_response = self.testapp.request("/posts/%d" % self.test_post_id) second_comment = post_page_response.html.select(".comment")[1] edited_comment = second_comment.select( '.comment-content')[0].get_text() self.assertNotEqual(edited_comment, "Test content 02 (edited)") self.assertEqual(edited_comment, "Test content 02")
def testFormValidationIsPerformedOnEditedPost(self): self.testapp.set_cookie('user', create_user_cookie('Valid User')) response = self.testapp.request('/posts/%d' % self.initial_post_key.integer_id()) # Check that an edit button is available and redirects to the edit page edit_button = response.html.select('.post-edit') self.assertEqual(len(edit_button), 1) edit_response = response.click(description="Edit", href="/posts/\d+/edit") # Perform an edit edit_response.form['title'] = "" edit_response.form['content'] = "EDITED CONTENT" invalid_form = edit_response.form.submit() validation_error = invalid_form.html.select('.validation-error') self.assertEqual(len(validation_error), 1)
def testUserCannotEditOtherComments(self): self.testapp.set_cookie('user', create_user_cookie('Test_User_01')) # Edit button is not available on post page post_page_response = self.testapp.request("/posts/%d" % self.test_post_id) comment = post_page_response.html.select('.comment')[1] comment_edit_controls = comment.select('.comment-options') self.assertEqual(len(comment_edit_controls), 0) # Edit page is inaccessible for non-author # (i.e. redirects to post page) edit_page_response = self.testapp.request( "/posts/%d/comments/%d" % (self.test_post_id, self.comment_ids[1])) redirect = edit_page_response.follow() self.assertEqual(redirect.request.path, '/posts/%d' % self.test_post_id)
def post(self): from model.user import check_password username = self.request.POST['username'] password = self.request.POST['password'] # If a password is valid, set the 'user' cookie and redirect # Otherwise, re-render the form with validation errors if check_password(username, password): self.response.set_cookie('user', value=auth.create_user_cookie(username)) self.redirect('/users/welcome') else: template = jinja_env.get_template('sign-in.html') self.write(template, { 'invalid_credentials': True, 'username': username })
def testNonAuthorCannotEditPostViaPOSTRequestToUrl(self): # Sign in as "Invalid User" and attempt to POST to /posts/postid self.testapp.set_cookie('user', create_user_cookie('Invalid User')) post_response = self.testapp.post( '/posts/%d/edit' % self.initial_post_key.integer_id(), { 'content': 'EDITED via POST', 'title': 'EDITED via POST' }) # Ensure that a redirect occurs to posts/post_id self.assertEqual(post_response.status_int, 302) post_page = post_response.follow() # Ensure that the content was not altered title = post_page.html.select('.post-title')[0] content = post_page.html.select('.post-content')[0] self.assertNotEqual(title, "EDITED via POST") self.assertNotEqual(content, "EDITED via POST")
def testAuthorCannotLikePostViaUrl(self): # Sign in as non author and navigate to another author's post self.testapp.set_cookie('user', create_user_cookie('Invalid Liker')) response = self.testapp.request( '/posts/%d' % self.initial_post_to_like_key.integer_id()) # Store the like counter value like_counter_before = response.html.select('.post-likes')[0].get_text() # Issue GET against the post's /like endpoint like_response = self.testapp.request( '/posts/%d/like' % self.initial_post_to_like_key.integer_id()) post_page = like_response.follow() like_counter_after = post_page.html.select('.post-likes')[0].get_text() like_counter_before = int(like_counter_before) like_counter_after = int(like_counter_after) self.assertTrue(like_counter_after == like_counter_before)
def testNonAuthorCanLikePost(self): # Sign in as non author and navigate to another author's post self.testapp.set_cookie('user', create_user_cookie('Valid Liker')) response = self.testapp.request( '/posts/%d' % self.initial_post_to_like_key.integer_id()) # Store the like counter value like_counter_before = response.html.select('.post-likes')[0].get_text() # Assert that the like button is available like_button = response.html.select('.post-like') self.assertEqual(len(like_button), 1) like_response = response.click(description="Like", href="/posts/\d+/like") # Follow like response to post page and check counter increments post_page = like_response.follow() like_counter_after = post_page.html.select('.post-likes')[0].get_text() like_counter_before = int(like_counter_before) like_counter_after = int(like_counter_after) self.assertTrue(like_counter_after == like_counter_before + 1)
def post(self): template = jinja_env.get_template('register.html') username = self.request.POST['username'] password = self.request.POST['password'] if username == '': self.write(template, {'username_blank': True}) elif User.get_by_id(username): self.write(template, { 'username_taken': True, 'username': username }) elif password == '': self.write(template, { 'password_blank': True, 'username': username }) else: User(id=username, password=str(password)).put() self.response.set_cookie('user', value=auth.create_user_cookie(username)) self.redirect('/users/welcome')
def testUserCanEditTheirOwnComment(self): self.testapp.set_cookie('user', create_user_cookie('Test_User_01')) # Edit button is available on post page post_page_response = self.testapp.request("/posts/%d" % self.test_post_id) comment = post_page_response.html.select('.comment')[0] comment_edit_controls = comment.select('.comment-options') self.assertEqual(len(comment_edit_controls), 1) # Edit form is available on edit page edit_page_response = self.testapp.request( "/posts/%d/comments/%d" % (self.test_post_id, self.comment_ids[0])) edit_form = edit_page_response.form self.assertEqual(edit_form['content'].value, "Test content 01") edit_form['content'].value = "Test content 01 (edited)" form_response = edit_form.submit() # Edit has taken effect on post page redirect_response = form_response.follow() edited_comment = redirect_response.html.select(".comment")[0] edited_comment_content = edited_comment.select( '.comment-content')[0].get_text() self.assertEqual(edited_comment_content, "Test content 01 (edited)")