def test_cart_int_get(self):
        """Testing the cart interface get method"""
        saved_state = {}

        def start_response(*args):
            """stub for start_response to do some checking"""
            self.assertEqual(args[0], '200 OK')
            self.assertEqual(args[1][0][0], 'Content-Type')
            self.assertEqual(args[1][0][1], 'application/octet-stream')

        def file_wrapper(rfd, blksize):
            """stub for file_wrapper to do some checking"""
            self.assertEqual(blksize, 1 << 20)
            self.assertEqual(type(rfd), file)
            saved_state['fd'] = rfd
            return iter(lambda: rfd.read(blksize), '')

        env = {
            'PATH_INFO': '/123',
            'wsgi.file_wrapper': file_wrapper,
            'QUERY_STRING': 'filename=my_file.tar'
        }
        with test_database(self.sqlite_db, (Cart, File)):
            sample_cart = Cart()
            sample_cart.cart_uid = 123
            sample_cart.bundle_path = mkdtemp()
            sample_cart.status = 'ready'
            sample_cart.save(force_insert=True)
            self.sqlite_db.close()
            cgen = CartGenerator()
            tarfile_read = cgen.get(env, start_response)
            for buf in tarfile_read:
                self.assertTrue(len(buf) > 0)
            saved_state['fd'].close()
    def test_read_cart_ioerrror(self):
        """Testing the cart interface get against not ready cart"""
        def start_response(*args):
            """stub for start_response to do some checking"""
            self.assertEqual(args[0], '404 Not Found')
            self.assertEqual(args[1][0][0], 'Content-Type')
            self.assertEqual(args[1][0][1], 'application/json')

        env = {'PATH_INFO': '/123', 'QUERY_STRING': ''}
        with test_database(self.sqlite_db, (Cart, File)):
            sample_cart = Cart()
            sample_cart.cart_uid = 123
            sample_cart.bundle_path = mkdtemp()
            sample_cart.status = 'ready'
            sample_cart.save(force_insert=True)
            self.sqlite_db.close()
            cgen = CartGenerator()
            orig_fork = os.fork

            def bad_fork():
                """The get method trys to fork so replacing it with a method that fails.
                """
                raise IOError('failed to fork')

            os.fork = bad_fork
            data = cgen.get(env, start_response)
            self.assertEqual(
                loads(data)['message'], 'The cart bundle does not exist')
            os.fork = orig_fork
    def test_not_ready_cart(self):
        """Testing the cart interface get against not ready cart"""
        def start_response(*args):
            """stub for start_response to do some checking"""
            self.assertEqual(args[0], '202 Accepted')
            self.assertEqual(args[1][0][0], 'Content-Type')
            self.assertEqual(args[1][0][1], 'application/json')

        env = {'PATH_INFO': '/123', 'QUERY_STRING': ''}
        with test_database(self.sqlite_db, (Cart, File)):
            sample_cart = Cart()
            sample_cart.cart_uid = 123
            sample_cart.bundle_path = mkdtemp()
            sample_cart.save(force_insert=True)
            self.sqlite_db.close()
            cgen = CartGenerator()
            data = cgen.get(env, start_response)
            self.assertEqual(
                loads(data)['message'], 'The cart is not ready for download')
    def test_cart_int_get_no_file_fd(self):
        """Testing the cart interface get method w/o file_wrapper"""
        def start_response(*args):
            """stub for start_response to do some checking"""
            self.assertEqual(args[0], '200 OK')
            self.assertEqual(args[1][0][0], 'Content-Type')
            self.assertEqual(args[1][0][1], 'application/octet-stream')

        env = {'PATH_INFO': '/123', 'QUERY_STRING': ''}
        with test_database(self.sqlite_db, (Cart, File)):
            sample_cart = Cart()
            sample_cart.cart_uid = 123
            sample_cart.bundle_path = mkdtemp()
            sample_cart.status = 'ready'
            sample_cart.save(force_insert=True)
            self.sqlite_db.close()
            cgen = CartGenerator()
            tarfile_read = cgen.get(env, start_response)
            for buf in tarfile_read:
                self.assertTrue(len(buf) > 0)
    def test_cart_int_delete(self):
        """Testing the cart interface delete"""
        def start_response(*args):
            """stub for start_response to do some checking"""
            self.assertEqual(args[0], '200 OK')
            self.assertEqual(args[1][0][0], 'Content-Type')
            self.assertEqual(args[1][0][1], 'application/json')

        env = {'PATH_INFO': '/123', 'QUERY_STRING': ''}
        with test_database(self.sqlite_db, (Cart, File)):
            sample_cart = Cart()
            sample_cart.cart_uid = 123
            sample_cart.bundle_path = mkdtemp()
            sample_cart.status = 'ready'
            sample_cart.save(force_insert=True)
            sample_cart.reload()
            path_to_files = os.path.join(VOLUME_PATH, str(sample_cart.id))
            os.makedirs(path_to_files)
            self.sqlite_db.close()
            cgen = CartGenerator()
            data = cgen.delete_cart(env, start_response)
            self.assertEqual(
                loads(data)['message'], 'Cart Deleted Successfully')