Exemple #1
0
    def assert_bad_request(self, params):
        me_articles_drafts_publish_with_header = MeArticlesDraftsPublishWithHeader(
            params, {},
            dynamodb=self.dynamodb,
            elasticsearch=self.elasticsearch)
        response = me_articles_drafts_publish_with_header.main()

        self.assertEqual(response['statusCode'], 400)
Exemple #2
0
 def test_validation_not_exists_private_eth_address(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'])
     response = MeArticlesDraftsPublishWithHeader(
         params, {},
         dynamodb=self.dynamodb,
         elasticsearch=self.elasticsearch).main()
     self.assertEqual(response['statusCode'], 400)
     self.assertEqual(
         response['body'],
         '{"message": "Invalid parameter: not exists private_eth_address"}')
Exemple #3
0
    def test_not_call_validate_img_url(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001'
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address':
                        '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        mock_lib = MagicMock()
        with patch('me_articles_drafts_publish_with_header.TextSanitizer',
                   mock_lib):
            MeArticlesDraftsPublishWithHeader(
                params, {},
                dynamodb=self.dynamodb,
                elasticsearch=self.elasticsearch).main()
            self.assertEqual(mock_lib.validate_img_url.call_count, 0)
Exemple #4
0
    def test_create_and_count_raise_exception(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001'
            },
            'body': {
                'topic': 'crypto',
                'tags': ['A']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address':
                        '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

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

        self.assertEqual(response['statusCode'], 200)
    def test_validation_paid_article_publish_without_price_ng(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001',
            },
            '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 = MeArticlesDraftsPublishWithHeader(
            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_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_drafts_publish_with_header.TextSanitizer',
                   mock_lib):
            MeArticlesDraftsPublishWithHeader(
                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': 'draftId00001'
            },
            '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_drafts_publish_with_header.ParameterUtil',
                   mock_lib):
            MeArticlesDraftsPublishWithHeader(
                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)
Exemple #8
0
    def test_main_ok_with_article_content_edit(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00002'
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address':
                        '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        article_info_before = self.article_info_table.scan()['Items']
        article_history_before = self.article_history_table.scan()['Items']
        article_content_edit_before = self.article_content_edit_table.scan(
        )['Items']

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

        article_info_after = self.article_info_table.scan()['Items']
        article_history_after = self.article_history_table.scan()['Items']
        article_content_edit_after = self.article_content_edit_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]

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(article_info['status'], 'public')
        self.assertEqual(article_info['topic'], 'crypto')
        self.assertIsNone(article_info.get('tags'))
        self.assertIsNone(article_info.get('eye_catch_url'))
        self.assertEqual(article_content['title'], article_history['title'])
        self.assertEqual(article_content['body'], article_history['body'])
        self.assertEqual(len(article_info_after) - len(article_info_before), 0)
        self.assertEqual(
            len(article_history_after) - len(article_history_before), 1)
        self.assertEqual(
            len(article_content_edit_after) - len(article_content_edit_before),
            -1)
    def test_main_ok_article_history_arleady_exists(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00003'
            },
            '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_history_before = self.article_history_table.scan()['Items']
        article_content_edit_before = self.article_content_edit_table.scan(
        )['Items']

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

        article_info_after = self.article_info_table.scan()['Items']
        article_history_after = self.article_history_table.scan()['Items']
        article_content_edit_after = self.article_content_edit_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]

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(article_info['status'], 'public')
        self.assertEqual(article_content['title'], article_history['title'])
        self.assertEqual(article_content['body'], article_history['body'])
        self.assertEqual(article_info['sort_key'], 1520150272000000)
        self.assertEqual(article_info['published_at'], 1520150000)
        self.assertEqual(len(article_info_after) - len(article_info_before), 0)
        self.assertEqual(
            len(article_history_after) - len(article_history_before), 1)
        self.assertEqual(
            len(article_content_edit_after) - len(article_content_edit_before),
            0)
Exemple #10
0
    def test_call_validate_methods(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001',
            },
            'body': {
                'topic': 'crypto'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address':
                        '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }
        params['body'] = json.dumps(params['body'])

        mock_lib = MagicMock()
        with patch('me_articles_drafts_publish_with_header.DBUtil', mock_lib):
            MeArticlesDraftsPublishWithHeader(
                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'], 'draft')
            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], 'draftId00001')
Exemple #11
0
    def test_call_tag_util_methods(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001'
            },
            'body': {
                'topic': 'crypto',
                'tags': ['A']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address':
                        '0x1234567890123456789012345678901234567890',
                        '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_drafts_publish_with_header.TagUtil', mock_lib):
            MeArticlesDraftsPublishWithHeader(
                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'])
Exemple #12
0
    def test_validation_paid_article_publish_ok(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001',
            },
            'body': {
                'topic': 'crypto',
                'tags': ['A', 'B', 'C', 'D', 'E' * 25],
                'eye_catch_url':
                'https://' + os.environ['DOMAIN'] + '/test.png',
                'paid_body': '有料記事の内容',
                'price': 10**20
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'custom:private_eth_address':
                        '0x1234567890123456789012345678901234567890',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

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

        article_info_before = self.article_info_table.scan()['Items']
        article_history_before = self.article_history_table.scan()['Items']
        article_content_edit_before = self.article_content_edit_table.scan(
        )['Items']

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

        article_info_after = self.article_info_table.scan()['Items']
        article_history_after = self.article_history_table.scan()['Items']
        article_content_edit_after = self.article_content_edit_table.scan(
        )['Items']

        self.assertEqual(response['statusCode'], 200)

        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]

        self.assertEqual(article_info['status'], 'public')
        self.assertEqual(article_info['sort_key'], 1520150552000000)
        self.assertEqual(article_info['published_at'], 1525000000)
        self.assertEqual(article_info['sync_elasticsearch'], 1)
        self.assertEqual(article_info['topic'], 'crypto')
        self.assertEqual(article_info['tags'], ['A', 'B', 'C', 'D', 'E' * 25])
        self.assertEqual(article_info['eye_catch_url'],
                         'https://' + os.environ['DOMAIN'] + '/test.png')
        self.assertEqual(article_info['price'], 10**20)
        self.assertEqual(article_content['title'], article_history['title'])
        self.assertEqual(article_content['body'], article_history['body'])
        self.assertEqual(article_content['paid_body'],
                         article_history['paid_body'])
        self.assertEqual(article_history['price'], 10**20)
        self.assertEqual(len(article_info_after) - len(article_info_before), 0)
        self.assertEqual(
            len(article_history_after) - len(article_history_before), 1)
        self.assertEqual(
            len(article_content_edit_after) - len(article_content_edit_before),
            0)
def lambda_handler(event, context):
    me_articles_drafts_publish_with_header = MeArticlesDraftsPublishWithHeader(
        event, context, dynamodb=dynamodb, elasticsearch=elasticsearch)
    return me_articles_drafts_publish_with_header.main()