Skip to content

Commit d70f243

Browse files
committed
Test: combine connect-functions
1 parent 71cd420 commit d70f243

File tree

1 file changed

+35
-100
lines changed

1 file changed

+35
-100
lines changed

tests/test_init.py

Lines changed: 35 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,14 @@ def connect_status(broken, timeout_happened, fail_auth):
277277

278278
async def connect(
279279
self,
280+
function,
280281
broken=False,
281282
timeout_happened=False,
282283
raise_timeout=False,
284+
real_timeout_value=10,
283285
fail_auth=False,
284286
stretch=False,
287+
url_part=CORE_DOMAIN_OBJECTS,
285288
):
286289
"""Connect to a smile environment and perform basic asserts."""
287290
port = aiohttp.test_utils.unused_port()
@@ -290,9 +293,7 @@ async def connect(
290293
)
291294

292295
# Happy flow
293-
app = self.setup_app(
294-
broken, timeout_happened, raise_timeout, fail_auth, stretch
295-
)
296+
app = function(broken, timeout_happened, raise_timeout, fail_auth, stretch)
296297

297298
server = aiohttp.test_utils.TestServer(
298299
app, port=port, scheme="http", host="127.0.0.1"
@@ -302,7 +303,7 @@ async def connect(
302303
client = aiohttp.test_utils.TestClient(server)
303304
websession = client.session
304305

305-
url = f"{server.scheme}://{server.host}:{server.port}{CORE_DOMAIN_OBJECTS}"
306+
url = f"{server.scheme}://{server.host}:{server.port}{url_part}"
306307

307308
# Try/exceptpass to accommodate for Timeout of aoihttp
308309
try:
@@ -350,93 +351,7 @@ async def connect(
350351
try:
351352
smile_version = await smile.connect()
352353
assert smile_version is not None
353-
assert smile._timeout == 10
354-
return server, smile, client
355-
except (
356-
pw_exceptions.ConnectionFailedError,
357-
pw_exceptions.InvalidXMLError,
358-
pw_exceptions.InvalidAuthentication,
359-
) as exception:
360-
assert smile_version is None
361-
await self.disconnect(server, client)
362-
raise exception
363-
364-
async def connect_legacy(
365-
self,
366-
broken=False,
367-
timeout_happened=False,
368-
raise_timeout=False,
369-
fail_auth=False,
370-
stretch=False,
371-
):
372-
"""Connect to a smile environment and perform basic asserts."""
373-
port = aiohttp.test_utils.unused_port()
374-
test_password = "".join(
375-
secrets.choice(string.ascii_lowercase) for _ in range(8)
376-
)
377-
378-
# Happy flow
379-
app = self.setup_legacy_app(
380-
broken, timeout_happened, raise_timeout, fail_auth, stretch
381-
)
382-
383-
server = aiohttp.test_utils.TestServer(
384-
app, port=port, scheme="http", host="127.0.0.1"
385-
)
386-
await server.start_server()
387-
388-
client = aiohttp.test_utils.TestClient(server)
389-
websession = client.session
390-
391-
url = f"{server.scheme}://{server.host}:{server.port}{CORE_LOCATIONS}"
392-
393-
# Try/exceptpass to accommodate for Timeout of aoihttp
394-
try:
395-
resp = await websession.get(url)
396-
assumed_status = self.connect_status(broken, timeout_happened, fail_auth)
397-
assert resp.status == assumed_status
398-
timeoutpass_result = False
399-
assert timeoutpass_result
400-
except Exception: # pylint: disable=broad-except
401-
timeoutpass_result = True
402-
assert timeoutpass_result
403-
404-
if not broken and not timeout_happened and not fail_auth:
405-
text = await resp.text()
406-
assert "xml" in text
407-
408-
# Test lack of websession
409-
try:
410-
smile = pw_smile.Smile(
411-
host=server.host,
412-
username=pw_constants.DEFAULT_USERNAME,
413-
password=test_password,
414-
port=server.port,
415-
websession=None,
416-
)
417-
lack_of_websession = False
418-
assert lack_of_websession
419-
except Exception: # pylint: disable=broad-except
420-
lack_of_websession = True
421-
assert lack_of_websession
422-
423-
smile = pw_smile.Smile(
424-
host=server.host,
425-
username=pw_constants.DEFAULT_USERNAME,
426-
password=test_password,
427-
port=server.port,
428-
websession=websession,
429-
)
430-
431-
if not timeout_happened:
432-
assert smile._timeout == 30
433-
434-
# Connect to the smile
435-
smile_version = None
436-
try:
437-
smile_version = await smile.connect()
438-
assert smile_version is not None
439-
assert smile._timeout == 30
354+
assert smile._timeout == real_timeout_value
440355
return server, smile, client
441356
except (
442357
pw_exceptions.ConnectionFailedError,
@@ -455,7 +370,7 @@ async def connect_wrapper(
455370
if fail_auth:
456371
try:
457372
_LOGGER.warning("Connecting to device with invalid credentials:")
458-
await self.connect(fail_auth=fail_auth)
373+
await self.connect(self.setup_app, fail_auth=fail_auth)
459374
_LOGGER.error(" - invalid credentials not handled") # pragma: no cover
460375
raise self.ConnectError # pragma: no cover
461376
except pw_exceptions.InvalidAuthentication as exc:
@@ -464,53 +379,73 @@ async def connect_wrapper(
464379

465380
if raise_timeout:
466381
_LOGGER.warning("Connecting to device exceeding timeout in handling:")
467-
return await self.connect(raise_timeout=True)
382+
return await self.connect(self.setup_app, raise_timeout=True)
468383

469384
try:
470385
_LOGGER.warning("Connecting to device exceeding timeout in response:")
471-
await self.connect(timeout_happened=True)
386+
await self.connect(self.setup_app, timeout_happened=True)
472387
_LOGGER.error(" - timeout not handled") # pragma: no cover
473388
raise self.ConnectError # pragma: no cover
474389
except pw_exceptions.ConnectionFailedError:
475390
_LOGGER.info(" + successfully passed timeout handling.")
476391

477392
try:
478393
_LOGGER.warning("Connecting to device with missing data:")
479-
await self.connect(broken=True)
394+
await self.connect(self.setup_app, broken=True)
480395
_LOGGER.error(" - broken information not handled") # pragma: no cover
481396
raise self.ConnectError # pragma: no cover
482397
except pw_exceptions.InvalidXMLError:
483398
_LOGGER.info(" + successfully passed XML issue handling.")
484399

485400
_LOGGER.info("Connecting to functioning device:")
486-
return await self.connect(stretch=stretch)
401+
return await self.connect(self.setup_app, stretch=stretch)
487402

488403
async def connect_legacy_wrapper(
489404
self, raise_timeout=False, fail_auth=False, stretch=False
490405
):
491406
"""Wrap connect to try negative testing before positive testing."""
492407
if raise_timeout:
493408
_LOGGER.warning("Connecting to device exceeding timeout in handling:")
494-
return await self.connect_legacy(raise_timeout=True)
409+
return await self.connect(
410+
self.setup_legacy_app,
411+
raise_timeout=True,
412+
real_timeout_value=30,
413+
url_part=CORE_LOCATIONS,
414+
)
495415

496416
try:
497417
_LOGGER.warning("Connecting to device exceeding timeout in response:")
498-
await self.connect_legacy(timeout_happened=True)
418+
await self.connect(
419+
self.setup_legacy_app,
420+
real_timeout_value=30,
421+
timeout_happened=True,
422+
url_part=CORE_LOCATIONS,
423+
)
499424
_LOGGER.error(" - timeout not handled") # pragma: no cover
500425
raise self.ConnectError # pragma: no cover
501426
except pw_exceptions.ConnectionFailedError:
502427
_LOGGER.info(" + successfully passed timeout handling.")
503428

504429
try:
505430
_LOGGER.warning("Connecting to device with missing data:")
506-
await self.connect_legacy(broken=True)
431+
await self.connect(
432+
self.setup_legacy_app,
433+
broken=True,
434+
real_timeout_value=30,
435+
url_part=CORE_LOCATIONS,
436+
)
507437
_LOGGER.error(" - broken information not handled") # pragma: no cover
508438
raise self.ConnectError # pragma: no cover
509439
except pw_exceptions.InvalidXMLError:
510440
_LOGGER.info(" + successfully passed XML issue handling.")
511441

512442
_LOGGER.info("Connecting to functioning device:")
513-
return await self.connect_legacy(stretch=stretch)
443+
return await self.connect(
444+
self.setup_legacy_app,
445+
real_timeout_value=30,
446+
stretch=stretch,
447+
url_part=CORE_LOCATIONS,
448+
)
514449

515450
# Generic disconnect
516451
@classmethod

0 commit comments

Comments
 (0)