def assert_bad_request(self, params): function = MeArticlesDraftsPublish(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch) response = function.main() self.assertEqual(response['statusCode'], 400)
def test_call_validate_array_unique(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto', 'tags': ['A', 'B', 'C', 'D', 'E' * 25] }, '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_drafts_publish.ParameterUtil', mock_lib): MeArticlesDraftsPublish(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertTrue(mock_lib.validate_array_unique.called) args, kwargs = mock_lib.validate_array_unique.call_args self.assertEqual(args[0], ['A', 'B', 'C', 'D', 'E' * 25]) self.assertEqual(args[1], 'tags') self.assertEqual(kwargs['case_insensitive'], True)
def test_create_and_count_raise_exception(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto', 'tags': ['A'] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) response = MeArticlesDraftsPublish( params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertEqual(response['statusCode'], 200)
def test_call_tag_util_methods(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto', 'tags': ['A'] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_articles_drafts_publish.TagUtil', mock_lib): MeArticlesDraftsPublish(params, {}, self.dynamodb).main() self.assertTrue(mock_lib.validate_format.called) args, _ = mock_lib.validate_format.call_args self.assertEqual(args[0], ['A']) self.assertTrue(mock_lib.create_and_count.called) args, _ = mock_lib.create_and_count.call_args self.assertTrue(args[0]) self.assertEqual(args[1], ['a', 'b', 'c']) self.assertEqual(args[2], ['A'])
def test_call_validate_methods(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_articles_drafts_publish.DBUtil', mock_lib): MeArticlesDraftsPublish(params, {}, self.dynamodb).main() self.assertTrue(mock_lib.validate_article_existence.called) args, kwargs = mock_lib.validate_article_existence.call_args self.assertTrue(args[0]) self.assertTrue(args[1]) self.assertTrue(kwargs['user_id']) self.assertEqual(kwargs['status'], 'draft') self.assertTrue(mock_lib.validate_topic.called) args, kwargs = mock_lib.validate_topic.call_args self.assertTrue(args[0]) self.assertEqual(args[1], 'crypto')
def test_validation_not_exists_private_eth_address(self): params = { 'queryStringParameters': {}, 'body': { 'topic': 'crypto' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) response = MeArticlesDraftsPublish( params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertEqual(response['statusCode'], 400) self.assertEqual( response['body'], '{"message": "Invalid parameter: not exists private_eth_address"}')
def test_main_ok_article_history_arleady_exists(self): params = { 'pathParameters': { 'article_id': 'draftId00003' }, 'body': { 'topic': 'crypto' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': '0x1234567890123456789012345678901234567890', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) article_info_before = self.article_info_table.scan()['Items'] article_history_before = self.article_history_table.scan()['Items'] article_content_edit_before = self.article_content_edit_table.scan( )['Items'] response = MeArticlesDraftsPublish( params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() article_info_after = self.article_info_table.scan()['Items'] article_history_after = self.article_history_table.scan()['Items'] article_content_edit_after = self.article_content_edit_table.scan( )['Items'] article_info = self.article_info_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] article_content = self.article_content_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] article_history = self.article_history_table.query( KeyConditionExpression=Key('article_id').eq( params['pathParameters']['article_id']))['Items'][-1] self.assertEqual(response['statusCode'], 200) self.assertEqual(article_info['status'], 'public') self.assertEqual(article_content['title'], article_history['title']) self.assertEqual(article_content['body'], article_history['body']) self.assertEqual(article_info['sort_key'], 1520150272000000) self.assertEqual(article_info['published_at'], 1520150000) self.assertEqual(len(article_info_after) - len(article_info_before), 0) self.assertEqual( len(article_history_after) - len(article_history_before), 1) self.assertEqual( len(article_content_edit_after) - len(article_content_edit_before), 0)
def test_main_ok(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto', 'tags': ['A', 'B', 'C', 'D', 'E' * 25] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } params['body'] = json.dumps(params['body']) article_info_before = self.article_info_table.scan()['Items'] article_history_before = self.article_history_table.scan()['Items'] article_content_edit_before = self.article_content_edit_table.scan( )['Items'] response = MeArticlesDraftsPublish(params, {}, self.dynamodb).main() article_info_after = self.article_info_table.scan()['Items'] article_history_after = self.article_history_table.scan()['Items'] article_content_edit_after = self.article_content_edit_table.scan( )['Items'] self.assertEqual(response['statusCode'], 200) article_info = self.article_info_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] article_content = self.article_content_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] article_history = self.article_history_table.query( KeyConditionExpression=Key('article_id').eq( params['pathParameters']['article_id']))['Items'][-1] self.assertEqual(article_info['status'], 'public') self.assertEqual(article_info['sort_key'], 1520150552000000) self.assertEqual(article_info['published_at'], 1525000000) self.assertEqual(article_info['sync_elasticsearch'], 1) self.assertEqual(article_info['topic'], 'crypto') self.assertEqual(article_info['tags'], ['A', 'B', 'C', 'D', 'E' * 25]) self.assertEqual(article_content['title'], article_history['title']) self.assertEqual(article_content['body'], article_history['body']) self.assertEqual(len(article_info_after) - len(article_info_before), 0) self.assertEqual( len(article_history_after) - len(article_history_before), 1) self.assertEqual( len(article_content_edit_after) - len(article_content_edit_before), 0)
def test_main_ok_article_history_arleady_exists(self): params = { 'pathParameters': { 'article_id': 'draftId00003' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } article_info_before = self.article_info_table.scan()['Items'] article_history_before = self.article_history_table.scan()['Items'] article_content_edit_before = self.article_content_edit_table.scan( )['Items'] response = MeArticlesDraftsPublish(params, {}, self.dynamodb).main() article_info_after = self.article_info_table.scan()['Items'] article_history_after = self.article_history_table.scan()['Items'] article_content_edit_after = self.article_content_edit_table.scan( )['Items'] article_info = self.article_info_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] article_content = self.article_content_table.get_item( Key={'article_id': params['pathParameters']['article_id']})['Item'] article_history = self.article_history_table.query( KeyConditionExpression=Key('article_id').eq( params['pathParameters']['article_id']))['Items'][-1] self.assertEqual(response['statusCode'], 200) self.assertEqual(article_info['status'], 'public') self.assertEqual(article_content['title'], article_history['title']) self.assertEqual(article_content['body'], article_history['body']) self.assertEqual(article_info['sort_key'], 1520150272000000) self.assertEqual(article_info['published_at'], 1520150000) self.assertEqual(len(article_info_after) - len(article_info_before), 0) self.assertEqual( len(article_history_after) - len(article_history_before), 1) self.assertEqual( len(article_content_edit_after) - len(article_content_edit_before), 0)
def test_call_tag_util_methods(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto', 'tags': ['A'] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': '0x1234567890123456789012345678901234567890', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() mock_lib.get_tags_with_name_collation.return_value = ['A'] with patch('me_articles_drafts_publish.TagUtil', mock_lib): MeArticlesDraftsPublish(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertTrue(mock_lib.validate_format.called) args, _ = mock_lib.validate_format.call_args self.assertEqual(args[0], ['A']) self.assertTrue(mock_lib.get_tags_with_name_collation.called) args, _ = mock_lib.get_tags_with_name_collation.call_args self.assertEqual(args[1], ['A']) self.assertTrue(mock_lib.create_and_count.called) args, _ = mock_lib.create_and_count.call_args self.assertTrue(args[0]) self.assertEqual(args[1], ['a', 'b', 'c']) self.assertEqual(args[2], ['A'])
def test_call_validate_methods(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'body': { 'topic': 'crypto' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'custom:private_eth_address': '0x1234567890123456789012345678901234567890', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) mock_lib = MagicMock() with patch('me_articles_drafts_publish.DBUtil', mock_lib): MeArticlesDraftsPublish(params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() self.assertTrue(mock_lib.validate_article_existence.called) args, kwargs = mock_lib.validate_article_existence.call_args self.assertTrue(args[0]) self.assertTrue(args[1]) self.assertTrue(kwargs['user_id']) self.assertEqual(kwargs['status'], 'draft') self.assertTrue(mock_lib.validate_topic.called) args, kwargs = mock_lib.validate_topic.call_args self.assertTrue(args[0]) self.assertEqual(args[1], 'crypto')
def test_call_validate_article_existence(self): params = { 'pathParameters': { 'article_id': 'draftId00001' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } mock_lib = MagicMock() with patch('me_articles_drafts_publish.DBUtil', mock_lib): response = MeArticlesDraftsPublish(params, {}, self.dynamodb).main() args, kwargs = mock_lib.validate_article_existence.call_args self.assertTrue(mock_lib.validate_article_existence.called) self.assertTrue(args[0]) self.assertTrue(args[1]) self.assertTrue(kwargs['user_id']) self.assertEqual(kwargs['status'], 'draft')
def lambda_handler(event, context): me_articles_drafts_publish = MeArticlesDraftsPublish(event, context, dynamodb) return me_articles_drafts_publish.main()