from fastapi import HTTPException from starlette import status from client import SheerkaClient, parse_arguments from mockserver import MockServer # @pytest.mark.skip("too long") 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): with MockServer([]): 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): with MockServer([{ "path": "/", "response": "Hello world" }]): 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): with MockServer([{ "path": "/", "response": "Hello world" }, { "method": "post", "path": "/token", "exception": HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect username or password", headers={"WWW-Authenticate": "Bearer"}, ) }]): client = SheerkaClient("http://localhost", 5000) res = client.connect("username", "wrong_password") assert not res.status assert res.message == 'Incorrect username or password'