#NEWLINE# def test_comments( self ) : #NEWLINE# #INDENT# r = Role . query . filter_by ( name = 'User' ) . first ( ) #NEWLINE# self . assertIsNotNone ( r ) #NEWLINE# u1 = User ( email = 'john@example.com' , username = 'john' , password = 'cat' , confirmed = True , role = r ) #NEWLINE# u2 = User ( email = 'susan@example.com' , username = 'susan' , password = 'dog' , confirmed = True , role = r ) #NEWLINE# db . session . add_all ( [ u1 , u2 ] ) #NEWLINE# db . session . commit ( ) #NEWLINE# post = Post ( body = 'body of the post' , author = r ) #NEWLINE# db . session . add ( post ) #NEWLINE# db . session . commit ( ) #NEWLINE# response = self . client . post ( url_for ( 'api.new_post_comment' , id = post . id ) , headers = self . get_api_headers ( 'susan@example.com' , 'dog' ) , data = json . dumps ( { #NEWLINE# #INDENT# 'body' : 'Good [post](http://example.com)!' , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# self . assertTrue ( ( response . status_code == 201 ) ) #NEWLINE# json_response = json . loads ( response . data . decode ( 'utf-8' ) ) #NEWLINE# url = response . headers . get ( 'Location' ) #NEWLINE# self . assertIsNotNone ( url ) #NEWLINE# self . assertTrue ( ( json_response [ 'body' ] == 'Good [post](http://example.com)!' ) ) #NEWLINE# self . assertTrue ( ( re . sub ( '<.*?>' , '' , json_response [ 'body_html' ] ) == 'Good post!' ) ) #NEWLINE# response = self . client . get ( url , headers = self . get_api_headers ( 'john@example.com' , 'cat' ) ) #NEWLINE# self . assertTrue ( ( response . status_code == 200 ) ) #NEWLINE# json_response = json . loads ( response . data . decode ( 'utf-8' ) ) #NEWLINE# self . assertTrue ( ( json_response [ 'url' ] == url ) ) #NEWLINE# self . assertTrue ( ( json_response [ 'body' ] == 'Good [post](http://example.com)!' ) ) #NEWLINE# comment = Comment ( body = 'Thank you!' , author = u1 , post = post ) #NEWLINE# db . session . add ( comment ) #NEWLINE# db . session . commit ( ) #NEWLINE# response = self . client . get ( url_for ( 'api.get_post_comments' , id = post . id ) , headers = self . get_api_headers ( 'susan@example.com' , 'dog' ) ) #NEWLINE# self . assertTrue ( ( response . status_code == 200 ) ) #NEWLINE# json_response = json . loads ( response . data . decode ( 'utf-8' ) ) #NEWLINE# self . assertIsNotNone ( json_response . get ( 'posts' ) ) #NEWLINE# self . assertTrue ( ( json_response . get ( 'count' , 0 ) == 2 ) ) #NEWLINE# response = self . client . get ( url_for ( 'api.get_comments' , id = post . id ) , headers = self . get_api_headers ( 'susan@example.com' , 'dog' ) ) #NEWLINE# self . assertTrue ( ( response . status_code == 200 ) ) #NEWLINE# json_response = json . loads ( response . data . decode ( 'utf-8' ) ) #NEWLINE# self . assertIsNotNone ( json_response . get ( 'posts' ) ) #NEWLINE# self . assertTrue ( ( json_response . get ( 'count' , 0 ) == 2 ) )