import unittest
from unittest.mock import patch

from zindi.user import Zindian

# Mock API responses
MOCK_SIGNIN_SUCCESS = {
    "auth_token": "mock_token",
    "user": {"username": "testuser", "id": 123},
}


# --- Test Class for Authentication ---
class TestUserAuth(unittest.TestCase):
    @patch("zindi.user.ZindiPlatformAPI.signin")
    @patch("zindi.user.getpass")
    def test_init_signin_success(self, mock_getpass, mock_signin):
        """Test successful initialization and sign-in."""
        mock_getpass.return_value = "password"
        mock_signin.return_value = MOCK_SIGNIN_SUCCESS
        user = Zindian(username="testuser")
        mock_getpass.assert_called_once()
        mock_signin.assert_called_once_with(username="testuser", password="password")
        self.assertEqual(user._Zindian__auth_data, MOCK_SIGNIN_SUCCESS)
        self.assertFalse(user._Zindian__challenge_selected)

    @patch("zindi.user.ZindiPlatformAPI.signin")
    @patch("zindi.user.getpass")
    def test_init_signin_failure(self, mock_getpass, mock_signin):
        """Test failed sign-in during initialization."""
        mock_getpass.return_value = "wrongpassword"
        mock_signin.side_effect = Exception("Wrong username or password")
        with self.assertRaises(Exception) as cm:
            Zindian(username="testuser")
        self.assertIn("Wrong username or password", str(cm.exception))
        mock_getpass.assert_called_once()


if __name__ == "__main__":
    unittest.main()
