diff --git a/kibicara/webapi/admin.py b/kibicara/webapi/admin.py index 905666b..7529481 100644 --- a/kibicara/webapi/admin.py +++ b/kibicara/webapi/admin.py @@ -9,6 +9,7 @@ from fastapi import APIRouter, Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm from kibicara import email +from kibicara.config import config from kibicara.model import Admin, AdminHoodRelation from logging import getLogger from nacl.encoding import URLSafeBase64Encoder @@ -96,13 +97,14 @@ async def admin_register(values: BodyAdmin): ) register_token = to_token(**values.__dict__) logger.debug(f'register_token={register_token}') - # TODO implement check to see if email already is in database try: + admin = await Admin.objects.filter(email=values.email).all() + if admin: + raise HTTPException(status_code=status.HTTP_409_CONFLICT) + body = f'{config["root_url"]}/confirm?token={register_token}' + logger.debug(body) email.send_email( - to=values.email, - subject='Confirm Account', - # XXX create real confirm link - body=register_token, + to=values.email, subject='Confirm Account', body=body, ) except (ConnectionRefusedError, SMTPException): logger.exception('Email sending failed') diff --git a/kibicara/webapi/hoods/__init__.py b/kibicara/webapi/hoods/__init__.py index c79f981..22452f6 100644 --- a/kibicara/webapi/hoods/__init__.py +++ b/kibicara/webapi/hoods/__init__.py @@ -83,7 +83,7 @@ async def hood_create(values: BodyHood, response: Response, admin=Depends(get_ad operation_id='get_hood', tags=['hoods'], ) -async def hood_read(hood=Depends(get_hood)): +async def hood_read(hood=Depends(get_hood_unauthorized)): """ Get hood with id **hood_id**. """ return hood diff --git a/tests/conftest.py b/tests/conftest.py index 8693470..316daae 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,6 +10,7 @@ from kibicara.model import Hood, Mapping from kibicara.platforms.twitter.model import Twitter from kibicara.webapi import router from pytest import fixture +from urllib.parse import urlparse @fixture(scope='module') @@ -50,7 +51,7 @@ def register_token(client, receive_email): '/api/admin/register/', json={'email': 'user', 'password': 'password'} ) assert response.status_code == status.HTTP_202_ACCEPTED - return receive_email()['body'] + return urlparse(receive_email()['body']).query.split('=', 1)[1] @fixture(scope='module') diff --git a/tests/test_api_hoods.py b/tests/test_api_hoods.py index b68845e..0ff7517 100644 --- a/tests/test_api_hoods.py +++ b/tests/test_api_hoods.py @@ -16,9 +16,9 @@ def test_hood_create_unauthorized(client, hood_id): assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_hood_read_unauthorized(client, hood_id): +def test_hood_read(client, hood_id): response = client.get('/api/hoods/%d' % hood_id) - assert response.status_code == status.HTTP_401_UNAUTHORIZED + assert response.status_code == status.HTTP_200_OK def test_hood_update_unauthorized(client, hood_id):