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

bump major #150

Merged
merged 3 commits into from
Dec 10, 2024
Merged
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
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
Changes
=======

Version 5.0.0 (release 2024-12-10)

- tests: remove dependency to invenio-oauth2server
- setup: bump major dependencies

Version 4.2.1 (release 2024-11-30)

- setup: change to reusable workflows
Expand Down
2 changes: 1 addition & 1 deletion invenio_stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def register_queries():
from .ext import InvenioStats
from .proxies import current_stats

__version__ = "4.2.1"
__version__ = "5.0.0"

__all__ = (
"__version__",
Expand Down
29 changes: 14 additions & 15 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,31 @@ python_requires = >=3.7
zip_safe = False
install_requires =
counter-robots>=2018.6
invenio-base>=1.2.13,<2.0.0
invenio-cache>=1.1.0,<2.0.0
invenio-celery>=1.2.5,<2.0.0
invenio-base>=2.0.0,<3.0.0
invenio-cache>=2.0.0,<3.0.0
invenio-celery>=2.0.0,<3.0.0
invenio-queues>=1.0.0a2
maxminddb-geolite2>=2018.703
python-dateutil>=2.7.0
python-geoip>=1.2

[options.extras_require]
tests =
pytest-black>=0.3.0,<0.3.10
invenio-accounts>=2.1.0,<6.0.0
invenio-app>=1.0.0,<2.0.0
invenio-db>=1.0.14,<2.0.0
invenio-files-rest>=1.3.0,<3.0.0
invenio-oauth2server>=1.3.0,<3.0.0
invenio-records>=2.0.0,<3.0.0
invenio-records-ui>=1.2.0,<2.0.0
pytest-invenio>=2.1.0,<3.0.0
pytest-black-ng>=0.4.0
invenio-accounts>=6.0.0,<7.0.0
invenio-app>=2.0.0,<3.0.0
invenio-db>=2.0.0,<3.0.0
invenio-files-rest>=3.0.0,<4.0.0
invenio-records>=3.0.0,<4.0.0
invenio-records-ui>=2.0.0,<3.0.0
pytest-invenio>=3.0.0,<4.0.0
Sphinx>=5,<6
elasticsearch7 =
invenio-search[elasticsearch7]>=2.1.0,<3.0.0
invenio-search[elasticsearch7]>=3.0.0,<4.0.0
opensearch1 =
invenio-search[opensearch1]>=2.1.0,<3.0.0
invenio-search[opensearch1]>=3.0.0,<4.0.0
opensearch2 =
invenio-search[opensearch2]>=2.1.0,<3.0.0
invenio-search[opensearch2]>=3.0.0,<4.0.0
# Kept for backwards compatibility
docs =

Expand Down
25 changes: 11 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""Pytest configuration."""

import datetime
import os
import shutil
import tempfile
import uuid
Expand All @@ -23,11 +22,9 @@
from flask import appcontext_pushed, g
from flask.cli import ScriptInfo
from helpers import mock_date
from invenio_accounts.testutils import create_test_user
from invenio_app.factory import create_api as _create_api
from invenio_db import db as db_
from invenio_files_rest.models import Bucket, Location, ObjectVersion
from invenio_oauth2server.models import Token
from invenio_pidstore.minters import recid_minter
from invenio_queues.proxies import current_queues
from invenio_records.api import Record
Expand Down Expand Up @@ -442,17 +439,14 @@ def aggregated_events(app, search_clear, mock_user_ctx, request):


@pytest.fixture()
def users(app, db):
def users(UserFixture, app, db):
"""Create users."""
user1 = create_test_user(email="[email protected]", password="tester")
user2 = create_test_user(email="[email protected]", password="tester2")

user1.allowed_token = Token.create_personal(
name="allowed_token", user_id=user1.id, scopes=[]
).access_token
user2.allowed_token = Token.create_personal(
name="allowed_token", user_id=user2.id, scopes=[]
).access_token
user1 = UserFixture(email="[email protected]", password="tester")
user1.create(app, db)

user2 = UserFixture(email="[email protected]", password="tester2")
user2.create(app, db)

return {"authorized": user1, "unauthorized": user2}


Expand Down Expand Up @@ -516,7 +510,10 @@ def permission_factory(query_name, params, *args, **kwargs):
permission_factory.params = params
from flask_login import current_user

if current_user.is_authenticated and current_user.id == users["authorized"].id:
if (
current_user.is_authenticated
and current_user.get_id() == users["authorized"].id
):
return type("Allow", (), {"can": lambda self: True})()
return type("Deny", (), {"can": lambda self: False})()

Expand Down
72 changes: 34 additions & 38 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# under the terms of the MIT License; see LICENSE file for more details.

"""Test view functions."""

import json

import pytest
Expand All @@ -15,32 +16,28 @@
from invenio_stats import current_stats


def test_post_request(app, db, users, queries_config, sample_histogram_query_data):
def test_post_request(
app, db, client, users, queries_config, sample_histogram_query_data
):
"""Test post request to stats API."""
with app.test_client() as client:
headers = [("Content-Type", "application/json"), ("Accept", "application/json")]
sample_histogram_query_data["mystat"]["stat"] = "test-query"
resp = client.post(
url_for(
"invenio_stats.stat_query",
access_token=users["authorized"].allowed_token,
),
headers=headers,
data=json.dumps(sample_histogram_query_data),
)
resp_json = json.loads(resp.data.decode("utf-8"))
assert resp_json["mystat"]["value"] == 100
headers = [("Content-Type", "application/json"), ("Accept", "application/json")]
sample_histogram_query_data["mystat"]["stat"] = "test-query"
users["authorized"].login(client)
resp = client.post(
url_for("invenio_stats.stat_query"),
headers=headers,
data=json.dumps(sample_histogram_query_data),
)
resp_json = json.loads(resp.data.decode("utf-8"))
assert resp_json["mystat"]["value"] == 100

sample_histogram_query_data["mystat"]["stat"] = "unknown-query"
resp = client.post(
url_for(
"invenio_stats.stat_query",
access_token=users["authorized"].allowed_token,
),
headers=headers,
data=json.dumps(sample_histogram_query_data),
)
assert resp.status_code == 400
sample_histogram_query_data["mystat"]["stat"] = "unknown-query"
resp = client.post(
url_for("invenio_stats.stat_query"),
headers=headers,
data=json.dumps(sample_histogram_query_data),
)
assert resp.status_code == 400


@pytest.mark.parametrize(
Expand All @@ -50,6 +47,7 @@ def test_post_request(app, db, users, queries_config, sample_histogram_query_dat
def test_unauthorized_request(
app,
sample_histogram_query_data,
client,
users,
queries_config,
custom_permission_factory,
Expand All @@ -59,20 +57,18 @@ def test_unauthorized_request(
"""Test rejecting unauthorized requests."""

def client_req(user):
with app.test_client() as client:
headers = [
("Content-Type", "application/json"),
("Accept", "application/json"),
]
resp = client.post(
url_for(
"invenio_stats.stat_query",
access_token=user.allowed_token if user else None,
),
headers=headers,
data=json.dumps(sample_histogram_query_data),
)
return resp.status_code
if user:
user.login(client)
headers = [
("Content-Type", "application/json"),
("Accept", "application/json"),
]
resp = client.post(
url_for("invenio_stats.stat_query"),
headers=headers,
data=json.dumps(sample_histogram_query_data),
)
return resp.status_code

sample_histogram_query_data["mystat"]["stat"] = "test-query"
user = users[which_user] if which_user else None
Expand Down