def test___get_thread_notification_targets_ignore_value_errors(self): user_id = 'comment10001' replyed_user_id = 'articleuser01' parent_id = 'comment00001' me_articles_comments_reply = MeArticlesCommentsReply({}, {}, self.dynamodb) try: me_articles_comments_reply._MeArticlesCommentsReply__get_thread_notification_targets( user_id, replyed_user_id, parent_id) except ValueError: self.fail('get_thread_notification_tagets() raised ValueError unexpectedly')
def test___create_comment_notifications_reply_article_user(self): params = { 'article_id': 'publicId0001', 'text': 'A', 'parent_id': 'comment00001', 'replyed_user_id': 'commentuser01' } article_info = self.article_info_table_items[0] comment = { 'comment_id': 'comment_id', 'user_id': 'articleuser01' # 記事投稿者が自らコメント } me_articles_comments_reply = MeArticlesCommentsReply( params, {}, self.dynamodb) me_articles_comments_reply.params = params notification_before = self.notification_table.scan()['Items'] unread_notification_manager_before = self.unread_notification_manager_table.scan( )['Items'] me_articles_comments_reply._MeArticlesCommentsReply__create_comment_notifications( article_info, comment) notification_after = self.notification_table.scan()['Items'] unread_notification_manager_after = self.unread_notification_manager_table.scan( )['Items'] self.assertEqual(len(notification_after) - len(notification_before), 3) self.assertEqual( len(unread_notification_manager_after) - len(unread_notification_manager_before), 3) self.assertIsNotNone( self.notification_table.get_item( Key={ 'notification_id': 'reply-commentuser01-comment_id' }).get('Item')) self.assertIsNotNone( self.notification_table.get_item( Key={ 'notification_id': 'thread-commentuser02-comment_id' }).get('Item')) self.assertIsNotNone( self.notification_table.get_item( Key={ 'notification_id': 'thread-commentuser03-comment_id' }).get('Item'))
def test_raise_exception_in_creating_notification(self): params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'text': 'A', 'parent_id': 'comment00001', 'replyed_user_id': 'commentuser02' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) response = MeArticlesCommentsReply(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 200)
def test_call_validate_methods(self): params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'text': 'sample content', 'parent_id': 'comment00001', 'replyed_user_id': 'commentuser02' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_articles_comments_reply.DBUtil', mock_lib): MeArticlesCommentsReply(params, {}, self.dynamodb).main() args, kwargs = mock_lib.validate_article_existence.call_args self.assertTrue(mock_lib.validate_article_existence.called) self.assertEqual(args[0], self.dynamodb) self.assertEqual(args[1], 'publicId0001') self.assertEqual(kwargs['status'], 'public') self.assertTrue(mock_lib.validate_write_blacklisted.called) args, kwargs = mock_lib.validate_write_blacklisted.call_args self.assertTrue(args[0]) self.assertEqual(args[1], 'comment_user_01') args, _ = mock_lib.validate_parent_comment_existence.call_args self.assertTrue(mock_lib.validate_parent_comment_existence.called) self.assertEqual(args[0], self.dynamodb) self.assertEqual(args[1], 'comment00001') args, _ = mock_lib.validate_user_existence.call_args self.assertTrue(mock_lib.validate_user_existence.called) self.assertEqual(args[0], self.dynamodb) self.assertEqual(args[1], 'commentuser02') args, _ = mock_lib.validate_user_existence_in_thread.call_args self.assertTrue(mock_lib.validate_user_existence_in_thread.called) self.assertEqual(args[0], self.dynamodb) self.assertEqual(args[1], 'commentuser02') self.assertEqual(args[2], 'comment00001')
def test_main_ok(self): params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'text': 'A', 'parent_id': 'comment00001', 'replyed_user_id': 'commentuser02' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) comment_before = self.comment_table.scan()['Items'] notification_before = self.notification_table.scan()['Items'] unread_notification_manager_before = self.unread_notification_manager_table.scan( )['Items'] response = MeArticlesCommentsReply(params, {}, self.dynamodb).main() comment_after = self.comment_table.scan()['Items'] notification_after = self.notification_table.scan()['Items'] unread_notification_manager_after = self.unread_notification_manager_table.scan( )['Items'] self.assertEqual(response['statusCode'], 200) self.assertEqual( json.loads(response['body'])['comment_id'], 'HOGEHOGEHOGE') self.assertEqual(len(comment_after) - len(comment_before), 1) self.assertEqual(len(notification_after) - len(notification_before), 4) self.assertEqual( len(unread_notification_manager_after) - len(unread_notification_manager_before), 4) comment = self.comment_table.get_item(Key={ 'comment_id': 'HOGEHOGEHOGE' }).get('Item') expected_comment = { 'comment_id': 'HOGEHOGEHOGE', 'text': 'A', 'article_id': 'publicId0001', 'user_id': 'test_user_id01', 'parent_id': 'comment00001', 'replyed_user_id': 'commentuser02', 'created_at': 1520150552, 'sort_key': 1520150552000003 } self.assertEqual(comment, expected_comment) reply_notification = self.notification_table.get_item( Key={ 'notification_id': 'reply-commentuser02-HOGEHOGEHOGE' }).get('Item') comment_notification = self.notification_table.get_item( Key={ 'notification_id': 'comment-articleuser01-HOGEHOGEHOGE' }).get('Item') thread_notification01 = self.notification_table.get_item( Key={ 'notification_id': 'thread-commentuser01-HOGEHOGEHOGE' }).get('Item') thread_notification02 = self.notification_table.get_item( Key={ 'notification_id': 'thread-commentuser03-HOGEHOGEHOGE' }).get('Item') expected_reply_notification = { 'notification_id': 'reply-commentuser02-HOGEHOGEHOGE', 'user_id': 'commentuser02', 'article_id': self.article_info_table_items[0]['article_id'], 'article_title': self.article_info_table_items[0]['title'], 'article_user_id': self.article_info_table_items[0]['user_id'], 'acted_user_id': 'test_user_id01', 'sort_key': 1520150552000003, 'type': 'reply', 'created_at': 1520150552 } expected_comment_notification = { 'notification_id': 'comment-articleuser01-HOGEHOGEHOGE', 'user_id': 'articleuser01', 'article_id': self.article_info_table_items[0]['article_id'], 'article_title': self.article_info_table_items[0]['title'], 'article_user_id': self.article_info_table_items[0]['user_id'], 'acted_user_id': 'test_user_id01', 'sort_key': 1520150552000003, 'type': 'comment', 'created_at': 1520150552 } expected_thread_notification01 = { 'notification_id': 'thread-commentuser01-HOGEHOGEHOGE', 'user_id': 'commentuser01', 'article_id': self.article_info_table_items[0]['article_id'], 'article_title': self.article_info_table_items[0]['title'], 'article_user_id': self.article_info_table_items[0]['user_id'], 'acted_user_id': 'test_user_id01', 'sort_key': 1520150552000003, 'type': 'thread', 'created_at': 1520150552 } expected_thread_notification02 = { 'notification_id': 'thread-commentuser03-HOGEHOGEHOGE', 'user_id': 'commentuser03', 'article_id': self.article_info_table_items[0]['article_id'], 'article_title': self.article_info_table_items[0]['title'], 'article_user_id': self.article_info_table_items[0]['user_id'], 'acted_user_id': 'test_user_id01', 'sort_key': 1520150552000003, 'type': 'thread', 'created_at': 1520150552 } self.assertEqual(reply_notification, expected_reply_notification) self.assertEqual(comment_notification, expected_comment_notification) self.assertEqual(thread_notification01, expected_thread_notification01) self.assertEqual(thread_notification02, expected_thread_notification02) for user_id in [ 'articleuser01', 'commentuser01', 'commentuser02', 'commentuser03' ]: self.assertEqual( self.unread_notification_manager_table.get_item( Key={ 'user_id': user_id }).get('Item'), { 'user_id': user_id, 'unread': True })
def assert_bad_request(self, params): function = MeArticlesCommentsReply(params, {}, self.dynamodb) response = function.main() self.assertEqual(response['statusCode'], 400)
def lambda_handler(event, context): me_articles_comments_reply = MeArticlesCommentsReply(event=event, context=context, dynamodb=dynamodb) return me_articles_comments_reply.main()