2021-03-22 12:42:42 +01:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
2018-11-11 10:51:22 +01:00
|
|
|
import json
|
2020-10-06 15:26:44 +02:00
|
|
|
from unittest.mock import patch
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2021-01-14 13:12:59 +01:00
|
|
|
import pytest
|
|
|
|
from falcon import testing
|
|
|
|
|
2021-03-21 09:32:58 +01:00
|
|
|
from dspace_statistics_api.app import app
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def client():
|
2021-03-21 09:32:58 +01:00
|
|
|
return testing.TestClient(app)
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_get_community(client):
|
|
|
|
"""Test requesting a single community."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_get("/community/bde7139c-d321-46bb-aef6-ae70799e5edb")
|
2018-11-11 16:01:17 +01:00
|
|
|
response_doc = json.loads(response.text)
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
assert isinstance(response_doc["downloads"], int)
|
2020-03-02 10:10:41 +01:00
|
|
|
assert isinstance(response_doc["id"], str)
|
2019-12-14 11:39:58 +01:00
|
|
|
assert isinstance(response_doc["views"], int)
|
2018-11-11 10:51:22 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_get_missing_community(client):
|
|
|
|
"""Test requesting a single non-existing community."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_get("/item/dec6bfc6-efeb-4f74-8436-79fa80bb5c21")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_get_communities(client):
|
|
|
|
"""Test requesting 100 communities."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_get("/communities", query_string="limit=100")
|
2018-11-11 16:05:31 +01:00
|
|
|
response_doc = json.loads(response.text)
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
assert isinstance(response_doc["currentPage"], int)
|
|
|
|
assert isinstance(response_doc["totalPages"], int)
|
|
|
|
assert isinstance(response_doc["statistics"], list)
|
2018-11-11 10:51:22 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_get_communities_invalid_limit(client):
|
|
|
|
"""Test requesting 100 communities with an invalid limit parameter."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_get("/communities", query_string="limit=101")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_get_communities_invalid_page(client):
|
|
|
|
"""Test requesting 100 communities with an invalid page parameter."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_get("/communities", query_string="page=-1")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_dateFrom(client):
|
|
|
|
"""Test POSTing a request to /communities with a valid dateFrom parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"dateFrom": "2020-01-01T00:00:00Z",
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 100
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 2
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_dateFrom_mocked(client):
|
|
|
|
"""Mock test POSTing a request to /communities with a valid dateFrom parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"dateFrom": "2020-01-01T00:00:00Z",
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
get_views_return_value = {
|
2020-12-20 21:12:13 +01:00
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb": 309,
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
|
2020-10-06 15:26:44 +02:00
|
|
|
}
|
|
|
|
get_downloads_return_value = {
|
2020-12-20 21:12:13 +01:00
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb": 400,
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 290,
|
2020-10-06 15:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
|
|
|
):
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_downloads",
|
|
|
|
return_value=get_downloads_return_value,
|
|
|
|
):
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 100
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 2
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_invalid_dateFrom(client):
|
|
|
|
"""Test POSTing a request to /communities with an invalid dateFrom parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"dateFrom": "2020-01-01T00:00:00",
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_dateTo(client):
|
|
|
|
"""Test POSTing a request to /communities with a valid dateTo parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"dateTo": "2020-01-01T00:00:00Z",
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 100
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 2
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_dateTo_mocked(client):
|
|
|
|
"""Mock test POSTing a request to /communities with a valid dateTo parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"dateTo": "2020-01-01T00:00:00Z",
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
get_views_return_value = {
|
2020-12-20 21:12:13 +01:00
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb": 21,
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
|
2020-10-06 15:26:44 +02:00
|
|
|
}
|
|
|
|
get_downloads_return_value = {
|
2020-12-20 21:12:13 +01:00
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb": 575,
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 899,
|
2020-10-06 15:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
|
|
|
):
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_downloads",
|
|
|
|
return_value=get_downloads_return_value,
|
|
|
|
):
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 100
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 2
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_invalid_dateTo(client):
|
|
|
|
"""Test POSTing a request to /communities with an invalid dateTo parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"dateFrom": "2020-01-01T00:00:00",
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_limit(client):
|
|
|
|
"""Test POSTing a request to /communities with a valid limit parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"limit": 1,
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 1
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 1
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_limit_mocked(client):
|
|
|
|
"""Mock test POSTing a request to /communities with a valid limit parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"limit": 1,
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
get_views_return_value = {"bde7139c-d321-46bb-aef6-ae70799e5edb": 200}
|
|
|
|
get_downloads_return_value = {"bde7139c-d321-46bb-aef6-ae70799e5edb": 309}
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
|
|
|
):
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_downloads",
|
|
|
|
return_value=get_downloads_return_value,
|
|
|
|
):
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 1
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 1
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_invalid_limit(client):
|
|
|
|
"""Test POSTing a request to /communities with an invalid limit parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"limit": -1,
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_page(client):
|
|
|
|
"""Test POSTing a request to /communities with a valid page parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"page": 0,
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 100
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert response.json["totalPages"] == 0
|
|
|
|
assert len(response.json["statistics"]) == 2
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_valid_page_mocked(client):
|
|
|
|
"""Mock test POSTing a request to communities with a valid page parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"page": 0,
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
get_views_return_value = {
|
2020-12-20 21:12:13 +01:00
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb": 21,
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
|
2020-10-06 15:26:44 +02:00
|
|
|
}
|
|
|
|
get_downloads_return_value = {
|
2020-12-20 21:12:13 +01:00
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb": 575,
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 899,
|
2020-10-06 15:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
|
|
|
):
|
|
|
|
with patch(
|
|
|
|
"dspace_statistics_api.app.get_downloads",
|
|
|
|
return_value=get_downloads_return_value,
|
|
|
|
):
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.json["limit"] == 100
|
|
|
|
assert response.json["currentPage"] == 0
|
|
|
|
assert isinstance(response.json["totalPages"], int)
|
|
|
|
assert len(response.json["statistics"]) == 2
|
|
|
|
assert isinstance(response.json["statistics"][0]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["views"], int)
|
|
|
|
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
|
|
|
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
def test_post_communities_invalid_page(client):
|
|
|
|
"""Test POSTing a request to /communities with an invalid page parameter in the request body."""
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
request_body = {
|
|
|
|
"page": -1,
|
2020-12-20 21:12:13 +01:00
|
|
|
"communities": [
|
|
|
|
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
|
|
|
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
2020-10-06 15:26:44 +02:00
|
|
|
],
|
|
|
|
}
|
|
|
|
|
2020-12-20 21:12:13 +01:00
|
|
|
response = client.simulate_post("/communities", json=request_body)
|
2020-10-06 15:26:44 +02:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
2020-12-24 12:11:12 +01:00
|
|
|
|
2020-12-24 12:12:06 +01:00
|
|
|
|
2020-12-24 12:11:12 +01:00
|
|
|
# vim: set sw=4 ts=4 expandtab:
|