def test_main_ok(self):
        params = {
            'queryStringParameters': {
                'limit': '2'
            },
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        response = ArticlesCommentsIndex(params, {}, self.dynamodb).main()

        expected_items = [self.comment_items[2], self.comment_items[1]]
        expected_last_evaluated_key = {
            'comment_id': self.comment_items[1]['comment_id'],
            'sort_key': self.comment_items[1]['sort_key'],
            'article_id': self.comment_items[1]['article_id']
        }

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(json.loads(response['body'])['Items'], expected_items)
        self.assertEqual(
            json.loads(response['body'])['LastEvaluatedKey'],
            expected_last_evaluated_key)
    def test_main_with_no_limit(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        comment_table = self.dynamodb.Table(os.environ['COMMENT_TABLE_NAME'])
        for i in range(11):
            comment_table.put_item(
                Item={
                    'comment_id': 'comment1000' + str(i),
                    'article_id': 'publicId0001',
                    'user_id': 'test_user_01',
                    'sort_key': 152015027200000 + i,
                    'created_at': 1520150272,
                    'text': 'コメントの内容'
                })

        response = ArticlesCommentsIndex(params, {}, self.dynamodb).main()

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(json.loads(response['body'])['Items']), 10)
    def test_call_validate_article_existence(self):
        params = {'pathParameters': {'article_id': 'testidlike02'}}

        mock_lib = MagicMock()
        with patch('articles_comments_index.DBUtil', mock_lib):
            ArticlesCommentsIndex(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):
     response = ArticlesCommentsIndex(params, {}, self.dynamodb).main()
     self.assertEqual(response['statusCode'], 400)
예제 #5
0
def lambda_handler(event, context):
    articles_comments_index = ArticlesCommentsIndex(event=event,
                                                    context=context,
                                                    dynamodb=dynamodb)
    return articles_comments_index.main()