Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework test content type / refactor if tests in connection/login #641

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions aiounifi/interfaces/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ async def login(self) -> None:
LOGGER.error("Login failed '%s'", data)
raise ERRORS.get(data["meta"]["msg"], AiounifiException)

elif response.content_type == "text/html":
if (csrf_token := response.headers.get("x-csrf-token")) is not None:
self.headers["x-csrf-token"] = csrf_token

else:
LOGGER.debug("Login Failed not JSON: '%s'", bytes_data)
raise RequestError("Login Failed: Host starting up")

mkmer marked this conversation as resolved.
Show resolved Hide resolved
if (
response.status == HTTPStatus.OK
and (csrf_token := response.headers.get("x-csrf-token")) is not None
):
self.headers["x-csrf-token"] = csrf_token

self.can_retry_login = True
LOGGER.debug("Logged in to UniFi %s", url)

Expand Down
22 changes: 11 additions & 11 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def test_login(
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
headers={"x-csrf-token": "123"},
content_type="text/json",
content_type="application/json",
)
await unifi_controller.connectivity.login()
assert unifi_called_with(
Expand Down Expand Up @@ -104,7 +104,7 @@ async def test_controller_login(
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
headers={"x-csrf-token": "123"},
content_type="text/json",
content_type="application/json",
)
await unifi_controller.login()
assert unifi_called_with(
Expand Down Expand Up @@ -145,7 +145,7 @@ async def test_relogin_success(mock_aioresponse, unifi_controller):
mock_aioresponse.post(
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
content_type="text/json",
content_type="application/json",
headers={"x-csrf-token": "123"},
status=200,
)
Expand All @@ -155,7 +155,7 @@ async def test_relogin_success(mock_aioresponse, unifi_controller):
mock_aioresponse.get(
"https://host:8443/proxy/network/api/s/default/stat/sta",
payload=EMPTY_RESPONSE,
content_type="text/json",
content_type="application/json",
status=200,
)
await unifi_controller.clients.update()
Expand All @@ -179,13 +179,13 @@ async def test_relogin_success(mock_aioresponse, unifi_controller):
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
headers={"x-csrf-token": "563"},
content_type="text/json",
content_type="application/json",
status=200,
)
mock_aioresponse.get(
"https://host:8443/proxy/network/api/s/default/stat/device",
payload=EMPTY_RESPONSE,
content_type="text/json",
content_type="application/json",
status=200,
)

Expand All @@ -210,7 +210,7 @@ async def test_relogin_fails(mock_aioresponse, unifi_controller):
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
headers={"x-csrf-token": "123"},
content_type="text/json",
content_type="application/json",
status=200,
)

Expand All @@ -219,7 +219,7 @@ async def test_relogin_fails(mock_aioresponse, unifi_controller):
mock_aioresponse.get(
"https://host:8443/proxy/network/api/s/default/stat/sta",
payload=EMPTY_RESPONSE,
content_type="text/json",
content_type="application/json",
status=200,
)
await unifi_controller.clients.update()
Expand All @@ -235,7 +235,7 @@ async def test_relogin_fails(mock_aioresponse, unifi_controller):
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
headers={"x-csrf-token": "456"},
content_type="text/json",
content_type="application/json",
status=401,
)

Expand Down Expand Up @@ -300,7 +300,7 @@ async def test_unifios_controller(
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
headers={"x-csrf-token": "123"},
content_type="text/json",
content_type="application/json",
)
await unifi_controller.connectivity.login()
await unifi_controller.initialize()
Expand Down Expand Up @@ -380,7 +380,7 @@ async def test_unifios_controller_no_csrf_token(
mock_aioresponse.post(
"https://host:8443/api/auth/login",
payload=LOGIN_UNIFIOS_JSON_RESPONSE,
content_type="text/json",
content_type="application/json",
)
await unifi_controller.connectivity.login()
assert unifi_called_with(
Expand Down