def test_delete_alias_already_in_trash(flask_client): """delete an alias that's already in alias trash""" user = User.create( email="[email protected]", password="******", name="Test User", activated=True, commit=True, ) alias = Alias.create( user_id=user.id, email="*****@*****.**", mailbox_id=user.default_mailbox_id, commit=True, ) # add the alias to global trash Session.add(DeletedAlias(email=alias.email)) Session.commit() delete_alias(alias, user) assert Alias.get_by(email="*****@*****.**") is None
def add_note(): """Добавить новую заметку""" response = request.get_json() # validation if "id" in response and "title" in response and "description" in response: try: new_note = Note( id=response["id"], title=response["title"], description=response["description"], ) Session.add(new_note) Session.commit() return make_response(jsonify(success=True), 201) # Created except: Session.rollback() # raise return make_response(jsonify(success=False), 409) # Conflict else: return make_response(jsonify(success=False), 400) # Bad Request
async def _hibp_check(api_key, queue): """ Uses a single API key to check the queue as fast as possible. This function to be ran simultaneously (multiple _hibp_check functions with different keys on the same queue) to make maximum use of multiple API keys. """ while True: try: alias_id = queue.get_nowait() except asyncio.QueueEmpty: return alias = Alias.get(alias_id) # an alias can be deleted in the meantime if not alias: return LOG.d("Checking HIBP for %s", alias) request_headers = { "user-agent": "SimpleLogin", "hibp-api-key": api_key, } r = requests.get( f"https://haveibeenpwned.com/api/v3/breachedaccount/{urllib.parse.quote(alias.email)}", headers=request_headers, ) if r.status_code == 200: # Breaches found alias.hibp_breaches = [ Hibp.get_by(name=entry["Name"]) for entry in r.json() ] if len(alias.hibp_breaches) > 0: LOG.w("%s appears in HIBP breaches %s", alias, alias.hibp_breaches) elif r.status_code == 404: # No breaches found alias.hibp_breaches = [] elif r.status_code == 429: # rate limited LOG.w("HIBP rate limited, check alias %s in the next run", alias) await asyncio.sleep(1.6) return elif r.status_code > 500: LOG.w("HIBP server 5** error %s", r.status_code) return else: LOG.error( "An error occured while checking alias %s: %s - %s", alias, r.status_code, r.text, ) return alias.hibp_last_check = arrow.utcnow() Session.add(alias) Session.commit() LOG.d("Updated breaches info for %s", alias) await asyncio.sleep(1.6)