def assert_bad_request(self, params):
        test_function = MeArticlesImagesCreate(params, {},
                                               dynamodb=self.dynamodb,
                                               s3=self.s3)
        response = test_function.main()

        self.assertEqual(response['statusCode'], 400)
    def test_call_validate_article_existence(self):
        image_data = Image.new('RGB', (1, 1))
        buf = BytesIO()
        image_format = 'png'
        image_data.save(buf, format=image_format)

        target_article_info = self.article_info_table_items[0]
        params = {
            'headers': {
                    'content-type': 'image/' + image_format
            },
            'pathParameters': {
                'article_id': target_article_info['article_id']
            },
            'body': json.dumps({'article_image': base64.b64encode(buf.getvalue()).decode('ascii')}),
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': target_article_info['user_id'],
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

        mock_lib = MagicMock()
        with patch('me_articles_images_create.DBUtil', mock_lib):
            MeArticlesImagesCreate(params, {}, self.dynamodb, self.s3).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'])
 def test_validation_article_image_over_data_size(self):
     image_format = 'png'
     target_article_info = self.article_info_table_items[0]
     params = {
         'headers': {
             'content-type': 'image/' + image_format
         },
         'pathParameters': {
             'article_id': target_article_info['article_id']
         },
         'body':
         json.dumps({
             'article_image':
             'a' * (settings.parameters['article_image']['maxLength'] + 1)
         }),
         'requestContext': {
             'authorizer': {
                 'claims': {
                     'cognito:username': target_article_info['user_id']
                 }
             }
         }
     }
     response = MeArticlesImagesCreate(params, {},
                                       dynamodb=self.dynamodb,
                                       s3=self.s3).main()
     self.assertEqual(response['statusCode'], 400)
     self.assertRegex(response['body'], 'Invalid parameter')
    def test_main_ok_max_size_jepg(self):
        image_data = Image.new('RGB', (settings.ARTICLE_IMAGE_MAX_WIDTH,
                                       settings.ARTICLE_IMAGE_MAX_HEIGHT))
        buf = BytesIO()
        image_format = 'jpeg'
        image_data.save(buf, format=image_format)

        target_article_info = self.article_info_table_items[0]
        params = {
            'headers': {
                'content-type': 'image/' + image_format
            },
            'pathParameters': {
                'article_id': target_article_info['article_id']
            },
            'body':
            json.dumps({
                'article_image':
                base64.b64encode(buf.getvalue()).decode('ascii')
            }),
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': target_article_info['user_id']
                    }
                }
            }
        }

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

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

        image_url_path = target_article_info[
            'user_id'] + '/' + target_article_info['article_id'] + '/'
        image_file_name = 'uuid.' + image_format
        key = settings.S3_ARTICLES_IMAGES_PATH + image_url_path + image_file_name
        expected_item = {
            'image_url': 'https://' + os.environ['DOMAIN'] + '/' + key
        }
        self.assertEqual(json.loads(response['body']), expected_item)
        self.assertTrue(self.equal_size_to_s3_image(key, image_data.size))
예제 #5
0
def lambda_handler(event, context):
    me_articles_images_create = MeArticlesImagesCreate(event=event,
                                                       context=context,
                                                       dynamodb=dynamodb,
                                                       s3=s3)
    return me_articles_images_create.main()