82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
"""Tests for API commands module."""
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
from devtoolbelt.commands.api import (
|
|
_parse_auth,
|
|
_parse_headers,
|
|
_resolve_url,
|
|
)
|
|
|
|
|
|
class TestAPiHelpers:
|
|
"""Tests for API helper functions."""
|
|
|
|
def test_parse_headers_empty(self):
|
|
"""Test parsing empty headers."""
|
|
result = _parse_headers(())
|
|
assert result == {}
|
|
|
|
def test_parse_headers_single(self):
|
|
"""Test parsing single header."""
|
|
result = _parse_headers(("Content-Type: application/json",))
|
|
assert result == {"Content-Type": "application/json"}
|
|
|
|
def test_parse_headers_multiple(self):
|
|
"""Test parsing multiple headers."""
|
|
result = _parse_headers((
|
|
"Content-Type: application/json",
|
|
"Authorization: Bearer token123"
|
|
))
|
|
assert result == {
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Bearer token123"
|
|
}
|
|
|
|
def test_parse_headers_with_spaces(self):
|
|
"""Test parsing headers with extra spaces."""
|
|
result = _parse_headers(("X-Custom-Header : value ",))
|
|
assert result == {"X-Custom-Header": "value"}
|
|
|
|
def test_parse_auth_none(self):
|
|
"""Test parsing no auth."""
|
|
result = _parse_auth(None)
|
|
assert result is None
|
|
|
|
def test_parse_auth_token(self):
|
|
"""Test parsing token auth."""
|
|
result = _parse_auth("bearer-token-123")
|
|
assert result == ("bearer-token-123", "")
|
|
|
|
def test_parse_auth_user_password(self):
|
|
"""Test parsing user:password auth."""
|
|
result = _parse_auth("user:password123")
|
|
assert result == ("user", "password123")
|
|
|
|
def test_resolve_url_direct(self):
|
|
"""Test resolving direct URL."""
|
|
result = _resolve_url("https://api.example.com", None)
|
|
assert result == "https://api.example.com"
|
|
|
|
def test_resolve_url_named_from_config(self):
|
|
"""Test resolving named endpoint from config."""
|
|
with patch('devtoolbelt.commands.api.get_config') as mock_get_config:
|
|
mock_config = Mock()
|
|
mock_config.get_api_endpoints.return_value = {
|
|
"myapi": "https://api.example.com/v1"
|
|
}
|
|
mock_get_config.return_value = mock_config
|
|
|
|
result = _resolve_url("myapi", None)
|
|
assert result == "https://api.example.com/v1"
|
|
|
|
def test_resolve_url_unknown_name(self):
|
|
"""Test resolving unknown name falls back to URL."""
|
|
with patch('devtoolbelt.commands.api.get_config') as mock_get_config:
|
|
mock_config = Mock()
|
|
mock_config.get_api_endpoints.return_value = {}
|
|
mock_get_config.return_value = mock_config
|
|
|
|
result = _resolve_url("unknownname", None)
|
|
assert result == "unknownname"
|