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

        article_pv_user_table = self.dynamodb.Table(
            os.environ['ARTICLE_PV_USER_TABLE_NAME'])
        article_pv_user_before = article_pv_user_table.scan()['Items']

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

        article_pv_user_after = article_pv_user_table.scan()['Items']

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(article_pv_user_after),
                         len(article_pv_user_before))
    def test_main_ok(self):
        params = {
            'pathParameters': {
                'article_id': self.article_pv_user_table_items[0]['article_id']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        article_pv_user_table = self.dynamodb.Table(
            os.environ['ARTICLE_PV_USER_TABLE_NAME'])
        article_pv_user_before = article_pv_user_table.scan()['Items']

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

        article_pv_user_after = article_pv_user_table.scan()['Items']

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

        article_pv_user = self.get_article_pv_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_pv_user_after),
                         len(article_pv_user_before) + 1)
        article_pv_user_param_names = [
            'article_id', 'user_id', 'article_user_id', 'created_at',
            'target_date', 'sort_key'
        ]
        for key in article_pv_user_param_names:
            self.assertEqual(expected_items[key], article_pv_user[key])
    def test_call_validate_article_existence(self):
        params = {
            'pathParameters': {
                'article_id': 'testid000002'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        mock_lib = MagicMock()
        with patch('me_articles_pv_create.DBUtil', mock_lib):
            MeArticlesPvCreate(event=params,
                               context={},
                               dynamodb=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.assertEqual(kwargs['status'], 'public')
    def assert_bad_request(self, params):
        test_function = MeArticlesPvCreate(params, {}, self.dynamodb)
        response = test_function.main()

        self.assertEqual(response['statusCode'], 400)
예제 #5
0
def lambda_handler(event, context):
    me_articles_pv_create = MeArticlesPvCreate(event=event,
                                               context=context,
                                               dynamodb=dynamodb)
    return me_articles_pv_create.main()