from unittest.mock import MagicMock, patch from fastapi import HTTPException from starlette import status from client import SheerkaClient, parse_arguments class TestSheerkaClient: def test_i_can_start_with_a_default_hostname(self): parsed = parse_arguments([]) assert parsed.hostname == "http://localhost" assert parsed.port == 56356 def test_i_can_override_hostname_and_port(self): parsed = parse_arguments(["new_host", "--port", "1515"]) assert parsed.hostname == "new_host" assert parsed.port == 1515 parsed = parse_arguments(["new_host", "-p", "1515"]) assert parsed.hostname == "new_host" assert parsed.port == 1515 def test_i_can_provide_user_and_password(self): parsed = parse_arguments(["--username", "my_user", "--password", "my_password"]) assert parsed.username == "my_user" assert parsed.password == "my_password" parsed = parse_arguments(["-u", "my_user", "-P", "my_password"]) assert parsed.username == "my_user" assert parsed.password == "my_password" def test_i_can_manage_when_no_server(self): client = SheerkaClient("http://localhost", 80) res = client.check_url() assert res.status is False assert res.message == "Connection refused." def test_i_can_manage_when_resource_is_not_found(self): mock_response = MagicMock() mock_response.__bool__ = MagicMock(return_value=False) mock_response.text = '{"detail":"Not Found"}' with patch("requests.get", return_value=mock_response): client = SheerkaClient("http://localhost", 5000) res = client.check_url() assert not res.status assert res.message == '{"detail":"Not Found"}' def test_i_can_connect_to_a_server(self): mock_response = MagicMock() mock_response.__bool__ = MagicMock(return_value=True) mock_response.text = '"Hello world"' with patch("requests.get", return_value=mock_response): client = SheerkaClient("http://localhost", 5000) res = client.check_url() assert res.status assert res.message == '"Hello world"' def test_i_can_manage_when_authentication_fails(self): mock_response = MagicMock() mock_response.__bool__ = MagicMock(return_value=False) mock_response.json.return_value = {"detail": "Incorrect username or password"} with patch("requests.post", return_value=mock_response): client = SheerkaClient("http://localhost", 5000) res = client.connect("username", "wrong_password") assert not res.status assert res.message == "Incorrect username or password"