Skip to content

Commit

Permalink
Rework test content type (#641)
Browse files Browse the repository at this point in the history
refactor login
  • Loading branch information
mkmer committed Apr 18, 2024
1 parent 8afb578 commit 7e755cc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
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")

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

0 comments on commit 7e755cc

Please sign in to comment.