def test_call_validate_article_existence(self):
        params = {
            'pathParameters': {
                'article_id': 'draftId00001'
            },
            'body': {
                'eye_catch_url': 'http://example.com/update',
                'title': 'update title',
                'body': '<p>update body</p>',
                'overview': 'update overview'
            },
            '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_update.DBUtil', mock_lib):
            MeArticlesPublicUpdate(params, {}, 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.assertTrue(kwargs['user_id'])
            self.assertEqual(kwargs['status'], 'public')
            self.assertEqual(kwargs['version'], 1)
    def test_main_ok_with_no_article_edit_content(self):
        prefix = 'http://'
        params = {
            'pathParameters': {
                'article_id': 'publicId0002'
            },
            'body': {
                'eye_catch_url': prefix + 'A' * (2048 - len(prefix)),
                'title': 'A' * 255,
                'body': 'A' * 65535,
                'overview': 'A' * 100
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        article_content_edit_before = self.article_content_edit_table.scan(
        )['Items']

        params['body'] = json.dumps(params['body'])
        response = MeArticlesPublicUpdate(params, {}, self.dynamodb).main()

        article_content_edit_after = self.article_content_edit_table.scan(
        )['Items']

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(
            len(article_content_edit_after) - len(article_content_edit_before),
            1)

        article_content_edit = self.article_content_edit_table.get_item(
            Key={'article_id': params['pathParameters']['article_id']})['Item']

        self.assertEqual(
            params['requestContext']['authorizer']['claims']
            ['cognito:username'], article_content_edit['user_id'])

        for key, value in json.loads(params['body']).items():
            self.assertEqual(value, article_content_edit[key])
    def test_main_ok(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'eye_catch_url': 'http://example.com/update',
                'title': 'update title',
                'body': '<p>update body</p>',
                'overview': 'update overview'
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        article_content_edit_before = self.article_content_edit_table.scan(
        )['Items']

        params['body'] = json.dumps(params['body'])
        response = MeArticlesPublicUpdate(params, {}, self.dynamodb).main()

        article_content_edit_after = self.article_content_edit_table.scan(
        )['Items']

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(
            len(article_content_edit_after) - len(article_content_edit_before),
            0)

        article_content_edit = self.article_content_edit_table.get_item(
            Key={'article_id': params['pathParameters']['article_id']})['Item']

        self.assertEqual(
            params['requestContext']['authorizer']['claims']
            ['cognito:username'], article_content_edit['user_id'])

        for key, value in json.loads(params['body']).items():
            self.assertEqual(value, article_content_edit[key])
    def test_main_ok_with_empty_string(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'eye_catch_url': 'http://example.com/update',
                'title': '',
                'body': '',
                'overview': ''
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        params['body'] = json.dumps(params['body'])
        response = MeArticlesPublicUpdate(params, {}, self.dynamodb).main()
        article_content_edit = self.article_content_edit_table.get_item(
            Key={'article_id': params['pathParameters']['article_id']})['Item']

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

        expected_items = {
            'article_id': 'publicId0001',
            'eye_catch_url': 'http://example.com/update',
            'title': None,
            'body': None,
            'overview': None,
            'user_id': 'test01'
        }

        self.assertEqual(article_content_edit, expected_items)
    def assert_bad_request(self, params):
        function = MeArticlesPublicUpdate(params, {}, self.dynamodb)
        response = function.main()

        self.assertEqual(response['statusCode'], 400)
Exemplo n.º 6
0
def lambda_handler(event, context):
    me_articles_public_update = MeArticlesPublicUpdate(event, context,
                                                       dynamodb)
    return me_articles_public_update.main()