def register(self, router): """Register the view with a router.""" handler = request_handler_factory(self, self.async_head) router.add_route('head', self.url, handler) handler = request_handler_factory(self, self.async_get) router.add_route('get', self.url, handler)
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 # This used to check that with trusted networks we reset login attempts # We no longer support trusted networks. resp = await client.get("/auth_true") assert resp.status == 200 assert app[KEY_FAILED_LOGIN_ATTEMPTS][remote_ip] == 2
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]
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]
def register(self, router): """Register the view with a router.""" handler = request_handler_factory(self, self.async_notify) router.add_route('notify', UpnpNotifyView.url, handler)