Skip to content

Commit

Permalink
Merge pull request #365 from AlisProject/develop
Browse files Browse the repository at this point in the history
Update 0.46.0
  • Loading branch information
keillera authored Jul 18, 2019
2 parents 6fd2aab + 3e206f6 commit 4e092f5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/handlers/me/articles/like/create/me_articles_like_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ def validate_params(self):
)

def exec_main_proc(self):
# 「いいね」情報登録処理
article_user_id = self.__get_article_user_id(self.event['pathParameters']['article_id'])
try:
article_liked_user_table = self.dynamodb.Table(os.environ['ARTICLE_LIKED_USER_TABLE_NAME'])
self.__create_article_liked_user(article_liked_user_table)
self.__create_article_liked_user(article_liked_user_table, article_user_id)
except ClientError as e:
if e.response['Error']['Code'] == 'ConditionalCheckFailedException':
return {
Expand All @@ -50,14 +52,16 @@ def exec_main_proc(self):
else:
raise

try:
article_info_table = self.dynamodb.Table(os.environ['ARTICLE_INFO_TABLE_NAME'])
article_info = article_info_table.get_item(Key={'article_id': self.params['article_id']}).get('Item')
self.__create_like_notification(article_info)
self.__update_unread_notification_manager(article_info)
except Exception as e:
logging.fatal(e)
traceback.print_exc()
# 通知情報登録処理。「セルフいいね」だった場合は通知を行わない
if article_user_id != self.event['requestContext']['authorizer']['claims']['cognito:username']:
try:
article_info_table = self.dynamodb.Table(os.environ['ARTICLE_INFO_TABLE_NAME'])
article_info = article_info_table.get_item(Key={'article_id': self.params['article_id']}).get('Item')
self.__create_like_notification(article_info)
self.__update_unread_notification_manager(article_info)
except Exception as e:
logging.fatal(e)
traceback.print_exc()

return {
'statusCode': 200
Expand Down Expand Up @@ -104,12 +108,12 @@ def __update_unread_notification_manager(self, article_info):
ExpressionAttributeValues={':unread': True}
)

def __create_article_liked_user(self, article_liked_user_table):
def __create_article_liked_user(self, article_liked_user_table, article_user_id):
epoch = int(time.time())
article_liked_user = {
'article_id': self.event['pathParameters']['article_id'],
'user_id': self.event['requestContext']['authorizer']['claims']['cognito:username'],
'article_user_id': self.__get_article_user_id(self.event['pathParameters']['article_id']),
'article_user_id': article_user_id,
'created_at': epoch,
'target_date': time.strftime('%Y-%m-%d', time.gmtime(epoch)),
'sort_key': TimeUtil.generate_sort_key()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,42 @@ def test_main_with_updating_notification(self):
self.assertEqual(notification, expected_notification)
self.assertEqual(len(notification_after), len(notification_before))

@patch('time.time', MagicMock(return_value=1520150272.000015))
def test_main_ok_with_self_liked_user(self):
params = {
'pathParameters': {
'article_id': self.article_info_table_items[2]['article_id']
},
'requestContext': {
'authorizer': {
'claims': {
'cognito:username': self.article_info_table_items[2]['user_id'],
'phone_number_verified': 'true',
'email_verified': 'true'
}
}
}
}

article_liked_user_table = self.dynamodb.Table(os.environ['ARTICLE_LIKED_USER_TABLE_NAME'])
notification_table = self.dynamodb.Table(os.environ['NOTIFICATION_TABLE_NAME'])
unread_notification_manager_table = self.dynamodb.Table(os.environ['UNREAD_NOTIFICATION_MANAGER_TABLE_NAME'])
article_liked_user_before = article_liked_user_table.scan()['Items']
notification_before = notification_table.scan()['Items']
unread_notification_manager_before = unread_notification_manager_table.scan()['Items']

response = MeArticlesLikeCreate(event=params, context={}, dynamodb=self.dynamodb).main()

notification_after = notification_table.scan()['Items']
unread_notification_manager_after = unread_notification_manager_table.scan()['Items']
article_liked_user_after = article_liked_user_table.scan()['Items']

self.assertEqual(response['statusCode'], 200)
# article_liked_user へは書き込まれるが、通知情報へは書き込まれないことを確認
self.assertEqual(len(article_liked_user_after), len(article_liked_user_before) + 1)
self.assertEqual(notification_before, notification_after)
self.assertEqual(unread_notification_manager_before, unread_notification_manager_after)

@patch('me_articles_like_create.MeArticlesLikeCreate._MeArticlesLikeCreate__create_like_notification',
MagicMock(side_effect=Exception()))
def test_raise_exception_in_creating_notification(self):
Expand Down

0 comments on commit 4e092f5

Please sign in to comment.