def validate_params(self): UserUtil.verified_phone_and_email(self.event) # check price type is integer or decimal ParameterUtil.validate_price_params(self.params.get('price')) if self.params.get('price') is not None: self.params['price'] = int(self.params['price']) validate(self.params, self.get_schema()) if self.params.get('eye_catch_url'): TextSanitizer.validate_img_url(self.params.get('eye_catch_url')) if self.params.get('tags'): ParameterUtil.validate_array_unique(self.params['tags'], 'tags', case_insensitive=True) TagUtil.validate_format(self.params['tags']) DBUtil.validate_article_existence( self.dynamodb, self.params['article_id'], user_id=self.event['requestContext']['authorizer']['claims'] ['cognito:username'], status='public', version=2) DBUtil.validate_topic(self.dynamodb, self.params['topic']) DBUtil.validate_exists_title_and_body(self.dynamodb, self.params['article_id'])
def test_main_with_null_before_tag_names(self): before_tag_names = None after_tag_names = ['A', 'D', 'E'] TagUtil.create_and_count(self.dynamodb, before_tag_names, after_tag_names) tag_table = self.dynamodb.Table(os.environ['TAG_TABLE_NAME']) tags = tag_table.scan()['Items'] expected = [ { 'name': 'A', 'count': Decimal('3'), }, { 'name': 'B', 'count': Decimal('3'), }, { 'name': 'D', 'count': Decimal('1'), }, { 'name': 'E', 'count': Decimal('5'), }, ] for tag in tags: del tag['created_at'] tags = sorted(tags, key=lambda t: t['name']) self.assertEqual(tags, expected)
def exec_main_proc(self): article_content_edit_table = self.dynamodb.Table( os.environ['ARTICLE_CONTENT_EDIT_TABLE_NAME']) article_content_edit = article_content_edit_table.get_item( Key={ 'article_id': self.params['article_id'] }).get('Item') article_info_table = self.dynamodb.Table( os.environ['ARTICLE_INFO_TABLE_NAME']) article_info_before = article_info_table.get_item( Key={ 'article_id': self.params['article_id'] }).get('Item') self.__validate_article_content_edit(article_content_edit) self.__create_article_history(article_content_edit) self.__update_article_info(article_content_edit) self.__update_article_content(article_content_edit) article_content_edit_table.delete_item( Key={'article_id': self.params['article_id']}) try: TagUtil.create_and_count(self.elasticsearch, article_info_before.get('tags'), self.params.get('tags')) except Exception as e: logging.fatal(e) traceback.print_exc() return {'statusCode': 200}
def exec_main_proc(self): self.__delete_article_content_edit() self.__create_article_history_and_update_sort_key() article_info_table = self.dynamodb.Table( os.environ['ARTICLE_INFO_TABLE_NAME']) article_info_before = article_info_table.get_item( Key={ 'article_id': self.params['article_id'] }).get('Item') article_info_table.update_item( Key={ 'article_id': self.params['article_id'], }, UpdateExpression= 'set #attr = :article_status, sync_elasticsearch = :one, topic = :topic, tags = :tags', ExpressionAttributeNames={'#attr': 'status'}, ExpressionAttributeValues={ ':article_status': 'public', ':one': 1, ':topic': self.params['topic'], ':tags': self.params.get('tags') }) try: TagUtil.create_and_count(self.dynamodb, article_info_before.get('tags'), self.params.get('tags')) except Exception as e: logging.fatal(e) traceback.print_exc() return {'statusCode': 200}
def exec_main_proc(self): # 公開する記事が有料設定か無料設定かの判定 if self.params.get('price') is not None and self.params.get( 'paid_body') is not None: is_priced = True elif self.params.get('price') is None and self.params.get( 'paid_body') is None: is_priced = False else: raise ValidationError('Both paid body and price are required.') # 共通処理 article_info_table = self.dynamodb.Table( os.environ['ARTICLE_INFO_TABLE_NAME']) article_info_before = article_info_table.get_item( Key={ 'article_id': self.params['article_id'] }).get('Item') article_content_edit_table = self.dynamodb.Table( os.environ['ARTICLE_CONTENT_EDIT_TABLE_NAME']) article_content_edit = article_content_edit_table.get_item( Key={ 'article_id': self.params['article_id'] }).get('Item') article_content_table = self.dynamodb.Table( os.environ['ARTICLE_CONTENT_TABLE_NAME']) self.__validate_article_content_edit(article_content_edit) # 有料記事の場合 if is_priced: self.__create_paid_article_history(article_content_edit) self.__update_paid_article_content(article_content_edit) self.__update_paid_article_info(article_content_edit, article_info_table) # 無料記事の場合 else: # 有料記事から無料記事にする場合を考慮している self.__remove_price_and_paid_body(article_info_table, article_content_table) self.__create_article_history(article_content_edit) self.__update_article_content(article_content_edit) self.__update_article_info(article_content_edit, article_info_table) article_content_edit_table.delete_item( Key={'article_id': self.params['article_id']}) try: TagUtil.create_and_count(self.elasticsearch, article_info_before.get('tags'), self.params.get('tags')) except Exception as e: logging.fatal(e) traceback.print_exc() return {'statusCode': 200}
def __update_paid_article_info(self, article_content_edit, article_info_table): info_expression_attribute_values = { ':title': article_content_edit['title'], ':eye_catch_url': self.params.get('eye_catch_url'), ':one': 1, ':topic': self.params['topic'], ':tags': TagUtil.get_tags_with_name_collation(self.elasticsearch, self.params.get('tags')), ':price': self.params.get('price') } article_info_table.update_item( Key={ 'article_id': self.params['article_id'], }, UpdateExpression= 'set sync_elasticsearch = :one, topic = :topic, tags = :tags,' ' eye_catch_url=:eye_catch_url, title = :title, price = :price', ExpressionAttributeValues=info_expression_attribute_values)
def __update_article_info(self, article_content_edit): article_info_table = self.dynamodb.Table( os.environ['ARTICLE_INFO_TABLE_NAME']) article_info_table.update_item( Key={ 'article_id': self.params['article_id'], }, UpdateExpression= ("set title = :title, overview=:overview, eye_catch_url=:eye_catch_url, " "sync_elasticsearch=:sync_elasticsearch, topic=:topic, tags=:tags" ), ExpressionAttributeValues={ ':title': article_content_edit['title'], ':overview': article_content_edit['overview'], ':eye_catch_url': article_content_edit['eye_catch_url'], ':sync_elasticsearch': 1, ':topic': self.params['topic'], ':tags': TagUtil.get_tags_with_name_collation(self.elasticsearch, self.params.get('tags')) })
def __update_paid_article_info(self, article_info_table): info_expression_attribute_values = { ':article_status': 'public', ':one': 1, ':topic': self.params['topic'], ':tags': TagUtil.get_tags_with_name_collation(self.elasticsearch, self.params.get('tags')), ':eye_catch_url': self.params.get('eye_catch_url'), ':price': self.params.get('price') } info_update_expression = 'set #attr = :article_status, sync_elasticsearch = :one, topic = :topic, tags = ' \ ':tags, eye_catch_url=:eye_catch_url, price = :price' article_info_table.update_item( Key={ 'article_id': self.params['article_id'], }, UpdateExpression=info_update_expression, ExpressionAttributeNames={'#attr': 'status'}, ExpressionAttributeValues=info_expression_attribute_values)
def validate_params(self): validate(self.params, self.get_schema()) if self.params.get('tags'): ParameterUtil.validate_array_unique(self.params['tags'], 'tags', case_insensitive=True) TagUtil.validate_format(self.params['tags']) DBUtil.validate_article_existence( self.dynamodb, self.params['article_id'], user_id=self.event['requestContext']['authorizer']['claims'] ['cognito:username'], status='draft') DBUtil.validate_topic(self.dynamodb, self.params['topic'])
def validate_params(self): if self.event.get('pathParameters') is None: raise ValidationError('pathParameters is required') validate(self.params, self.get_schema()) if self.params.get('tags'): ParameterUtil.validate_array_unique(self.params['tags'], 'tags', case_insensitive=True) TagUtil.validate_format(self.params['tags']) DBUtil.validate_article_existence( self.dynamodb, self.params['article_id'], user_id=self.event['requestContext']['authorizer']['claims']['cognito:username'], status='public' ) DBUtil.validate_topic(self.dynamodb, self.params['topic'])
def validate_params(self): UserUtil.verified_phone_and_email(self.event) if self.event['requestContext']['authorizer']['claims'].get( 'custom:private_eth_address') is None: raise ValidationError('not exists private_eth_address') validate(self.params, self.get_schema()) if self.params.get('tags'): ParameterUtil.validate_array_unique(self.params['tags'], 'tags', case_insensitive=True) TagUtil.validate_format(self.params['tags']) DBUtil.validate_article_existence( self.dynamodb, self.params['article_id'], user_id=self.event['requestContext']['authorizer']['claims'] ['cognito:username'], status='draft') DBUtil.validate_topic(self.dynamodb, self.params['topic'])
def __update_article_info(self, article_content_edit, article_info_table): article_info_table.update_item( Key={ 'article_id': self.params['article_id'], }, UpdateExpression= ("set title = :title, eye_catch_url=:eye_catch_url, " "sync_elasticsearch=:sync_elasticsearch, topic=:topic, tags=:tags" ), ExpressionAttributeValues={ ':title': article_content_edit['title'], ':eye_catch_url': self.params.get('eye_catch_url'), ':sync_elasticsearch': 1, ':topic': self.params['topic'], ':tags': TagUtil.get_tags_with_name_collation(self.elasticsearch, self.params.get('tags')) })
def test_main_ok(self): TagUtil.create_tag(self.elasticsearch, 'a') TagUtil.create_tag(self.elasticsearch, 'B') self.elasticsearch.indices.refresh(index='tags') 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']) 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'] 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(self): TagUtil.create_tag(self.elasticsearch, 'a') TagUtil.create_tag(self.elasticsearch, 'B') self.elasticsearch.indices.refresh(index='tags') params = { 'pathParameters': { 'article_id': 'publicId0001' }, 'body': { 'topic': 'crypto', 'tags': ['A', 'B', 'C', 'D', 'E' * 25], 'eye_catch_url': 'https://' + os.environ['DOMAIN'] + '/00001.png' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } params['body'] = json.dumps(params['body']) article_info_before = self.article_info_table.scan()['Items'] article_content_before = self.article_content_table.scan()['Items'] article_content_edit_before = self.article_content_edit_table.scan( )['Items'] article_history_before = self.article_history_table.scan()['Items'] response = MeArticlesPublicRepublishWithHeader( params, {}, dynamodb=self.dynamodb, elasticsearch=self.elasticsearch).main() article_info_after = self.article_info_table.scan()['Items'] article_content_after = self.article_content_table.scan()['Items'] article_content_edit_after = self.article_content_edit_table.scan( )['Items'] article_history_after = self.article_history_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] expected_item = { 'article_id': 'publicId0001', 'user_id': 'test01', 'title': 'edit_title1_edit', 'body': 'edit_body1_edit', 'eye_catch_url': 'https://' + os.environ['DOMAIN'] + '/00001.png', 'topic': 'crypto', 'tags': ['a', 'B', 'C', 'D', 'E' * 25] } article_info_param_names = ['eye_catch_url', 'title'] article_content_param_names = ['title', 'body'] self.assertEqual(response['statusCode'], 200) self.assertEqual(article_info['status'], 'public') self.assertEqual(article_info['sync_elasticsearch'], 1) self.assertEqual( params['requestContext']['authorizer']['claims'] ['cognito:username'], article_info['user_id']) for key in article_info_param_names: self.assertEqual(expected_item[key], article_info[key]) for key in article_content_param_names: self.assertEqual(expected_item[key], article_content[key]) self.assertEqual(expected_item[key], article_history[key]) self.assertEqual(len(article_info_after) - len(article_info_before), 0) self.assertEqual( len(article_content_after) - len(article_content_before), 0) self.assertEqual( len(article_content_edit_after) - len(article_content_edit_before), -1) self.assertEqual( len(article_history_after) - len(article_history_before), 1)
def expected_raise_no_error(args): try: TagUtil.validate_format(args) except ValidationError: self.fail('expected no error is raised')
def expected_raise_error(args): with self.assertRaises(ValidationError): TagUtil.validate_format(args)