mirror of
https://github.com/home-assistant/core.git
synced 2026-09-27 18:08:37 -04:00
Add authentication support to Nightscout (#40602)
* Add API Key to the Nightscout integration config * Add tests for nightscout config changes * Apply suggestions from code review Co-authored-by: springstan <46536646+springstan@users.noreply.github.com> Co-authored-by: Chris Talkington <chris@talkingtontech.com> * Update homeassistant/components/nightscout/__init__.py * Run translations script to fix en.json Co-authored-by: springstan <46536646+springstan@users.noreply.github.com> Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
co-authored by
springstan
Chris Talkington
parent
6028953eca
commit
3fba4274f5
@@ -22,6 +22,11 @@ SERVER_STATUS = ServerStatus.new_from_json_dict(
|
||||
'{"status":"ok","name":"nightscout","version":"13.0.1","serverTime":"2020-08-05T18:14:02.032Z","serverTimeEpoch":1596651242032,"apiEnabled":true,"careportalEnabled":true,"boluscalcEnabled":true,"settings":{},"extendedSettings":{},"authorized":null}'
|
||||
)
|
||||
)
|
||||
SERVER_STATUS_STATUS_ONLY = ServerStatus.new_from_json_dict(
|
||||
json.loads(
|
||||
'{"status":"ok","name":"nightscout","version":"14.0.4","serverTime":"2020-09-25T21:03:59.315Z","serverTimeEpoch":1601067839315,"apiEnabled":true,"careportalEnabled":true,"boluscalcEnabled":true,"settings":{"units":"mg/dl","timeFormat":12,"nightMode":false,"editMode":true,"showRawbg":"never","customTitle":"Nightscout","theme":"default","alarmUrgentHigh":true,"alarmUrgentHighMins":[30,60,90,120],"alarmHigh":true,"alarmHighMins":[30,60,90,120],"alarmLow":true,"alarmLowMins":[15,30,45,60],"alarmUrgentLow":true,"alarmUrgentLowMins":[15,30,45],"alarmUrgentMins":[30,60,90,120],"alarmWarnMins":[30,60,90,120],"alarmTimeagoWarn":true,"alarmTimeagoWarnMins":15,"alarmTimeagoUrgent":true,"alarmTimeagoUrgentMins":30,"alarmPumpBatteryLow":false,"language":"en","scaleY":"log","showPlugins":"dbsize delta direction upbat","showForecast":"ar2","focusHours":3,"heartbeat":60,"baseURL":"","authDefaultRoles":"status-only","thresholds":{"bgHigh":260,"bgTargetTop":180,"bgTargetBottom":80,"bgLow":55},"insecureUseHttp":true,"secureHstsHeader":false,"secureHstsHeaderIncludeSubdomains":false,"secureHstsHeaderPreload":false,"secureCsp":false,"deNormalizeDates":false,"showClockDelta":false,"showClockLastTime":false,"bolusRenderOver":1,"frameUrl1":"","frameUrl2":"","frameUrl3":"","frameUrl4":"","frameUrl5":"","frameUrl6":"","frameUrl7":"","frameUrl8":"","frameName1":"","frameName2":"","frameName3":"","frameName4":"","frameName5":"","frameName6":"","frameName7":"","frameName8":"","DEFAULT_FEATURES":["bgnow","delta","direction","timeago","devicestatus","upbat","errorcodes","profile","dbsize"],"alarmTypes":["predict"],"enable":["careportal","boluscalc","food","bwp","cage","sage","iage","iob","cob","basal","ar2","rawbg","pushover","bgi","pump","openaps","treatmentnotify","bgnow","delta","direction","timeago","devicestatus","upbat","errorcodes","profile","dbsize","ar2"]},"extendedSettings":{"devicestatus":{"advanced":true,"days":1}},"authorized":null}'
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def init_integration(hass) -> MockConfigEntry:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
"""Test the Nightscout config flow."""
|
||||
from aiohttp import ClientConnectionError
|
||||
from aiohttp import ClientConnectionError, ClientResponseError
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow, setup
|
||||
from homeassistant.components.nightscout.const import DOMAIN
|
||||
@@ -8,7 +8,11 @@ from homeassistant.const import CONF_URL
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.nightscout import GLUCOSE_READINGS, SERVER_STATUS
|
||||
from tests.components.nightscout import (
|
||||
GLUCOSE_READINGS,
|
||||
SERVER_STATUS,
|
||||
SERVER_STATUS_STATUS_ONLY,
|
||||
)
|
||||
|
||||
CONFIG = {CONF_URL: "https://some.url:1234"}
|
||||
|
||||
@@ -55,6 +59,28 @@ async def test_user_form_cannot_connect(hass):
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_user_form_api_key_required(hass):
|
||||
"""Test we handle an unauthorized error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.nightscout.NightscoutAPI.get_server_status",
|
||||
return_value=SERVER_STATUS_STATUS_ONLY,
|
||||
), patch(
|
||||
"homeassistant.components.nightscout.NightscoutAPI.get_sgvs",
|
||||
side_effect=ClientResponseError(None, None, status=401),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_URL: "https://some.url:1234"},
|
||||
)
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_user_form_unexpected_exception(hass):
|
||||
"""Test we handle unexpected exception."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
||||
Reference in New Issue
Block a user