Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jahwag committed Aug 22, 2024
1 parent 2cbc572 commit 8d8f161
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 376 deletions.
152 changes: 59 additions & 93 deletions tests/cli/test_api.py
Original file line number Diff line number Diff line change
@@ -1,120 +1,86 @@
import unittest
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock, patch
from datetime import datetime, timedelta
import click
from click.testing import CliRunner
from claudesync.cli.main import cli
from claudesync.exceptions import ProviderError
from claudesync.config_manager import ConfigManager


class TestAPICLI(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
self.mock_config = MagicMock()
self.mock_provider = MagicMock()
self.config_mock = MagicMock(spec=ConfigManager)

@patch("claudesync.providers.base_claude_ai._get_session_key_expiry")
@patch("claudesync.cli.api.get_provider")
@patch("claudesync.cli.main.ConfigManager")
def test_login_provider_error(self, mock_get_provider):
mock_get_provider.return_value = ["claude.ai", "claude.ai-curl"]
result = self.runner.invoke(
cli, ["api", "login", "invalid_provider"], obj=self.config_mock
)
self.assertIn("Error: Unknown provider 'invalid_provider'", result.output)
self.assertEqual(result.exit_code, 0)

@patch("claudesync.cli.api.get_provider")
@patch("claudesync.cli.api.click.confirm")
@patch("claudesync.cli.api.org_select")
@patch("claudesync.cli.api.proj_select")
@patch("claudesync.cli.api.project_create")
@patch("claudesync.cli.api.submodule_create")
def test_login_success(
self,
mock_submodule_create,
mock_project_create,
mock_proj_select,
mock_org_select,
mock_config_manager,
mock_confirm,
mock_get_provider,
mock_get_session_key_expiry,
):
mock_config_manager.return_value = self.mock_config
mock_get_provider.return_value = self.mock_provider
mock_get_provider.side_effect = lambda x=None: (
["claude.ai"] if x is None else self.mock_provider
)
expiry = "Tue, 03 Sep 2099 06:51:21 UTC"
self.mock_provider.login.return_value = ("test_session_key", expiry)

mock_get_session_key_expiry.return_value = expiry

result = self.runner.invoke(cli, ["api", "login", "claude.ai"])

self.assertEqual(result.exit_code, 0)
self.assertIn("Logged in successfully.", result.output)
self.mock_config.set_session_key.assert_called_once_with(
"test_session_key", expiry
)
self.mock_config.set.assert_called_with("active_provider", "claude.ai")
mock_org_select.assert_called_once()
mock_proj_select.assert_called_once()

@patch("claudesync.cli.api.get_provider")
@patch("claudesync.cli.main.ConfigManager")
def test_login_provider_error(self, mock_config_manager, mock_get_provider):
mock_config_manager.return_value = self.mock_config
mock_get_provider.return_value = self.mock_provider
mock_get_provider.side_effect = lambda x=None: (
["claude.ai"] if x is None else self.mock_provider
)
self.mock_provider.login.side_effect = ProviderError("Login failed")

result = self.runner.invoke(cli, ["api", "login", "claude.ai"])

self.assertEqual(result.exit_code, 0)
self.assertIn("Error: Login failed", result.output)

@patch("claudesync.cli.main.ConfigManager")
def test_logout(self, mock_config_manager):
mock_config_manager.return_value = self.mock_config

result = self.runner.invoke(cli, ["api", "logout"])

self.assertEqual(result.exit_code, 0)
self.assertIn("Logged out successfully.", result.output)
self.mock_config.set.assert_any_call("session_key", None)
self.mock_config.set.assert_any_call("active_provider", None)
self.mock_config.set.assert_any_call("active_organization_id", None)

@patch("claudesync.cli.main.ConfigManager")
def test_ratelimit_set(self, mock_config_manager):
mock_config_manager.return_value = self.mock_config

result = self.runner.invoke(cli, ["api", "ratelimit", "--delay", "1.5"])

self.assertEqual(result.exit_code, 0)
self.assertIn("Upload delay set to 1.5 seconds.", result.output)
self.mock_config.set.assert_called_once_with("upload_delay", 1.5)

@patch("claudesync.cli.main.ConfigManager")
def test_ratelimit_negative_value(self, mock_config_manager):
mock_config_manager.return_value = self.mock_config

result = self.runner.invoke(cli, ["api", "ratelimit", "--delay", "-1"])

self.assertEqual(result.exit_code, 0)
self.assertIn(
"Error: Upload delay must be a non-negative number.", result.output
# Mock provider instance
mock_provider = MagicMock()
mock_provider.login.return_value = (
"mock_session_key",
datetime.now() + timedelta(days=30),
)
self.mock_config.set.assert_not_called()

@patch("claudesync.cli.main.ConfigManager")
def test_max_filesize_set(self, mock_config_manager):
mock_config_manager.return_value = self.mock_config

result = self.runner.invoke(cli, ["api", "max-filesize", "--size", "1048576"])
mock_provider.get_organizations.return_value = [
{"id": "org1", "name": "Test Org"}
]
mock_provider.get_projects.return_value = [
{"id": "proj1", "name": "Test Project"}
]

# Mock get_provider to return the list of providers and then the mock provider instance
mock_get_provider.side_effect = [["claude.ai", "claude.ai-curl"], mock_provider]

# Mock user confirmations
mock_confirm.side_effect = [
False,
True,
True,
] # Don't use existing session, select existing project, delete remote files

# Mock config operations
self.config_mock.get_session_key.return_value = None
self.config_mock.get.return_value = None

# Mock organization and project selection
mock_org_select.return_value = None
mock_proj_select.return_value = None

runner = CliRunner()
result = runner.invoke(cli, ["api", "login", "claude.ai"], obj=self.config_mock)

self.assertEqual(result.exit_code, 0)
self.assertIn("Maximum file size set to 1048576 bytes.", result.output)
self.mock_config.set.assert_called_once_with("max_file_size", 1048576)
self.assertIn("Logged in successfully", result.output)

@patch("claudesync.cli.main.ConfigManager")
def test_max_filesize_negative_value(self, mock_config_manager):
mock_config_manager.return_value = self.mock_config
# Verify that organization select was invoked
mock_org_select.assert_called_once()

result = self.runner.invoke(cli, ["api", "max-filesize", "--size", "-1"])
# Verify that project select was invoked
mock_proj_select.assert_called_once()

self.assertEqual(result.exit_code, 0)
self.assertIn(
"Error: Maximum file size must be a non-negative number.", result.output
)
self.mock_config.set.assert_not_called()
# Verify that submodule create was not called (we selected an existing project)
mock_submodule_create.assert_not_called()


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 8d8f161

Please sign in to comment.