mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
Add user via api
Leave original user object in for this slice. Remove on next
This commit is contained in:
committed by
Rebecca Law
parent
762ab8e394
commit
c86b53f7f1
@@ -1,12 +1,21 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import render_template, redirect, session
|
||||
from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
session,
|
||||
current_app,
|
||||
abort
|
||||
)
|
||||
|
||||
from client.errors import HTTPError
|
||||
|
||||
from app.main import main
|
||||
from app.models import User
|
||||
from app.main.dao import users_dao
|
||||
from app.main.forms import RegisterUserForm
|
||||
from app.models import User
|
||||
|
||||
from app.notify_client.user_api_client import UserApiClient
|
||||
|
||||
# TODO how do we handle duplicate unverifed email addresses?
|
||||
# malicious or otherwise.
|
||||
@@ -18,6 +27,8 @@ def register():
|
||||
form = RegisterUserForm(users_dao.get_user_by_email)
|
||||
|
||||
if form.validate_on_submit():
|
||||
|
||||
# TODO remove once all api integrations done
|
||||
user = User(name=form.name.data,
|
||||
email_address=form.email_address.data,
|
||||
mobile_number=form.mobile_number.data,
|
||||
@@ -25,6 +36,21 @@ def register():
|
||||
created_at=datetime.now(),
|
||||
role_id=1)
|
||||
users_dao.insert_user(user)
|
||||
|
||||
user_api_client = UserApiClient(current_app.config['NOTIFY_API_URL'],
|
||||
current_app.config['ADMIN_CLIENT_USER_NAME'],
|
||||
current_app.config['ADMIN_CLIENT_SECRET'])
|
||||
try:
|
||||
user_api_client.register_user(form.name.data,
|
||||
form.email_address.data,
|
||||
form.mobile_number.data,
|
||||
form.password.data)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
|
||||
# TODO possibly there should be some exception handling
|
||||
# for sending sms and email codes.
|
||||
# How do we report to the user there is a problem with
|
||||
|
||||
18
app/notify_client/user_api_client.py
Normal file
18
app/notify_client/user_api_client.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from client.notifications import BaseAPIClient
|
||||
|
||||
|
||||
class UserApiClient(BaseAPIClient):
|
||||
|
||||
def __init__(self, base_url, client_id, secret):
|
||||
super(self.__class__, self).__init__(base_url=base_url,
|
||||
client_id=client_id,
|
||||
secret=secret)
|
||||
|
||||
def register_user(self, name, email_address, mobile_number, password):
|
||||
data = {
|
||||
"name": name,
|
||||
"email_address": email_address,
|
||||
"mobile_number": mobile_number,
|
||||
"password": password}
|
||||
|
||||
return self.post("/user", data)
|
||||
@@ -22,7 +22,7 @@ class Config(object):
|
||||
SESSION_COOKIE_HTTPONLY = True
|
||||
SESSION_COOKIE_SECURE = True
|
||||
|
||||
NOTIFY_API_URL = os.getenv('NOTIFY_API_URL', "http://localhost:6001")
|
||||
NOTIFY_API_URL = os.getenv('NOTIFY_API_URL')
|
||||
NOTIFY_API_SECRET = os.getenv('NOTIFY_API_SECRET', "dev-secret")
|
||||
NOTIFY_API_CLIENT = os.getenv('NOTIFY_API_CLIENT', "admin")
|
||||
|
||||
@@ -41,12 +41,12 @@ class Config(object):
|
||||
|
||||
class Development(Config):
|
||||
DEBUG = True
|
||||
NOTIFY_API_URL = 'http://localhost:6011'
|
||||
ADMIN_CLIENT_USER_NAME = 'dev-notify-admin'
|
||||
ADMIN_CLIENT_SECRET = 'dev-notify-secret-key'
|
||||
|
||||
|
||||
class Test(Config):
|
||||
DEBUG = True
|
||||
class Test(Development):
|
||||
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/test_notifications_admin'
|
||||
WTF_CSRF_ENABLED = False
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from flask import url_for
|
||||
|
||||
from tests.conftest import mock_register_user
|
||||
|
||||
|
||||
def test_render_register_returns_template_with_form(app_, db_, db_session):
|
||||
response = app_.test_client().get('/register')
|
||||
@@ -12,13 +14,21 @@ def test_process_register_creates_new_user(app_,
|
||||
db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
mock_send_email,
|
||||
mocker):
|
||||
|
||||
user_data = {
|
||||
'name': 'Some One Valid',
|
||||
'email_address': 'someone@example.gov.uk',
|
||||
'mobile_number': '+4407700900460',
|
||||
'password': 'validPassword!'
|
||||
}
|
||||
|
||||
mock_register_user(mocker, user_data)
|
||||
|
||||
with app_.test_request_context():
|
||||
response = app_.test_client().post('/register',
|
||||
data={'name': 'Some One Valid',
|
||||
'email_address': 'someone@example.gov.uk',
|
||||
'mobile_number': '+4407700900460',
|
||||
'password': 'validPassword!'})
|
||||
data=user_data)
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for('main.verify', _external=True)
|
||||
|
||||
@@ -57,13 +67,19 @@ def test_should_add_verify_codes_on_session(app_,
|
||||
db_,
|
||||
db_session,
|
||||
mock_send_sms,
|
||||
mock_send_email):
|
||||
mock_send_email,
|
||||
mocker):
|
||||
user_data = {
|
||||
'name': 'Test Codes',
|
||||
'email_address': 'test@example.gov.uk',
|
||||
'mobile_number': '+4407700900460',
|
||||
'password': 'validPassword!'
|
||||
}
|
||||
|
||||
mock_register_user(mocker, user_data)
|
||||
with app_.test_client() as client:
|
||||
response = client.post('/register',
|
||||
data={'name': 'Test Codes',
|
||||
'email_address': 'test_codes@example.gov.uk',
|
||||
'mobile_number': '+4407700900460',
|
||||
'password': 'validPassword!'})
|
||||
data=user_data)
|
||||
assert response.status_code == 302
|
||||
assert 'notify_admin_session' in response.headers.get('Set-Cookie')
|
||||
|
||||
|
||||
@@ -142,3 +142,17 @@ def mock_delete_service(mocker, mock_get_service):
|
||||
mock_class = mocker.patch(
|
||||
'app.notifications_api_client.delete_service', side_effect=_delete)
|
||||
return mock_class
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_register_user(mocker, user_data):
|
||||
data = {
|
||||
"email_address": user_data['email_address'],
|
||||
"failed_login_count": 0,
|
||||
"mobile_number": user_data['mobile_number'],
|
||||
"name": user_data['name'],
|
||||
"state": "pending"
|
||||
}
|
||||
mock_class = mocker.patch('app.main.views.register.UserApiClient')
|
||||
mock_class.register_user.return_value = data
|
||||
return mock_class
|
||||
|
||||
Reference in New Issue
Block a user