def test_call_response(self, ts=httptest.NoServer()): config_file = f'{os.getcwd()}/tests/fixtures/config_nocache.yaml' cache_folder = f'{os.getcwd()}/tests/fixtures' mec = myenergi.Client(config_file) mec.base_url = ts.url().replace('1.0.0.127.in-addr.arpa', '127.0.0.1') mec.populate_devices() self.assertListEqual(mec.list_devices(), ['Zappi[12345678]', 'Harvi[12345678]'])
async def test_cached_download_unpack_archive(self, ts=httptest.NoServer()): with tempfile.TemporaryDirectory() as tempdir: extracted = await cached_download_unpack_archive( ts.url() + "/archive.tar.gz", pathlib.Path(tempdir) / "archive.tar.gz", pathlib.Path(tempdir) / "archive", ARCHIVE_HASH, protocol_allowlist=["http://"], ) self.verify_extracted_contents(extracted)
async def test_cached_download_unpack_archive_failure( self, ts=httptest.NoServer()): with unittest.mock.patch("shutil.unpack_archive", side_effect=Exception): with self.assertRaises(DirectoryNotExtractedError): with tempfile.TemporaryDirectory() as tempdir: await cached_download_unpack_archive( ts.url() + "/archive.tar.gz", pathlib.Path(tempdir) / "archive.tar.gz", pathlib.Path(tempdir) / "archive", ARCHIVE_HASH, protocol_allowlist=["http://"], ) self.assertFalse((pathlib.Path(tempdir) / "archive").is_dir())
async def test_cached_download_unpack_archive(self, ts=httptest.NoServer()): with tempfile.TemporaryDirectory() as tempdir: @cached_download_unpack_archive( ts.url() + "/archive.tar.gz", pathlib.Path(tempdir) / "archive.tar.gz", pathlib.Path(tempdir) / "archive", "cd538a17ce51458e3315639eba0650e96740d3d6abadbf174209ee7c5cae000ac739e99d9f32c9c2ba417b0cf67e69b8", protocol_allowlist=["http://"], ) async def func(extracted): return extracted self.verify_extracted_contents(await func())
async def test_cached_download(self, ts=httptest.NoServer()): with tempfile.TemporaryDirectory() as tempdir: filename = await cached_download( ts.url() + "/archive.tar.gz", pathlib.Path(tempdir) / "archive.tar.gz", ARCHIVE_HASH, protocol_allowlist=["http://"], ) # Directory to extract to extracted = pathlib.Path(tempdir, "extracted") # Unpack the archive shutil.unpack_archive(filename, extracted) self.verify_extracted_contents(extracted)
async def test_call_response(self, ts=httptest.NoServer()): with tempfile.TemporaryDirectory() as tempdir: @cached_download( ts.url() + "/archive.tar.gz", pathlib.Path(tempdir) / "archive.tar.gz", "cd538a17ce51458e3315639eba0650e96740d3d6abadbf174209ee7c5cae000ac739e99d9f32c9c2ba417b0cf67e69b8", protocol_allowlist=["http://"], ) async def func(filename): return filename # Directory to extract to extracted = pathlib.Path(tempdir, "extracted") # Unpack the archive shutil.unpack_archive(await func(), extracted) # Verify contents are correct self.assertTrue((extracted / "somedir").is_dir()) self.assertEqual((extracted / "somedir" / "hello.txt").read_text(), "world") self.assertEqual((extracted / "somedir" / "dead.bin").read_bytes(), b"\xBE\xEF")
async def query_an_api(self, uuid: str, ts=httptest.NoServer()) -> str: return { "result": await make_request_to_example_server(self.parent.session) }
async def make_request_to_example_server(session, ts=httptest.NoServer()): async with session.get(ts.url()) as resp: return (await resp.read()).decode()