File: api_test.py

package info (click to toggle)
python-matrix-nio 0.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,224 kB
  • sloc: python: 23,670; makefile: 36; sh: 8
file content (78 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright © 2021 Famedly GmbH
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


from nio.api import MATRIX_API_PATH_V3, Api


class TestClass:
    def test_profile_get(self) -> None:
        """Test that profile_get returns the HTTP path for the request."""
        api = Api()
        encode_pairs = [
            # a normal username
            ("@bob:example.com", "%40bob%3Aexample.com"),
            # an irregular but legal username
            (
                "@a-z0-9._=-/:example.com",
                "%40a-z0-9._%3D-%2F%3Aexample.com",
                # Why include this? https://github.com/poljar/matrix-nio/issues/211
                # There were issues with a username that included slashes, which is
                # legal by the standard: https://matrix.org/docs/spec/appendices#user-identifiers
            ),
        ]
        for unencoded, encoded in encode_pairs:
            expected_path = f"{MATRIX_API_PATH_V3}/profile/{encoded}"
            (_method, actual_path) = api.profile_get(unencoded)
            assert actual_path == expected_path

    def test_profile_get_authed(self) -> None:
        """Test that profile_get sets access_token in query param"""
        api = Api()
        user_id = "@bob:example.com"
        encoded = "%40bob%3Aexample.com"
        token = "SECRET_TOKEN"

        expected = f"{MATRIX_API_PATH_V3}/profile/{encoded}?access_token={token}"
        resp = api.profile_get(user_id, token)

        assert resp == ("GET", expected)

    def test_delete_room_alias(self) -> None:
        """Test that profile_get sets access_token in query param"""
        api = Api()
        room_alias = "#room:example.com"
        encoded = "%23room%3Aexample.com"
        token = "SECRET_TOKEN"

        expected = f"{MATRIX_API_PATH_V3}/directory/room/{encoded}?access_token={token}"
        resp = api.delete_room_alias(token, room_alias)

        assert resp == ("DELETE", expected)

    def test_put_room_alias(self) -> None:
        """Test that profile_get sets access_token in query param"""
        api = Api()
        room_alias = "#room:example.com"
        encoded = "%23room%3Aexample.com"
        room_id = "!room_id:example.com"
        token = "SECRET_TOKEN"

        expected_path = (
            f"{MATRIX_API_PATH_V3}/directory/room/{encoded}?access_token={token}"
        )
        expected_data = '{"room_id":"!room_id:example.com"}'
        resp = api.put_room_alias(token, room_alias, room_id)

        assert resp == ("PUT", expected_path, expected_data)