<commit_before>#!/usr/bin/env python
# encoding: utf-8

"""
   Copyright 2014 CryptoIM Development Team

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

import cryptoim.xmpp as xmpp
from cryptoim.cli import CryptoShell

from nose.tools import ok_, eq_, nottest
import time

def test_xmpp():
    """
        Test for various xmpp methods: connect, send_message, receive message
    """
    xmpp_cli = init_xmpp_cli()
    check_connect(xmpp_cli)

    xmpp_cli = init_xmpp_cli()
    check_send_message(xmpp_cli)

    xmpp_cli = init_xmpp_cli()
    check_receive_message(xmpp_cli)

def init_xmpp_cli():
    """
        Initializes the xmpp_client and connects it
    """
    crypto_shell = CryptoShell('main.cfg')

    xmpp_cli = xmpp.XMPPClient('<EMAIL>', 'crypto_test', crypto_shell)
    xmpp_cli.connect_server(should_block=False)

    waitForConnection(xmpp_cli, True)
    return xmpp_cli

def check_connect(xmpp_client):
    """
        Check for xmpp.XMPPClient.connect_server and disconnect_server
    """

    eq_(xmpp_client.is_connected(), True)

    xmpp_client.disconnect_server()
    waitForConnection(xmpp_client, False)

    # Uncomment the following to enable a second check -- note, will require a ~10s timeout
    """
    xmpp_client.connect_server(should_block=False)
    waitForConnection(xmpp_client, True)

    xmpp_client.disconnect_server()
    waitForConnection(xmpp_client, False)
    """

def check_send_message(xmpp_client):
    """
        Check for xmpp.XMPPClient.send_message
    """

    waitForConnection(xmpp_client, True)
    waitForSession(xmpp_client, True)
    msg = 'Hello, CryptoIM check_send_message!'
    recipient = '<EMAIL>'
    xmpp_client.send_message(recipient, msg)

    xmpp_client.disconnect_server()
    waitForConnection(xmpp_client, False)

    # Assert that xmpp_client sent the message (it is bound to be sent after disconnect if it waits)
    crypto_shell = xmpp_client.xmpp.parent
    ok_(0 != len(crypto_shell.sent_msg_list))
    eq_(len(crypto_shell.sent_jid_list), len(crypto_shell.sent_msg_list))
    eq_(msg, crypto_shell.sent_msg_list[-1])
    eq_(recipient, crypto_shell.sent_jid_list[-1])

def test_not_connect():
    """
        Check for xmpp.XMPPClient.connect_server and disconnect_server
    """

    crypto_shell = CryptoShell('main.cfg')
<commit_msg>Probably fixed message bug<commit_after>#!/usr/bin/env python
# encoding: utf-8

"""
   Copyright 2014 CryptoIM Development Team

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

import cryptoim.xmpp as xmpp
from cryptoim.cli import CryptoShell

from nose.tools import ok_, eq_, nottest
import time

def test_xmpp():
    """
        Test for various xmpp methods: connect, send_message, receive message
    """
    xmpp_cli = init_xmpp_cli()
    check_connect(xmpp_cli)

    xmpp_cli = init_xmpp_cli()
    check_send_message(xmpp_cli)

    xmpp_cli = init_xmpp_cli()
    check_receive_message(xmpp_cli)

def init_xmpp_cli():
    """
        Initializes the xmpp_client and connects it
    """
    crypto_shell = CryptoShell('main.cfg')

    xmpp_cli = xmpp.XMPPClient('<EMAIL>', 'crypto_test', crypto_shell)
    xmpp_cli.connect_server(should_block=False)

    waitForConnection(xmpp_cli, True)
    return xmpp_cli

def check_connect(xmpp_client):
    """
        Check for xmpp.XMPPClient.connect_server and disconnect_server
    """

    eq_(xmpp_client.is_connected(), True)

    xmpp_client.disconnect_server()
    waitForConnection(xmpp_client, False)

    # Uncomment the following to enable a second check -- note, will require a ~10s timeout
    """
    xmpp_client.connect_server(should_block=False)
    waitForConnection(xmpp_client, True)

    xmpp_client.disconnect_server()
    waitForConnection(xmpp_client, False)
    """

def check_send_message(xmpp_client):
    """
        Check for xmpp.XMPPClient.send_message
    """
    
    crypto_shell = xmpp_client.xmpp.parent
    waitForConnection(xmpp_client, True)
    waitForSession(xmpp_client, True)
    msg = 'Hello, CryptoIM check_send_message!'
    recipient = '<EMAIL>'
    xmpp_client.send_message(recipient, msg)

    while len(crypto_shell.sent_msg_list) < 1:
        time.sleep(0.1)

    xmpp_client.disconnect_server()
    waitForConnection(xmpp_client, False)

    # Assert that xmpp_client sent the message (it is bound to be sent after disconnect if it waits)   
    ok_(0 != len(crypto_shell.sent_msg_list))
    eq_(len(crypto_shell.sent_jid_list), len(crypto_shell.sent_msg_list))
    eq_(msg, crypto_shell.sent_msg_list[-1])
    eq_(recipient, crypto_shell.sent_jid_list[-1])

def test_not_connect():
    """
        Check for xmpp.XMPPClient.connect_server and disconnect_server
    """

    crypto_shell = CryptoShell('main.cfg')

{'fraction_non_alphanumeric': 0.08649495301079012, 'fraction_numerical': 0.005395057431256526, 'mean_word_length': 3.8132328308207706, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3578076', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1872, 'n_words': 467}