Reset failed login attempts counter when login success (#15564)

This commit is contained in:
Jason Hu authored and Paulus Schoutsen committed 2018-07-20 12:09:48 +02:00
1 parent f2a99e83cd
commit f1286f8e6b
4 files changed
+91 -6

No files matched your search

+56 -2
View File
@@ -1,14 +1,18 @@
"""The tests for the Home Assistant HTTP component."""
# pylint: disable=protected-access
from unittest.mock import patch, mock_open
from ipaddress import ip_address
from unittest.mock import patch, mock_open, Mock
from aiohttp import web
from aiohttp.web_exceptions import HTTPUnauthorized
from aiohttp.web_middlewares import middleware
from homeassistant.components.http import KEY_AUTHENTICATED
from homeassistant.components.http.view import request_handler_factory
from homeassistant.setup import async_setup_component
import homeassistant.components.http as http
from homeassistant.components.http.ban import (
IpBan, IP_BANS_FILE, setup_bans, KEY_BANNED_IPS)
IpBan, IP_BANS_FILE, setup_bans, KEY_BANNED_IPS, KEY_FAILED_LOGIN_ATTEMPTS)
from . import mock_real_ip
@@ -88,3 +92,53 @@ async def test_ip_bans_file_creation(hass, aiohttp_client):
resp = await client.get('/')
assert resp.status == 403
assert m.call_count == 1
async def test_failed_login_attempts_counter(hass, aiohttp_client):
"""Testing if failed login attempts counter increased."""
app = web.Application()
app['hass'] = hass
async def auth_handler(request):
"""Return 200 status code."""
return None, 200
app.router.add_get('/auth_true', request_handler_factory(
Mock(requires_auth=True), auth_handler))
app.router.add_get('/auth_false', request_handler_factory(
Mock(requires_auth=True), auth_handler))
app.router.add_get('/', request_handler_factory(
Mock(requires_auth=False), auth_handler))
setup_bans(hass, app, 5)
remote_ip = ip_address("200.201.202.204")
mock_real_ip(app)("200.201.202.204")
@middleware
async def mock_auth(request, handler):
"""Mock auth middleware."""
if 'auth_true' in request.path:
request[KEY_AUTHENTICATED] = True
else:
request[KEY_AUTHENTICATED] = False
return await handler(request)
app.middlewares.append(mock_auth)
client = await aiohttp_client(app)
resp = await client.get('/auth_false')
assert resp.status == 401
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 1
resp = await client.get('/auth_false')
assert resp.status == 401
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 2
resp = await client.get('/')
assert resp.status == 200
assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 2
resp = await client.get('/auth_true')
assert resp.status == 200
assert remote_ip not in app[KEY_FAILED_LOGIN_ATTEMPTS]