diff --git a/.gitignore b/.gitignore index 002f5f8..9f0f847 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ data/* tests/certs/ tests/venv/ +tests/__pycache__/ diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..bcd35ba --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,3 @@ +aioxmpp==0.11.0 +pip-chill==1.0.0 +pytest-asyncio==0.14.0 diff --git a/tests/test.py b/tests/test.py deleted file mode 100644 index b80e383..0000000 --- a/tests/test.py +++ /dev/null @@ -1,50 +0,0 @@ -import asyncio -import aioxmpp -import aioxmpp.dispatcher - -jid = aioxmpp.JID.fromstr("admin@localhost") -recipient_jid = aioxmpp.JID.fromstr("admin@localhost") -password = "12345678" - -client = aioxmpp.PresenceManagedClient( - jid, - aioxmpp.make_security_layer( - password, - no_verify=True - ), -) - -def message_received(msg): - print(msg) - print(msg.body) - -# obtain an instance of the service -message_dispatcher = client.summon( - aioxmpp.dispatcher.SimpleMessageDispatcher -) - -# register a message callback here -message_dispatcher.register_callback( - aioxmpp.MessageType.CHAT, - None, - message_received, -) - -async def sendMessage(): - async with client.connected() as stream: - msg = aioxmpp.Message( - to=recipient_jid, - type_=aioxmpp.MessageType.CHAT, - ) - # None is for "default language" - msg.body[None] = "Hello World!" - - await client.send(msg) - -def main(): - loop = asyncio.get_event_loop() - loop.run_until_complete(sendMessage()) - loop.close() - -if __name__ == '__main__': - main() diff --git a/tests/test.zsh b/tests/test.zsh index 870e43a..a8cd316 100755 --- a/tests/test.zsh +++ b/tests/test.zsh @@ -4,11 +4,13 @@ generateCert() { DOMAIN="$1" - mkdir -p certs/"$DOMAIN" - cd certs/"$DOMAIN" - openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -days 365 -subj "/CN=$DOMAIN" -nodes - chmod 777 *.pem - cd ../../ + if [[ ! -d certs/"$DOMAIN" ]] ; then + mkdir -p certs/"$DOMAIN" + cd certs/"$DOMAIN" + openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -days 365 -subj "/CN=$DOMAIN" -nodes + chmod 777 *.pem + cd ../../ + fi } generateCert "localhost" @@ -21,6 +23,10 @@ sudo docker-compose up -d sudo docker exec tests_prosody_1 /bin/bash -c "/entrypoint.sh register admin localhost 12345678" -python test.py +python -m venv venv +source venv/bin/activate +pip install -r requirements.txt +pytest +deactivate sudo docker-compose down diff --git a/tests/test_prosody.py b/tests/test_prosody.py new file mode 100644 index 0000000..b53c716 --- /dev/null +++ b/tests/test_prosody.py @@ -0,0 +1,60 @@ +import aioxmpp +import aioxmpp.dispatcher +import asyncio +import pytest + +@pytest.fixture +def client(): + + jid = aioxmpp.JID.fromstr("admin@localhost") + password = "12345678" + + client = aioxmpp.PresenceManagedClient( + jid, + aioxmpp.make_security_layer( + password, + no_verify=True + ), + ) + return client + +@pytest.fixture +def client_with_messageDispatcher(client): + def message_received(msg): + print(msg) + print(msg.body) + + # obtain an instance of the service + message_dispatcher = client.summon( + aioxmpp.dispatcher.SimpleMessageDispatcher + ) + + # register a message callback here + message_dispatcher.register_callback( + aioxmpp.MessageType.CHAT, + None, + message_received, + ) + return client + +@pytest.mark.asyncio +async def test_sendMessage(client_with_messageDispatcher): + client = client_with_messageDispatcher + recipient_jid = aioxmpp.JID.fromstr("admin@localhost") + async with client.connected() as stream: + msg = aioxmpp.Message( + to=recipient_jid, + type_=aioxmpp.MessageType.CHAT, + ) + # None is for "default language" + msg.body[None] = "Hello World!" + + await client.send(msg) + +# def main(): +# loop = asyncio.get_event_loop() +# loop.run_until_complete(sendMessage()) +# loop.close() + +# if __name__ == '__main__': +# main()