Example #1
0
    def test_call_validate_methods(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0003'
            },
            'body': {
                'text': 'sample content',
            },
            '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_comments_create.DBUtil', mock_lib):
            MeArticlesCommentsCreate(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.assertEqual(kwargs['status'], 'public')

            self.assertTrue(mock_lib.validate_write_blacklisted.called)
            args, kwargs = mock_lib.validate_write_blacklisted.call_args
            self.assertTrue(args[0])
            self.assertEqual(args[1], 'comment_user_01')
Example #2
0
    def test_main_ok_already_commented_by_myself(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0002'
            },
            'body': {
                'text': 'A' * 400,
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

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

        comment_before = self.comment_table.scan()['Items']

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

        comment_after = self.comment_table.scan()['Items']

        comment = self.comment_table.get_item(Key={'comment_id': 'FUGAFUGAFUGA'}).get('Item')

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(len(comment_after) - len(comment_before), 1)
        self.assertIsNotNone(comment)
    def test_main_ok_with_adding_comment_on_own_article(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'text': 'A' * 400,
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

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

        comment_before = self.comment_table.scan()['Items']
        notification_before = self.notification_table.scan()['Items']
        unread_notification_manager_before = self.unread_notification_manager_table.scan(
        )['Items']

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

        comment_after = self.comment_table.scan()['Items']
        notification_after = self.notification_table.scan()['Items']
        unread_notification_manager_after = self.unread_notification_manager_table.scan(
        )['Items']

        comment = self.comment_table.get_item(Key={
            'comment_id': 'PIYOPIYO'
        }).get('Item')

        self.assertEqual(response['statusCode'], 200)
        self.assertIsNotNone(comment)
        self.assertEqual(len(comment_after) - len(comment_before), 1)
        self.assertEqual(len(notification_after) - len(notification_before), 0)
        self.assertEqual(
            len(unread_notification_manager_after) -
            len(unread_notification_manager_before), 0)
Example #4
0
    def test_raise_exception_in_creating_notification(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0002'
            },
            'body': {
                'text': 'sample content',
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

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

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

        self.assertEqual(response['statusCode'], 200)
Example #5
0
    def test_main_ok(self):
        params = {
            'pathParameters': {
                'article_id': 'publicId0001'
            },
            'body': {
                'text': 'A',
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******',
                        'phone_number_verified': 'true',
                        'email_verified': 'true'
                    }
                }
            }
        }

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

        comment_before = self.comment_table.scan()['Items']
        notification_before = self.notification_table.scan()['Items']
        unread_notification_manager_before = self.unread_notification_manager_table.scan()['Items']

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

        comment_after = self.comment_table.scan()['Items']
        notification_after = self.notification_table.scan()['Items']
        unread_notification_manager_after = self.unread_notification_manager_table.scan()['Items']

        comment = self.comment_table.get_item(Key={'comment_id': 'HOGEHOGEHOGE'}).get('Item')
        notification = self.notification_table.get_item(
            Key={'notification_id': 'comment-article_user01-HOGEHOGEHOGE'}
        ).get('Item')

        unread_manager = self.unread_notification_manager_table.get_item(Key={'user_id': 'article_user01'}).get('Item')

        self.assertEqual(response['statusCode'], 200)
        self.assertEqual(json.loads(response['body'])['comment_id'], 'HOGEHOGEHOGE')
        self.assertEqual(len(comment_after) - len(comment_before), 1)
        self.assertEqual(len(notification_after) - len(notification_before), 1)
        self.assertEqual(len(unread_notification_manager_after) - len(unread_notification_manager_before), 1)

        expected_comment = {
            'comment_id': 'HOGEHOGEHOGE',
            'text': 'A',
            'article_id': 'publicId0001',
            'user_id': 'test_user_id01',
            'created_at': 1520150552,
            'sort_key': 1520150552000003
        }

        expected_notification = {
            'notification_id': 'comment-article_user01-HOGEHOGEHOGE',
            'user_id': self.article_info_table_items[0]['user_id'],
            'article_id': self.article_info_table_items[0]['article_id'],
            'article_title': self.article_info_table_items[0]['title'],
            'acted_user_id': 'test_user_id01',
            'sort_key': 1520150552000003,
            'type': 'comment',
            'created_at': 1520150552
        }

        expected_unread_manager = {
            'user_id': self.article_info_table_items[0]['user_id'],
            'unread': True
        }

        self.assertEqual(comment, expected_comment)
        self.assertEqual(notification, expected_notification)
        self.assertEqual(unread_manager, expected_unread_manager)
Example #6
0
    def assert_bad_request(self, params):
        function = MeArticlesCommentsCreate(params, {}, self.dynamodb)
        response = function.main()

        self.assertEqual(response['statusCode'], 400)
def lambda_handler(event, context):
    me_articles_comments_create = MeArticlesCommentsCreate(event=event,
                                                           context=context,
                                                           dynamodb=dynamodb)
    return me_articles_comments_create.main()