mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-25 08:44:23 -04:00
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
This commit is contained in:
@@ -14,6 +14,7 @@ from app.main.forms import (
|
||||
ConfirmPasswordForm,
|
||||
TwoFactorForm,
|
||||
)
|
||||
from app.models.user import User
|
||||
from app.utils import user_is_gov_user
|
||||
|
||||
NEW_EMAIL = 'new-email'
|
||||
@@ -37,7 +38,7 @@ def user_profile_name():
|
||||
form = ChangeNameForm(new_name=current_user.name)
|
||||
|
||||
if form.validate_on_submit():
|
||||
user_api_client.update_user_attribute(current_user.id, name=form.new_name.data)
|
||||
current_user.update(name=form.new_name.data)
|
||||
return redirect(url_for('.user_profile'))
|
||||
|
||||
return render_template(
|
||||
@@ -52,9 +53,7 @@ def user_profile_name():
|
||||
@user_is_gov_user
|
||||
def user_profile_email():
|
||||
|
||||
def _is_email_already_in_use(email):
|
||||
return user_api_client.is_email_already_in_use(email)
|
||||
form = ChangeEmailForm(_is_email_already_in_use,
|
||||
form = ChangeEmailForm(User.already_registered,
|
||||
email_address=current_user.email_address)
|
||||
|
||||
if form.validate_on_submit():
|
||||
@@ -99,9 +98,8 @@ def user_profile_email_confirm(token):
|
||||
current_app.config['DANGEROUS_SALT'],
|
||||
current_app.config['EMAIL_EXPIRY_SECONDS'])
|
||||
token_data = json.loads(token_data)
|
||||
user_id = token_data['user_id']
|
||||
new_email = token_data['email']
|
||||
user_api_client.update_user_attribute(user_id, email_address=new_email)
|
||||
user = User.from_id(token_data['user_id'])
|
||||
user.update(email_address=token_data['email'])
|
||||
session.pop(NEW_EMAIL, None)
|
||||
|
||||
return redirect(url_for('.user_profile'))
|
||||
@@ -138,7 +136,7 @@ def user_profile_mobile_number_authenticate():
|
||||
|
||||
if form.validate_on_submit():
|
||||
session[NEW_MOBILE_PASSWORD_CONFIRMED] = True
|
||||
user_api_client.send_verify_code(current_user.id, 'sms', session[NEW_MOBILE])
|
||||
current_user.send_verify_code(to=session[NEW_MOBILE])
|
||||
return redirect(url_for('.user_profile_mobile_number_confirm'))
|
||||
|
||||
return render_template(
|
||||
@@ -163,13 +161,11 @@ def user_profile_mobile_number_confirm():
|
||||
form = TwoFactorForm(_check_code)
|
||||
|
||||
if form.validate_on_submit():
|
||||
user = user_api_client.get_user(current_user.id)
|
||||
# the user will have a new current_session_id set by the API - store it in the cookie for future requests
|
||||
session['current_session_id'] = user.current_session_id
|
||||
current_user.refresh_session_id()
|
||||
mobile_number = session[NEW_MOBILE]
|
||||
del session[NEW_MOBILE]
|
||||
del session[NEW_MOBILE_PASSWORD_CONFIRMED]
|
||||
user_api_client.update_user_attribute(current_user.id, mobile_number=mobile_number)
|
||||
current_user.update(mobile_number=mobile_number)
|
||||
return redirect(url_for('.user_profile'))
|
||||
|
||||
return render_template(
|
||||
|
||||
Reference in New Issue
Block a user