Add an event if a user is archived

This adds a new type of event, 'archive_user', which stores the id of
the archived user and the id of the user who is doing the archiving.
This commit is contained in:
Katie Smith
2019-05-22 14:05:19 +01:00
parent 73b4aa4d85
commit f57f8641ad
4 changed files with 50 additions and 1 deletions

View File

@@ -25,6 +25,13 @@ def create_mobile_number_change_event(user_id, updated_by_id, original_mobile_nu
new_mobile_number=new_mobile_number)
def create_archive_user_event(user_id, archived_by_id):
_send_event(
'archive_user',
user_id=user_id,
archived_by_id=archived_by_id)
def _send_event(event_type, **kwargs):
event_data = _construct_event_data(request)
event_data.update(kwargs)

View File

@@ -1,7 +1,8 @@
from flask import flash, redirect, render_template, request, url_for
from flask_login import login_required
from flask_login import current_user, login_required
from app import user_api_client
from app.event_handlers import create_archive_user_event
from app.main import main
from app.main.forms import SearchUsersByEmailForm
from app.models.user import User
@@ -45,6 +46,8 @@ def user_information(user_id):
def archive_user(user_id):
if request.method == 'POST':
user_api_client.archive_user(user_id)
create_archive_user_event(str(user_id), current_user.id)
return redirect(url_for('.user_information', user_id=user_id))
else:
flash('There\'s no way to reverse this! Are you sure you want to archive this user?', 'delete')

View File

@@ -1,3 +1,4 @@
import pytest
from bs4 import BeautifulSoup
from flask import url_for
from lxml import html
@@ -166,6 +167,7 @@ def test_archive_user_posts_to_user_client(
logged_in_platform_admin_client,
api_user_active,
mocker,
mock_events,
):
mock_user_client = mocker.patch('app.user_api_client.post')
@@ -176,3 +178,24 @@ def test_archive_user_posts_to_user_client(
assert response.status_code == 302
assert response.location == url_for('main.user_information', user_id=api_user_active.id, _external=True)
mock_user_client.assert_called_once_with('/user/{}/archive'.format(api_user_active.id), data=None)
assert mock_events.called
def test_archive_user_does_not_create_event_if_user_client_raises_exception(
logged_in_platform_admin_client,
api_user_active,
mocker,
mock_events,
):
mock_user_client = mocker.patch('app.user_api_client.post', side_effect=Exception())
with pytest.raises(Exception):
response = logged_in_platform_admin_client.post(
url_for('main.archive_user', user_id=api_user_active.id)
)
assert response.status_code == 500
assert response.location == url_for('main.user_information', user_id=api_user_active.id, _external=True)
mock_user_client.assert_called_once_with('/user/{}/archive'.format(api_user_active.id), data=None)
assert not mock_events.called

View File

@@ -2,6 +2,7 @@ import uuid
from unittest.mock import ANY
from app.event_handlers import (
create_archive_user_event,
create_email_change_event,
create_mobile_number_change_event,
on_user_logged_in,
@@ -51,3 +52,18 @@ def test_create_mobile_number_change_event_calls_events_api(app_, mock_events):
'updated_by_id': updated_by_id,
'original_mobile_number': '07700900000',
'new_mobile_number': '07700900999'})
def test_create_archive_user_event_calls_events_api(app_, mock_events):
user_id = str(uuid.uuid4())
archived_by_id = str(uuid.uuid4())
with app_.test_request_context():
create_archive_user_event(user_id, archived_by_id)
mock_events.assert_called_with('archive_user',
{'browser_fingerprint':
{'browser': ANY, 'version': ANY, 'platform': ANY, 'user_agent_string': ''},
'ip_address': ANY,
'user_id': user_id,
'archived_by_id': archived_by_id})