Skip to content

Commit

Permalink
Fix races in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed May 13, 2024
1 parent 6cb7886 commit 14f22d4
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import Generator
from unittest.mock import patch

Expand Down Expand Up @@ -54,12 +55,20 @@ async def test_full_user_flow_implementation(
headers={"Content-Type": "application/json"},
),
)
# User has not yet entered the code

access_token_responses = [
# User has not yet entered the code
{"error": "authorization_pending"},
# User enters the code
{CONF_ACCESS_TOKEN: TOKEN, "token_type": "bearer", "scope": ""},
]

async def json(**kwargs):
return access_token_responses.pop(0)

response_mocker.add(
url="https://github.com/login/oauth/access_token",
response=MockedResponse(
content={"error": "authorization_pending"}, headers={"Content-Type": "application/json"}
),
response=MockedResponse(json=json, headers={"Content-Type": "application/json"}, keep=True),
)

result = await hass.config_entries.flow.async_init(
Expand Down Expand Up @@ -97,19 +106,6 @@ async def test_full_user_flow_implementation(
assert result["step_id"] == "device"
assert result["type"] == FlowResultType.SHOW_PROGRESS

# User enters the code
response_mocker.add(
url="https://github.com/login/oauth/access_token",
response=MockedResponse(
content={
CONF_ACCESS_TOKEN: TOKEN,
"token_type": "bearer",
"scope": "",
},
headers={"Content-Type": "application/json"},
),
)

time_freezer.tick(10)
await hass.async_block_till_done()

Expand Down Expand Up @@ -142,11 +138,16 @@ async def test_flow_with_remove_while_activating(
headers={"Content-Type": "application/json"},
),
)

access_token_event = asyncio.Event()

async def json(**kwargs):
access_token_event.set()
return {"error": "authorization_pending"}

response_mocker.add(
url="https://github.com/login/oauth/access_token",
response=MockedResponse(
content={"error": "authorization_pending"}, headers={"Content-Type": "application/json"}
),
response=MockedResponse(json=json, headers={"Content-Type": "application/json"}, keep=True),
)

result = await hass.config_entries.flow.async_init(
Expand All @@ -171,6 +172,9 @@ async def test_flow_with_remove_while_activating(
assert result["step_id"] == "device"
assert result["type"] == FlowResultType.SHOW_PROGRESS

# Wait for access token request
await access_token_event.wait()

assert hass.config_entries.flow.async_get(result["flow_id"])

# Simulate user canceling the flow
Expand Down Expand Up @@ -244,12 +248,23 @@ async def test_flow_with_activation_failure(
headers={"Content-Type": "application/json"},
),
)
# User has not yet entered the code

def raise_github_exception() -> None:
raise GitHubException("Activation failed")

access_token_responses = [
# User has not yet entered the code
lambda: {"error": "authorization_pending"},
# Activation fails
raise_github_exception,
]

async def json(**kwargs):
return access_token_responses.pop(0)()

response_mocker.add(
url="https://github.com/login/oauth/access_token",
response=MockedResponse(
content={"error": "authorization_pending"}, headers={"Content-Type": "application/json"}
),
response=MockedResponse(json=json, headers={"Content-Type": "application/json"}, keep=True),
)

result = await hass.config_entries.flow.async_init(
Expand All @@ -274,12 +289,6 @@ async def test_flow_with_activation_failure(
assert result["step_id"] == "device"
assert result["type"] == FlowResultType.SHOW_PROGRESS

# Activation fails
response_mocker.add(
url="https://github.com/login/oauth/access_token",
response=MockedResponse(exception=GitHubException("Activation failed")),
)

time_freezer.tick(10)

await hass.config_entries.flow.async_configure(result["flow_id"])
Expand Down

0 comments on commit 14f22d4

Please sign in to comment.