Ejemplo n.º 1
0
    def test_main_ok(self):
        params = {
            'body': {
                "eye_catch_url": "http://example.com",
                "title": "sample title",
                "body": "<p>sample body</p>",
                "overview": " "
            },
            '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']

        me_articles_drafts_create = MeArticlesDraftsCreate(
            params, {}, self.dynamodb)

        response = me_articles_drafts_create.main()

        article_info_after = self.article_info_table.scan()['Items']
        article_content_after = self.article_content_table.scan()['Items']

        self.assertEqual(json.loads(response['body']),
                         {'article_id': 'HOGEHOGEHOGE'})

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(article_info_after) - len(article_info_before), 1)
        self.assertEqual(
            len(article_content_after) - len(article_content_before), 1)

        article_info_param_names = ['eye_catch_url', 'title', 'overview']
        article_content_param_names = ['title', 'body']

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

        for key in article_info_param_names:
            self.assertEqual(
                json.loads(params['body'])[key], article_info_after[0][key])

        for key in article_content_param_names:
            self.assertEqual(
                json.loads(params['body'])[key], article_content_after[0][key])
Ejemplo n.º 2
0
    def test_generate_article_id(self):
        me_articles_drafts_create = MeArticlesDraftsCreate({}, {},
                                                           self.dynamodb)

        target_sort_key1 = 1521120784000001
        target_sort_key2 = 1521120784000002

        hashid1 = me_articles_drafts_create._MeArticlesDraftsCreate__generate_article_id(
            target_sort_key1)
        hashid2 = me_articles_drafts_create._MeArticlesDraftsCreate__generate_article_id(
            target_sort_key2)

        self.assertNotEqual(hashid1, hashid2)
        self.assertEqual(len(hashid1), 12)
        self.assertEqual(len(hashid2), 12)
Ejemplo n.º 3
0
    def test_main_ok_with_empty_string(self):
        params = {
            'body': {
                'eye_catch_url': 'http://example.com',
                'title': '',
                'body': '',
                'overview': ''
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

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

        response = MeArticlesDraftsCreate(params, {}, self.dynamodb).main()
        article_info = self.article_info_table.get_item(
            Key={'article_id': 'HOGEHOGEHOGE'})['Item']
        article_content = self.article_content_table.get_item(
            Key={'article_id': 'HOGEHOGEHOGE'})['Item']

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(article_info['title'], None)
        self.assertEqual(article_info['overview'], None)
        self.assertEqual(article_content['title'], None)
        self.assertEqual(article_content['body'], None)
Ejemplo n.º 4
0
    def test_main_with_error_on_create_article_content(self):
        params = {
            'body': {
                'eye_catch_url': 'http://example.com',
                'title': 'sample title',
                'body': '<p>sample body</p>',
                'overview': 'sample body'
            },
            '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']

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

        article_info_after = self.article_info_table.scan()['Items']
        article_content_after = self.article_content_table.scan()['Items']

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(json.loads(response['body']),
                         {'article_id': 'HOGEHOGEHOGE'})
        self.assertEqual(len(article_info_after) - len(article_info_before), 1)
        self.assertEqual(
            len(article_content_after) - len(article_content_before), 0)
Ejemplo n.º 5
0
    def test_create_article_content_with_article_id_already_exsits(self):
        self.article_content_table.put_item(
            Item={
                'article_id': 'HOGEHOGEHOGE',
                'title': 'test_title',
                'body': 'test_body'
            })

        article_id = 'HOGEHOGEHOGE'
        params = {'title': 'sample title', 'body': '<p>sample body</p>'}

        article_draft_create = MeArticlesDraftsCreate({}, {}, self.dynamodb)

        article_content_before = self.article_content_table.scan()['Items']

        with self.assertRaises(ClientError):
            article_draft_create._MeArticlesDraftsCreate__create_article_content(
                params, article_id)

        article_content_after = self.article_content_table.scan()['Items']

        self.assertEqual(
            len(article_content_after) - len(article_content_before), 0)
Ejemplo n.º 6
0
    def test_main_with_article_id_already_exsits(self):
        self.article_info_table.put_item(
            Item={
                'article_id': 'HOGEHOGEHOGE',
                'user_id': 'USER_ID',
                'status': 'draft',
                'sort_key': 1521120784000001,
            })

        params = {
            'body': {
                'eye_catch_url': 'http://example.com',
                'title': 'sample title',
                'body': '<p>sample body</p>',
                'overview': 'sample body'
            },
            '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']

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

        article_info_after = self.article_info_table.scan()['Items']
        article_content_after = self.article_content_table.scan()['Items']

        self.assertEqual(response['statusCode'], 400)
        self.assertEqual(json.loads(response['body']),
                         {'message': 'Already exists'})
        self.assertEqual(len(article_info_after) - len(article_info_before), 0)
        self.assertEqual(
            len(article_content_after) - len(article_content_before), 0)
Ejemplo n.º 7
0
    def assert_bad_request(self, params):
        function = MeArticlesDraftsCreate(params, {}, self.dynamodb)
        response = function.main()

        self.assertEqual(response['statusCode'], 400)
Ejemplo n.º 8
0
def lambda_handler(event, context):
    me_articles_drafts_create = MeArticlesDraftsCreate(event, context, dynamodb)
    return me_articles_drafts_create.main()