def test_add_comment(self): """The add_comment procedure adds a new comment for a user Test relies on list_comments_user working properly""" useremail = "*****@*****.**" page = "http://example.com/" firstcomment = "First Comment" secondcomment = "Second Comment" interface.add_comment(self.db, useremail, page, firstcomment) # now retrieve the info and verify comments = interface.list_comments_user(self.db, useremail) # our comment should be the first i, e, p, c = comments[0] self.assertEquals( (useremail, page, firstcomment), (e, p, c), "Stored comment not found first in comments list, got %s instead" % str(comments[0])) # do it again to check we don't lose the above interface.add_comment(self.db, useremail, page, secondcomment) # now retrieve the info and verify comments = interface.list_comments_user(self.db, useremail) # our comment should be the first i, e, p, c = comments[0] self.assertEquals((useremail, page, secondcomment), ( e, p, c ), "Second stored comment not found first in comments list, got %s instead" % str(comments[0])) i, e, p, c = comments[1] self.assertEquals((useremail, page, firstcomment), ( e, p, c ), "First stored comment not found second in comments list, got %s instead" % str(comments[0]))
def add_comment(environ, start_response): formdata = cgi.FieldStorage(environ=environ, fp=environ["wsgi.input"]) if "comment" in formdata: image = formdata.getvalue("image", "") comment = formdata.getvalue("comment", "") interface.add_comment(db, image, cgi.escape(comment)) start_response("301 Redirect", [("content-type", "text/html")]) return [redirect_page.encode()]
def test_add_comment(self): """Test that add_comment is able to add new comments""" image = 'seashell.jpg' testcomment = 'she sells sea shells on the sea shore' interface.add_comment(self.db, image, testcomment) # use list_comments to retrieve the comments, this assumes that list_comments works comments = interface.list_comments(self.db, image) self.assertEqual(1, len(comments), "expected just one comment for seashell.jpg") self.assertEqual(testcomment, comments[0])
def test_add_comment(self): """The add_comment procedure adds a new comment for a user Test relies on list_comments_user working properly""" useremail = "*****@*****.**" page = "http://example.com/" firstcomment = "First Comment" secondcomment = "Second Comment" interface.add_comment(self.db, useremail, page, firstcomment) # now retrieve the info and verify comments = interface.list_comments_user(self.db, useremail) # our comment should be the first i, e, p, c = comments[0] self.assertEquals( (useremail, page, firstcomment), (e, p, c), "Stored comment not found first in comments list, got %s instead" % str(comments[0]), ) # do it again to check we don't lose the above interface.add_comment(self.db, useremail, page, secondcomment) # now retrieve the info and verify comments = interface.list_comments_user(self.db, useremail) # our comment should be the first i, e, p, c = comments[0] self.assertEquals( (useremail, page, secondcomment), (e, p, c), "Second stored comment not found first in comments list, got %s instead" % str(comments[0]), ) i, e, p, c = comments[1] self.assertEquals( (useremail, page, firstcomment), (e, p, c), "First stored comment not found second in comments list, got %s instead" % str(comments[0]), )
def application(environ, start_response): """Demo WSGI application""" formdata = cgi.FieldStorage(environ=environ, fp=environ['wsgi.input']) if formdata.has_key('username') and formdata.has_key('password'): username = formdata.getvalue('username') password = formdata.getvalue('password') if interface.check_login(db, username, password) == True: cookie = interface.generate_session(db, username) # Correct username or password, return a page saying so. return login_success(cookie, environ, start_response) else: # Invalid username or password, return a page with an error box saying so. return invalid(environ, start_response) elif formdata.has_key('username') or formdata.has_key('password'): # Invalid username or password, return a page with an error box saying so. return invalid(environ, start_response) session = str(interface.user_from_cookie(db, environ)) if formdata.has_key('website') and formdata.has_key('addcomment'): if formdata.getvalue('website') == 'http://': # Shouldn't post a comment about http://! return comment_unsuccessful(environ, start_response) elif session != 'None': topic = formdata.getvalue('website') comment = formdata.getvalue('addcomment') interface.add_comment(db, session, topic, comment) #Comment successfully added. Return a page saying so, with a redirect link to main page. return comment_successful(environ, start_response) else: # Unlogged users should receive a 404 Not Found error return no_permission(environ, start_response) elif formdata.has_key('website') or formdata.has_key('addcomment'): if session != 'None': #Invalid form returned. Return a page showing an error box and how to fix it. return comment_unsuccessful(environ, start_response) else: # Unlogged users should receive a 404 Not Found error. return no_permission(environ, start_response) if environ['PATH_INFO'] == '/': return main_page(environ, start_response) elif environ['PATH_INFO'] == '/login': return login_page(environ, start_response) elif environ['PATH_INFO'] == '/my': if session != 'None': return my_comments(environ, start_response) else: return show_404_app(environ, start_response) elif environ['PATH_INFO'] == '/conversation': return conversation_page(environ, start_response) elif environ['PATH_INFO'] == '/comment': if session != 'None': return add_comment(environ, start_response) else: #Unlogged users should be told they do not have permission to access this page. return no_permission(environ, start_response) elif environ['PATH_INFO'] == '/logout': if session != 'None': interface.delete_session(db, session) return logout(environ, start_response) else: # Unlogged users should receive a 404 Not Found error. return show_404_app(environ, start_response) else: # Path invalid, return 404 Not Found return show_404_app(environ, start_response)
def action_comment(environ): form = cgi.FieldStorage(environ=environ, fp=environ['wsgi.input']) if 'image' in form and 'comment' in form: add_comment(db, form.getvalue('image'), html.escape(form.getvalue('comment')))