Skip to content

Commit

Permalink
Added alerts APIs (#46)
Browse files Browse the repository at this point in the history
* Added alerts APIs

* Fixed tests
  • Loading branch information
Lorygold committed Jul 25, 2023
1 parent fbd2602 commit 393693f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions buffalogs/buffalogs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@
path("users_pie_chart_api/", views.users_pie_chart_api, name="users_pie_chart_api"),
path("alerts_line_chart_api/", views.alerts_line_chart_api, name="alerts_line_chart_api"),
path("world_map_chart_api/", views.world_map_chart_api, name="world_map_chart_api"),
path("alerts_api/", views.alerts_api, name="alerts_api"),
path("risk_score_api/", views.risk_score_api, name="risk_score_api"),
path("authentication/", include("authentication.urls")),
]
26 changes: 26 additions & 0 deletions buffalogs/impossible_travel/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,29 @@ def test_world_map_chart_api(self):
response = self.client.get(f"{reverse('world_map_chart_api')}?start={start.strftime('%Y-%m-%dT%H:%M:%SZ')}&end={end.strftime('%Y-%m-%dT%H:%M:%SZ')}")
self.assertEqual(response.status_code, 200)
self.assertDictEqual(dict_expected_result, json.loads(response.content))

def test_alerts_api(self):
creation_mock_time = datetime(2023, 7, 25, 12, 0)
alert = Alert.objects.get(login_raw_data__timestamp="2023-05-20T11:45:01.229Z")
alert.created = creation_mock_time
alert.save()
alert = Alert.objects.get(login_raw_data__timestamp="2023-06-20T10:17:33.358Z")
alert.created = creation_mock_time + timedelta(minutes=5)
alert.save()
start = creation_mock_time
end = creation_mock_time + timedelta(minutes=10)
list_expected_result = [
{"timestamp": "2023-05-20T11:45:01.229Z", "username": "Lorena Goldoni", "rule_name": "Login from new device"},
{"timestamp": "2023-06-20T10:17:33.358Z", "username": "Lorena Goldoni", "rule_name": "Impossible Travel detected"},
]
response = self.client.get(f"{reverse('alerts_api')}?start={start.strftime('%Y-%m-%dT%H:%M:%SZ')}&end={end.strftime('%Y-%m-%dT%H:%M:%SZ')}")
self.assertEqual(response.status_code, 200)
self.assertListEqual(list_expected_result, json.loads(response.content))

def test_risk_score_api(self):
end = datetime.now() + timedelta(seconds=1)
start = end - timedelta(minutes=1)
dict_expected_result = {"Lorena Goldoni": "No risk", "Lorygold": "Low", "Lory": "Low", "Lor": "Low", "Loryg": "Medium"}
response = self.client.get(f"{reverse('risk_score_api')}?start={start.strftime('%Y-%m-%dT%H:%M:%SZ')}&end={end.strftime('%Y-%m-%dT%H:%M:%SZ')}")
self.assertEqual(response.status_code, 200)
self.assertDictEqual(dict_expected_result, json.loads(response.content))
27 changes: 27 additions & 0 deletions buffalogs/impossible_travel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,30 @@ def world_map_chart_api(request):
result[key] = country_alerts
data = json.dumps(result)
return HttpResponse(data, content_type="json")


@require_http_methods(["GET"])
def alerts_api(request):
result = []
timestamp_format = "%Y-%m-%dT%H:%M:%SZ"
start_date = datetime.strptime(request.GET.get("start", ""), timestamp_format)
end_date = datetime.strptime(request.GET.get("end", ""), timestamp_format)
alerts_list = Alert.objects.filter(created__range=(start_date, end_date))
for alert in alerts_list:
tmp = {"timestamp": alert.login_raw_data["timestamp"], "username": User.objects.get(id=alert.user_id).username, "rule_name": alert.name}
result.append(tmp)
data = json.dumps(result)
return HttpResponse(data, content_type="json")


@require_http_methods(["GET"])
def risk_score_api(request):
result = {}
timestamp_format = "%Y-%m-%dT%H:%M:%SZ"
start_date = datetime.strptime(request.GET.get("start", ""), timestamp_format)
end_date = datetime.strptime(request.GET.get("end", ""), timestamp_format)
user_risk_list = User.objects.filter(updated__range=(start_date, end_date)).values()
for key in user_risk_list:
result[key["username"]] = key["risk_score"]
data = json.dumps(result)
return HttpResponse(data, content_type="json")

0 comments on commit 393693f

Please sign in to comment.