|
| 1 | +import responses |
| 2 | +import json |
| 3 | +from matrix_client.admin_api import MatrixHttpAdminApi |
| 4 | + |
| 5 | + |
| 6 | +class TestAdminApi: |
| 7 | + admin_api = MatrixHttpAdminApi("http://example.com") |
| 8 | + user_id = "@alice:matrix.org" |
| 9 | + room_id = "!gveUzqBzXPqmwvDaCZ:example.org" |
| 10 | + event_id = "$153119074937XoqNn::example.org" |
| 11 | + up_to_ts = 1531190749090 |
| 12 | + purge_id = "dLVEjckmfggyQduS" |
| 13 | + |
| 14 | + @responses.activate |
| 15 | + def test_purge_history_eventid(self): |
| 16 | + purge_history_url = \ |
| 17 | + "http://example.com/_matrix/client/r0/" \ |
| 18 | + "admin/purge_history/%s" % self.room_id |
| 19 | + responses.add( |
| 20 | + responses.POST, |
| 21 | + purge_history_url, |
| 22 | + body='{"purge_id": "%s"}' % self.purge_id |
| 23 | + ) |
| 24 | + self.admin_api.purge_history(self.room_id, self.event_id) |
| 25 | + req = responses.calls[0].request |
| 26 | + assert req.url == purge_history_url |
| 27 | + assert req.method == 'POST' |
| 28 | + j = json.loads(req.body) |
| 29 | + assert j["delete_local_events"] |
| 30 | + assert j["purge_up_to_event_id"] == self.event_id |
| 31 | + |
| 32 | + @responses.activate |
| 33 | + def test_purge_history_up_to_ts(self): |
| 34 | + purge_history_url = \ |
| 35 | + "http://example.com/_matrix/client/r0/" \ |
| 36 | + "admin/purge_history/%s" % self.room_id |
| 37 | + responses.add( |
| 38 | + responses.POST, |
| 39 | + purge_history_url, |
| 40 | + body='{"purge_id": "%s"}' % self.purge_id |
| 41 | + ) |
| 42 | + self.admin_api.purge_history(self.room_id, self.up_to_ts) |
| 43 | + req = responses.calls[0].request |
| 44 | + j = json.loads(req.body) |
| 45 | + assert j["delete_local_events"] |
| 46 | + assert j["purge_up_to_ts"] == self.up_to_ts |
| 47 | + |
| 48 | + @responses.activate |
| 49 | + def test_purge_history_status(self): |
| 50 | + purge_history_status_url = \ |
| 51 | + "http://example.com/_matrix/client/r0/" \ |
| 52 | + "admin/purge_history_status/%s" % self.purge_id |
| 53 | + responses.add( |
| 54 | + responses.GET, |
| 55 | + purge_history_status_url, |
| 56 | + body='{"status": "complete"}' |
| 57 | + ) |
| 58 | + self.admin_api.purge_history_status(self.purge_id) |
| 59 | + req = responses.calls[0].request |
| 60 | + assert req.url == purge_history_status_url |
| 61 | + |
| 62 | + @responses.activate |
| 63 | + def test_media_in_room(self): |
| 64 | + media_url = \ |
| 65 | + "http://example.com/_matrix/client/r0/" \ |
| 66 | + "admin/room/%s/media" % self.room_id |
| 67 | + responses.add( |
| 68 | + responses.GET, |
| 69 | + media_url, |
| 70 | + body='{"local": ["mxc://example.com/xwvutsrqponmlkjihgfedcba"],' |
| 71 | + ' "remote": ["mxc://matrix.org/xwtttsrqponmlkjihgfedcba"]}' |
| 72 | + ) |
| 73 | + resp = self.admin_api.media_in_room(self.room_id) |
| 74 | + req = responses.calls[0].request |
| 75 | + assert req.url == media_url |
| 76 | + assert req.method == 'GET' |
| 77 | + assert "local" in resp |
| 78 | + assert "remote" in resp |
| 79 | + |
| 80 | + @responses.activate |
| 81 | + def test_whois(self): |
| 82 | + whois_url = \ |
| 83 | + "http://example.com/_matrix/client/r0/" \ |
| 84 | + "admin/whois/%s" % self.user_id |
| 85 | + responses.add( |
| 86 | + responses.GET, |
| 87 | + whois_url, |
| 88 | + body='{"user_id": "%s", "devices": {}}' % self.user_id |
| 89 | + ) |
| 90 | + self.admin_api.whois(self.user_id) |
| 91 | + req = responses.calls[0].request |
| 92 | + assert req.url == whois_url |
| 93 | + assert req.method == 'GET' |
| 94 | + |
| 95 | + @responses.activate |
| 96 | + def test_deactivate_no_erase(self): |
| 97 | + erase_url = \ |
| 98 | + "http://example.com/_matrix/client/r0/" \ |
| 99 | + "admin/deactivate/%s" % self.user_id |
| 100 | + responses.add(responses.POST, erase_url, body='{}') |
| 101 | + self.admin_api.deactivate(self.user_id) |
| 102 | + req = responses.calls[0].request |
| 103 | + assert req.url == erase_url |
| 104 | + assert req.method == 'POST' |
| 105 | + |
| 106 | + @responses.activate |
| 107 | + def test_deactivate(self): |
| 108 | + erase_url = \ |
| 109 | + "http://example.com/_matrix/client/r0/" \ |
| 110 | + "admin/deactivate/%s" % self.user_id |
| 111 | + responses.add(responses.POST, erase_url, body='{}') |
| 112 | + self.admin_api.deactivate(self.user_id, erase=True) |
| 113 | + req = responses.calls[0].request |
| 114 | + assert req.url == erase_url |
| 115 | + assert req.method == 'POST' |
| 116 | + j = json.loads(req.body) |
| 117 | + assert j["erase"] |
| 118 | + |
| 119 | + @responses.activate |
| 120 | + def test_reset_password(self): |
| 121 | + reset_url = \ |
| 122 | + "http://example.com/_matrix/client/r0/" \ |
| 123 | + "admin/reset_password/%s" % self.user_id |
| 124 | + responses.add(responses.POST, reset_url, body='{}') |
| 125 | + self.admin_api.reset_password(self.user_id, 'secret') |
| 126 | + req = responses.calls[0].request |
| 127 | + assert req.url == reset_url |
| 128 | + assert req.method == 'POST' |
| 129 | + j = json.loads(req.body) |
| 130 | + assert j["new_password"] == 'secret' |
| 131 | + |
| 132 | + @responses.activate |
| 133 | + def test_quarantine_media(self): |
| 134 | + quarantine_media_url = \ |
| 135 | + "http://example.com/_matrix/client/r0/" \ |
| 136 | + "admin/quarantine_media/%s" % self.room_id |
| 137 | + responses.add( |
| 138 | + responses.POST, |
| 139 | + quarantine_media_url, |
| 140 | + body='{"num_quarantined": 1}' |
| 141 | + ) |
| 142 | + self.admin_api.quarantine_media(self.room_id) |
| 143 | + req = responses.calls[0].request |
| 144 | + assert req.url == quarantine_media_url |
| 145 | + assert req.method == 'POST' |
| 146 | + |
| 147 | + @responses.activate |
| 148 | + def test_shutdown_room(self): |
| 149 | + shutdown_room_url = \ |
| 150 | + "http://example.com/_matrix/client/r0/" \ |
| 151 | + "admin/shutdown_room/%s" % self.room_id |
| 152 | + responses.add( |
| 153 | + responses.POST, |
| 154 | + shutdown_room_url, |
| 155 | + body='{"kicked_users": 2, ' |
| 156 | + '"local_aliases": [], ' |
| 157 | + '"new_room_id": "!hepuyalbwtkjapqdhq:example.org"}' |
| 158 | + ) |
| 159 | + self.admin_api.shutdown_room( |
| 160 | + self.room_id, |
| 161 | + self.user_id, |
| 162 | + room_name="New room", |
| 163 | + message="Old room closed by admin" |
| 164 | + ) |
| 165 | + req = responses.calls[0].request |
| 166 | + assert req.url == shutdown_room_url |
| 167 | + assert req.method == 'POST' |
| 168 | + j = json.loads(req.body) |
| 169 | + assert j["new_room_user_id"] == self.user_id |
0 commit comments