def assert_bad_request(self, params):
        me_articles_public_republish_with_header = MeArticlesPublicRepublishWithHeader(
            params, {},
            dynamodb=self.dynamodb,
            elasticsearch=self.elasticsearch)
        response = me_articles_public_republish_with_header.main()

        self.assertEqual(response['statusCode'], 400)
    def test_not_call_validate_img_url(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001'
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        mock_lib = MagicMock()
        with patch('me_articles_public_republish_with_header.TextSanitizer',
                   mock_lib):
            MeArticlesPublicRepublishWithHeader(
                params, {},
                dynamodb=self.dynamodb,
                elasticsearch=self.elasticsearch).main()
            self.assertEqual(mock_lib.validate_img_url.call_count, 0)
    def test_call_validate_img_url(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001',
                'eye_catch_url':
                'https://' + os.environ['DOMAIN'] + '/test.png'
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        mock_lib = MagicMock()
        with patch('me_articles_public_republish_with_header.TextSanitizer',
                   mock_lib):
            MeArticlesPublicRepublishWithHeader(
                params, {},
                dynamodb=self.dynamodb,
                elasticsearch=self.elasticsearch).main()

            self.assertTrue(mock_lib.validate_img_url.called)
            args, kwargs = mock_lib.validate_img_url.call_args
            self.assertEqual(args[0],
                             'https://' + os.environ['DOMAIN'] + '/test.png')
    def test_call_validate_array_unique(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            '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'])

        mock_lib = MagicMock()

        with patch('me_articles_public_republish_with_header.ParameterUtil',
                   mock_lib):
            MeArticlesPublicRepublishWithHeader(
                params, {},
                dynamodb=self.dynamodb,
                elasticsearch=self.elasticsearch).main()

            self.assertTrue(mock_lib.validate_array_unique.called)
            args, kwargs = mock_lib.validate_array_unique.call_args
            self.assertEqual(args[0], ['A', 'B', 'C', 'D', 'E' * 25])
            self.assertEqual(args[1], 'tags')
            self.assertEqual(kwargs['case_insensitive'], True)
    def test_create_and_count_raise_exception(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'topic': 'crypto',
                'tags': ['A'],
                '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'])

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

        self.assertEqual(response['statusCode'], 200)
    def test_validation_paid_article_republish_without_price_ng(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001',
            },
            'body': {
                'topic': 'crypto',
                'tags': ['A', 'B', 'C', 'D', 'E' * 25],
                'eye_catch_url':
                'https://' + os.environ['DOMAIN'] + '/test.png',
                'paid_body': '有料記事のコンテンツです'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        params['body'] = json.dumps(params['body'])

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

        self.assertEqual(response['statusCode'], 400)
        self.assertEqual(
            response['body'],
            '{"message": "Invalid parameter: Both paid body and price are required."}'
        )
    def test_main_ng_with_none_body(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        self.article_content_edit_table.update_item(
            Key={'article_id': params['pathParameters']['article_id']},
            UpdateExpression="set body = :body",
            ExpressionAttributeValues={':body': None})

        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']

        self.assertEqual(response['statusCode'], 400)
        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),
            0)
        self.assertEqual(
            len(article_history_after) - len(article_history_before), 0)
    def test_call_tag_util_methods(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'topic': 'crypto',
                'tags': ['A'],
                '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'])

        mock_lib = MagicMock()
        mock_lib.get_tags_with_name_collation.return_value = ['A']
        with patch('me_articles_public_republish_with_header.TagUtil',
                   mock_lib):
            MeArticlesPublicRepublishWithHeader(
                params, {},
                dynamodb=self.dynamodb,
                elasticsearch=self.elasticsearch).main()

            self.assertTrue(mock_lib.validate_format.called)
            args, _ = mock_lib.validate_format.call_args
            self.assertEqual(args[0], ['A'])

            self.assertTrue(mock_lib.get_tags_with_name_collation.called)
            args, _ = mock_lib.get_tags_with_name_collation.call_args
            self.assertEqual(args[1], ['A'])

            self.assertTrue(mock_lib.create_and_count.called)
            args, _ = mock_lib.create_and_count.call_args

            self.assertTrue(args[0])
            self.assertEqual(args[1], ['a', 'b', 'c'])
            self.assertEqual(args[2], ['A'])
    def test_main_ok_with_no_article_content_edit(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0002'
            },
            'body': {
                'topic': 'crypto'
            },
            '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']

        self.assertEqual(response['statusCode'], 404)
        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),
            0)
        self.assertEqual(
            len(article_history_after) - len(article_history_before), 0)
    def test_call_validate_methods(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        mock_lib = MagicMock()
        with patch('me_articles_public_republish_with_header.DBUtil',
                   mock_lib):
            MeArticlesPublicRepublishWithHeader(
                params, {},
                dynamodb=self.dynamodb,
                elasticsearch=self.elasticsearch).main()

            self.assertTrue(mock_lib.validate_article_existence.called)
            args, kwargs = mock_lib.validate_article_existence.call_args
            self.assertTrue(args[0])
            self.assertTrue(args[1])
            self.assertTrue(kwargs['user_id'])
            self.assertEqual(kwargs['status'], 'public')
            self.assertEqual(kwargs['version'], 2)

            self.assertTrue(mock_lib.validate_topic.called)
            args, kwargs = mock_lib.validate_topic.call_args
            self.assertTrue(args[0])
            self.assertEqual(args[1], 'crypto')

            self.assertTrue(mock_lib.validate_exists_title_and_body.called)
            args, kwargs = mock_lib.validate_exists_title_and_body.call_args
            self.assertTrue(args[0])
            self.assertEqual(args[1], 'publicId0001')
    def test_make_article_free_ok(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0003'
            },
            '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': 'publicId0003',
            'user_id': 'test01',
            'title': 'edit_title3_edit',
            'body': 'edit_body3_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(article_info.get('price'), None)
        self.assertEqual(article_content.get('paid_body'), None)

        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)
Ejemplo n.º 12
0
def lambda_handler(event, context):
    me_articles_public_republish_with_header = MeArticlesPublicRepublishWithHeader(
        event, context, dynamodb=dynamodb, elasticsearch=elasticsearch)
    return me_articles_public_republish_with_header.main()