async def test_packages(aresponses): """Test getting packages.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response(text=load_fixture("packages_response.json"), status=200), ) async with aiohttp.ClientSession() as session: client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) packages = await client.profile.packages() assert len(packages) == 5 assert packages[0].location == "Paris" assert packages[1].location == "Spain" assert packages[2].location == "Milano Italy" assert packages[3].location == ""
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Configure the platform and add the sensors.""" from py17track import Client from py17track.errors import SeventeenTrackError websession = aiohttp_client.async_get_clientsession(hass) client = Client(websession) try: login_result = await client.profile.login(config[CONF_USERNAME], config[CONF_PASSWORD]) if not login_result: _LOGGER.error("Invalid username and password provided") return except SeventeenTrackError as err: _LOGGER.error("There was an error while logging in: %s", err) return scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) data = SeventeenTrackData( client, async_add_entities, scan_interval, config[CONF_SHOW_ARCHIVED], config[CONF_SHOW_DELIVERED], ) await data.async_update()
async def test_packages_with_unknown_state(aresponses): """Test getting packages.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response( text=load_fixture("packages_response_with_unknown_state.json"), status=200), ) async with aiohttp.ClientSession() as session: client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) packages = await client.profile.packages() assert len(packages) == 3 assert packages[0].status == "Not Found" assert packages[1].status == "In Transit" assert packages[2].status == "Unknown"
async def test_summary(aresponses): """Test getting package summary.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response(text=load_fixture("summary_response.json"), status=200), ) async with aiohttp.ClientSession() as session: client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) summary = await client.profile.summary() assert summary["Delivered"] == 0 assert summary["Expired"] == 0 assert summary["In Transit"] == 6 assert summary["Not Found"] == 2 assert summary["Ready to be Picked Up"] == 0 assert summary["Returned"] == 0 assert summary["Undelivered"] == 0 assert summary["Unknown"] == 3
async def test_packages_user_defined_timezone(aresponses): """Test getting packages with user-defined timezone.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response(text=load_fixture("packages_response.json"), status=200), ) async with aiohttp.ClientSession() as session: client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) packages = await client.profile.packages(tz="Asia/Jakarta") assert len(packages) == 5 assert packages[0].timestamp.isoformat() == "2018-04-23T05:02:00+00:00" assert packages[1].timestamp.isoformat() == "2019-02-25T18:05:34+00:00" assert packages[2].timestamp.isoformat() == "1970-01-01T00:00:00+00:00"
async def test_bad_request(aresponses): """Test that a failed login returns the correct response.""" aresponses.add("random.domain", "/no/good", "get", aresponses.Response(text="", status=404)) with pytest.raises(RequestError): async with aiohttp.ClientSession() as session: client = Client(session=session) await client._request("get", "https://random.domain/no/good")
async def test_no_explicit_session(aresponses): """Test not providing an explicit aiohttp ClientSession.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) client = Client() login_result = await client.profile.login(TEST_EMAIL, TEST_PASSWORD) assert login_result is True
async def test_login_success(aresponses): """Test that a successful login returns the correct response.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) async with aiohttp.ClientSession() as session: client = Client(session=session) login_result = await client.profile.login(TEST_EMAIL, TEST_PASSWORD) assert login_result is True
async def main() -> None: """Create the aiohttp session and run the example.""" logging.basicConfig(level=logging.INFO) async with ClientSession() as session: try: client = Client(session=session) await client.profile.login("<EMAIL>", "<PASSWORD>") _LOGGER.info("Account ID: %s", client.profile.account_id) # await client.profile.add_package("<TRACKING NUMBER>", "<FRIENDLY NAME>") summary = await client.profile.summary() _LOGGER.info("Account Summary: %s", summary) packages = await client.profile.packages() _LOGGER.info("Package Summary: %s", packages) except SeventeenTrackError as err: print(err)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Configure the platform and add the sensors.""" from py17track import Client from py17track.errors import SeventeenTrackError websession = aiohttp_client.async_get_clientsession(hass) client = Client(websession) try: login_result = await client.profile.login(config[CONF_USERNAME], config[CONF_PASSWORD]) if not login_result: _LOGGER.error('Invalid username and password provided') return except SeventeenTrackError as err: _LOGGER.error('There was an error while logging in: %s', err) return scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) data = SeventeenTrackData(client, async_add_entities, scan_interval, config[CONF_SHOW_ARCHIVED], config[CONF_SHOW_DELIVERED]) await data.async_update() sensors = [] for status, quantity in data.summary.items(): sensors.append(SeventeenTrackSummarySensor(data, status, quantity)) for package in data.packages: sensors.append(SeventeenTrackPackageSensor(data, package)) async_add_entities(sensors, True)
async def test_add_new_package_with_friendly_name_error_response(aresponses): """Test adding a new package with friendly name but setting the name fails.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response(text=load_fixture("add_package_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response(text=load_fixture("packages_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response( text=load_fixture("set_friendly_name_failure_response.json"), status=200), ) async with aiohttp.ClientSession() as session: with pytest.raises(RequestError): client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) await client.profile.add_package("1234567890987654321", "Friendly name")
async def test_add_new_package(aresponses): """Test adding a new package.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response(text=load_fixture("add_package_response.json"), status=200), ) async with aiohttp.ClientSession() as session: client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) await client.profile.add_package("LP00432912409987")
async def test_add_existing_package(aresponses): """Test adding an existing new package.""" aresponses.add( "user.17track.net", "/userapi/call", "post", aresponses.Response( text=load_fixture("authentication_success_response.json"), status=200), ) aresponses.add( "buyer.17track.net", "/orderapi/call", "post", aresponses.Response( text=load_fixture("add_package_existing_response.json"), status=200), ) async with aiohttp.ClientSession() as session: with pytest.raises(RequestError): client = Client(session=session) await client.profile.login(TEST_EMAIL, TEST_PASSWORD) await client.profile.add_package("1234567890987654321")