def test_main_with_NotAuthorizedError(self): params = { 'pathParameters': { 'comment_id': self.comment_items[1]['comment_id'] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } comment_before = self.comment_table.scan()['Items'] deleted_comment_before = self.deleted_comment_table.scan()['Items'] response = MeCommentsDelete(params, {}, self.dynamodb).main() comment_after = self.comment_table.scan()['Items'] deleted_comment_after = self.deleted_comment_table.scan()['Items'] self.assertEqual(response['statusCode'], 403) self.assertEqual(len(comment_after) - len(comment_before), 0) self.assertEqual(len(deleted_comment_after) - len(deleted_comment_before), 0)
def test_main_ok(self): params = { 'pathParameters': { 'comment_id': self.comment_items[0]['comment_id'] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } comment_before = self.comment_table.scan()['Items'] deleted_comment_before = self.deleted_comment_table.scan()['Items'] response = MeCommentsDelete(params, {}, self.dynamodb).main() comment_after = self.comment_table.scan()['Items'] deleted_comment_after = self.deleted_comment_table.scan()['Items'] comment = self.comment_table.get_item(Key={'comment_id': self.comment_items[0]['comment_id']}).get('Item') deleted_comment = self.deleted_comment_table.get_item( Key={'comment_id': self.comment_items[0]['comment_id']}).get('Item') self.assertEqual(response['statusCode'], 200) self.assertEqual(len(comment_after) - len(comment_before), -1) self.assertEqual(len(deleted_comment_after) - len(deleted_comment_before), 1) self.assertIsNone(comment) for key, value in self.comment_items[0].items(): self.assertEqual(deleted_comment[key], value)
def test_call_validate_article_existence(self): params = { 'pathParameters': { 'comment_id': 'comment00001' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } mock_lib = MagicMock() with patch('me_comments_delete.DBUtil', mock_lib): mock_lib.get_validated_comment.return_value = self.comment_items[0] MeCommentsDelete(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.assertEqual(args[1], 'publicId0001') self.assertEqual(kwargs['status'], 'public')
def test__is_accessable_comment_with_invalid_comment_user(self): params = { 'pathParameters': { 'comment_id': 'comment00001' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } comment = self.comment_items[0] response = MeCommentsDelete(params, {}, self.dynamodb)._MeCommentsDelete__is_accessable_comment(comment) self.assertEqual(response, False)
def test_main_ok_with_thread_comments(self): params = { 'pathParameters': { 'comment_id': self.comment_items[1]['comment_id'] }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } comment_before = self.comment_table.scan()['Items'] deleted_comment_before = self.deleted_comment_table.scan()['Items'] response = MeCommentsDelete(params, {}, self.dynamodb).main() comment_after = self.comment_table.scan()['Items'] deleted_comment_after = self.deleted_comment_table.scan()['Items'] self.assertEqual(response['statusCode'], 200) self.assertEqual(len(comment_after) - len(comment_before), -3) self.assertEqual( len(deleted_comment_after) - len(deleted_comment_before), 2) for targets in [ self.comment_items[1], self.comment_items[2], self.comment_items[3] ]: comment = self.comment_table.get_item( Key={ 'comment_id': targets['comment_id'] }).get('Item') deleted_comment = self.deleted_comment_table.get_item( Key={ 'comment_id': targets['comment_id'] }).get('Item') self.assertIsNone(comment) for key, value in targets.items(): self.assertEqual(deleted_comment[key], value)
def test_call_get_validated_comment_existence(self): params = { 'pathParameters': { 'comment_id': 'comment00008' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******' } } } } mock_lib = MagicMock() with patch('me_comments_delete.DBUtil', mock_lib): MeCommentsDelete(params, {}, self.dynamodb).main() args, _ = mock_lib.get_validated_comment.call_args self.assertTrue(mock_lib.get_validated_comment.called) self.assertEqual(args[1], 'comment00008')
def test__is_accessable_comment_with_valid_comment_user(self): params = { 'pathParameters': { 'comment_id': 'comment00001' }, 'requestContext': { 'authorizer': { 'claims': { 'cognito:username': '******', 'phone_number_verified': 'true', 'email_verified': 'true' } } } } comment = self.comment_items[0] response = MeCommentsDelete( params, {}, self.dynamodb)._MeCommentsDelete__is_accessable_comment(comment) self.assertEqual(response, True)
def assert_bad_request(self, params): response = MeCommentsDelete(params, {}, self.dynamodb).main() self.assertEqual(response['statusCode'], 400)
def lambda_handler(event, context): me_comments_delete = MeCommentsDelete(event=event, context=context, dynamodb=dynamodb) return me_comments_delete.main()