Add Redmine API token authentication
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import pytest
|
||||
from unittest import mock
|
||||
from redmine_reporter.client import fetch_issues_with_spent_time
|
||||
from redmine_reporter.config import Config
|
||||
|
||||
|
||||
@mock.patch.multiple(
|
||||
Config,
|
||||
REDMINE_URL="https://red.eltex.loc",
|
||||
REDMINE_API_KEY=None,
|
||||
REDMINE_USER="user",
|
||||
REDMINE_PASSWORD="password",
|
||||
)
|
||||
@mock.patch("redmine_reporter.client.Redmine")
|
||||
def test_fetch_aggregates_hours_per_issue(mock_redmine_class):
|
||||
"""Два time entry на одну задачу -- часы суммируются."""
|
||||
@@ -34,6 +41,13 @@ def test_fetch_aggregates_hours_per_issue(mock_redmine_class):
|
||||
assert total_hours == 3.5
|
||||
|
||||
|
||||
@mock.patch.multiple(
|
||||
Config,
|
||||
REDMINE_URL="https://red.eltex.loc",
|
||||
REDMINE_API_KEY=None,
|
||||
REDMINE_USER="user",
|
||||
REDMINE_PASSWORD="password",
|
||||
)
|
||||
@mock.patch("redmine_reporter.client.Redmine")
|
||||
def test_fetch_returns_none_when_no_entries(mock_redmine_class):
|
||||
"""Нет time entries -- возвращается None."""
|
||||
@@ -47,6 +61,13 @@ def test_fetch_returns_none_when_no_entries(mock_redmine_class):
|
||||
assert result is None
|
||||
|
||||
|
||||
@mock.patch.multiple(
|
||||
Config,
|
||||
REDMINE_URL="https://red.eltex.loc",
|
||||
REDMINE_API_KEY=None,
|
||||
REDMINE_USER="user",
|
||||
REDMINE_PASSWORD="password",
|
||||
)
|
||||
@mock.patch("redmine_reporter.client.Redmine")
|
||||
def test_fetch_skips_entries_without_issue(mock_redmine_class):
|
||||
"""Time entry без привязки к задаче игнорируется."""
|
||||
@@ -65,6 +86,13 @@ def test_fetch_skips_entries_without_issue(mock_redmine_class):
|
||||
assert result is None
|
||||
|
||||
|
||||
@mock.patch.multiple(
|
||||
Config,
|
||||
REDMINE_URL="https://red.eltex.loc",
|
||||
REDMINE_API_KEY=None,
|
||||
REDMINE_USER="user",
|
||||
REDMINE_PASSWORD="password",
|
||||
)
|
||||
@mock.patch("redmine_reporter.client.Redmine")
|
||||
def test_fetch_multiple_issues(mock_redmine_class):
|
||||
"""Несколько задач -- каждая с правильным суммарным временем."""
|
||||
@@ -100,3 +128,51 @@ def test_fetch_multiple_issues(mock_redmine_class):
|
||||
hours_by_id = {issue.id: hours for issue, hours in result}
|
||||
assert hours_by_id[1] == 1.5
|
||||
assert hours_by_id[2] == 2.0
|
||||
|
||||
|
||||
@mock.patch.multiple(
|
||||
Config,
|
||||
REDMINE_URL="https://red.eltex.loc",
|
||||
REDMINE_API_KEY="api-token",
|
||||
REDMINE_USER="user",
|
||||
REDMINE_PASSWORD="password",
|
||||
)
|
||||
@mock.patch("redmine_reporter.client.Redmine")
|
||||
def test_fetch_uses_api_key_when_present(mock_redmine_class):
|
||||
"""Если задан API key, он используется вместо логина/пароля."""
|
||||
mock_redmine = mock_redmine_class.return_value
|
||||
mock_user = mock.MagicMock()
|
||||
mock_user.id = 1
|
||||
mock_redmine.user.get.return_value = mock_user
|
||||
mock_redmine.time_entry.filter.return_value = []
|
||||
|
||||
fetch_issues_with_spent_time("2026-01-01", "2026-01-31")
|
||||
|
||||
_, kwargs = mock_redmine_class.call_args
|
||||
assert kwargs["key"] == "api-token"
|
||||
assert "username" not in kwargs
|
||||
assert "password" not in kwargs
|
||||
|
||||
|
||||
@mock.patch.multiple(
|
||||
Config,
|
||||
REDMINE_URL="https://red.eltex.loc",
|
||||
REDMINE_API_KEY=None,
|
||||
REDMINE_USER="user",
|
||||
REDMINE_PASSWORD="password",
|
||||
)
|
||||
@mock.patch("redmine_reporter.client.Redmine")
|
||||
def test_fetch_uses_username_password_when_no_api_key(mock_redmine_class):
|
||||
"""Если API key не задан, остаётся старая схема логин/пароль."""
|
||||
mock_redmine = mock_redmine_class.return_value
|
||||
mock_user = mock.MagicMock()
|
||||
mock_user.id = 1
|
||||
mock_redmine.user.get.return_value = mock_user
|
||||
mock_redmine.time_entry.filter.return_value = []
|
||||
|
||||
fetch_issues_with_spent_time("2026-01-01", "2026-01-31")
|
||||
|
||||
_, kwargs = mock_redmine_class.call_args
|
||||
assert kwargs["username"] == "user"
|
||||
assert kwargs["password"] == "password"
|
||||
assert "key" not in kwargs
|
||||
|
||||
Reference in New Issue
Block a user