Beispiel #1
0
async def test_send(stub_connection_manager, request_ping, response_pong):
    client = Client()
    client.connection = stub_connection_manager(return_value=response_pong)

    response = await client.send(request_ping)

    assert isinstance(response, Response)
Beispiel #2
0
async def test_raises_timeout_exception(stub_connection_manager, ex_timeout,
                                        spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_timeout)

    with pytest.raises(TimeoutException):
        await client.send(spam)
Beispiel #3
0
async def test_response_general_exception(stub_connection_manager,
                                          ex_undefined, request_ping):
    c = Client()
    c.connection = stub_connection_manager(return_value=ex_undefined)

    with pytest.raises(ResponseException):
        await c.send(request_ping)
Beispiel #4
0
async def test_raises_no_perm_exception(stub_connection_manager, ex_no_perm,
                                        spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_no_perm)

    with pytest.raises(NoPermissionException):
        await client.send(spam)
Beispiel #5
0
async def test_raises_config_exception(stub_connection_manager, ex_config,
                                       spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_config)

    with pytest.raises(ConfigException):
        await client.send(spam)
Beispiel #6
0
async def test_raises_temp_fail_exception(stub_connection_manager,
                                          ex_temp_fail, spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_temp_fail)

    with pytest.raises(TemporaryFailureException):
        await client.send(spam)
Beispiel #7
0
async def test_raises_protocol_exception(stub_connection_manager, ex_protocol,
                                         spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_protocol)

    with pytest.raises(ProtocolException):
        await client.send(spam)
Beispiel #8
0
async def test_raises_cant_create_exception(stub_connection_manager,
                                            ex_cant_create, spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_cant_create)

    with pytest.raises(CantCreateException):
        await client.send(spam)
Beispiel #9
0
async def test_raises_io_error_exception(stub_connection_manager, ex_io_err,
                                         spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_io_err)

    with pytest.raises(IOErrorException):
        await client.send(spam)
Beispiel #10
0
async def test_raises_os_file_exception(stub_connection_manager, ex_os_file,
                                        spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_os_file)

    with pytest.raises(OSFileException):
        await client.send(spam)
Beispiel #11
0
async def test_raises_software_exception(stub_connection_manager, ex_software,
                                         spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_software)

    with pytest.raises(InternalSoftwareException):
        await client.send(spam)
Beispiel #12
0
async def test_raises_no_user_exception(stub_connection_manager, ex_no_user,
                                        spam):
    client = Client()
    client.connection = stub_connection_manager(return_value=ex_no_user)

    with pytest.raises(NoUserException):
        await client.send(spam)
Beispiel #13
0
async def test_request_ping(mocker):
    c = Client()
    c.send = CoroutineMock()
    mocker.spy(c, 'send')
    await c.ping()
    request = c.send.call_args[0][0]

    assert request.verb == 'PING'
Beispiel #14
0
async def test_requests_with_body(verb, method, spam, mocker):
    c = Client()
    c.send = CoroutineMock()
    mocker.spy(c, 'send')
    await getattr(c, method)(spam)
    request = c.send.call_args[0][0]

    assert request.verb == verb
    assert request.body == spam
Beispiel #15
0
async def test_send_with_user(stub_connection_manager, response_with_body,
                              spam, mocker):
    client = Client(host='localhost', user='******')
    client.connection = stub_connection_manager(
        return_value=response_with_body)
    send_spy = mocker.spy(client.connection.connection_stub, 'send')
    await client.check(spam)
    headers = send_spy.call_args[0][0].split(b'\r\n')[1:-1]
    has_user_header = [header.startswith(b'User') for header in headers]

    assert any(has_user_header)
Beispiel #16
0
def test_client_repr():
    client = Client()
    assert repr(client) == ('Client(socket_path=None, '
                            'host=\'localhost\', '
                            'port=783, '
                            'user=None, '
                            'compress=False)')
Beispiel #17
0
async def test_symbols_invalid_response(mock_connection, response_invalid,
                                        spam):
    mock_connection.side_effect = [response_invalid]

    client = Client(host='localhost')
    with pytest.raises(BadResponse):
        response = await client.symbols(spam)
Beispiel #18
0
async def test_gtk_encoding(stub_connection_manager, response_ok):
    message = EmailMessage()
    message.add_header('From', 'wevsty <*****@*****.**>')
    message.add_header('Subject', '=?UTF-8?B?5Lit5paH5rWL6K+V?=')
    message.add_header('Message-ID', '<*****@*****.**>')
    message.add_header('Date', '')
    message.add_header('X-Mozilla-Draft-Info', '')
    message.add_header('User-Agent', '')
    message.set_param('charset', 'gbk')
    message.set_content('这是Unicode文字.' 'This is Unicode characters.')

    c = Client()
    c.connection = stub_connection_manager(return_value=response_ok)
    result = await c.check(message)

    assert result
Beispiel #19
0
async def test_tell_invalid_response(mock_connection, response_invalid, spam):
    mock_connection.side_effect = [response_invalid]

    client = Client(host='localhost')
    with pytest.raises(BadResponse):
        response = await client.tell(MessageClassOption.spam, spam,
                                     ActionOption(local=True, remote=True))
Beispiel #20
0
def test_ssl_context_from_dir(mocker, tmp_path):
    mocker.spy(ssl, 'create_default_context')
    temp = Path(str(tmp_path))
    s = Client.new_ssl_context(temp)

    args, kwargs = ssl.create_default_context.call_args
    assert kwargs['capath'] == str(temp)
Beispiel #21
0
async def test_ping_valid_request(mock_connection):
    client = Client(host='localhost')
    response = await client.ping()

    request = client.send.call_args[0][0]

    assert isinstance(request, Request)
    assert request.verb == 'PING'
Beispiel #22
0
def test_ssl_context_from_false(mocker):
    mocker.spy(ssl, 'create_default_context')
    s = Client.new_ssl_context(False)

    args, kwargs = ssl.create_default_context.call_args
    assert kwargs['cafile'] == certifi.where()
    assert s.check_hostname is False
    assert s.verify_mode == ssl.CERT_NONE
Beispiel #23
0
async def test_request_tell(message_class, remove_action, set_action, spam,
                            mocker):
    c = Client()
    c.send = CoroutineMock()
    mocker.spy(c, 'send')
    await c.tell(message=spam,
                 message_class=message_class,
                 remove_action=remove_action,
                 set_action=set_action)
    request = c.send.call_args[0][0]

    assert request.headers['Message-class'].value == message_class
    if remove_action:
        assert request.headers['Remove'].action == remove_action
    if set_action:
        assert request.headers['Set'].action == set_action
    assert request.body == spam
async def tell_spamd_message_is_spam(message, loop=None):
    client = Client(host='localhost', loop=loop)

    response = await client.tell(message,
                                 message_class='spam',
                                 set_action='local, remote')
    response.raise_for_status()

    return True
Beispiel #25
0
async def test_symbols_valid_request(mock_connection, spam):
    client = Client(host='localhost')
    response = await client.symbols(spam)

    request = client.send.call_args[0][0]

    assert isinstance(request, Request)
    assert request.verb == 'SYMBOLS'
    assert request.body
Beispiel #26
0
async def test_send(mock_connection, request_ping, response_pong):
    mock_connection.side_effect = [
        response_pong,
    ]
    client = Client(host='localhost')

    response = await client.send(request_ping)

    assert isinstance(response, Response)
Beispiel #27
0
def test_client_repr():
    client = Client(host='localhost')
    assert repr(client) == (
        'Client(socket_path=\'/var/run/spamassassin/spamd.sock\', '
        'host=\'localhost\', '
        'port=783, '
        'user=None, '
        'compress=False, '
        'ssl=False)')
Beispiel #28
0
async def test_headers_req_valid_request(mock_connection, spam):
    client = Client(host='localhost')
    response = await client.headers(spam)

    request = client.send.call_args[0][0]

    assert isinstance(request, Request)
    assert request.verb == 'HEADERS'
    assert request.body
Beispiel #29
0
async def test_report_if_spam_valid_request(mock_connection, spam):
    client = Client(host='localhost')
    response = await client.report_if_spam(spam)

    request = client.send.call_args[0][0]

    assert isinstance(request, Request)
    assert request.verb == 'REPORT_IFSPAM'
    assert request.body
Beispiel #30
0
async def test_check_valid_request(mock_connection, spam):
    client = Client(host='localhost')
    response = await client.check(spam)

    request = client.send.call_args[0][0]

    assert isinstance(request, Request)
    assert request.verb == 'CHECK'
    assert request.body