Add multi-factor auth module setup flow (#16141)
* Add mfa setup flow * Lint * Address code review comment * Fix unit test * Add assertion for WS response ordering * Missed a return * Remove setup_schema from MFA base class * Move auth.util.validate_current_user -> webscoket_api.ws_require_user
This commit is contained in:
1 parent
e567b2281d
commit
914d90a2bc
8 files changed
+386
-36
No files matched your search
@@ -125,3 +125,21 @@ async def test_login(hass):
|
||||
result['flow_id'], {'pin': '123456'})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result['data'].id == 'mock-user'
|
||||
|
||||
|
||||
async def test_setup_flow(hass):
|
||||
"""Test validating pin."""
|
||||
auth_module = await auth_mfa_module_from_config(hass, {
|
||||
'type': 'insecure_example',
|
||||
'data': [{'user_id': 'test-user', 'pin': '123456'}]
|
||||
})
|
||||
|
||||
flow = await auth_module.async_setup_flow('new-user')
|
||||
|
||||
result = await flow.async_step_init()
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
||||
result = await flow.async_step_init({'pin': 'abcdefg'})
|
||||
assert result['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert auth_module._data[1]['user_id'] == 'new-user'
|
||||
assert auth_module._data[1]['pin'] == 'abcdefg'
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Tests for the mfa setup flow."""
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.auth import auth_manager_from_config
|
||||
from homeassistant.components.auth import mfa_setup_flow
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockUser, CLIENT_ID, ensure_auth_manager_loaded
|
||||
|
||||
|
||||
async def test_ws_setup_depose_mfa(hass, hass_ws_client):
|
||||
"""Test set up mfa module for current user."""
|
||||
hass.auth = await auth_manager_from_config(
|
||||
hass, provider_configs=[{
|
||||
'type': 'insecure_example',
|
||||
'users': [{
|
||||
'username': 'test-user',
|
||||
'password': 'test-pass',
|
||||
'name': 'Test Name',
|
||||
}]
|
||||
}], module_configs=[{
|
||||
'type': 'insecure_example',
|
||||
'id': 'example_module',
|
||||
'data': [{'user_id': 'mock-user', 'pin': '123456'}]
|
||||
}])
|
||||
ensure_auth_manager_loaded(hass.auth)
|
||||
await async_setup_component(hass, 'auth', {'http': {}})
|
||||
|
||||
user = MockUser(id='mock-user').add_to_hass(hass)
|
||||
cred = await hass.auth.auth_providers[0].async_get_or_create_credentials(
|
||||
{'username': 'test-user'})
|
||||
await hass.auth.async_link_user(user, cred)
|
||||
refresh_token = await hass.auth.async_create_refresh_token(user, CLIENT_ID)
|
||||
access_token = hass.auth.async_create_access_token(refresh_token)
|
||||
|
||||
client = await hass_ws_client(hass, access_token)
|
||||
|
||||
await client.send_json({
|
||||
'id': 10,
|
||||
'type': mfa_setup_flow.WS_TYPE_SETUP_MFA,
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 10
|
||||
assert result['success'] is False
|
||||
assert result['error']['code'] == 'no_module'
|
||||
|
||||
await client.send_json({
|
||||
'id': 11,
|
||||
'type': mfa_setup_flow.WS_TYPE_SETUP_MFA,
|
||||
'mfa_module_id': 'example_module',
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 11
|
||||
assert result['success']
|
||||
|
||||
flow = result['result']
|
||||
assert flow['type'] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert flow['handler'] == 'example_module'
|
||||
assert flow['step_id'] == 'init'
|
||||
assert flow['data_schema'][0] == {'type': 'string', 'name': 'pin'}
|
||||
|
||||
await client.send_json({
|
||||
'id': 12,
|
||||
'type': mfa_setup_flow.WS_TYPE_SETUP_MFA,
|
||||
'flow_id': flow['flow_id'],
|
||||
'user_input': {'pin': '654321'},
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 12
|
||||
assert result['success']
|
||||
|
||||
flow = result['result']
|
||||
assert flow['type'] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert flow['handler'] == 'example_module'
|
||||
assert flow['data']['result'] is None
|
||||
|
||||
await client.send_json({
|
||||
'id': 13,
|
||||
'type': mfa_setup_flow.WS_TYPE_DEPOSE_MFA,
|
||||
'mfa_module_id': 'invalid_id',
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 13
|
||||
assert result['success'] is False
|
||||
assert result['error']['code'] == 'disable_failed'
|
||||
|
||||
await client.send_json({
|
||||
'id': 14,
|
||||
'type': mfa_setup_flow.WS_TYPE_DEPOSE_MFA,
|
||||
'mfa_module_id': 'example_module',
|
||||
})
|
||||
|
||||
result = await client.receive_json()
|
||||
assert result['id'] == 14
|
||||
assert result['success']
|
||||
assert result['result'] == 'done'
|
||||
Reference in new issue
Block a user