def test03_test_tag_list(self): """/tag_list is called when someone changes tags on the UI. Add the test tag to a post and ensure is has been added to the post_obj """ with self.app.app_context(): # we need to get the tag_string associate with the original post post_obj = (db_session.query(Post).filter( Post.path == TagsTest.post_path).first()) post_tags_string = post_obj.format_tags() # add test/tag to this string test_tag_string = TagsTest.tag_name post_tags_string += ", " + test_tag_string data = {"tags": post_tags_string} rv = self.client.post('/tag_list?post_id=' + TagsTest.post_path, data=json.dumps(data), content_type='application/json', method='POST', headers=self.headers) assert rv.status == "200 OK" # check the new post tags string, and assert that "test/tag" post_obj = (db_session.query(Post).filter( Post.path == TagsTest.post_path).first()) post_tags_string = post_obj.format_tags() assert test_tag_string in post_tags_string
def test02_delete_a_comment(self): """ Delete a comment """ with self.repo_app.app_context(): # get a post post = (db_session.query(Post).filter(Post.is_published).first()) # create a comment data = {'text': 'This is a comment'} rv = self.app.post('/comment?path={}'.format(post.path), headers=self.headers, data=json.dumps(data), content_type='application/json') # get comment id comment_id = (db_session.query(Comment).filter( Comment.post_id == post.id).filter( Comment.user_id == self.user_id).filter( Comment.text == data['text']).first()).id # test deleting a commment rv = self.app.get( '/delete_comment?comment_id={}'.format(comment_id), headers=self.headers) assert rv.status == '200 OK' delete_comment = (db_session.query(Comment).filter( Comment.id == comment_id).all()) assert not delete_comment, 'comments found after deletion'
def test05_delete_tag(self): """/delete_tag_post deletes a tag from all posts. Check to make sure the tag is deleted, and all the posts with that tag don't have an associate """ with self.app.app_context(): tag_id = Tag(name=TagsTest.tag_name + '_renamed').id post_tags = (db_session.query(assoc_post_tag) .filter(assoc_post_tag.c.tag_id == tag_id) .all()) post_ids = [x[0] for x in post_tags] delete_tag_url = '/delete_tag_post?tag_id={tag_id}'.format(**locals()) rv = self.client.get(delete_tag_url, headers=self.headers) assert rv.status == "200 OK" tag = (db_session.query(Tag) .filter(Tag.name == TagsTest.tag_name + '_renamed') .first()) assert not tag for post_id in post_ids: post_tag = (db_session.query(assoc_post_tag) .filter(assoc_post_tag.c.tag_id == tag_id) .filter(assoc_post_tag.c.post_id == post_id) .all()) assert not post_tag
def test05_delete_tag(self): """/delete_tag_post deletes a tag from all posts. Check to make sure the tag is deleted, and all the posts with that tag don't have an associate """ with self.app.app_context(): tag_id = Tag(name=TagsTest.tag_name + '_renamed').id post_tags = (db_session.query(assoc_post_tag).filter( assoc_post_tag.c.tag_id == tag_id).all()) post_ids = [x[0] for x in post_tags] delete_tag_url = '/delete_tag_post?tag_id={tag_id}'.format( **locals()) rv = self.client.get(delete_tag_url, headers=self.headers) assert rv.status == "200 OK" tag = (db_session.query(Tag).filter(Tag.name == TagsTest.tag_name + '_renamed').first()) assert not tag for post_id in post_ids: post_tag = (db_session.query(assoc_post_tag).filter( assoc_post_tag.c.tag_id == tag_id).filter( assoc_post_tag.c.post_id == post_id).all()) assert not post_tag
def test03_test_tag_list(self): """/tag_list is called when someone changes tags on the UI. Add the test tag to a post and ensure is has been added to the post_obj """ with self.app.app_context(): # we need to get the tag_string associate with the original post post_obj = (db_session.query(Post) .filter(Post.path == TagsTest.post_path) .first()) post_tags_string = post_obj.format_tags() # add test/tag to this string test_tag_string = TagsTest.tag_name post_tags_string += ", " + test_tag_string data = {"tags": post_tags_string} rv = self.client.post('/tag_list?post_id=' + TagsTest.post_path, data=json.dumps(data), content_type='application/json', method='POST', headers=self.headers) assert rv.status == "200 OK" # check the new post tags string, and assert that "test/tag" post_obj = (db_session.query(Post) .filter(Post.path == TagsTest.post_path) .first()) post_tags_string = post_obj.format_tags() assert test_tag_string in post_tags_string
def test02_delete_a_comment(self): """ Delete a comment """ with self.repo_app.app_context(): # get a post post = (db_session.query(Post) .filter(Post.is_published) .first()) # create a comment data = {'text': 'This is a comment'} rv = self.app.post('/comment?path={}'.format(post.path), headers=self.headers, data=json.dumps(data), content_type='application/json') # get comment id comment_id = (db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.user_id == self.user_id) .filter(Comment.text == data['text']) .first()).id # test deleting a commment rv = self.app.get('/delete_comment?comment_id={}'.format(comment_id), headers=self.headers) assert rv.status == '200 OK' delete_comment = (db_session.query(Comment) .filter(Comment.id == comment_id) .all()) assert not delete_comment, 'comments found after deletion'
def test04_subscription_email(self): """ Test Subscription Email """ # Subscribe to a tag from gitpost and webpost Note, don't send # a request to do it so that create_index() will happen later, # as it is before_first_request with self.app.app_context(): db_session.expire_on_commit = False post = db_session.query(Post).get(self.post_id) user = User(username=self.knowledge_username) if user.id is None: db_session.commit() user_id = user.id # grab the first tag from the post tag_id = post.tags[0].id post_id = post.id subscription = Subscription(user_id=user_id, object_type='tag', object_id=tag_id) db_session.add(subscription) db_session.commit() # Run a request, which should trigger a subscription email for # tag_to_subscribe with self.app.config['mail'].record_messages() as outbox: with self.app.app_context(): send_subscription_emails(post) emails_sent = (db_session.query(Email).filter( and_(Email.object_id == post_id, Email.user_id == user_id, Email.trigger_id == tag_id, Email.trigger_type == 'subscription')).all()) # There should be exactly 1 email sent to the user for the target post # Check EmailSent records assert len( outbox) == 1, 'One subscription email should be actually sent' assert len(emails_sent ) == 1, 'One subscription email should recorded as sent' # Check outbox of messages assert len( outbox[0].bcc ) == 1, 'One subscription email should be actually sent to one user' assert "A knowledge post was just posted under tag" in outbox[ 0].body, 'Email should be the subscription email' username_to_email = self.app.repository.config.username_to_email assert outbox[0].bcc[0] == username_to_email( self.knowledge_username)
def setUp(self): """Set up the TagsTest Class by instantiating a tag.""" self.repo = KnowledgeRepository.for_uri('tests/test_repo', auto_create=True) self.app = self.repo.get_app(config='tests/config_server.py') self.client = self.app.test_client() self.knowledge_username = '******' with self.app.app_context(): user = User(identifier=self.knowledge_username) if user.id is None: db_session.commit() self.user_id = user.id # we'll create a tag for the purpose of tag testing test_run = datetime.datetime.now().strftime('%Y-%m-%d') tag = Tag(name='test/tag_' + test_run) db_session.commit() TagsTest.tag_id = tag.id TagsTest.tag_name = tag.name TagsTest.user_id = self.user_id # We need an example post as well to test some of the # add/delete/rename functionality example_post = (db_session.query(Post).first()) TagsTest.post_path = example_post.path self.headers = {self.app.config['AUTH_USER_IDENTIFIER_REQUEST_HEADER']: self.knowledge_username}
def setUp(self): """Set up the TagsTest Class by instantiating a tag.""" self.repo = KnowledgeRepository.for_uri('tests/test_repo', auto_create=True) self.app = self.repo.get_app(config='tests/config_server.py') self.client = self.app.test_client() self.knowledge_username = '******' with self.app.app_context(): user = User(identifier=self.knowledge_username) if user.id is None: db_session.commit() self.user_id = user.id # we'll create a tag for the purpose of tag testing test_run = datetime.datetime.now().strftime('%Y-%m-%d') tag = Tag(name='test/tag_' + test_run) db_session.commit() TagsTest.tag_id = tag.id TagsTest.tag_name = tag.name TagsTest.user_id = self.user_id # We need an example post as well to test some of the # add/delete/rename functionality example_post = (db_session.query(Post).first()) TagsTest.post_path = example_post.path self.headers = { self.app.config['AUTH_USER_IDENTIFIER_REQUEST_HEADER']: self.knowledge_username }
def test02_check_post_stats(self): """ Make sure pageview, distinct viewers correct for post """ rv = self.client.get(self.post_rendered_url) data = rv.data.decode('utf-8') soup = BeautifulSoup(data, 'html.parser') # we'll check the post_stats of the post by computing them # and ensuring that the upper-right-hand text is correct icon = soup.findAll("div", {"id": "pageview_stats"}) pageviews_str = icon[0].text.strip() with self.app.app_context(): post = (db_session.query(Post).filter( Post.id == self.post_id).first()) distinct_viewers = post.view_user_count pageviews = post.view_count db_pageviews_str = "Viewed {pageviews} times by {distinct_viewers} different users".format( **locals()) assert db_pageviews_str == pageviews_str, "'{}' is not '{}'!".format( db_pageviews_str, pageviews_str)
def test01_webeditor_route(self): """Test the /webposts route. This will need to test the security as well. The user 'Knowledge Default; is able to see all webposts, whereas 'Test User' should only see posts they've authored """ with self.app.app_context(): posts = db_session.query(Post).all() rv = self.client.get( '/webposts', headers={'user_header': 'test_knowledge_editor'}) assert rv.status == '200 OK' data = rv.data.decode('utf-8') soup = BeautifulSoup(data, 'html.parser') table_rows = soup.findAll("tr") # Subtract one for the header of the table assert len(table_rows) - 1 == len(posts) rv = self.client.get( '/webposts', headers={'user_header': 'webeditor_test_user'}) data = rv.data.decode('utf-8', 'html.parser') soup = BeautifulSoup(data, 'html.parser') table_rows = soup.findAll("tr") assert len(table_rows) - 1 == 1
def test04_subscription_email(self): """ Test Subscription Email """ # Subscribe to a tag from gitpost and webpost Note, don't send # a request to do it so that create_index() will happen later, # as it is before_first_request with self.app.app_context(): db_session.expire_on_commit = False post = db_session.query(Post).get(self.post_id) user = User(identifier=self.knowledge_username) if user.id is None: db_session.commit() user_id = user.id # grab the first tag from the post tag_id = post.tags[0].id post_id = post.id subscription = Subscription(user_id=user_id, object_type='tag', object_id=tag_id) db_session.add(subscription) db_session.commit() # Run a request, which should trigger a subscription email for # tag_to_subscribe with self.app.config['mail'].record_messages() as outbox: with self.app.app_context(): send_subscription_emails(post) emails_sent = (db_session.query(Email) .filter(and_(Email.object_id == post_id, Email.user_id == user_id, Email.trigger_id == tag_id, Email.trigger_type == 'subscription')) .all()) # There should be exactly 1 email sent to the user for the target post # Check EmailSent records assert len(outbox) == 1, 'One subscription email should be actually sent' assert len(emails_sent) == 1, 'One subscription email should recorded as sent' # Check outbox of messages assert len(outbox[0].bcc) == 1, 'One subscription email should be actually sent to one user' assert "A knowledge post was just posted under tag" in outbox[0].body, 'Email should be the subscription email' username_to_email = self.app.repository.config.username_to_email assert outbox[0].bcc[0] == username_to_email(self.knowledge_username)
def tearDown(self): """Delete the tag that we created.""" with self.app.app_context(): tag = (db_session.query(Tag).filter( Tag.name == TagsTest.tag_name).first()) if tag: db_session.delete(tag) db_session.commit()
def test_favorites(self): """ test the favorites route, and make sure the number of posts = number of votes with that user_id """ with self.repo_app.app_context(): rv = self.app.get("/favorites", headers=self.headers) assert rv.status == "200 OK" data = rv.data.decode('utf-8') soup = BeautifulSoup(data, 'html.parser') all_posts = soup.findAll( 'div', {'class': 'row row-space-4 panel feed-post'}) user = (db_session.query(User).filter( User.identifier == self.knowledge_username).first()) votes = (db_session.query(Vote).filter( Vote.user_id == user.id).all()) assert len(votes) == len(all_posts)
def test04_rename_tag_post(self): """/rename_tag is called from the batch_edit tag page. Rename the test/tag tag to test/tag_renamed, and check to make sure that old tag is deleted (we'll re-add it), the new tag is added, and the tag_string for related posts has changed """ new_tag_string = TagsTest.tag_name + '_renamed' with self.app.app_context(): # get all the posts with the old_tag_string post_tags = (db_session.query(assoc_post_tag) .filter(assoc_post_tag.c.tag_id == TagsTest.tag_id) .all()) post_ids = [x[0] for x in post_tags] data = {'oldTagId': TagsTest.tag_id, 'newTag': new_tag_string} rv = self.client.post('/rename_tag', data=json.dumps(data), content_type='application/json', headers=self.headers) assert(rv.status == "200 OK") old_tag_obj = (db_session.query(Tag) .filter(Tag.id == TagsTest.tag_id) .all()) assert not old_tag_obj new_tag_obj = (db_session.query(Tag) .filter(Tag.name == new_tag_string) .all()) assert new_tag_obj and len(new_tag_obj) == 1 new_id = new_tag_obj[0].id for post_id in post_ids: post_tag = (db_session.query(assoc_post_tag) .filter(assoc_post_tag.c.post_id == post_id) .filter(assoc_post_tag.c.tag_id == new_id) .all()) assert post_tag
def tearDown(self): """Delete the tag that we created.""" with self.app.app_context(): tag = (db_session.query(Tag) .filter(Tag.name == TagsTest.tag_name) .first()) if tag: db_session.delete(tag) db_session.commit()
def test01_comment_on_a_post(self): """ Comment on a post """ with self.repo_app.app_context(): # get the number of comments on a given post post = (db_session.query(Post) .filter(Post.is_published) .first()) n_comments = len(db_session.query(Comment) .filter(Comment.post_id == post.id) .all()) assert post.status == self.repo.PostStatus.PUBLISHED # test commenting on a post data = {'text': 'This is a comment'} rv = self.app.post('/comment?path={}'.format(post.path), headers=self.headers, data=json.dumps(data), content_type='application/json') assert rv.status == '200 OK' assert post.status == self.repo.PostStatus.PUBLISHED # check that new comment exists in db comment_exists = (db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.user_id == self.user_id) .filter(Comment.text == data['text']) .first()) assert comment_exists assert post.status == self.repo.PostStatus.PUBLISHED # check that the number of comments rendered increased by 1 rv = self.app.get('/post/' + post.path, headers=self.headers) soup = BeautifulSoup(rv.data.decode('utf-8'), 'html.parser') comments = soup.findAll("div", {"class": "post_comment"}) assert comments, 'comments found in ' assert len(comments) == n_comments + 1
def test02_test_unsubscribe_to_tag(self): """Test the unsubscribe feature.""" route_name = '/toggle_tag_subscription' with self.app.app_context(): tag = db_session.query(Tag).first() tag_name = 'tag_name=' + TagsTest.tag_name action = 'subscribe_action=unsubscribe' full_url = '{route_name}?{tag_name}&{action}'.format(**locals()) # check that status == 200 & the tag has been subscribed too rv = self.client.post(full_url, headers=self.headers) assert (rv.status == "200 OK") subscription = (db_session.query(Subscription).filter( Subscription.object_id == TagsTest.tag_id).filter( Subscription.object_type == 'tag').first()) assert not subscription
def test_favorites(self): """ test the favorites route, and make sure the number of posts = number of votes with that user_id """ with self.repo_app.app_context(): rv = self.app.get("/favorites", headers=self.headers) assert rv.status == "200 OK" data = rv.data.decode('utf-8') soup = BeautifulSoup(data, 'html.parser') all_posts = soup.findAll( 'div', {'class': 'row row-space-4 panel feed-post'}) user = (db_session.query(User) .filter(User.identifier == self.knowledge_username) .first()) votes = (db_session.query(Vote) .filter(Vote.user_id == user.id) .all()) assert len(votes) == len(all_posts)
def setUp(self): self.repo = KnowledgeRepository.for_uri('tests/test_repo', auto_create=True) self.app = self.repo.get_app(config='tests/config_server.py') self.client = self.app.test_client() with self.app.app_context(): # for this test, pick one post post = (db_session.query(Post).filter(Post.is_published).first()) self.post_id = post.id self.post_rendered_url = '/render?markdown={}'.format(post.path)
def test02_test_unsubscribe_to_tag(self): """Test the unsubscribe feature.""" route_name = '/toggle_tag_subscription' with self.app.app_context(): tag = db_session.query(Tag).first() tag_name = 'tag_name=' + TagsTest.tag_name action = 'subscribe_action=unsubscribe' full_url = '{route_name}?{tag_name}&{action}'.format( **locals()) # check that status == 200 & the tag has been subscribed too rv = self.client.post(full_url, headers=self.headers) assert (rv.status == "200 OK") subscription = (db_session.query(Subscription) .filter(Subscription.object_id == TagsTest.tag_id) .filter(Subscription.object_type == 'tag') .first()) assert not subscription
def test04_rename_tag_post(self): """/rename_tag is called from the batch_edit tag page. Rename the test/tag tag to test/tag_renamed, and check to make sure that old tag is deleted (we'll re-add it), the new tag is added, and the tag_string for related posts has changed """ new_tag_string = TagsTest.tag_name + '_renamed' with self.app.app_context(): # get all the posts with the old_tag_string post_tags = (db_session.query(assoc_post_tag).filter( assoc_post_tag.c.tag_id == TagsTest.tag_id).all()) post_ids = [x[0] for x in post_tags] data = {'oldTagId': TagsTest.tag_id, 'newTag': new_tag_string} rv = self.client.post('/rename_tag', data=json.dumps(data), content_type='application/json', headers=self.headers) assert (rv.status == "200 OK") old_tag_obj = (db_session.query(Tag).filter( Tag.id == TagsTest.tag_id).all()) assert not old_tag_obj new_tag_obj = (db_session.query(Tag).filter( Tag.name == new_tag_string).all()) assert new_tag_obj and len(new_tag_obj) == 1 new_id = new_tag_obj[0].id for post_id in post_ids: post_tag = (db_session.query(assoc_post_tag).filter( assoc_post_tag.c.post_id == post_id).filter( assoc_post_tag.c.tag_id == new_id).all()) assert post_tag
def test03_test_post_stats(self): """ Test post_stats """ posts = self.data for post in posts: title_link = post.findAll("a")[0] title_href = title_link['href'] find_equal = title_href.find("=") page_id = title_href[find_equal + 1:] # get pageviews # TODO: refine this for latest pageview model pageviews = post.findAll( "i", id=lambda x: x and x.startswith('tooltip-pageviews')) assert (len(pageviews) == 1) pageview_value = int(pageviews[0].text) pageview_query = (db_session.query(func.count( PageView.id)).filter(PageView.page == page_id).scalar()) assert (pageview_query == pageview_value + 1) # get likes likes = post.findAll( "i", id=lambda x: x and x.startswith('tooltip-likes')) assert (len(likes) == 1) likes_value = int(likes[0].text) likes_query = (db_session.query(Vote).filter( Vote.page == page_id).all()) assert (likes_value == len(likes_query)) # get comments comments = post.findAll( "i", id=lambda x: x and x.startswith('tooltip-comments')) assert (len(comments) == 1) comments_values = int(comments[0].text) comments_query = (db_session.query(Comment).filter( Comment.post_id == page_id).all()) assert (comments_values == len(comments_query))
def test01_comment_on_a_post(self): """ Comment on a post """ with self.repo_app.app_context(): # get the number of comments on a given post post = (db_session.query(Post).filter(Post.is_published).first()) n_comments = len( db_session.query(Comment).filter( Comment.post_id == post.id).all()) assert post.status == self.repo.PostStatus.PUBLISHED # test commenting on a post data = {'text': 'This is a comment'} rv = self.app.post('/comment?path={}'.format(post.path), headers=self.headers, data=json.dumps(data), content_type='application/json') assert rv.status == '200 OK' assert post.status == self.repo.PostStatus.PUBLISHED # check that new comment exists in db comment_exists = (db_session.query(Comment).filter( Comment.post_id == post.id).filter( Comment.user_id == self.user_id).filter( Comment.text == data['text']).first()) assert comment_exists assert post.status == self.repo.PostStatus.PUBLISHED # check that the number of comments rendered increased by 1 rv = self.app.get('/post/' + post.path, headers=self.headers) soup = BeautifulSoup(rv.data.decode('utf-8'), 'html.parser') comments = soup.findAll("div", {"class": "post_comment"}) assert comments, 'comments found in ' assert len(comments) == n_comments + 1
def test_like_and_unlike_a_post(self): """ Like and then unlike a post """ with self.repo_app.app_context(): post = (db_session.query(Post) .filter(Post.is_published) .first()) old_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) rv = self.app.get("/like?post_id=" + str(post.id), headers=self.headers) assert rv.status == '200 OK', post.path + rv.status new_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) assert len(old_likes) + 1 == len(new_likes) # assert that if you re-like the page, the number of likes doesn't # change rv = self.app.get("/like?post_id=" + str(post.id), headers=self.headers) assert rv.status == '200 OK' like_again = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) assert len(like_again) == len(new_likes) """ Let's unlike it again """ old_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) rv = self.app.get("/unlike?post_id=" + str(post.id), headers=self.headers) assert rv.status == '200 OK' new_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) assert len(new_likes) == len(old_likes) - 1
def test_like_and_unlike_a_post(self): """ Like and then unlike a post """ with self.repo_app.app_context(): post = (db_session.query(Post) .filter(Post.is_published) .first()) old_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) rv = self.app.get("/like?post_id=" + str(post.id), headers=self.headers) assert rv.status == '200 OK', post.path + rv.status new_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) assert len(old_likes) + 1 == len(new_likes) # assert that if you re-like the page, the number of likes doesn't # change rv = self.app.get("/like?post_id=" + str(post.id), headers=self.headers) assert rv.status == '200 OK' like_again = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) assert len(like_again) == len(new_likes) """ Let's unlike it again """ old_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) rv = self.app.get("/unlike?post_id=" + str(post.id), headers=self.headers) assert rv.status == '200 OK' new_likes = (db_session.query(Vote) .filter(Vote.object_id == post.id) .filter(Vote.object_type == 'post') .all()) assert len(new_likes) == len(old_likes) - 1
def test02_comment_email(self): """ Test Comment Email """ with self.app.app_context(): with self.app.config['mail'].record_messages() as outbox: with self.app.test_request_context(): commenter = 'test_commenter' comment_text = 'this is a test for comment email' send_comment_email(commenter=commenter, comment_text=comment_text, post_id=self.git_post_id) git_post = (db_session.query(Post).filter( Post.id == self.git_post_id).first()) assert len(outbox) == 1 assert outbox[0].subject == "Someone commented on your post!" # assert format_commenter(commenter) in outbox[0].body assert comment_text in outbox[0].body assert git_post.title in outbox[0].body
def test01_review_email(self): """ Test Review Email """ commenter = 'test_commenter' comment_text = 'this is a test for review email' with self.app.app_context(): with self.app.config['mail'].record_messages() as outbox: with self.app.test_request_context(): send_review_email(commenter=commenter, comment_text=comment_text, post_id=self.web_post_id) web_post = (db_session.query(Post).filter( Post.id == self.web_post_id).first()) assert len(outbox) == 1 assert outbox[0].subject == "Someone reviewed your web post!" # TODO assert format_commenter(commenter) in outbox[0].body assert comment_text in outbox[0].body assert web_post.title in outbox[0].body
def setUpClass(self): self.repo = KnowledgeRepository.for_uri('tests/test_repo', auto_create=True) self.app = self.repo.get_app(config='tests/config_server.py') self.app.repository.config.editors = ['knowledge_editors'] self.client = self.app.test_client() self.knowledge_username = '******' # this creates a user with self.app.app_context(): User(username=self.knowledge_username) web_post = Post(title='test_webpost') git_post = (db_session.query(Post).first()) db_session.add(web_post) web_post.authors = ['test_author'] git_post.tags = ['test_tag'] db_session.commit() self.web_post_id = web_post.id self.git_post_id = git_post.id
def test04_subscription_email(self): """ Test Subscription Email """ # Subscribe to a tag from gitpost and webpost Note, don't send # a request to do it so that create_index() will happen later, # as it is before_first_request with self.app.app_context(): db_session.expire_on_commit = False post = db_session.query(Post).get(self.post_id) user = User(identifier=self.knowledge_username) if user.id is None: db_session.commit() user_id = user.id # grab the first tag from the post tag_id = post.tags[0].id post_id = post.id subscription = Subscription(user_id=user_id, object_type='tag', object_id=tag_id) db_session.add(subscription) db_session.commit() # Run a request, which should trigger a subscription email for # tag_to_subscribe with self.app.config['mail'].record_messages() as outbox: with self.app.app_context(): send_subscription_emails(post) emails_sent = (db_session.query(Email).filter( and_(Email.object_id == post_id, Email.user_id == user_id, Email.trigger_id == tag_id, Email.trigger_type == 'subscription')).all()) # There should be exactly 1 email sent to the user for the target post # Check EmailSent records assert len( outbox) == 1, 'One subscription email should be actually sent' assert len(emails_sent ) == 1, 'One subscription email should recorded as sent' # Check outbox of messages assert len( outbox[0].bcc ) == 1, 'One subscription email should be actually sent to one user' assert "Title: " in outbox[ 0].body, 'Plain email should use the txt template' assert "cid:Thumb" in outbox[ 0].html, 'Rich email should use the html template' # Check thumbnail attachment assert len( outbox[0].attachments) == 1, 'One attachment should be sent' thumbnail = outbox[0].attachments[0] assert base64.b64decode( self.thumbnail) == thumbnail.data, 'Attachment should match' assert [['Content-ID', '<Thumb>'] ] == thumbnail.headers, 'Attachment id should match' username_to_email = self.app.repository.config.username_to_email assert outbox[0].bcc[0] == username_to_email( self.knowledge_username)