def test_create_notification_and_unread_notification_manager(self):
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[1]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        notification_table = self.dynamodb.Table(
            os.environ['NOTIFICATION_TABLE_NAME'])
        unread_notification_manager_table = self.dynamodb.Table(
            os.environ['UNREAD_NOTIFICATION_MANAGER_TABLE_NAME'])

        notification_before = notification_table.scan()['Items']
        unread_notification_manager_before = unread_notification_manager_table.scan(
        )['Items']

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

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

        expected_notification = {
            'notification_id': 'like-article_user_id_01-testid000001',
            'user_id': 'article_user_id_01',
            'sort_key': 1520150272000015,
            'article_id': 'testid000001',
            'article_title': 'title2',
            'type': 'like',
            'liked_count': 1,
            'created_at': 1520150272
        }

        notification = notification_table.get_item(
            Key={
                'notification_id': 'like-article_user_id_01-testid000001'
            }).get('Item')
        unread_notification_manager = unread_notification_manager_table.get_item(
            Key={
                'user_id': 'article_user_id_01'
            }).get('Item')

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(notification, expected_notification)
        self.assertEqual(len(notification_after), len(notification_before) + 1)
        self.assertEqual(unread_notification_manager['unread'], True)
        self.assertEqual(len(unread_notification_manager_after),
                         len(unread_notification_manager_before) + 1)
    def test_create_notification_and_unread_notification_manager(self):
        settings.LIKED_TWEET_COUNT = 100
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[1]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address': '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        notification_table = self.dynamodb.Table(os.environ['NOTIFICATION_TABLE_NAME'])
        unread_notification_manager_table = self.dynamodb.Table(os.environ['UNREAD_NOTIFICATION_MANAGER_TABLE_NAME'])

        notification_before = notification_table.scan()['Items']
        unread_notification_manager_before = unread_notification_manager_table.scan()['Items']

        mock_lib = MagicMock()
        with patch('me_articles_like_create.TwitterUtil', mock_lib):
            article_liked_user = MeArticlesLikeCreate(event=params, context={}, dynamodb=self.dynamodb)
            response = article_liked_user.main()
            # いいねの数が規定数ではないため、実行されないこと
            self.assertEqual(mock_lib.call_count, 0)

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

        expected_notification = {
            'notification_id': 'like-article_user_id_01-testid000001',
            'user_id': 'article_user_id_01',
            'sort_key': 1520150272000015,
            'article_id': 'testid000001',
            'article_title': 'title2',
            'type': 'like',
            'liked_count': 1,
            'created_at': 1520150272
        }

        notification = notification_table.get_item(Key={'notification_id': 'like-article_user_id_01-testid000001'}).get('Item')
        unread_notification_manager = unread_notification_manager_table.get_item(
            Key={'user_id': 'article_user_id_01'}
        ).get('Item')

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(notification, expected_notification)
        self.assertEqual(len(notification_after), len(notification_before) + 1)
        self.assertEqual(unread_notification_manager['unread'], True)
        self.assertEqual(len(unread_notification_manager_after), len(unread_notification_manager_before) + 1)
    def test_main_with_updating_notification(self):
        settings.LIKED_TWEET_COUNT = 5
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[2]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address': '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        notification_table = self.dynamodb.Table(os.environ['NOTIFICATION_TABLE_NAME'])
        notification_before = notification_table.scan()['Items']

        mock_lib = MagicMock()
        with patch('me_articles_like_create.TwitterUtil', mock_lib):
            article_liked_user = MeArticlesLikeCreate(event=params, context={}, dynamodb=self.dynamodb)
            response = article_liked_user.main()
            args, _ = mock_lib.return_value.post_tweet.call_args
            self.assertTrue(mock_lib.return_value.post_tweet.called)
            self.assertEqual(
                args[0],
                {'text':
                    'title3_updated\n'
                    'https://dummy/article_user_id_02/articles/testid000002\n'
                    '※人気記事ボット🤖'
                 }
            )

        notification_after = notification_table.scan()['Items']

        expected_notification = {
            'notification_id': 'like-article_user_id_02-testid000002',
            'user_id': 'article_user_id_02',
            'sort_key': 1520150272000015,
            'article_id': 'testid000002',
            'article_title': 'title3_updated',
            'type': 'like',
            'liked_count': 5,
            'created_at': 1520150272
        }

        notification = notification_table.get_item(Key={'notification_id': 'like-article_user_id_02-testid000002'}).get('Item')

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(notification, expected_notification)
        self.assertEqual(len(notification_after), len(notification_before))
Example #4
0
    def test_main_ok_exist_article_id(self):
        params = {
            'pathParameters': {
                'article_id':
                self.article_liked_user_table_items[0]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        article_liked_user_table = self.dynamodb.Table(
            os.environ['ARTICLE_LIKED_USER_TABLE_NAME'])
        article_liked_user_before = article_liked_user_table.scan()['Items']

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

        article_liked_user_after = article_liked_user_table.scan()['Items']

        target_article_id = params['pathParameters']['article_id']
        target_user_id = params['requestContext']['authorizer']['claims'][
            'cognito:username']

        article_liked_user = self.get_article_liked_user(
            target_article_id, target_user_id)

        expected_items = {
            'article_id': target_article_id,
            'user_id': target_user_id,
            'article_user_id': 'article_user_id_00',
            'created_at': 1520150272,
            'target_date': '2018-03-04',
            'sort_key': 1520150272000003
        }

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(article_liked_user_after),
                         len(article_liked_user_before) + 1)
        article_liked_user_param_names = [
            'article_id', 'user_id', 'article_user_id', 'created_at',
            'target_date', 'sort_key'
        ]
        for key in article_liked_user_param_names:
            self.assertEqual(expected_items[key], article_liked_user[key])
    def test_raise_exception_in_creating_notification(self):
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[0]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

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

        self.assertEqual(response['statusCode'], 200)
    def test_raise_exception_in_creating_notification(self):
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[0]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address': '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

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

        self.assertEqual(response['statusCode'], 200)
    def test_main_ok_exist_article_id(self):
        settings.LIKED_TWEET_COUNT = 2
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[0]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address': '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        article_liked_user_table = self.dynamodb.Table(os.environ['ARTICLE_LIKED_USER_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']
        unread_notification_manager_before = unread_notification_manager_table.scan()['Items']

        mock_lib = MagicMock()
        with patch('me_articles_like_create.TwitterUtil', mock_lib):
            article_liked_user = MeArticlesLikeCreate(event=params, context={}, dynamodb=self.dynamodb)
            response = article_liked_user.main()
            args, _ = mock_lib.return_value.post_tweet.call_args
            self.assertTrue(mock_lib.return_value.post_tweet.called)
            self.assertEqual(
                args[0],
                {'text':
                    'title1\n'
                    'https://dummy/article_user_id_00/articles/testid000000\n'
                    '#aaa #bbb #ccc #あいうえお\n'
                    '※人気記事ボット🤖'
                 }
            )

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

        target_article_id = params['pathParameters']['article_id']
        target_user_id = params['requestContext']['authorizer']['claims']['cognito:username']

        article_liked_user = self.get_article_liked_user(target_article_id, target_user_id)

        expected_items = {
            'article_id': target_article_id,
            'user_id': target_user_id,
            'article_user_id': 'article_user_id_00',
            'created_at': 1520150272,
            'target_date': '2018-03-04',
            'sort_key': 1520150272000003
        }

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(article_liked_user_after), len(article_liked_user_before) + 1)
        self.assertEqual(len(unread_notification_manager_after), len(unread_notification_manager_before))
        article_liked_user_param_names = ['article_id', 'user_id', 'article_user_id', 'created_at', 'target_date', 'sort_key']
        for key in article_liked_user_param_names:
            self.assertEqual(expected_items[key], article_liked_user[key])

        unread_notification_manager = unread_notification_manager_table.get_item(
            Key={'user_id': 'article_user_id_00'}
        ).get('Item')
        self.assertEqual(unread_notification_manager['unread'], True)
    def assert_bad_request(self, params):
        test_function = MeArticlesLikeCreate(params, {}, self.dynamodb)
        response = test_function.main()

        self.assertEqual(response['statusCode'], 400)
    def test_main_ok_exist_article_id(self):
        params = {
            'pathParameters': {
                'article_id': self.article_info_table_items[0]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        article_liked_user_table = self.dynamodb.Table(
            os.environ['ARTICLE_LIKED_USER_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']
        unread_notification_manager_before = unread_notification_manager_table.scan(
        )['Items']

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

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

        target_article_id = params['pathParameters']['article_id']
        target_user_id = params['requestContext']['authorizer']['claims'][
            'cognito:username']

        article_liked_user = self.get_article_liked_user(
            target_article_id, target_user_id)

        expected_items = {
            'article_id': target_article_id,
            'user_id': target_user_id,
            'article_user_id': 'article_user_id_00',
            'created_at': 1520150272,
            'target_date': '2018-03-04',
            'sort_key': 1520150272000003
        }

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(article_liked_user_after),
                         len(article_liked_user_before) + 1)
        self.assertEqual(len(unread_notification_manager_after),
                         len(unread_notification_manager_before))
        article_liked_user_param_names = [
            'article_id', 'user_id', 'article_user_id', 'created_at',
            'target_date', 'sort_key'
        ]
        for key in article_liked_user_param_names:
            self.assertEqual(expected_items[key], article_liked_user[key])

        unread_notification_manager = unread_notification_manager_table.get_item(
            Key={
                'user_id': 'article_user_id_00'
            }).get('Item')
        self.assertEqual(unread_notification_manager['unread'], True)
Example #10
0
def lambda_handler(event, context):
    articles_article_id_likes_post = MeArticlesLikeCreate(event=event,
                                                          context=context,
                                                          dynamodb=dynamodb)
    return articles_article_id_likes_post.main()