Skip to content

Commit

Permalink
Fix removed filtering based just on deleted_ and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelGeo committed Dec 16, 2024
1 parent 1d4c45a commit 3c2c186
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/mergin/auth/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ def get_server_usage():
"projects": Project.query.filter(Project.removed_at.is_(None)).count(),
"storage": files_size(),
"users": User.query.filter(
is_(User.username.ilike("deleted_%"), False) | User.active,
is_(User.username.ilike("deleted_%"), False),
).count(),
"workspaces": current_app.ws_handler.workspace_count(),
"editors": current_app.ws_handler.server_editors_count(),
Expand Down
2 changes: 1 addition & 1 deletion server/mergin/stats/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def send_statistics():
"licence": current_app.config["SERVER_TYPE"],
"projects_count": Project.query.filter(Project.removed_at.is_(None)).count(),
"users_count": User.query.filter(
is_(User.username.ilike("deleted_%"), False) | is_(User.active, True)
is_(User.username.ilike("deleted_%"), False)
).count(),
"workspaces_count": current_app.ws_handler.workspace_count(),
"last_change": str(last_change_item.updated) + "Z" if last_change_item else "",
Expand Down
9 changes: 7 additions & 2 deletions server/mergin/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,6 @@ def test_server_usage(client):
login_as_admin(client)
workspace = create_workspace()
init_project = Project.query.filter_by(workspace_id=workspace.id).first()
resp = client.get("/app/admin/usage")
print(resp.json)
user = add_user()
admin = User.query.filter_by(username="mergin").first()
# create new project
Expand All @@ -877,3 +875,10 @@ def test_server_usage(client):
db.session.commit()
resp = client.get("/app/admin/usage")
assert resp.json["editors"] == 2
user.inactivate()
user.anonymize()
project.delete()
resp = client.get("/app/admin/usage")
assert resp.json["editors"] == 1
assert resp.json["users"] == 1
assert resp.json["projects"] == 1
31 changes: 27 additions & 4 deletions server/mergin/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import json
from unittest.mock import patch
import requests
from sqlalchemy.sql.operators import is_

from mergin.auth.models import User
from mergin.sync.models import Project, ProjectRole

from ..app import db
from ..stats.tasks import send_statistics
from ..stats.models import MerginInfo
from .utils import Response
from .utils import Response, add_user, create_project, create_workspace


def test_send_statistics(app, caplog):
Expand All @@ -24,6 +28,12 @@ def test_send_statistics(app, caplog):
mock.return_value = Response(True, {})
app.config["COLLECT_STATISTICS"] = False
app.config["CONTACT_EMAIL"] = "[email protected]"
user = add_user()
admin = User.query.filter_by(username="mergin").first()
# create new project
workspace = create_workspace()
project = create_project("project", workspace, admin)
project.set_role(user.id, ProjectRole.EDITOR)
task = send_statistics.s().apply()
# nothing was done
assert task.status == "SUCCESS"
Expand Down Expand Up @@ -55,16 +65,29 @@ def test_send_statistics(app, caplog):
assert data["service_uuid"] == app.config["SERVICE_ID"]
assert data["licence"] == "ce"
assert data["monthly_contributors"] == 1
assert data["users_count"] == 1
assert data["projects_count"] == 1
assert data["users_count"] == 2
assert data["projects_count"] == 2
assert data["contact_email"] == "[email protected]"
assert data["editors"] == 1
assert data["editors"] == 2

# repeated action does not do anything
task = send_statistics.s().apply()
assert task.status == "SUCCESS"
assert info.last_reported == ts

# project removed / users removed in time
info.last_reported = None
project.delete()
user.inactivate()
user.anonymize()
db.session.commit()
task = send_statistics.s().apply()
url, data = mock.call_args
data = json.loads(data["data"])
assert data["projects_count"] == 1
assert data["users_count"] == 1
assert data["editors"] == 1

info.last_reported = None
db.session.commit()
# server responds with non-ok status
Expand Down

0 comments on commit 3c2c186

Please sign in to comment.