|
| 1 | +import datetime as dt |
| 2 | + |
| 3 | + |
| 4 | +def _login(client, username: str, password: str) -> dict: |
| 5 | + res = client.post("/auth/login", json={"username": username, "password": password}) |
| 6 | + assert res.status_code == 200 |
| 7 | + return res.json() |
| 8 | + |
| 9 | + |
| 10 | +def _auth_headers(token: str) -> dict[str, str]: |
| 11 | + return {"Authorization": f"Bearer {token}"} |
| 12 | + |
| 13 | + |
| 14 | +def test_dashboard_stats_counts_and_copies_range(client): |
| 15 | + login = _login(client, "admin", "admin") |
| 16 | + token = login["access_token"] |
| 17 | + |
| 18 | + # Need to be a "ready" user |
| 19 | + res = client.post( |
| 20 | + "/auth/change-password", |
| 21 | + json={"old_password": "admin", "new_password": "admin123"}, |
| 22 | + headers=_auth_headers(token), |
| 23 | + ) |
| 24 | + assert res.status_code == 200 |
| 25 | + |
| 26 | + login2 = _login(client, "admin", "admin123") |
| 27 | + token2 = login2["access_token"] |
| 28 | + |
| 29 | + # Create group |
| 30 | + res = client.post( |
| 31 | + "/groups", |
| 32 | + json={"name": "Projet A"}, |
| 33 | + headers=_auth_headers(token2), |
| 34 | + ) |
| 35 | + assert res.status_code == 201 |
| 36 | + group = res.json() |
| 37 | + |
| 38 | + # Create command with 2 tags |
| 39 | + res = client.post( |
| 40 | + "/commands", |
| 41 | + json={ |
| 42 | + "group_id": group["id"], |
| 43 | + "title": "Hello", |
| 44 | + "command": "echo hi", |
| 45 | + "tags": ["docker", "git"], |
| 46 | + }, |
| 47 | + headers=_auth_headers(token2), |
| 48 | + ) |
| 49 | + assert res.status_code == 201 |
| 50 | + cmd = res.json() |
| 51 | + |
| 52 | + # Simulate 3 copies (PATCH copy_count) |
| 53 | + res = client.patch( |
| 54 | + f"/commands/{cmd['id']}", |
| 55 | + json={"copy_count": 3}, |
| 56 | + headers=_auth_headers(token2), |
| 57 | + ) |
| 58 | + assert res.status_code == 200 |
| 59 | + assert res.json()["copy_count"] == 3 |
| 60 | + |
| 61 | + today = dt.date.today().isoformat() |
| 62 | + |
| 63 | + res = client.get( |
| 64 | + "/stats/dashboard", |
| 65 | + params={"from_date": today, "to_date": today}, |
| 66 | + headers=_auth_headers(token2), |
| 67 | + ) |
| 68 | + assert res.status_code == 200 |
| 69 | + data = res.json() |
| 70 | + |
| 71 | + assert data["commands"] == 1 |
| 72 | + assert data["groups"] == 1 |
| 73 | + assert data["tags"] == 2 |
| 74 | + assert data["copies"] == 3 |
| 75 | + assert data["from_date"] == today |
| 76 | + assert data["to_date"] == today |
0 commit comments