From f57f8641ada499632623ce5a7bb47ee2aa26a494 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Wed, 22 May 2019 14:05:19 +0100 Subject: [PATCH] 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. --- app/event_handlers.py | 7 +++++++ app/main/views/find_users.py | 5 ++++- tests/app/main/views/test_find_users.py | 23 +++++++++++++++++++++++ tests/app/test_event_handlers.py | 16 ++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/event_handlers.py b/app/event_handlers.py index 59fa35703..166a694b9 100644 --- a/app/event_handlers.py +++ b/app/event_handlers.py @@ -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) diff --git a/app/main/views/find_users.py b/app/main/views/find_users.py index 3016da327..3d04eb42e 100644 --- a/app/main/views/find_users.py +++ b/app/main/views/find_users.py @@ -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') diff --git a/tests/app/main/views/test_find_users.py b/tests/app/main/views/test_find_users.py index 225c85258..9d3993ad4 100644 --- a/tests/app/main/views/test_find_users.py +++ b/tests/app/main/views/test_find_users.py @@ -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 diff --git a/tests/app/test_event_handlers.py b/tests/app/test_event_handlers.py index ef72a038f..e132188af 100644 --- a/tests/app/test_event_handlers.py +++ b/tests/app/test_event_handlers.py @@ -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})