mirror of
https://github.com/bellingcat/auto-archiver-api.git
synced 2026-06-12 05:28:34 +03:00
missing tests for security
This commit is contained in:
@@ -36,8 +36,26 @@ async def test_get_token_or_user_auth_with_user():
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_get_user_auth(m1):
|
async def test_get_user_auth(m1):
|
||||||
from web.security import get_user_auth
|
from web.security import get_user_auth
|
||||||
bad_user = HTTPAuthorizationCredentials(scheme="ipsum", credentials="valid-and-good")
|
good_user = HTTPAuthorizationCredentials(scheme="ipsum", credentials="valid-and-good")
|
||||||
assert await get_user_auth(bad_user) == "summer@example.com"
|
assert await get_user_auth(good_user) == "summer@example.com"
|
||||||
|
|
||||||
|
|
||||||
|
@patch("web.security.authenticate_user", return_value=(True, "summer@example.com"))
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_active_user_auth_inactive(m1, db_session):
|
||||||
|
from web.security import get_active_user_auth
|
||||||
|
|
||||||
|
# inactive at first
|
||||||
|
creds = HTTPAuthorizationCredentials(scheme="ipsum", credentials="valid-and-good")
|
||||||
|
with pytest.raises(HTTPException):
|
||||||
|
await get_active_user_auth(creds)
|
||||||
|
|
||||||
|
from db import models
|
||||||
|
db_session.add(models.User(email="summer@example.com", is_active=True))
|
||||||
|
db_session.commit()
|
||||||
|
assert await get_active_user_auth(creds) == "summer@example.com"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@patch("web.security.secure_compare", return_value=False)
|
@patch("web.security.secure_compare", return_value=False)
|
||||||
|
|||||||
@@ -58,14 +58,11 @@ async def get_user_auth(credentials: HTTPAuthorizationCredentials = Depends(bear
|
|||||||
|
|
||||||
async def get_active_user_auth(credentials: HTTPAuthorizationCredentials = Depends(bearer_security)):
|
async def get_active_user_auth(credentials: HTTPAuthorizationCredentials = Depends(bearer_security)):
|
||||||
# validates Bearer token and Active User status
|
# validates Bearer token and Active User status
|
||||||
try:
|
email = await get_user_auth(credentials)
|
||||||
email = await get_user_auth(credentials)
|
with get_db() as db:
|
||||||
with get_db() as db:
|
if crud.is_active_user(db, email):
|
||||||
if crud.is_active_user(db, email):
|
return email
|
||||||
return email
|
raise HTTPException(status_code=403, detail="User is not active")
|
||||||
raise HTTPException(status_code=403, detail="User is not active")
|
|
||||||
except HTTPException as e:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
|
|
||||||
def authenticate_user(access_token):
|
def authenticate_user(access_token):
|
||||||
|
|||||||
Reference in New Issue
Block a user