import asyncio
import io
import zipfile
from unittest import mock

from tornado import testing

from waterbutler.core import streams

from tests import utils


class TestZipHandler(utils.HandlerTestCase):

    def setUp(self):
        super().setUp()
        identity_future = asyncio.Future()
        identity_future.set_result({
            'auth': {},
            'credentials': {},
            'settings': {},
        })
        self.mock_identity = mock.Mock()
        self.mock_identity.return_value = identity_future
        self.identity_patcher = mock.patch('waterbutler.server.handlers.core.get_identity', self.mock_identity)
        self.identity_patcher.start()

    def tearDown(self):
        super().tearDown()
        self.identity_patcher.stop()

    @mock.patch('waterbutler.core.utils.make_provider')
    @testing.gen_test
    def test_download_stream(self, mock_make_provider):
        stream = asyncio.StreamReader()
        data = b'freddie brian john roger'
        stream.feed_data(data)
        stream.feed_eof()
        stream.size = len(data)
        stream.content_type = 'application/octet-stream'

        zipstream = streams.ZipStreamReader(('file.txt', stream))

        mock_provider = utils.mock_provider_method(mock_make_provider,
                                                   'zip',
                                                   zipstream)
        resp = yield self.http_client.fetch(
            self.get_url('/zip?provider=queenhub&path=freddie.png'),
        )

        zip = zipfile.ZipFile(io.BytesIO(resp.body))

        assert zip.testzip() is None

        assert zip.open('file.txt').read() == data
import asyncio
import io
import zipfile
from unittest import mock

from tornado import testing

from waterbutler.core import streams

from tests import utils


class TestZipHandler(utils.HandlerTestCase):

    @testing.gen_test
    def test_download_stream(self):
        data = b'freddie brian john roger'
        stream = streams.StringStream(data)
        stream.content_type = 'application/octet-stream'

        zipstream = streams.ZipStreamReader(('file.txt', stream))

        self.mock_provider.zip = utils.MockCoroutine(return_value=zipstream)

        resp = yield self.http_client.fetch(
            self.get_url('/zip?provider=queenhub&path=/freddie.png'),
        )

        zip = zipfile.ZipFile(io.BytesIO(resp.body))

        assert zip.testzip() is None

        assert zip.open('file.txt').read() == data

Remove deprecated test setup and teardown code
Remove deprecated test setup and teardown code

  import asyncio
  import io
  import zipfile
  from unittest import mock
  
  from tornado import testing
  
  from waterbutler.core import streams
  
  from tests import utils
  
  
  class TestZipHandler(utils.HandlerTestCase):
  
-     def setUp(self):
-         super().setUp()
-         identity_future = asyncio.Future()
-         identity_future.set_result({
-             'auth': {},
-             'credentials': {},
-             'settings': {},
-         })
-         self.mock_identity = mock.Mock()
-         self.mock_identity.return_value = identity_future
-         self.identity_patcher = mock.patch('waterbutler.server.handlers.core.get_identity', self.mock_identity)
-         self.identity_patcher.start()
- 
-     def tearDown(self):
-         super().tearDown()
-         self.identity_patcher.stop()
- 
-     @mock.patch('waterbutler.core.utils.make_provider')
      @testing.gen_test
-     def test_download_stream(self, mock_make_provider):
+     def test_download_stream(self):
-         stream = asyncio.StreamReader()
          data = b'freddie brian john roger'
+         stream = streams.StringStream(data)
-         stream.feed_data(data)
-         stream.feed_eof()
-         stream.size = len(data)
          stream.content_type = 'application/octet-stream'
  
          zipstream = streams.ZipStreamReader(('file.txt', stream))
  
+         self.mock_provider.zip = utils.MockCoroutine(return_value=zipstream)
+ 
-         mock_provider = utils.mock_provider_method(mock_make_provider,
-                                                    'zip',
-                                                    zipstream)
          resp = yield self.http_client.fetch(
-             self.get_url('/zip?provider=queenhub&path=freddie.png'),
+             self.get_url('/zip?provider=queenhub&path=/freddie.png'),
          )
  
          zip = zipfile.ZipFile(io.BytesIO(resp.body))
  
          assert zip.testzip() is None
  
          assert zip.open('file.txt').read() == data
+ 
Remove deprecated test setup and teardown code
## Code Before:
import asyncio
import io
import zipfile
from unittest import mock

from tornado import testing

from waterbutler.core import streams

from tests import utils


class TestZipHandler(utils.HandlerTestCase):

    def setUp(self):
        super().setUp()
        identity_future = asyncio.Future()
        identity_future.set_result({
            'auth': {},
            'credentials': {},
            'settings': {},
        })
        self.mock_identity = mock.Mock()
        self.mock_identity.return_value = identity_future
        self.identity_patcher = mock.patch('waterbutler.server.handlers.core.get_identity', self.mock_identity)
        self.identity_patcher.start()

    def tearDown(self):
        super().tearDown()
        self.identity_patcher.stop()

    @mock.patch('waterbutler.core.utils.make_provider')
    @testing.gen_test
    def test_download_stream(self, mock_make_provider):
        stream = asyncio.StreamReader()
        data = b'freddie brian john roger'
        stream.feed_data(data)
        stream.feed_eof()
        stream.size = len(data)
        stream.content_type = 'application/octet-stream'

        zipstream = streams.ZipStreamReader(('file.txt', stream))

        mock_provider = utils.mock_provider_method(mock_make_provider,
                                                   'zip',
                                                   zipstream)
        resp = yield self.http_client.fetch(
            self.get_url('/zip?provider=queenhub&path=freddie.png'),
        )

        zip = zipfile.ZipFile(io.BytesIO(resp.body))

        assert zip.testzip() is None

        assert zip.open('file.txt').read() == data
## Instruction:
Remove deprecated test setup and teardown code
## Code After:
import asyncio
import io
import zipfile
from unittest import mock

from tornado import testing

from waterbutler.core import streams

from tests import utils


class TestZipHandler(utils.HandlerTestCase):

    @testing.gen_test
    def test_download_stream(self):
        data = b'freddie brian john roger'
        stream = streams.StringStream(data)
        stream.content_type = 'application/octet-stream'

        zipstream = streams.ZipStreamReader(('file.txt', stream))

        self.mock_provider.zip = utils.MockCoroutine(return_value=zipstream)

        resp = yield self.http_client.fetch(
            self.get_url('/zip?provider=queenhub&path=/freddie.png'),
        )

        zip = zipfile.ZipFile(io.BytesIO(resp.body))

        assert zip.testzip() is None

        assert zip.open('file.txt').read() == data
