Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions matrix_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,13 @@ def _make_txn_id(self):
txn_id = str(self.txn_id) + str(int(time() * 1000))
self.txn_id += 1
return txn_id

def whoami(self):
"""Determine user_id for authentificated user.
"""
if not self.token:
raise MatrixError("Authentification required.")
return self._send(
"GET",
"/account/whoami"
)
17 changes: 9 additions & 8 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from .api import MatrixHttpApi
from .checks import check_user_id
from .errors import MatrixRequestError, MatrixUnexpectedResponse
from .room import Room
from .user import User
Expand Down Expand Up @@ -54,9 +53,7 @@ class MatrixClient(object):
e.g. (ex: https://localhost:8008 )
token (Optional[str]): If you have an access token
supply it here.
user_id (Optional[str]): You must supply the user_id
(as obtained when initially logging in to obtain
the token) if supplying a token; otherwise, ignored.
user_id (Optional[str]): Optional. Obsolete. For backward compatibility.
valid_cert_check (bool): Check the homeservers
certificate on connections?
cache_level (CACHE): One of CACHE.NONE, CACHE.SOME, or
Expand Down Expand Up @@ -108,8 +105,12 @@ def global_callback(incoming_event):
def __init__(self, base_url, token=None, user_id=None,
valid_cert_check=True, sync_filter_limit=20,
cache_level=CACHE.ALL, encryption=False, encryption_conf=None):
if token is not None and user_id is None:
raise ValueError("must supply user_id along with token")
if user_id:
warn(
"user_id is deprecated. "
"Now it is requested from the server.", DeprecationWarning
)

if encryption and not ENCRYPTION_SUPPORT:
raise ValueError("Failed to enable encryption. Please make sure the olm "
"library is available.")
Expand Down Expand Up @@ -145,8 +146,8 @@ def __init__(self, base_url, token=None, user_id=None,
# room_id: Room
}
if token:
check_user_id(user_id)
self.user_id = user_id
response = self.api.whoami()
self.user_id = response["user_id"]
self._sync()

def get_sync_token(self):
Expand Down
33 changes: 33 additions & 0 deletions test/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,36 @@ def test_create_room_federate_false(self):
assert req.method == 'POST'
j = json.loads(req.body)
assert not j["creation_content"]["m.federate"]


class TestWhoamiQuery:
user_id = "@alice:example.com"
token = "Dp0YKRXwx0iWDhFj7lg3DVjwsWzGcUIgARljgyAip2JD8qd5dSaW" \
"cxowTKEFetPulfLijAhv8eOmUSScyGcWgZyNMRTBmoJ0RFc0HotPvTBZ" \
"U98yKRLtat7V43aCpFmK"

@responses.activate
def test_whoami(self):
mapi = api.MatrixHttpApi("http://example.com", token=self.token)
whoami_url = "http://example.com/_matrix/client/r0/account/whoami"
responses.add(
responses.GET,
whoami_url,
body='{"user_id": "%s"}' % self.user_id
)
mapi.whoami()
req = responses.calls[0].request
assert req.method == 'GET'
assert whoami_url in req.url

@responses.activate
def test_whoami_unauth(self):
mapi = api.MatrixHttpApi("http://example.com")
whoami_url = "http://example.com/_matrix/client/r0/account/whoami"
responses.add(
responses.GET,
whoami_url,
body='{"user_id": "%s"}' % self.user_id
)
with pytest.raises(MatrixError):
mapi.whoami()
22 changes: 22 additions & 0 deletions test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ def test_create_client():
MatrixClient("http://example.com")


@responses.activate
def test_create_client_with_token():
user_id = "@alice:example.com"
token = "Dp0YKRXwx0iWDhFj7lg3DVjwsWzGcUIgARljgyAip2JD8qd5dSaW" \
"cxowTKEFetPulfLijAhv8eOmUSScyGcWgZyNMRTBmoJ0RFc0HotPvTBZ" \
"U98yKRLtat7V43aCpFmK"
whoami_url = HOSTNAME+MATRIX_V2_API_PATH+"/account/whoami"
responses.add(
responses.GET,
whoami_url,
body='{"user_id": "%s"}' % user_id
)
sync_response = deepcopy(response_examples.example_sync)
response_body = json.dumps(sync_response)
sync_url = HOSTNAME + MATRIX_V2_API_PATH + "/sync"
responses.add(responses.GET, sync_url, body=response_body)
MatrixClient(HOSTNAME, token=token)
req = responses.calls[0].request
assert req.method == 'GET'
assert whoami_url in req.url


def test_sync_token():
client = MatrixClient("http://example.com")
assert client.get_sync_token() is None
Expand Down