Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadair committed Nov 5, 2018
1 parent 53bd90d commit 8a829ad
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 1 deletion.
2 changes: 1 addition & 1 deletion matrix_client/api_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
if sys.version_info < (3, 5):
raise ValueError("The asyncio version of the api is only supported on Python 3.5+")
else:
from _api_asyncio import AsyncHttpApi
from ._api_asyncio import AsyncHTTPAPI

__all__ = ['AsyncHttpApi']
203 changes: 203 additions & 0 deletions test/test_async_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import json
from asyncio import Future
from functools import partial
from urllib.parse import quote
from unittest.mock import MagicMock, Mock, call

import pytest
import matrix_client.errors

from matrix_client.api_asyncio import AsyncHTTPAPI


def client_session(json, status=200):
client_session = MagicMock()

class MockResponse(MagicMock):
called = 0
async def __aenter__(self):
response = MagicMock()
f = Future()
f.set_result(json)
response.json = Mock(return_value=f)
response.status = self.status()
f = Future()
f.set_result("hello")
response.text = Mock(return_value=f)
return response

async def __aexit__(self, exc_type, exc_val, exc_tb):
pass

def status(self):
if status == 429 and self.called > 0:
return 200
else:
self.called += 1
return status


client_session.request = MockResponse()

return client_session

@pytest.fixture
def api():
return partial(AsyncHTTPAPI, base_url="base_url", token="1234")


@pytest.mark.asyncio
async def test_send(api):
api = api(client_session=client_session({}))

response = await api._send("GET", "/createRoom")
api.client_session.request.assert_called_once_with("GET",
"base_url/_matrix/client/r0/createRoom",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


@pytest.mark.asyncio
async def test_send_429(api):
api = api(client_session=client_session({}, status=429))

response = await api._send("GET", "/createRoom")
call429 = call("GET",
"base_url/_matrix/client/r0/createRoom",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})

# If we 429 we should call request twice with the same parameters
api.client_session.request.assert_has_calls([call429, call429])



@pytest.mark.parametrize("json", [{"error": '{"retry_after_ms": 10}'},
{"error": {"retry_after_ms": 10}},
{"retry_after_ms": 10}])
@pytest.mark.asyncio
async def test_send_429_timeout(api, json):
api = api(client_session=client_session(json, status=429))

response = await api._send("GET", "/createRoom")

call429 = call("GET",
"base_url/_matrix/client/r0/createRoom",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})

# If we 429 we should call request twice with the same parameters
api.client_session.request.assert_has_calls([call429, call429])


@pytest.mark.asyncio
async def test_send_404(api):
api = api(client_session=client_session({}, status=404))

with pytest.raises(matrix_client.errors.MatrixRequestError) as exc:
response = await api._send("GET", "/createRoom")
assert exc.status == 404
assert exc.content == "hello"


@pytest.mark.asyncio
async def test_get_displayname(api):
api = api(client_session=client_session({"displayname": "African swallow"}))
mxid = "@user:test"
displayname = await api.get_display_name(mxid)
assert displayname == "African swallow"

api.client_session.request.assert_called_once_with("GET",
f"base_url/_matrix/client/r0/profile/{mxid}/displayname",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


@pytest.mark.asyncio
async def test_set_displayname(api):
api = api(client_session=client_session({}))
mxid = "@user:test"
await api.set_display_name(mxid, "African swallow")

api.client_session.request.assert_called_once_with("PUT",
f"base_url/_matrix/client/r0/profile/{mxid}/displayname",
data='{"displayname": "African swallow"}',
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


@pytest.mark.asyncio
async def test_get_avatar_url(api):
api = api(client_session=client_session({"avatar_url": "mxc://hello"}))
mxid = "@user:test"
url = await api.get_avatar_url(mxid)
assert url == "mxc://hello"

api.client_session.request.assert_called_once_with("GET",
f"base_url/_matrix/client/r0/profile/{mxid}/avatar_url",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


@pytest.mark.asyncio
async def test_get_room_id(api):
api = api(client_session=client_session({"room_id": "aroomid"}))
mxid = "@user:test"
room_alias = "#test:test"
aid = await api.get_room_id(room_alias)
assert aid == "aroomid"

api.client_session.request.assert_called_once_with("GET",
f"base_url/_matrix/client/r0/directory/room/{quote(room_alias)}",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


@pytest.mark.asyncio
async def test_get_room_displayname(api):
mxid = "@user:test"
api = api(client_session=client_session({"chunk":
[{"sender": mxid, "content": {"displayname": "African swallow"}}]}))
room_alias = "#test:test"
displayname = await api.get_room_displayname("arromid", mxid)
assert displayname == "African swallow"

api.client_session.request.assert_called_once_with("GET",
f"base_url/_matrix/client/r0/rooms/arromid/members",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


# Test the wrapping of a sync method
@pytest.mark.asyncio
async def test_sync_wrap(api):
api = api(client_session=client_session({}))
roomid = "!ldjaslkdja:test"
eventid = "$alskdjsalkdjal:test"
displayname = await api.get_event_in_room(roomid, eventid)

api.client_session.request.assert_called_once_with("GET",
f"base_url/_matrix/client/r0/rooms/{roomid}/event/{eventid}",
data="{}",
headers={"Content-Type": "application/json"},
params={"access_token": "1234"})


# Test no access token
@pytest.mark.asyncio
async def test_login(api):
api = AsyncHTTPAPI(base_url="base_url", client_session=client_session({}))
await api.login("bob")

api.client_session.request.assert_called_once_with("POST",
f"base_url/_matrix/client/r0/login",
data=json.dumps({"type": "bob"}),
headers={"Content-Type": "application/json"},
params={})

0 comments on commit 8a829ad

Please sign in to comment.