Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reporting endpoints #150

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Encryption,
)
from emergency_alerts_utils.clients.redis.redis_client import RedisClient
from emergency_alerts_utils.clients.slack.slack_client import SlackClient
from emergency_alerts_utils.clients.statsd.statsd_client import StatsdClient
from emergency_alerts_utils.clients.zendesk.zendesk_client import ZendeskClient
from flask import (
Expand Down Expand Up @@ -63,6 +64,7 @@ def apply_driver_hacks(self, app, info, options):
aws_ses_stub_client = AwsSesStubClient()
encryption = Encryption()
zendesk_client = ZendeskClient()
slack_client = SlackClient()
statsd_client = StatsdClient()
redis_store = RedisClient()
cbc_proxy_client = CBCProxyClient()
Expand Down Expand Up @@ -161,6 +163,7 @@ def register_blueprint(application):
from app.organisation.invite_rest import organisation_invite_blueprint
from app.organisation.rest import organisation_blueprint
from app.platform_stats.rest import platform_stats_blueprint
from app.reports.rest import reports_blueprint
from app.service.callback_rest import service_callback_blueprint
from app.service.rest import service_blueprint
from app.service_invite.rest import (
Expand Down Expand Up @@ -240,6 +243,9 @@ def register_blueprint(application):
failed_logins_blueprint.before_request(requires_admin_auth)
application.register_blueprint(failed_logins_blueprint)

reports_blueprint.before_request(requires_admin_auth)
application.register_blueprint(reports_blueprint)


def register_v2_blueprints(application):
from app.authentication.auth import requires_auth
Expand Down
3 changes: 3 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Config(object):
EXPIRE_CACHE_EIGHT_DAYS = 8 * 24 * 60 * 60

ZENDESK_API_KEY = os.environ.get("ZENDESK_API_KEY")
REPORTS_SLACK_WEBHOOK_URL = os.environ.get("REPORTS_SLACK_WEBHOOK_URL")

# Logging
DEBUG = True
Expand Down Expand Up @@ -395,6 +396,8 @@ class Test(Config):
)
ADMIN_EXTERNAL_URL = f"https://{TENANT}admin.{SUBDOMAIN}emergency-alerts.service.gov.uk"

REPORTS_SLACK_WEBHOOK_URL = "https://hooks.slack.com/somewhere"

CBC_PROXY_ENABLED = True
DVLA_EMAIL_ADDRESSES = ["success@simulator.amazonses.com", "success+2@simulator.amazonses.com"]

Expand Down
Empty file added app/reports/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions app/reports/rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json

from emergency_alerts_utils.clients.slack.slack_client import SlackMessage
from flask import Blueprint, current_app, jsonify, request

from app import slack_client
from app.errors import register_errors

reports_blueprint = Blueprint("reports", __name__, url_prefix="/reports")
register_errors(reports_blueprint)


@reports_blueprint.route("", methods=["POST"])
def log_report():
data = request.get_json()
message = SlackMessage(
webhook_url=current_app.config["REPORTS_SLACK_WEBHOOK_URL"],
subject="Reporting Endpoint Submission",
message_type="info",
markdown_sections=[
(
f"*Type*: {data.get('type', 'N/A')}\n\n"
f"*URL*: {data.get('url', 'N/A')}\n\n"
f"*User Agent*: {data.get('user_agent', 'N/A')}\n\n"
f"*Body*: ```{json.dumps(data.get('body', {}), indent=4)}```"
)
],
)
response = slack_client.send_message_to_slack(message)

return jsonify(data=response), 201
17 changes: 17 additions & 0 deletions tests/app/reports/test_rest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def test_create_report(admin_request, mocker):
data = {
"type": "some-violation",
"url": "https://gov.uk",
"user_agent": "some-browser",
"body": {"something": "random", "could_be": "anything"},
}

mock_log_report = mocker.patch(
"app.reports.rest.slack_client.send_message_to_slack",
autospec=True,
return_value={"message": "Slack message sent to the provided webhook URL."},
)

admin_request.post("reports.log_report", _data=data, _expected_status=201)

mock_log_report.assert_called_once()
Loading