2021-03-10 13:55:06 +00:00
|
|
|
import pytest
|
2017-08-23 15:04:37 +01:00
|
|
|
import requests
|
2017-01-27 12:21:08 +00:00
|
|
|
import requests_mock
|
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.clients.performance_platform.performance_platform_client import (
|
|
|
|
|
PerformancePlatformClient,
|
|
|
|
|
)
|
2017-01-27 12:21:08 +00:00
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@pytest.fixture(scope="function")
|
2017-08-24 17:08:39 +01:00
|
|
|
def perf_client(client, mocker):
|
2017-08-23 15:04:37 +01:00
|
|
|
perf_client = PerformancePlatformClient()
|
2023-08-29 14:54:30 -07:00
|
|
|
current_app = mocker.Mock(
|
|
|
|
|
config={
|
|
|
|
|
"PERFORMANCE_PLATFORM_ENABLED": True,
|
|
|
|
|
"PERFORMANCE_PLATFORM_ENDPOINTS": {"foo": "my_token", "bar": "other_token"},
|
|
|
|
|
"PERFORMANCE_PLATFORM_URL": "https://performance-platform-url/",
|
|
|
|
|
}
|
|
|
|
|
)
|
2017-08-23 15:04:37 +01:00
|
|
|
perf_client.init_app(current_app)
|
|
|
|
|
return perf_client
|
2017-01-27 12:21:08 +00:00
|
|
|
|
|
|
|
|
|
2017-08-23 15:04:37 +01:00
|
|
|
def test_should_not_call_if_not_enabled(perf_client):
|
|
|
|
|
with requests_mock.Mocker() as request_mock:
|
2023-08-29 14:54:30 -07:00
|
|
|
request_mock.post(
|
|
|
|
|
"https://performance-platform-url/foo", json={}, status_code=200
|
|
|
|
|
)
|
2017-08-23 15:04:37 +01:00
|
|
|
perf_client._active = False
|
2023-08-29 14:54:30 -07:00
|
|
|
perf_client.send_stats_to_performance_platform({"dataType": "foo"})
|
2017-01-27 12:21:08 +00:00
|
|
|
|
2017-08-23 15:04:37 +01:00
|
|
|
assert request_mock.called is False
|
2017-01-27 12:21:08 +00:00
|
|
|
|
|
|
|
|
|
2017-08-24 17:08:39 +01:00
|
|
|
def test_should_call_datatype_endpoint_if_enabled(perf_client):
|
2017-01-27 12:21:08 +00:00
|
|
|
with requests_mock.Mocker() as request_mock:
|
2023-08-29 14:54:30 -07:00
|
|
|
request_mock.post(
|
|
|
|
|
"https://performance-platform-url/foo", json={}, status_code=200
|
|
|
|
|
)
|
|
|
|
|
perf_client.send_stats_to_performance_platform({"dataType": "foo"})
|
2017-01-27 12:21:08 +00:00
|
|
|
|
|
|
|
|
assert request_mock.call_count == 1
|
2023-08-29 14:54:30 -07:00
|
|
|
assert request_mock.last_request.method == "POST"
|
2017-01-27 12:21:08 +00:00
|
|
|
|
2017-01-27 16:39:01 +00:00
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"dataset, token", [("foo", "my_token"), ("bar", "other_token")]
|
|
|
|
|
)
|
2017-08-23 15:04:37 +01:00
|
|
|
def test_should_use_correct_token(perf_client, dataset, token):
|
|
|
|
|
with requests_mock.Mocker() as request_mock:
|
2023-08-29 14:54:30 -07:00
|
|
|
request_mock.post(
|
|
|
|
|
"https://performance-platform-url/foo", json={}, status_code=200
|
|
|
|
|
)
|
|
|
|
|
request_mock.post(
|
|
|
|
|
"https://performance-platform-url/bar", json={}, status_code=200
|
|
|
|
|
)
|
|
|
|
|
perf_client.send_stats_to_performance_platform({"dataType": dataset})
|
2017-01-27 16:39:01 +00:00
|
|
|
|
2017-08-23 15:04:37 +01:00
|
|
|
assert request_mock.call_count == 1
|
2023-08-29 14:54:30 -07:00
|
|
|
assert request_mock.last_request.headers.get("authorization") == "Bearer {}".format(
|
|
|
|
|
token
|
|
|
|
|
)
|
2017-01-27 16:39:01 +00:00
|
|
|
|
|
|
|
|
|
2017-08-23 15:04:37 +01:00
|
|
|
def test_should_raise_for_status(perf_client):
|
|
|
|
|
with pytest.raises(requests.HTTPError), requests_mock.Mocker() as request_mock:
|
2023-08-29 14:54:30 -07:00
|
|
|
request_mock.post(
|
|
|
|
|
"https://performance-platform-url/foo", json={}, status_code=403
|
|
|
|
|
)
|
|
|
|
|
perf_client.send_stats_to_performance_platform({"dataType": "foo"})
|
2023-08-11 11:47:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_generate_payload_id():
|
2023-08-29 14:54:30 -07:00
|
|
|
payload = {
|
|
|
|
|
"_timestamp": "2023-01-01 00:00:00",
|
|
|
|
|
"service": "my_service",
|
|
|
|
|
"group_name": "group_name",
|
|
|
|
|
"dataType": "dataType",
|
|
|
|
|
"period": "period",
|
|
|
|
|
}
|
2023-08-11 11:47:57 -07:00
|
|
|
result = PerformancePlatformClient.generate_payload_id(payload, "group_name")
|
2023-08-29 14:54:30 -07:00
|
|
|
assert (
|
|
|
|
|
result
|
|
|
|
|
== "MjAyMy0wMS0wMSAwMDowMDowMG15X3NlcnZpY2Vncm91cF9uYW1lZGF0YVR5cGVwZXJpb2Q="
|
|
|
|
|
)
|