Esempio n. 1
0
def test_connect_to_new_host(server_root_response):
    """
    GIVEN
        A client which has previously connected to a valid host
    WHEN
        The client changes to a new valid host
    THEN
        The client validates the new host (i.e. no exceptions are thrown)
        AND the client stores the new host URL (overwriting the old host URL)
        AND the client determines if the new host has authentication enabled
    """

    host = 'https://sal.testing'
    new_host = 'https://sal.new'

    with patch('sal.client.main.requests.get',
               return_value=server_root_response):
        sc = SALClient(host)

    new_host_requires_auth = not sc.auth_required
    new_response = server_root_response.json()
    new_response['api']['requires_auth'] = new_host_requires_auth

    with patch('sal.client.main.requests.get', return_value=new_response):
        assert sc.auth_required == True
        sc.host = new_host
        assert sc.host == new_host
        assert sc.auth_required == False
Esempio n. 2
0
def test_connect_to_host_invalid_url():
    """
    GIVEN
        An invalid URL
    WHEN
        A client attempts to connect
    THEN
        The client raises a ValueError
    """

    with pytest.raises(ValueError):
        SALClient('989s0d8fisdfk')
def test_validate_xml_response(host, xml_response):
    """
    GIVEN
        A host which returns XML responses
    WHEN
        The client connects to the host
    THEN
        An InvalidResponse is raised
    """

    with patch('sal.client.main.requests.get', return_value=xml_response):
        with pytest.raises(exception.InvalidResponse):
            SALClient(host)
Esempio n. 4
0
def test_connect_to_host_no_response():
    """
    GIVEN
        A URL to which the client cannot connect
    WHEN
        A client attempts to connect to the host
    THEN
        The client raises a ConnectionError
    """

    with patch('sal.client.main.requests.get',
               side_effect=requests.ConnectionError()):
        with pytest.raises(ConnectionError):
            SALClient('https://sal.testing')
Esempio n. 5
0
def patched_client(host):

    """
    Patches SALClient host setting (which normally connects to a host server)
    so that SALClient can be initialized.
    """

    def host_setter(self, url):
        self._host = url

    patched_host = SALClient.host.setter(host_setter)

    with patch.object(SALClient, 'host', patched_host):
        return SALClient(host)
Esempio n. 6
0
def test_connect_to_host_with_new_api(patched_client, server_root_response):
    """
    GIVEN
        A URL for a SAL server with a newer API than the client
    WHEN
        The client connects to the host
    THEN
        The client raises a ConnectionError
    """

    server_root_response.json.return_value['api']['version'] = (1 +
                                                                _API_VERSION)

    with patch('sal.client.main.requests.get',
               return_value=server_root_response):
        with pytest.raises(ConnectionError):
            SALClient('https://sal.testing')
Esempio n. 7
0
def test_connect_to_host_not_sal_server(server_root_response):
    """
    GIVEN
        A URL to which the client can connect, but the host is not a SAL server
    WHEN
        A client connects to the host
    THEN
        The client raises a ConnectionError
    """

    server_root_response.json.return_value['service'][
        'name'] = 'Generic Server'

    with patch('sal.client.main.requests.get',
               return_value=server_root_response):
        with pytest.raises(ConnectionError):
            SALClient('https://sal.testing')
Esempio n. 8
0
def test_connect_to_valid_host(host, server_root_response):
    """
    GIVEN
        A URL to a valid SAL server
    WHEN
        A client connects to the host
    THEN
        The client validates the host (i.e. no exceptions are thrown)
        AND the client stores the host URL
        AND the client determines if the host has authentication enabled
    """

    requires_auth = server_root_response.json()['api']['requires_auth']

    with patch('sal.client.main.requests.get',
               return_value=server_root_response):
        sc = SALClient(host)
        assert sc.host == host
        assert sc.auth_required == requires_auth
def test_validate_error_response(host, server_response, response_exception,
                                 status_code):
    """
    GIVEN
        A response with a client or server error status
    WHEN
        The response is validated
    THEN
        The relevant exception is raised
    """

    server_response.status_code = status_code
    exception_str = response_exception.__name__
    server_response.json.return_value = {
        'exception': exception_str,
        'message': response_exception.message
    }

    with patch('sal.client.main.requests.get', return_value=server_response):
        with pytest.raises(response_exception):
            SALClient(host)